Node.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/HTML/HTMLAnchorElement.h>
  41. #include <LibWeb/Layout/LayoutBlock.h>
  42. #include <LibWeb/Layout/LayoutDocument.h>
  43. #include <LibWeb/Layout/LayoutInline.h>
  44. #include <LibWeb/Layout/LayoutNode.h>
  45. #include <LibWeb/Layout/LayoutText.h>
  46. //#define EVENT_DEBUG
  47. namespace Web::DOM {
  48. Node::Node(Document& document, NodeType type)
  49. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  50. , m_document(&document)
  51. , m_type(type)
  52. {
  53. if (!is_document())
  54. m_document->ref_from_node({});
  55. }
  56. Node::~Node()
  57. {
  58. ASSERT(m_deletion_has_begun);
  59. if (layout_node() && layout_node()->parent())
  60. layout_node()->parent()->remove_child(*layout_node());
  61. if (!is_document())
  62. m_document->unref_from_node({});
  63. }
  64. const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
  65. {
  66. for (auto* node = this; node; node = node->parent()) {
  67. if (is<HTML::HTMLAnchorElement>(*node) && downcast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
  68. return downcast<HTML::HTMLAnchorElement>(node);
  69. }
  70. return nullptr;
  71. }
  72. const HTML::HTMLElement* Node::enclosing_html_element() const
  73. {
  74. return first_ancestor_of_type<HTML::HTMLElement>();
  75. }
  76. String Node::text_content() const
  77. {
  78. StringBuilder builder;
  79. for (auto* child = first_child(); child; child = child->next_sibling()) {
  80. builder.append(child->text_content());
  81. }
  82. return builder.to_string();
  83. }
  84. void Node::set_text_content(const String& content)
  85. {
  86. if (is_text()) {
  87. downcast<Text>(this)->set_data(content);
  88. } else {
  89. remove_all_children();
  90. append_child(document().create_text_node(content));
  91. }
  92. set_needs_style_update(true);
  93. document().schedule_style_update();
  94. document().invalidate_layout();
  95. }
  96. RefPtr<LayoutNode> 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. void Node::dispatch_event(NonnullRefPtr<Event> event)
  113. {
  114. EventDispatcher::dispatch(*this, event);
  115. // FIXME: This is a hack. We should follow the real rules of event bubbling.
  116. if (parent())
  117. parent()->dispatch_event(move(event));
  118. }
  119. String Node::child_text_content() const
  120. {
  121. if (!is<ParentNode>(*this))
  122. return String::empty();
  123. StringBuilder builder;
  124. downcast<ParentNode>(*this).for_each_child([&](auto& child) {
  125. if (is<Text>(child))
  126. builder.append(downcast<Text>(child).text_content());
  127. });
  128. return builder.build();
  129. }
  130. const Node* Node::root() const
  131. {
  132. const Node* root = this;
  133. while (root->parent())
  134. root = root->parent();
  135. return root;
  136. }
  137. bool Node::is_connected() const
  138. {
  139. return root() && root()->is_document();
  140. }
  141. Element* Node::parent_element()
  142. {
  143. if (!parent() || !is<Element>(parent()))
  144. return nullptr;
  145. return downcast<Element>(parent());
  146. }
  147. const Element* Node::parent_element() const
  148. {
  149. if (!parent() || !is<Element>(parent()))
  150. return nullptr;
  151. return downcast<Element>(parent());
  152. }
  153. RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
  154. {
  155. if (&node->document() != &document())
  156. document().adopt_node(node);
  157. TreeNode<Node>::append_child(node, notify);
  158. return node;
  159. }
  160. RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
  161. {
  162. if (!child)
  163. return append_child(move(node), notify);
  164. if (child->parent_node() != this) {
  165. dbg() << "FIXME: Trying to insert_before() a bogus child";
  166. return nullptr;
  167. }
  168. if (&node->document() != &document())
  169. document().adopt_node(node);
  170. TreeNode<Node>::insert_before(node, child, notify);
  171. return node;
  172. }
  173. void Node::remove_all_children()
  174. {
  175. while (RefPtr<Node> child = first_child()) {
  176. remove_child(*child);
  177. }
  178. }
  179. void Node::set_document(Badge<Document>, Document& document)
  180. {
  181. if (m_document == &document)
  182. return;
  183. document.ref_from_node({});
  184. m_document->unref_from_node({});
  185. m_document = &document;
  186. }
  187. bool Node::is_editable() const
  188. {
  189. return parent() && parent()->is_editable();
  190. }
  191. Bindings::EventTargetWrapper* Node::create_wrapper(JS::GlobalObject& global_object)
  192. {
  193. return wrap(global_object, *this);
  194. }
  195. void Node::removed_last_ref()
  196. {
  197. if (is<Document>(*this)) {
  198. downcast<Document>(*this).removed_last_ref();
  199. return;
  200. }
  201. m_deletion_has_begun = true;
  202. delete this;
  203. }
  204. void Node::set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const
  205. {
  206. if (layout_node)
  207. m_layout_node = layout_node->make_weak_ptr();
  208. else
  209. m_layout_node = nullptr;
  210. }
  211. }