Node.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <LibHTML/TreeNode.h>
  32. enum class NodeType : unsigned {
  33. INVALID = 0,
  34. ELEMENT_NODE = 1,
  35. TEXT_NODE = 3,
  36. COMMENT_NODE = 8,
  37. DOCUMENT_NODE = 9,
  38. DOCUMENT_TYPE_NODE = 10,
  39. DOCUMENT_FRAGMENT_NODE = 11,
  40. };
  41. class Document;
  42. class Element;
  43. class HTMLElement;
  44. class HTMLAnchorElement;
  45. class ParentNode;
  46. class LayoutNode;
  47. class StyleResolver;
  48. class StyleProperties;
  49. class Node : public TreeNode<Node> {
  50. public:
  51. virtual ~Node();
  52. NodeType type() const { return m_type; }
  53. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  54. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  55. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  56. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  57. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  58. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  59. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  60. bool is_parent_node() const { return is_element() || is_document(); }
  61. virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const;
  62. virtual String tag_name() const = 0;
  63. virtual String text_content() const;
  64. Document& document() { return m_document; }
  65. const Document& document() const { return m_document; }
  66. const HTMLAnchorElement* enclosing_link_element() const;
  67. const HTMLElement* enclosing_html_element() const;
  68. virtual bool is_html_element() const { return false; }
  69. template<typename T>
  70. const T* first_child_of_type() const;
  71. template<typename T>
  72. const T* first_ancestor_of_type() const;
  73. virtual void inserted_into(Node&) {}
  74. virtual void removed_from(Node&) {}
  75. const LayoutNode* layout_node() const { return m_layout_node; }
  76. LayoutNode* layout_node() { return m_layout_node; }
  77. void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
  78. const Element* previous_element_sibling() const;
  79. const Element* next_element_sibling() const;
  80. virtual bool is_child_allowed(const Node&) const { return true; }
  81. bool needs_style_update() const { return m_needs_style_update; }
  82. void set_needs_style_update(bool value) { m_needs_style_update = value; }
  83. void invalidate_style();
  84. bool is_link() const;
  85. protected:
  86. Node(Document&, NodeType);
  87. Document& m_document;
  88. mutable LayoutNode* m_layout_node { nullptr };
  89. NodeType m_type { NodeType::INVALID };
  90. bool m_needs_style_update { false };
  91. };
  92. template<typename T>
  93. inline bool is(const Node&)
  94. {
  95. return false;
  96. }
  97. template<typename T>
  98. inline bool is(const Node* node)
  99. {
  100. return !node || is<T>(*node);
  101. }
  102. template<>
  103. inline bool is<Node>(const Node&)
  104. {
  105. return true;
  106. }
  107. template<>
  108. inline bool is<ParentNode>(const Node& node)
  109. {
  110. return node.is_parent_node();
  111. }
  112. template<typename T>
  113. inline const T& to(const Node& node)
  114. {
  115. ASSERT(is<T>(node));
  116. return static_cast<const T&>(node);
  117. }
  118. template<typename T>
  119. inline T* to(Node* node)
  120. {
  121. ASSERT(is<T>(node));
  122. return static_cast<T*>(node);
  123. }
  124. template<typename T>
  125. inline const T* to(const Node* node)
  126. {
  127. ASSERT(is<T>(node));
  128. return static_cast<const T*>(node);
  129. }
  130. template<typename T>
  131. inline T& to(Node& node)
  132. {
  133. ASSERT(is<T>(node));
  134. return static_cast<T&>(node);
  135. }
  136. template<typename T>
  137. inline const T* Node::first_child_of_type() const
  138. {
  139. for (auto* child = first_child(); child; child = child->next_sibling()) {
  140. if (is<T>(*child))
  141. return to<T>(child);
  142. }
  143. return nullptr;
  144. }
  145. template<typename T>
  146. inline const T* Node::first_ancestor_of_type() const
  147. {
  148. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  149. if (is<T>(*ancestor))
  150. return to<T>(ancestor);
  151. }
  152. return nullptr;
  153. }