Node.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. m_document->ref_from_node({});
  54. }
  55. Node::~Node()
  56. {
  57. if (layout_node() && layout_node()->parent())
  58. layout_node()->parent()->remove_child(*layout_node());
  59. m_document->unref_from_node({});
  60. }
  61. const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
  62. {
  63. for (auto* node = this; node; node = node->parent()) {
  64. if (is<HTML::HTMLAnchorElement>(*node) && downcast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
  65. return downcast<HTML::HTMLAnchorElement>(node);
  66. }
  67. return nullptr;
  68. }
  69. const HTML::HTMLElement* Node::enclosing_html_element() const
  70. {
  71. return first_ancestor_of_type<HTML::HTMLElement>();
  72. }
  73. String Node::text_content() const
  74. {
  75. StringBuilder builder;
  76. for (auto* child = first_child(); child; child = child->next_sibling()) {
  77. builder.append(child->text_content());
  78. }
  79. return builder.to_string();
  80. }
  81. void Node::set_text_content(const String& content)
  82. {
  83. if (is_text()) {
  84. downcast<Text>(this)->set_data(content);
  85. } else {
  86. remove_all_children();
  87. append_child(document().create_text_node(content));
  88. }
  89. set_needs_style_update(true);
  90. document().schedule_style_update();
  91. document().invalidate_layout();
  92. }
  93. RefPtr<LayoutNode> Node::create_layout_node(const CSS::StyleProperties*)
  94. {
  95. return nullptr;
  96. }
  97. void Node::invalidate_style()
  98. {
  99. for_each_in_subtree_of_type<Element>([&](auto& element) {
  100. element.set_needs_style_update(true);
  101. return IterationDecision::Continue;
  102. });
  103. document().schedule_style_update();
  104. }
  105. bool Node::is_link() const
  106. {
  107. return enclosing_link_element();
  108. }
  109. void Node::dispatch_event(NonnullRefPtr<Event> event)
  110. {
  111. EventDispatcher::dispatch(*this, event);
  112. // FIXME: This is a hack. We should follow the real rules of event bubbling.
  113. if (parent())
  114. parent()->dispatch_event(move(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. const Node* Node::root() const
  128. {
  129. const Node* root = this;
  130. while (root->parent())
  131. root = root->parent();
  132. return root;
  133. }
  134. bool Node::is_connected() const
  135. {
  136. return root() && root()->is_document();
  137. }
  138. Element* Node::parent_element()
  139. {
  140. if (!parent() || !is<Element>(parent()))
  141. return nullptr;
  142. return downcast<Element>(parent());
  143. }
  144. const Element* Node::parent_element() const
  145. {
  146. if (!parent() || !is<Element>(parent()))
  147. return nullptr;
  148. return downcast<Element>(parent());
  149. }
  150. RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
  151. {
  152. if (&node->document() != &document())
  153. document().adopt_node(node);
  154. TreeNode<Node>::append_child(node, notify);
  155. return node;
  156. }
  157. RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
  158. {
  159. if (!child)
  160. return append_child(move(node), notify);
  161. if (child->parent_node() != this) {
  162. dbg() << "FIXME: Trying to insert_before() a bogus child";
  163. return nullptr;
  164. }
  165. if (&node->document() != &document())
  166. document().adopt_node(node);
  167. TreeNode<Node>::insert_before(node, child, notify);
  168. return node;
  169. }
  170. void Node::remove_all_children()
  171. {
  172. while (RefPtr<Node> child = first_child()) {
  173. remove_child(*child);
  174. }
  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. Bindings::EventTargetWrapper* 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. delete this;
  199. }
  200. void Node::set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const
  201. {
  202. if (layout_node)
  203. m_layout_node = layout_node->make_weak_ptr();
  204. else
  205. m_layout_node = nullptr;
  206. }
  207. }