Node.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <LibJS/Runtime/ScriptFunction.h>
  30. #include <LibWeb/Bindings/EventWrapper.h>
  31. #include <LibWeb/Bindings/EventWrapperFactory.h>
  32. #include <LibWeb/Bindings/NodeWrapper.h>
  33. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  34. #include <LibWeb/CSS/StyleResolver.h>
  35. #include <LibWeb/DOM/Element.h>
  36. #include <LibWeb/DOM/Event.h>
  37. #include <LibWeb/DOM/EventDispatcher.h>
  38. #include <LibWeb/DOM/EventListener.h>
  39. #include <LibWeb/DOM/Node.h>
  40. #include <LibWeb/DOM/ShadowRoot.h>
  41. #include <LibWeb/HTML/HTMLAnchorElement.h>
  42. #include <LibWeb/Layout/BlockBox.h>
  43. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  44. #include <LibWeb/Layout/InlineNode.h>
  45. #include <LibWeb/Layout/Node.h>
  46. #include <LibWeb/Layout/TextNode.h>
  47. //#define EVENT_DEBUG
  48. namespace Web::DOM {
  49. Node::Node(Document& document, NodeType type)
  50. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  51. , m_document(&document)
  52. , m_type(type)
  53. {
  54. if (!is_document())
  55. m_document->ref_from_node({});
  56. }
  57. Node::~Node()
  58. {
  59. ASSERT(m_deletion_has_begun);
  60. if (layout_node() && layout_node()->parent())
  61. layout_node()->parent()->remove_child(*layout_node());
  62. if (!is_document())
  63. m_document->unref_from_node({});
  64. }
  65. const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
  66. {
  67. for (auto* node = this; node; node = node->parent()) {
  68. if (is<HTML::HTMLAnchorElement>(*node) && downcast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
  69. return downcast<HTML::HTMLAnchorElement>(node);
  70. }
  71. return nullptr;
  72. }
  73. const HTML::HTMLElement* Node::enclosing_html_element() const
  74. {
  75. return first_ancestor_of_type<HTML::HTMLElement>();
  76. }
  77. String Node::text_content() const
  78. {
  79. StringBuilder builder;
  80. for (auto* child = first_child(); child; child = child->next_sibling()) {
  81. builder.append(child->text_content());
  82. }
  83. return builder.to_string();
  84. }
  85. void Node::set_text_content(const String& content)
  86. {
  87. if (is_text()) {
  88. downcast<Text>(this)->set_data(content);
  89. } else {
  90. remove_all_children();
  91. append_child(document().create_text_node(content));
  92. }
  93. set_needs_style_update(true);
  94. document().invalidate_layout();
  95. }
  96. RefPtr<Layout::Node> Node::create_layout_node(const CSS::StyleProperties*)
  97. {
  98. return nullptr;
  99. }
  100. void Node::invalidate_style()
  101. {
  102. for_each_in_subtree_of_type<Element>([&](auto& element) {
  103. element.set_needs_style_update(true);
  104. return IterationDecision::Continue;
  105. });
  106. document().schedule_style_update();
  107. }
  108. bool Node::is_link() const
  109. {
  110. return enclosing_link_element();
  111. }
  112. bool Node::dispatch_event(NonnullRefPtr<Event> event)
  113. {
  114. return EventDispatcher::dispatch(*this, event);
  115. }
  116. String Node::child_text_content() const
  117. {
  118. if (!is<ParentNode>(*this))
  119. return String::empty();
  120. StringBuilder builder;
  121. downcast<ParentNode>(*this).for_each_child([&](auto& child) {
  122. if (is<Text>(child))
  123. builder.append(downcast<Text>(child).text_content());
  124. });
  125. return builder.build();
  126. }
  127. Node* Node::root()
  128. {
  129. Node* root = this;
  130. while (root->parent())
  131. root = root->parent();
  132. return root;
  133. }
  134. Node* Node::shadow_including_root()
  135. {
  136. auto node_root = root();
  137. if (is<ShadowRoot>(node_root))
  138. return downcast<ShadowRoot>(node_root)->host()->shadow_including_root();
  139. return node_root;
  140. }
  141. bool Node::is_connected() const
  142. {
  143. return shadow_including_root() && shadow_including_root()->is_document();
  144. }
  145. Element* Node::parent_element()
  146. {
  147. if (!parent() || !is<Element>(parent()))
  148. return nullptr;
  149. return downcast<Element>(parent());
  150. }
  151. const Element* Node::parent_element() const
  152. {
  153. if (!parent() || !is<Element>(parent()))
  154. return nullptr;
  155. return downcast<Element>(parent());
  156. }
  157. RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
  158. {
  159. if (&node->document() != &document())
  160. document().adopt_node(node);
  161. TreeNode<Node>::append_child(node, notify);
  162. return node;
  163. }
  164. RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
  165. {
  166. if (!child)
  167. return append_child(move(node), notify);
  168. if (child->parent_node() != this) {
  169. dbg() << "FIXME: Trying to insert_before() a bogus child";
  170. return nullptr;
  171. }
  172. if (&node->document() != &document())
  173. document().adopt_node(node);
  174. TreeNode<Node>::insert_before(node, child, notify);
  175. return node;
  176. }
  177. void Node::remove_all_children()
  178. {
  179. while (RefPtr<Node> child = first_child()) {
  180. remove_child(*child);
  181. }
  182. }
  183. void Node::set_document(Badge<Document>, Document& document)
  184. {
  185. if (m_document == &document)
  186. return;
  187. document.ref_from_node({});
  188. m_document->unref_from_node({});
  189. m_document = &document;
  190. }
  191. bool Node::is_editable() const
  192. {
  193. return parent() && parent()->is_editable();
  194. }
  195. Bindings::EventTargetWrapper* Node::create_wrapper(JS::GlobalObject& global_object)
  196. {
  197. return wrap(global_object, *this);
  198. }
  199. void Node::removed_last_ref()
  200. {
  201. if (is<Document>(*this)) {
  202. downcast<Document>(*this).removed_last_ref();
  203. return;
  204. }
  205. m_deletion_has_begun = true;
  206. delete this;
  207. }
  208. void Node::set_layout_node(Badge<Layout::Node>, Layout::Node* layout_node) const
  209. {
  210. if (layout_node)
  211. m_layout_node = layout_node->make_weak_ptr();
  212. else
  213. m_layout_node = nullptr;
  214. }
  215. EventTarget* Node::get_parent(const Event&)
  216. {
  217. // FIXME: returns the node’s assigned slot, if node is assigned, and node’s parent otherwise.
  218. return parent();
  219. }
  220. void Node::set_needs_style_update(bool value)
  221. {
  222. if (m_needs_style_update == value)
  223. return;
  224. m_needs_style_update = value;
  225. if (m_needs_style_update)
  226. document().schedule_style_update();
  227. }
  228. }