Node.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #include <AK/Badge.h>
  3. #include <AK/RefPtr.h>
  4. #include <AK/String.h>
  5. #include <AK/Vector.h>
  6. #include <LibHTML/TreeNode.h>
  7. enum class NodeType : unsigned {
  8. INVALID = 0,
  9. ELEMENT_NODE = 1,
  10. TEXT_NODE = 3,
  11. COMMENT_NODE = 8,
  12. DOCUMENT_NODE = 9,
  13. DOCUMENT_TYPE_NODE = 10,
  14. DOCUMENT_FRAGMENT_NODE = 11,
  15. };
  16. class Document;
  17. class Element;
  18. class HTMLElement;
  19. class HTMLAnchorElement;
  20. class ParentNode;
  21. class LayoutNode;
  22. class StyleResolver;
  23. class StyleProperties;
  24. class Node : public TreeNode<Node> {
  25. public:
  26. virtual ~Node();
  27. NodeType type() const { return m_type; }
  28. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  29. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  30. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  31. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  32. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  33. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  34. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  35. bool is_parent_node() const { return is_element() || is_document(); }
  36. virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const;
  37. virtual String tag_name() const = 0;
  38. virtual String text_content() const;
  39. Document& document() { return m_document; }
  40. const Document& document() const { return m_document; }
  41. const HTMLAnchorElement* enclosing_link_element() const;
  42. const HTMLElement* enclosing_html_element() const;
  43. virtual bool is_html_element() const { return false; }
  44. template<typename T>
  45. const T* first_child_of_type() const;
  46. template<typename T>
  47. const T* first_ancestor_of_type() const;
  48. virtual void inserted_into(Node&) {}
  49. virtual void removed_from(Node&) {}
  50. const LayoutNode* layout_node() const { return m_layout_node; }
  51. LayoutNode* layout_node() { return m_layout_node; }
  52. void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
  53. const Element* previous_element_sibling() const;
  54. const Element* next_element_sibling() const;
  55. virtual bool is_child_allowed(const Node&) const { return true; }
  56. bool needs_style_update() const { return m_needs_style_update; }
  57. void set_needs_style_update(bool value) { m_needs_style_update = value; }
  58. void invalidate_style();
  59. bool is_link() const;
  60. protected:
  61. Node(Document&, NodeType);
  62. Document& m_document;
  63. mutable LayoutNode* m_layout_node { nullptr };
  64. NodeType m_type { NodeType::INVALID };
  65. bool m_needs_style_update { false };
  66. };
  67. template<typename T>
  68. inline bool is(const Node&)
  69. {
  70. return false;
  71. }
  72. template<typename T>
  73. inline bool is(const Node* node)
  74. {
  75. return !node || is<T>(*node);
  76. }
  77. template<>
  78. inline bool is<Node>(const Node&)
  79. {
  80. return true;
  81. }
  82. template<>
  83. inline bool is<ParentNode>(const Node& node)
  84. {
  85. return node.is_parent_node();
  86. }
  87. template<typename T>
  88. inline const T& to(const Node& node)
  89. {
  90. ASSERT(is<T>(node));
  91. return static_cast<const T&>(node);
  92. }
  93. template<typename T>
  94. inline T* to(Node* node)
  95. {
  96. ASSERT(is<T>(node));
  97. return static_cast<T*>(node);
  98. }
  99. template<typename T>
  100. inline const T* to(const Node* node)
  101. {
  102. ASSERT(is<T>(node));
  103. return static_cast<const T*>(node);
  104. }
  105. template<typename T>
  106. inline T& to(Node& node)
  107. {
  108. ASSERT(is<T>(node));
  109. return static_cast<T&>(node);
  110. }
  111. template<typename T>
  112. inline const T* Node::first_child_of_type() const
  113. {
  114. for (auto* child = first_child(); child; child = child->next_sibling()) {
  115. if (is<T>(*child))
  116. return to<T>(child);
  117. }
  118. return nullptr;
  119. }
  120. template<typename T>
  121. inline const T* Node::first_ancestor_of_type() const
  122. {
  123. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  124. if (is<T>(*ancestor))
  125. return to<T>(ancestor);
  126. }
  127. return nullptr;
  128. }