Node.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/RefPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <LibWeb/Bindings/Wrappable.h>
  32. #include <LibWeb/DOM/EventTarget.h>
  33. #include <LibWeb/TreeNode.h>
  34. namespace Web {
  35. enum class NodeType : unsigned {
  36. INVALID = 0,
  37. ELEMENT_NODE = 1,
  38. TEXT_NODE = 3,
  39. COMMENT_NODE = 8,
  40. DOCUMENT_NODE = 9,
  41. DOCUMENT_TYPE_NODE = 10,
  42. DOCUMENT_FRAGMENT_NODE = 11,
  43. };
  44. class Document;
  45. class Element;
  46. class HTMLElement;
  47. class HTMLAnchorElement;
  48. class ParentNode;
  49. class LayoutNode;
  50. class StyleResolver;
  51. class StyleProperties;
  52. class Node
  53. : public TreeNode<Node>
  54. , public EventTarget
  55. , public Bindings::Wrappable {
  56. public:
  57. using WrapperType = Bindings::NodeWrapper;
  58. using TreeNode<Node>::ref;
  59. using TreeNode<Node>::unref;
  60. // ^EventTarget
  61. virtual void ref_event_target() final { ref(); }
  62. virtual void unref_event_target() final { unref(); }
  63. virtual void dispatch_event(NonnullRefPtr<Event>) final;
  64. virtual ~Node();
  65. NodeType type() const { return m_type; }
  66. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  67. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  68. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  69. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  70. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  71. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  72. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  73. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  74. virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const;
  75. virtual FlyString tag_name() const = 0;
  76. virtual String text_content() const;
  77. Document& document() { return m_document; }
  78. const Document& document() const { return m_document; }
  79. const HTMLAnchorElement* enclosing_link_element() const;
  80. const HTMLElement* enclosing_html_element() const;
  81. String child_text_content() const;
  82. virtual bool is_html_element() const { return false; }
  83. const Node* root() const;
  84. bool is_connected() const;
  85. template<typename T>
  86. const T* first_child_of_type() const;
  87. template<typename T>
  88. const T* first_ancestor_of_type() const;
  89. virtual void inserted_into(Node&) {}
  90. virtual void removed_from(Node&) {}
  91. virtual void children_changed() {}
  92. const LayoutNode* layout_node() const { return m_layout_node; }
  93. LayoutNode* layout_node() { return m_layout_node; }
  94. void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
  95. const Element* previous_element_sibling() const;
  96. const Element* next_element_sibling() const;
  97. virtual bool is_child_allowed(const Node&) const { return true; }
  98. bool needs_style_update() const { return m_needs_style_update; }
  99. void set_needs_style_update(bool value) { m_needs_style_update = value; }
  100. void invalidate_style();
  101. bool is_link() const;
  102. virtual void document_did_attach_to_frame(Frame&) {}
  103. virtual void document_will_detach_from_frame(Frame&) {}
  104. protected:
  105. Node(Document&, NodeType);
  106. Document& m_document;
  107. mutable LayoutNode* m_layout_node { nullptr };
  108. NodeType m_type { NodeType::INVALID };
  109. bool m_needs_style_update { false };
  110. };
  111. template<typename T>
  112. inline bool is(const Node&)
  113. {
  114. return false;
  115. }
  116. template<typename T>
  117. inline bool is(const Node* node)
  118. {
  119. return !node || is<T>(*node);
  120. }
  121. template<>
  122. inline bool is<Node>(const Node&)
  123. {
  124. return true;
  125. }
  126. template<>
  127. inline bool is<ParentNode>(const Node& node)
  128. {
  129. return node.is_parent_node();
  130. }
  131. template<typename T>
  132. inline const T& to(const Node& node)
  133. {
  134. ASSERT(is<T>(node));
  135. return static_cast<const T&>(node);
  136. }
  137. template<typename T>
  138. inline T* to(Node* node)
  139. {
  140. ASSERT(is<T>(node));
  141. return static_cast<T*>(node);
  142. }
  143. template<typename T>
  144. inline const T* to(const Node* node)
  145. {
  146. ASSERT(is<T>(node));
  147. return static_cast<const T*>(node);
  148. }
  149. template<typename T>
  150. inline T& to(Node& node)
  151. {
  152. ASSERT(is<T>(node));
  153. return static_cast<T&>(node);
  154. }
  155. template<typename T>
  156. inline const T* Node::first_child_of_type() const
  157. {
  158. for (auto* child = first_child(); child; child = child->next_sibling()) {
  159. if (is<T>(*child))
  160. return to<T>(child);
  161. }
  162. return nullptr;
  163. }
  164. template<typename T>
  165. inline const T* Node::first_ancestor_of_type() const
  166. {
  167. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  168. if (is<T>(*ancestor))
  169. return to<T>(ancestor);
  170. }
  171. return nullptr;
  172. }
  173. }