Node.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/DOM/MutationObserver.h>
  17. #include <LibWeb/TreeNode.h>
  18. namespace Web::DOM {
  19. enum class NodeType : u16 {
  20. INVALID = 0,
  21. ELEMENT_NODE = 1,
  22. ATTRIBUTE_NODE = 2,
  23. TEXT_NODE = 3,
  24. CDATA_SECTION_NODE = 4,
  25. ENTITY_REFERENCE_NODE = 5,
  26. ENTITY_NODE = 6,
  27. PROCESSING_INSTRUCTION_NODE = 7,
  28. COMMENT_NODE = 8,
  29. DOCUMENT_NODE = 9,
  30. DOCUMENT_TYPE_NODE = 10,
  31. DOCUMENT_FRAGMENT_NODE = 11,
  32. NOTATION_NODE = 12
  33. };
  34. struct GetRootNodeOptions {
  35. bool composed { false };
  36. };
  37. class Node
  38. : public TreeNode<Node>
  39. , public EventTarget
  40. , public Bindings::Wrappable {
  41. public:
  42. using WrapperType = Bindings::NodeWrapper;
  43. using TreeNode<Node>::ref;
  44. using TreeNode<Node>::unref;
  45. ParentNode* parent_or_shadow_host();
  46. ParentNode const* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
  47. // ^EventTarget
  48. virtual void ref_event_target() final { ref(); }
  49. virtual void unref_event_target() final { unref(); }
  50. virtual JS::Object* create_wrapper(JS::Realm&) override;
  51. virtual ~Node();
  52. void removed_last_ref();
  53. NodeType type() const { return m_type; }
  54. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  55. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  56. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  57. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  58. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  59. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  60. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  61. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  62. bool is_slottable() const { return is_element() || is_text(); }
  63. bool is_attribute() const { return type() == NodeType::ATTRIBUTE_NODE; }
  64. bool is_cdata_section() const { return type() == NodeType::CDATA_SECTION_NODE; }
  65. virtual bool is_shadow_root() const { return false; }
  66. virtual bool requires_svg_container() const { return false; }
  67. virtual bool is_svg_container() const { return false; }
  68. virtual bool is_svg_svg_element() const { return false; }
  69. bool in_a_document_tree() const;
  70. // NOTE: This is intended for the JS bindings.
  71. u16 node_type() const { return (u16)m_type; }
  72. virtual bool is_editable() const;
  73. virtual bool is_html_element() const { return false; }
  74. virtual bool is_html_html_element() const { return false; }
  75. virtual bool is_html_anchor_element() const { return false; }
  76. virtual bool is_html_base_element() const { return false; }
  77. virtual bool is_html_template_element() const { return false; }
  78. virtual bool is_browsing_context_container() const { return false; }
  79. ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
  80. ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);
  81. ExceptionOr<NonnullRefPtr<Node>> append_child(NonnullRefPtr<Node>);
  82. ExceptionOr<NonnullRefPtr<Node>> remove_child(NonnullRefPtr<Node>);
  83. void insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers = false);
  84. void remove(bool suppress_observers = false);
  85. void remove_all_children(bool suppress_observers = false);
  86. u16 compare_document_position(RefPtr<Node> other);
  87. ExceptionOr<NonnullRefPtr<Node>> replace_child(NonnullRefPtr<Node> node, NonnullRefPtr<Node> child);
  88. NonnullRefPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
  89. ExceptionOr<NonnullRefPtr<Node>> clone_node_binding(bool deep);
  90. // NOTE: This is intended for the JS bindings.
  91. bool has_child_nodes() const { return has_children(); }
  92. NonnullRefPtr<NodeList> child_nodes();
  93. NonnullRefPtrVector<Node> children_as_vector() const;
  94. virtual FlyString node_name() const = 0;
  95. String base_uri() const;
  96. String descendant_text_content() const;
  97. String text_content() const;
  98. void set_text_content(String const&);
  99. String node_value() const;
  100. void set_node_value(String const&);
  101. Document& document() { return *m_document; }
  102. Document const& document() const { return *m_document; }
  103. RefPtr<Document> owner_document() const;
  104. const HTML::HTMLAnchorElement* enclosing_link_element() const;
  105. const HTML::HTMLElement* enclosing_html_element() const;
  106. const HTML::HTMLElement* enclosing_html_element_with_attribute(FlyString const&) const;
  107. String child_text_content() const;
  108. Node& root();
  109. Node const& root() const
  110. {
  111. return const_cast<Node*>(this)->root();
  112. }
  113. Node& shadow_including_root();
  114. Node const& shadow_including_root() const
  115. {
  116. return const_cast<Node*>(this)->shadow_including_root();
  117. }
  118. bool is_connected() const;
  119. Node* parent_node() { return parent(); }
  120. Node const* parent_node() const { return parent(); }
  121. Element* parent_element();
  122. Element const* parent_element() const;
  123. virtual void inserted();
  124. virtual void removed_from(Node*) { }
  125. virtual void children_changed() { }
  126. virtual void adopted_from(Document&) { }
  127. virtual void cloned(Node&, bool) {};
  128. Layout::Node const* layout_node() const { return m_layout_node; }
  129. Layout::Node* layout_node() { return m_layout_node; }
  130. Painting::PaintableBox const* paint_box() const;
  131. Painting::Paintable const* paintable() const;
  132. void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
  133. virtual bool is_child_allowed(Node const&) const { return true; }
  134. bool needs_style_update() const { return m_needs_style_update; }
  135. void set_needs_style_update(bool);
  136. bool child_needs_style_update() const { return m_child_needs_style_update; }
  137. void set_child_needs_style_update(bool b) { m_child_needs_style_update = b; }
  138. void invalidate_style();
  139. bool is_link() const;
  140. void set_document(Badge<Document>, Document&);
  141. virtual EventTarget* get_parent(Event const&) override;
  142. template<typename T>
  143. bool fast_is() const = delete;
  144. ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
  145. bool is_host_including_inclusive_ancestor_of(Node const&) const;
  146. bool is_scripting_enabled() const;
  147. bool is_scripting_disabled() const;
  148. bool contains(RefPtr<Node>) const;
  149. // Used for dumping the DOM Tree
  150. void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const;
  151. bool is_shadow_including_descendant_of(Node const&) const;
  152. bool is_shadow_including_inclusive_descendant_of(Node const&) const;
  153. bool is_shadow_including_ancestor_of(Node const&) const;
  154. bool is_shadow_including_inclusive_ancestor_of(Node const&) const;
  155. i32 id() const { return m_id; }
  156. static Node* from_id(i32 node_id);
  157. String serialize_fragment() const;
  158. void replace_all(RefPtr<Node>);
  159. void string_replace_all(String const&);
  160. bool is_same_node(Node const*) const;
  161. bool is_equal_node(Node const*) const;
  162. NonnullRefPtr<Node> get_root_node(GetRootNodeOptions const& options = {});
  163. bool is_uninteresting_whitespace_node() const;
  164. String debug_description() const;
  165. size_t length() const;
  166. NonnullRefPtrVector<RegisteredObserver>& registered_observers_list() { return m_registered_observer_list; }
  167. NonnullRefPtrVector<RegisteredObserver> const& registered_observers_list() const { return m_registered_observer_list; }
  168. void add_registered_observer(RegisteredObserver& registered_observer) { m_registered_observer_list.append(registered_observer); }
  169. void queue_mutation_record(FlyString const& type, String attribute_name, String attribute_namespace, String old_value, NonnullRefPtr<NodeList> added_nodes, NonnullRefPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
  170. // https://dom.spec.whatwg.org/#concept-shadow-including-descendant
  171. template<typename Callback>
  172. IterationDecision for_each_shadow_including_descendant(Callback);
  173. protected:
  174. Node(Document&, NodeType);
  175. Document* m_document { nullptr };
  176. mutable WeakPtr<Layout::Node> m_layout_node;
  177. NodeType m_type { NodeType::INVALID };
  178. bool m_needs_style_update { false };
  179. bool m_child_needs_style_update { false };
  180. i32 m_id;
  181. // https://dom.spec.whatwg.org/#registered-observer-list
  182. // "Nodes have a strong reference to registered observers in their registered observer list." https://dom.spec.whatwg.org/#garbage-collection
  183. NonnullRefPtrVector<RegisteredObserver> m_registered_observer_list;
  184. private:
  185. void queue_tree_mutation_record(NonnullRefPtr<NodeList> added_nodes, NonnullRefPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
  186. };
  187. }