Node.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/RefPtr.h>
  9. #include <AK/String.h>
  10. #include <AK/TypeCasts.h>
  11. #include <AK/Vector.h>
  12. #include <LibWeb/Bindings/Wrappable.h>
  13. #include <LibWeb/DOM/EventTarget.h>
  14. #include <LibWeb/DOM/ExceptionOr.h>
  15. #include <LibWeb/TreeNode.h>
  16. namespace Web::DOM {
  17. enum class NodeType : u16 {
  18. INVALID = 0,
  19. ELEMENT_NODE = 1,
  20. ATTRIBUTE_NODE = 2,
  21. TEXT_NODE = 3,
  22. CDATA_SECTION_NODE = 4,
  23. ENTITY_REFERENCE_NODE = 5,
  24. ENTITY_NODE = 6,
  25. PROCESSING_INSTRUCTION_NODE = 7,
  26. COMMENT_NODE = 8,
  27. DOCUMENT_NODE = 9,
  28. DOCUMENT_TYPE_NODE = 10,
  29. DOCUMENT_FRAGMENT_NODE = 11,
  30. NOTATION_NODE = 12
  31. };
  32. class Node
  33. : public TreeNode<Node>
  34. , public EventTarget
  35. , public Bindings::Wrappable {
  36. public:
  37. using WrapperType = Bindings::NodeWrapper;
  38. using TreeNode<Node>::ref;
  39. using TreeNode<Node>::unref;
  40. ParentNode* parent_or_shadow_host();
  41. const ParentNode* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
  42. // ^EventTarget
  43. virtual void ref_event_target() final { ref(); }
  44. virtual void unref_event_target() final { unref(); }
  45. virtual bool dispatch_event(NonnullRefPtr<Event>) final;
  46. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  47. virtual ~Node();
  48. void removed_last_ref();
  49. NodeType type() const { return m_type; }
  50. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  51. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  52. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  53. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  54. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  55. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  56. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  57. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  58. bool is_slottable() const { return is_element() || is_text(); }
  59. // NOTE: This is intended for the JS bindings.
  60. u16 node_type() const { return (u16)m_type; }
  61. virtual bool is_editable() const;
  62. ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
  63. ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);
  64. ExceptionOr<NonnullRefPtr<Node>> append_child(NonnullRefPtr<Node>);
  65. void insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers = false);
  66. void remove(bool suppress_observers = false);
  67. void remove_all_children(bool suppress_observers = false);
  68. u16 compare_document_position(RefPtr<Node> other);
  69. NonnullRefPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false) const;
  70. ExceptionOr<NonnullRefPtr<Node>> clone_node_binding(bool deep) const;
  71. // NOTE: This is intended for the JS bindings.
  72. bool has_child_nodes() const { return has_children(); }
  73. NonnullRefPtrVector<Node> child_nodes() const;
  74. virtual RefPtr<Layout::Node> create_layout_node();
  75. virtual FlyString node_name() const = 0;
  76. virtual String text_content() const;
  77. void set_text_content(const String&);
  78. Document& document() { return *m_document; }
  79. const Document& document() const { return *m_document; }
  80. RefPtr<Document> owner_document() const;
  81. const HTML::HTMLAnchorElement* enclosing_link_element() const;
  82. const HTML::HTMLElement* enclosing_html_element() const;
  83. const HTML::HTMLElement* enclosing_html_element_with_attribute(const FlyString&) const;
  84. String child_text_content() const;
  85. Node* root();
  86. const Node* root() const
  87. {
  88. return const_cast<Node*>(this)->root();
  89. }
  90. Node* shadow_including_root();
  91. const Node* shadow_including_root() const
  92. {
  93. return const_cast<Node*>(this)->shadow_including_root();
  94. }
  95. bool is_connected() const;
  96. Node* parent_node() { return parent(); }
  97. const Node* parent_node() const { return parent(); }
  98. Element* parent_element();
  99. const Element* parent_element() const;
  100. virtual void inserted();
  101. virtual void removed_from(Node*) { }
  102. virtual void children_changed() { }
  103. virtual void adopted_from(const Document&) { }
  104. const Layout::Node* layout_node() const { return m_layout_node; }
  105. Layout::Node* layout_node() { return m_layout_node; }
  106. void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
  107. virtual bool is_child_allowed(const Node&) const { return true; }
  108. bool needs_style_update() const { return m_needs_style_update; }
  109. void set_needs_style_update(bool);
  110. bool child_needs_style_update() const { return m_child_needs_style_update; }
  111. void set_child_needs_style_update(bool b) { m_child_needs_style_update = b; }
  112. void invalidate_style();
  113. bool is_link() const;
  114. void set_document(Badge<Document>, Document&);
  115. virtual EventTarget* get_parent(const Event&) override;
  116. template<typename T>
  117. bool fast_is() const = delete;
  118. ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
  119. bool is_host_including_inclusive_ancestor_of(const Node&) const;
  120. protected:
  121. Node(Document&, NodeType);
  122. Document* m_document { nullptr };
  123. mutable WeakPtr<Layout::Node> m_layout_node;
  124. NodeType m_type { NodeType::INVALID };
  125. bool m_needs_style_update { false };
  126. bool m_child_needs_style_update { false };
  127. };
  128. }