Node.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 HTMLElement;
  15. class HTMLAnchorElement;
  16. class ParentNode;
  17. class LayoutNode;
  18. class StyleResolver;
  19. class StyleProperties;
  20. class Node : public TreeNode<Node> {
  21. public:
  22. virtual ~Node();
  23. NodeType type() const { return m_type; }
  24. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  25. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  26. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  27. bool is_parent_node() const { return is_element() || is_document(); }
  28. virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_properties) const = 0;
  29. RefPtr<LayoutNode> create_layout_tree(const StyleResolver&, const StyleProperties* parent_properties) const;
  30. virtual String tag_name() const = 0;
  31. virtual String text_content() const;
  32. Document& document() { return m_document; }
  33. const Document& document() const { return m_document; }
  34. const HTMLAnchorElement* enclosing_link_element() const;
  35. const HTMLElement* enclosing_html_element() const;
  36. virtual bool is_html_element() const { return false; }
  37. const Node* first_child_with_tag_name(const StringView& tag_name) const
  38. {
  39. for (auto* child = first_child(); child; child = child->next_sibling()) {
  40. if (child->tag_name() == tag_name)
  41. return child;
  42. }
  43. return nullptr;
  44. }
  45. virtual void inserted_into(Node&) {}
  46. virtual void removed_from(Node&) {}
  47. const LayoutNode* layout_node() const { return m_layout_node; }
  48. LayoutNode* layout_node() { return m_layout_node; }
  49. void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
  50. protected:
  51. Node(Document&, NodeType);
  52. Document& m_document;
  53. mutable LayoutNode* m_layout_node { nullptr };
  54. NodeType m_type { NodeType::INVALID };
  55. };