Node.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/ScriptFunction.h>
  31. #include <LibWeb/Bindings/EventWrapper.h>
  32. #include <LibWeb/Bindings/EventWrapperFactory.h>
  33. #include <LibWeb/Bindings/NodeWrapper.h>
  34. #include <LibWeb/Bindings/NodeWrapperFactory.h>
  35. #include <LibWeb/CSS/StyleResolver.h>
  36. #include <LibWeb/DOM/Element.h>
  37. #include <LibWeb/DOM/Event.h>
  38. #include <LibWeb/DOM/EventDispatcher.h>
  39. #include <LibWeb/DOM/EventListener.h>
  40. #include <LibWeb/DOM/Node.h>
  41. #include <LibWeb/HTML/HTMLAnchorElement.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. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  51. , m_document(&document)
  52. , m_type(type)
  53. {
  54. }
  55. Node::~Node()
  56. {
  57. if (layout_node() && layout_node()->parent())
  58. layout_node()->parent()->remove_child(*layout_node());
  59. }
  60. const HTML::HTMLAnchorElement* Node::enclosing_link_element() const
  61. {
  62. for (auto* node = this; node; node = node->parent()) {
  63. if (is<HTML::HTMLAnchorElement>(*node) && downcast<HTML::HTMLAnchorElement>(*node).has_attribute(HTML::AttributeNames::href))
  64. return downcast<HTML::HTMLAnchorElement>(node);
  65. }
  66. return nullptr;
  67. }
  68. const HTML::HTMLElement* Node::enclosing_html_element() const
  69. {
  70. return first_ancestor_of_type<HTML::HTMLElement>();
  71. }
  72. String Node::text_content() const
  73. {
  74. StringBuilder builder;
  75. for (auto* child = first_child(); child; child = child->next_sibling()) {
  76. builder.append(child->text_content());
  77. }
  78. return builder.to_string();
  79. }
  80. void Node::set_text_content(const String& content)
  81. {
  82. if (is_text()) {
  83. downcast<Text>(this)->set_data(content);
  84. } else {
  85. remove_all_children();
  86. append_child(document().create_text_node(content));
  87. }
  88. set_needs_style_update(true);
  89. document().schedule_style_update();
  90. document().invalidate_layout();
  91. }
  92. RefPtr<LayoutNode> Node::create_layout_node(const CSS::StyleProperties*)
  93. {
  94. return nullptr;
  95. }
  96. void Node::invalidate_style()
  97. {
  98. for_each_in_subtree_of_type<Element>([&](auto& element) {
  99. element.set_needs_style_update(true);
  100. return IterationDecision::Continue;
  101. });
  102. document().schedule_style_update();
  103. }
  104. bool Node::is_link() const
  105. {
  106. return enclosing_link_element();
  107. }
  108. void Node::dispatch_event(NonnullRefPtr<Event> event)
  109. {
  110. EventDispatcher::dispatch(*this, event);
  111. // FIXME: This is a hack. We should follow the real rules of event bubbling.
  112. if (parent())
  113. parent()->dispatch_event(move(event));
  114. }
  115. String Node::child_text_content() const
  116. {
  117. if (!is<ParentNode>(*this))
  118. return String::empty();
  119. StringBuilder builder;
  120. downcast<ParentNode>(*this).for_each_child([&](auto& child) {
  121. if (is<Text>(child))
  122. builder.append(downcast<Text>(child).text_content());
  123. });
  124. return builder.build();
  125. }
  126. const Node* Node::root() const
  127. {
  128. const Node* root = this;
  129. while (root->parent())
  130. root = root->parent();
  131. return root;
  132. }
  133. bool Node::is_connected() const
  134. {
  135. return root() && root()->is_document();
  136. }
  137. Element* Node::parent_element()
  138. {
  139. if (!parent() || !is<Element>(parent()))
  140. return nullptr;
  141. return downcast<Element>(parent());
  142. }
  143. const Element* Node::parent_element() const
  144. {
  145. if (!parent() || !is<Element>(parent()))
  146. return nullptr;
  147. return downcast<Element>(parent());
  148. }
  149. RefPtr<Node> Node::append_child(NonnullRefPtr<Node> node, bool notify)
  150. {
  151. TreeNode<Node>::append_child(node, notify);
  152. return node;
  153. }
  154. RefPtr<Node> Node::insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify)
  155. {
  156. if (!child)
  157. return append_child(move(node), notify);
  158. if (child->parent_node() != this) {
  159. dbg() << "FIXME: Trying to insert_before() a bogus child";
  160. return nullptr;
  161. }
  162. TreeNode<Node>::insert_before(node, child, notify);
  163. return node;
  164. }
  165. void Node::remove_all_children()
  166. {
  167. while (RefPtr<Node> child = first_child()) {
  168. remove_child(*child);
  169. }
  170. }
  171. void Node::set_document(Badge<Document>, Document& document)
  172. {
  173. m_document = &document;
  174. }
  175. bool Node::is_editable() const
  176. {
  177. return parent() && parent()->is_editable();
  178. }
  179. Bindings::EventTargetWrapper* Node::create_wrapper(JS::GlobalObject& global_object)
  180. {
  181. return wrap(global_object, *this);
  182. }
  183. }