Node.cpp 5.9 KB

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