Node.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2018-2022, 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/JsonObjectSerializer.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/String.h>
  11. #include <AK/TypeCasts.h>
  12. #include <AK/Vector.h>
  13. #include <LibWeb/Bindings/Wrappable.h>
  14. #include <LibWeb/DOM/EventTarget.h>
  15. #include <LibWeb/DOM/ExceptionOr.h>
  16. #include <LibWeb/TreeNode.h>
  17. namespace Web::DOM {
  18. enum class NodeType : u16 {
  19. INVALID = 0,
  20. ELEMENT_NODE = 1,
  21. ATTRIBUTE_NODE = 2,
  22. TEXT_NODE = 3,
  23. CDATA_SECTION_NODE = 4,
  24. ENTITY_REFERENCE_NODE = 5,
  25. ENTITY_NODE = 6,
  26. PROCESSING_INSTRUCTION_NODE = 7,
  27. COMMENT_NODE = 8,
  28. DOCUMENT_NODE = 9,
  29. DOCUMENT_TYPE_NODE = 10,
  30. DOCUMENT_FRAGMENT_NODE = 11,
  31. NOTATION_NODE = 12
  32. };
  33. struct GetRootNodeOptions {
  34. bool composed { false };
  35. };
  36. class Node
  37. : public TreeNode<Node>
  38. , public EventTarget
  39. , public Bindings::Wrappable {
  40. public:
  41. using WrapperType = Bindings::NodeWrapper;
  42. using TreeNode<Node>::ref;
  43. using TreeNode<Node>::unref;
  44. ParentNode* parent_or_shadow_host();
  45. const ParentNode* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
  46. // ^EventTarget
  47. virtual void ref_event_target() final { ref(); }
  48. virtual void unref_event_target() final { unref(); }
  49. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  50. virtual ~Node();
  51. void removed_last_ref();
  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() || is_document_fragment(); }
  61. bool is_slottable() const { return is_element() || is_text(); }
  62. bool is_attribute() const { return type() == NodeType::ATTRIBUTE_NODE; }
  63. virtual bool is_shadow_root() const { return false; }
  64. virtual bool requires_svg_container() const { return false; }
  65. virtual bool is_svg_container() const { return false; }
  66. virtual bool is_svg_svg_element() const { return false; }
  67. bool in_a_document_tree() const;
  68. // NOTE: This is intended for the JS bindings.
  69. u16 node_type() const { return (u16)m_type; }
  70. virtual bool is_editable() const;
  71. virtual bool is_html_html_element() const { return false; }
  72. virtual bool is_html_anchor_element() const { return false; }
  73. virtual bool is_html_template_element() const { return false; }
  74. virtual bool is_browsing_context_container() const { return false; }
  75. ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
  76. ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);
  77. ExceptionOr<NonnullRefPtr<Node>> append_child(NonnullRefPtr<Node>);
  78. ExceptionOr<NonnullRefPtr<Node>> remove_child(NonnullRefPtr<Node>);
  79. void insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers = false);
  80. void remove(bool suppress_observers = false);
  81. void remove_all_children(bool suppress_observers = false);
  82. u16 compare_document_position(RefPtr<Node> other);
  83. ExceptionOr<NonnullRefPtr<Node>> replace_child(NonnullRefPtr<Node> node, NonnullRefPtr<Node> child);
  84. NonnullRefPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
  85. ExceptionOr<NonnullRefPtr<Node>> clone_node_binding(bool deep);
  86. // NOTE: This is intended for the JS bindings.
  87. bool has_child_nodes() const { return has_children(); }
  88. NonnullRefPtr<NodeList> child_nodes();
  89. NonnullRefPtrVector<Node> children_as_vector() const;
  90. virtual FlyString node_name() const = 0;
  91. String descendant_text_content() const;
  92. String text_content() const;
  93. void set_text_content(String const&);
  94. String node_value() const;
  95. void set_node_value(String const&);
  96. Document& document() { return *m_document; }
  97. const Document& document() const { return *m_document; }
  98. RefPtr<Document> owner_document() const;
  99. const HTML::HTMLAnchorElement* enclosing_link_element() const;
  100. const HTML::HTMLElement* enclosing_html_element() const;
  101. const HTML::HTMLElement* enclosing_html_element_with_attribute(const FlyString&) const;
  102. String child_text_content() const;
  103. Node& root();
  104. const Node& root() const
  105. {
  106. return const_cast<Node*>(this)->root();
  107. }
  108. Node& shadow_including_root();
  109. const Node& shadow_including_root() const
  110. {
  111. return const_cast<Node*>(this)->shadow_including_root();
  112. }
  113. bool is_connected() const;
  114. Node* parent_node() { return parent(); }
  115. const Node* parent_node() const { return parent(); }
  116. Element* parent_element();
  117. const Element* parent_element() const;
  118. virtual void inserted();
  119. virtual void removed_from(Node*) { }
  120. virtual void children_changed() { }
  121. virtual void adopted_from(Document&) { }
  122. virtual void cloned(Node&, bool) {};
  123. const Layout::Node* layout_node() const { return m_layout_node; }
  124. Layout::Node* layout_node() { return m_layout_node; }
  125. Painting::PaintableBox const* paint_box() const;
  126. Painting::Paintable const* paintable() const;
  127. void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
  128. virtual bool is_child_allowed(const Node&) const { return true; }
  129. bool needs_style_update() const { return m_needs_style_update; }
  130. void set_needs_style_update(bool);
  131. bool child_needs_style_update() const { return m_child_needs_style_update; }
  132. void set_child_needs_style_update(bool b) { m_child_needs_style_update = b; }
  133. void invalidate_style();
  134. bool is_link() const;
  135. void set_document(Badge<Document>, Document&);
  136. virtual EventTarget* get_parent(const Event&) override;
  137. template<typename T>
  138. bool fast_is() const = delete;
  139. ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
  140. bool is_host_including_inclusive_ancestor_of(const Node&) const;
  141. bool is_scripting_disabled() const;
  142. bool contains(RefPtr<Node>) const;
  143. // Used for dumping the DOM Tree
  144. void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const;
  145. bool is_shadow_including_descendant_of(Node const&) const;
  146. bool is_shadow_including_inclusive_descendant_of(Node const&) const;
  147. bool is_shadow_including_ancestor_of(Node const&) const;
  148. bool is_shadow_including_inclusive_ancestor_of(Node const&) const;
  149. i32 id() const { return m_id; }
  150. static Node* from_id(i32 node_id);
  151. String serialize_fragment() const;
  152. void replace_all(RefPtr<Node>);
  153. void string_replace_all(String const&);
  154. bool is_same_node(Node const*) const;
  155. bool is_equal_node(Node const*) const;
  156. NonnullRefPtr<Node> get_root_node(GetRootNodeOptions const& options = {});
  157. bool is_uninteresting_whitespace_node() const;
  158. String debug_description() const;
  159. size_t length() const;
  160. protected:
  161. Node(Document&, NodeType);
  162. Document* m_document { nullptr };
  163. mutable WeakPtr<Layout::Node> m_layout_node;
  164. NodeType m_type { NodeType::INVALID };
  165. bool m_needs_style_update { false };
  166. bool m_child_needs_style_update { false };
  167. i32 m_id;
  168. };
  169. }