Node.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/Interpreter.h>
  29. #include <LibJS/Runtime/Function.h>
  30. #include <LibJS/Runtime/MarkedValueList.h>
  31. #include <LibJS/Runtime/ScriptFunction.h>
  32. #include <LibWeb/Bindings/EventWrapper.h>
  33. #include <LibWeb/Bindings/EventWrapperFactory.h>
  34. #include <LibWeb/Bindings/NodeWrapper.h>
  35. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  36. #include <LibWeb/CSS/StyleResolver.h>
  37. #include <LibWeb/DOM/Element.h>
  38. #include <LibWeb/DOM/Event.h>
  39. #include <LibWeb/DOM/EventListener.h>
  40. #include <LibWeb/HTML/HTMLAnchorElement.h>
  41. #include <LibWeb/DOM/Node.h>
  42. #include <LibWeb/Layout/LayoutBlock.h>
  43. #include <LibWeb/Layout/LayoutDocument.h>
  44. #include <LibWeb/Layout/LayoutInline.h>
  45. #include <LibWeb/Layout/LayoutNode.h>
  46. #include <LibWeb/Layout/LayoutText.h>
  47. //#define EVENT_DEBUG
  48. namespace Web::DOM {
  49. Node::Node(Document& document, NodeType type)
  50. : m_document(&document)
  51. , m_type(type)
  52. {
  53. }
  54. Node::~Node()
  55. {
  56. if (layout_node() && layout_node()->parent())
  57. layout_node()->parent()->remove_child(*layout_node());
  58. }
  59. const HTMLAnchorElement* Node::enclosing_link_element() const
  60. {
  61. for (auto* node = this; node; node = node->parent()) {
  62. if (is<HTMLAnchorElement>(*node) && downcast<HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
  63. return downcast<HTMLAnchorElement>(node);
  64. }
  65. return nullptr;
  66. }
  67. const HTMLElement* Node::enclosing_html_element() const
  68. {
  69. return first_ancestor_of_type<HTMLElement>();
  70. }
  71. String Node::text_content() const
  72. {
  73. Vector<String> strings;
  74. StringBuilder builder;
  75. for (auto* child = first_child(); child; child = child->next_sibling()) {
  76. auto text = child->text_content();
  77. if (!text.is_empty()) {
  78. builder.append(child->text_content());
  79. builder.append(' ');
  80. }
  81. }
  82. if (builder.length() > 1)
  83. builder.trim(1);
  84. return builder.to_string();
  85. }
  86. const Element* Node::next_element_sibling() const
  87. {
  88. for (auto* node = next_sibling(); node; node = node->next_sibling()) {
  89. if (node->is_element())
  90. return static_cast<const Element*>(node);
  91. }
  92. return nullptr;
  93. }
  94. const Element* Node::previous_element_sibling() const
  95. {
  96. for (auto* node = previous_sibling(); node; node = node->previous_sibling()) {
  97. if (node->is_element())
  98. return static_cast<const Element*>(node);
  99. }
  100. return nullptr;
  101. }
  102. RefPtr<LayoutNode> Node::create_layout_node(const CSS::StyleProperties*)
  103. {
  104. return nullptr;
  105. }
  106. void Node::invalidate_style()
  107. {
  108. for_each_in_subtree_of_type<Element>([&](auto& element) {
  109. element.set_needs_style_update(true);
  110. return IterationDecision::Continue;
  111. });
  112. document().schedule_style_update();
  113. }
  114. bool Node::is_link() const
  115. {
  116. return enclosing_link_element();
  117. }
  118. void Node::dispatch_event(NonnullRefPtr<Event> event)
  119. {
  120. for (auto& listener : listeners()) {
  121. if (listener.event_name == event->type()) {
  122. auto& function = const_cast<EventListener&>(*listener.listener).function();
  123. #ifdef EVENT_DEBUG
  124. static_cast<const JS::ScriptFunction&>(function).body().dump(0);
  125. #endif
  126. auto& global_object = function.global_object();
  127. auto* this_value = wrap(global_object, *this);
  128. #ifdef EVENT_DEBUG
  129. dbg() << "calling event listener with this=" << this_value;
  130. #endif
  131. auto* event_wrapper = wrap(global_object, *event);
  132. JS::MarkedValueList arguments(global_object.heap());
  133. arguments.append(event_wrapper);
  134. document().interpreter().call(function, this_value, move(arguments));
  135. }
  136. }
  137. // FIXME: This is a hack. We should follow the real rules of event bubbling.
  138. if (parent())
  139. parent()->dispatch_event(move(event));
  140. }
  141. String Node::child_text_content() const
  142. {
  143. if (!is<ParentNode>(*this))
  144. return String::empty();
  145. StringBuilder builder;
  146. downcast<ParentNode>(*this).for_each_child([&](auto& child) {
  147. if (is<Text>(child))
  148. builder.append(downcast<Text>(child).text_content());
  149. });
  150. return builder.build();
  151. }
  152. const Node* Node::root() const
  153. {
  154. const Node* root = this;
  155. while (root->parent())
  156. root = root->parent();
  157. return root;
  158. }
  159. bool Node::is_connected() const
  160. {
  161. return root() && root()->is_document();
  162. }
  163. Element* Node::parent_element()
  164. {
  165. if (!parent() || !is<Element>(parent()))
  166. return nullptr;
  167. return downcast<Element>(parent());
  168. }
  169. const Element* Node::parent_element() const
  170. {
  171. if (!parent() || !is<Element>(parent()))
  172. return nullptr;
  173. return downcast<Element>(parent());
  174. }
  175. RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
  176. {
  177. TreeNode<Node>::append_child(node, notify);
  178. return node;
  179. }
  180. RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
  181. {
  182. if (!child)
  183. return append_child(move(node), notify);
  184. if (child->parent_node() != this) {
  185. dbg() << "FIXME: Trying to insert_before() a bogus child";
  186. return nullptr;
  187. }
  188. TreeNode<Node>::insert_before(node, child, notify);
  189. return node;
  190. }
  191. void Node::set_document(Badge<Document>, Document& document)
  192. {
  193. m_document = &document;
  194. }
  195. }