Node.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibJS/AST.h>
  28. #include <LibJS/Runtime/Function.h>
  29. #include <LibWeb/Bindings/EventWrapper.h>
  30. #include <LibWeb/Bindings/NodeWrapper.h>
  31. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  32. #include <LibWeb/DOM/Element.h>
  33. #include <LibWeb/DOM/Event.h>
  34. #include <LibWeb/DOM/EventDispatcher.h>
  35. #include <LibWeb/DOM/EventListener.h>
  36. #include <LibWeb/DOM/Node.h>
  37. #include <LibWeb/DOM/ShadowRoot.h>
  38. #include <LibWeb/HTML/HTMLAnchorElement.h>
  39. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  40. #include <LibWeb/Layout/Node.h>
  41. #include <LibWeb/Layout/TextNode.h>
  42. namespace Web::DOM {
  43. Node::Node(Document& document, NodeType type)
  44. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  45. , m_document(&document)
  46. , m_type(type)
  47. {
  48. if (!is_document())
  49. m_document->ref_from_node({});
  50. }
  51. Node::~Node()
  52. {
  53. ASSERT(m_deletion_has_begun);
  54. if (layout_node() && layout_node()->parent())
  55. layout_node()->parent()->remove_child(*layout_node());
  56. if (!is_document())
  57. m_document->unref_from_node({});
  58. }
  59. const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
  60. {
  61. for (auto* node = this; node; node = node->parent()) {
  62. if (is<HTML::HTMLAnchorElement>(*node) && downcast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
  63. return downcast<HTML::HTMLAnchorElement>(node);
  64. }
  65. return nullptr;
  66. }
  67. const HTML::HTMLElement* Node::enclosing_html_element() const
  68. {
  69. return first_ancestor_of_type<HTML::HTMLElement>();
  70. }
  71. String Node::text_content() const
  72. {
  73. StringBuilder builder;
  74. for (auto* child = first_child(); child; child = child->next_sibling()) {
  75. builder.append(child->text_content());
  76. }
  77. return builder.to_string();
  78. }
  79. void Node::set_text_content(const String& content)
  80. {
  81. if (is_text()) {
  82. downcast<Text>(this)->set_data(content);
  83. } else {
  84. remove_all_children();
  85. append_child(document().create_text_node(content));
  86. }
  87. set_needs_style_update(true);
  88. document().invalidate_layout();
  89. }
  90. RefPtr<Layout::Node> Node::create_layout_node()
  91. {
  92. return nullptr;
  93. }
  94. void Node::invalidate_style()
  95. {
  96. for_each_in_subtree_of_type<Element>([&](auto& element) {
  97. element.set_needs_style_update(true);
  98. return IterationDecision::Continue;
  99. });
  100. document().schedule_style_update();
  101. }
  102. bool Node::is_link() const
  103. {
  104. return enclosing_link_element();
  105. }
  106. bool Node::dispatch_event(NonnullRefPtr<Event> event)
  107. {
  108. return EventDispatcher::dispatch(*this, event);
  109. }
  110. String Node::child_text_content() const
  111. {
  112. if (!is<ParentNode>(*this))
  113. return String::empty();
  114. StringBuilder builder;
  115. downcast<ParentNode>(*this).for_each_child([&](auto& child) {
  116. if (is<Text>(child))
  117. builder.append(downcast<Text>(child).text_content());
  118. });
  119. return builder.build();
  120. }
  121. Node* Node::root()
  122. {
  123. Node* root = this;
  124. while (root->parent())
  125. root = root->parent();
  126. return root;
  127. }
  128. Node* Node::shadow_including_root()
  129. {
  130. auto node_root = root();
  131. if (is<ShadowRoot>(node_root))
  132. return downcast<ShadowRoot>(node_root)->host()->shadow_including_root();
  133. return node_root;
  134. }
  135. bool Node::is_connected() const
  136. {
  137. return shadow_including_root() && shadow_including_root()->is_document();
  138. }
  139. Element* Node::parent_element()
  140. {
  141. if (!parent() || !is<Element>(parent()))
  142. return nullptr;
  143. return downcast<Element>(parent());
  144. }
  145. const Element* Node::parent_element() const
  146. {
  147. if (!parent() || !is<Element>(parent()))
  148. return nullptr;
  149. return downcast<Element>(parent());
  150. }
  151. RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
  152. {
  153. if (&node->document() != &document())
  154. document().adopt_node(node);
  155. TreeNode<Node>::append_child(node, notify);
  156. return node;
  157. }
  158. RefPtr<Node> Node::remove_child(NonnullRefPtr<Node> node)
  159. {
  160. TreeNode<Node>::remove_child(node);
  161. return node;
  162. }
  163. RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
  164. {
  165. if (!child)
  166. return append_child(move(node), notify);
  167. if (child->parent_node() != this) {
  168. dbgln("FIXME: Trying to insert_before() a bogus child");
  169. return nullptr;
  170. }
  171. if (&node->document() != &document())
  172. document().adopt_node(node);
  173. TreeNode<Node>::insert_before(node, child, notify);
  174. return node;
  175. }
  176. void Node::set_document(Badge<Document>, Document& document)
  177. {
  178. if (m_document == &document)
  179. return;
  180. document.ref_from_node({});
  181. m_document->unref_from_node({});
  182. m_document = &document;
  183. }
  184. bool Node::is_editable() const
  185. {
  186. return parent() && parent()->is_editable();
  187. }
  188. JS::Object* Node::create_wrapper(JS::GlobalObject& global_object)
  189. {
  190. return wrap(global_object, *this);
  191. }
  192. void Node::removed_last_ref()
  193. {
  194. if (is<Document>(*this)) {
  195. downcast<Document>(*this).removed_last_ref();
  196. return;
  197. }
  198. m_deletion_has_begun = true;
  199. delete this;
  200. }
  201. void Node::set_layout_node(Badge<Layout::Node>, Layout::Node* layout_node) const
  202. {
  203. if (layout_node)
  204. m_layout_node = layout_node->make_weak_ptr();
  205. else
  206. m_layout_node = nullptr;
  207. }
  208. EventTarget* Node::get_parent(const Event&)
  209. {
  210. // FIXME: returns the node’s assigned slot, if node is assigned, and node’s parent otherwise.
  211. return parent();
  212. }
  213. void Node::set_needs_style_update(bool value)
  214. {
  215. if (m_needs_style_update == value)
  216. return;
  217. m_needs_style_update = value;
  218. if (m_needs_style_update) {
  219. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent())
  220. ancestor->m_child_needs_style_update = true;
  221. document().schedule_style_update();
  222. }
  223. }
  224. void Node::inserted_into(Node&)
  225. {
  226. set_needs_style_update(true);
  227. }
  228. ParentNode* Node::parent_or_shadow_host()
  229. {
  230. if (is<ShadowRoot>(*this))
  231. return downcast<ShadowRoot>(*this).host();
  232. return downcast<ParentNode>(parent());
  233. }
  234. }