Node.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. VERIFY(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. const HTML::HTMLElement* Node::enclosing_html_element_with_attribute(const FlyString& attribute) const
  72. {
  73. for (auto* node = this; node; node = node->parent()) {
  74. if (is<HTML::HTMLElement>(*node) && downcast<HTML::HTMLElement>(*node).has_attribute(attribute))
  75. return downcast<HTML::HTMLElement>(node);
  76. }
  77. return nullptr;
  78. }
  79. String Node::text_content() const
  80. {
  81. StringBuilder builder;
  82. for (auto* child = first_child(); child; child = child->next_sibling()) {
  83. builder.append(child->text_content());
  84. }
  85. return builder.to_string();
  86. }
  87. void Node::set_text_content(const String& content)
  88. {
  89. if (is_text()) {
  90. downcast<Text>(this)->set_data(content);
  91. } else {
  92. remove_all_children();
  93. append_child(document().create_text_node(content));
  94. }
  95. set_needs_style_update(true);
  96. document().invalidate_layout();
  97. }
  98. RefPtr<Layout::Node> Node::create_layout_node()
  99. {
  100. return nullptr;
  101. }
  102. void Node::invalidate_style()
  103. {
  104. for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  105. element.set_needs_style_update(true);
  106. return IterationDecision::Continue;
  107. });
  108. document().schedule_style_update();
  109. }
  110. bool Node::is_link() const
  111. {
  112. return enclosing_link_element();
  113. }
  114. bool Node::dispatch_event(NonnullRefPtr<Event> event)
  115. {
  116. return EventDispatcher::dispatch(*this, event);
  117. }
  118. String Node::child_text_content() const
  119. {
  120. if (!is<ParentNode>(*this))
  121. return String::empty();
  122. StringBuilder builder;
  123. downcast<ParentNode>(*this).for_each_child([&](auto& child) {
  124. if (is<Text>(child))
  125. builder.append(downcast<Text>(child).text_content());
  126. });
  127. return builder.build();
  128. }
  129. Node* Node::root()
  130. {
  131. Node* root = this;
  132. while (root->parent())
  133. root = root->parent();
  134. return root;
  135. }
  136. Node* Node::shadow_including_root()
  137. {
  138. auto node_root = root();
  139. if (is<ShadowRoot>(node_root))
  140. return downcast<ShadowRoot>(node_root)->host()->shadow_including_root();
  141. return node_root;
  142. }
  143. bool Node::is_connected() const
  144. {
  145. return shadow_including_root() && shadow_including_root()->is_document();
  146. }
  147. Element* Node::parent_element()
  148. {
  149. if (!parent() || !is<Element>(parent()))
  150. return nullptr;
  151. return downcast<Element>(parent());
  152. }
  153. const Element* Node::parent_element() const
  154. {
  155. if (!parent() || !is<Element>(parent()))
  156. return nullptr;
  157. return downcast<Element>(parent());
  158. }
  159. RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
  160. {
  161. if (node->parent())
  162. node->parent()->remove_child(node);
  163. if (&node->document() != &document())
  164. document().adopt_node(node);
  165. TreeNode<Node>::append_child(node, notify);
  166. return node;
  167. }
  168. RefPtr<Node> Node::remove_child(NonnullRefPtr<Node> node)
  169. {
  170. TreeNode<Node>::remove_child(node);
  171. return node;
  172. }
  173. RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
  174. {
  175. if (!child)
  176. return append_child(move(node), notify);
  177. if (child->parent_node() != this) {
  178. dbgln("FIXME: Trying to insert_before() a bogus child");
  179. return nullptr;
  180. }
  181. if (node->parent())
  182. node->parent()->remove_child(node);
  183. if (&node->document() != &document())
  184. document().adopt_node(node);
  185. TreeNode<Node>::insert_before(node, child, notify);
  186. return node;
  187. }
  188. void Node::set_document(Badge<Document>, Document& document)
  189. {
  190. if (m_document == &document)
  191. return;
  192. document.ref_from_node({});
  193. m_document->unref_from_node({});
  194. m_document = &document;
  195. }
  196. bool Node::is_editable() const
  197. {
  198. return parent() && parent()->is_editable();
  199. }
  200. JS::Object* Node::create_wrapper(JS::GlobalObject& global_object)
  201. {
  202. return wrap(global_object, *this);
  203. }
  204. void Node::removed_last_ref()
  205. {
  206. if (is<Document>(*this)) {
  207. downcast<Document>(*this).removed_last_ref();
  208. return;
  209. }
  210. m_deletion_has_begun = true;
  211. delete this;
  212. }
  213. void Node::set_layout_node(Badge<Layout::Node>, Layout::Node* layout_node) const
  214. {
  215. if (layout_node)
  216. m_layout_node = layout_node->make_weak_ptr();
  217. else
  218. m_layout_node = nullptr;
  219. }
  220. EventTarget* Node::get_parent(const Event&)
  221. {
  222. // FIXME: returns the node’s assigned slot, if node is assigned, and node’s parent otherwise.
  223. return parent();
  224. }
  225. void Node::set_needs_style_update(bool value)
  226. {
  227. if (m_needs_style_update == value)
  228. return;
  229. m_needs_style_update = value;
  230. if (m_needs_style_update) {
  231. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent())
  232. ancestor->m_child_needs_style_update = true;
  233. document().schedule_style_update();
  234. }
  235. }
  236. void Node::inserted_into(Node&)
  237. {
  238. set_needs_style_update(true);
  239. }
  240. ParentNode* Node::parent_or_shadow_host()
  241. {
  242. if (is<ShadowRoot>(*this))
  243. return downcast<ShadowRoot>(*this).host();
  244. return downcast<ParentNode>(parent());
  245. }
  246. NonnullRefPtrVector<Node> Node::child_nodes() const
  247. {
  248. NonnullRefPtrVector<Node> nodes;
  249. for_each_child([&](auto& child) {
  250. nodes.append(child);
  251. });
  252. return nodes;
  253. }
  254. }