Node.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. DOCUMENT_NODE = 9,
  12. };
  13. class Document;
  14. class Element;
  15. class HTMLElement;
  16. class HTMLAnchorElement;
  17. class ParentNode;
  18. class LayoutNode;
  19. class StyleResolver;
  20. class StyleProperties;
  21. class Node : public TreeNode<Node> {
  22. public:
  23. virtual ~Node();
  24. NodeType type() const { return m_type; }
  25. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  26. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  27. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  28. bool is_parent_node() const { return is_element() || is_document(); }
  29. virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_properties) const = 0;
  30. RefPtr<LayoutNode> create_layout_tree(const StyleResolver&, const StyleProperties* parent_properties) const;
  31. virtual String tag_name() const = 0;
  32. virtual String text_content() const;
  33. Document& document() { return m_document; }
  34. const Document& document() const { return m_document; }
  35. const HTMLAnchorElement* enclosing_link_element() const;
  36. const HTMLElement* enclosing_html_element() const;
  37. virtual bool is_html_element() const { return false; }
  38. template<typename T>
  39. const T* first_child_of_type() const;
  40. virtual void inserted_into(Node&) {}
  41. virtual void removed_from(Node&) {}
  42. const LayoutNode* layout_node() const { return m_layout_node; }
  43. LayoutNode* layout_node() { return m_layout_node; }
  44. void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
  45. const Element* previous_element_sibling() const;
  46. const Element* next_element_sibling() const;
  47. protected:
  48. Node(Document&, NodeType);
  49. Document& m_document;
  50. mutable LayoutNode* m_layout_node { nullptr };
  51. NodeType m_type { NodeType::INVALID };
  52. };
  53. template<typename T>
  54. inline bool is(const Node&)
  55. {
  56. return false;
  57. }
  58. template<typename T>
  59. inline bool is(const Node* node)
  60. {
  61. return node && is<T>(*node);
  62. }
  63. template<>
  64. inline bool is<Node>(const Node&)
  65. {
  66. return true;
  67. }
  68. template<>
  69. inline bool is<ParentNode>(const Node& node)
  70. {
  71. return node.is_parent_node();
  72. }
  73. template<typename T>
  74. inline const T& to(const Node& node)
  75. {
  76. ASSERT(is<T>(node));
  77. return static_cast<const T&>(node);
  78. }
  79. template<typename T>
  80. inline T* to(Node* node)
  81. {
  82. ASSERT(is<T>(node));
  83. return static_cast<T*>(node);
  84. }
  85. template<typename T>
  86. inline const T* to(const Node* node)
  87. {
  88. ASSERT(is<T>(node));
  89. return static_cast<const T*>(node);
  90. }
  91. template<typename T>
  92. inline T& to(Node& node)
  93. {
  94. ASSERT(is<T>(node));
  95. return static_cast<T&>(node);
  96. }
  97. template<typename T>
  98. inline const T* Node::first_child_of_type() const
  99. {
  100. for (auto* child = first_child(); child; child = child->next_sibling()) {
  101. if (is<T>(*child))
  102. return to<T>(child);
  103. }
  104. return nullptr;
  105. }