Node.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/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. class Node
  34. : public TreeNode<Node>
  35. , public EventTarget
  36. , public Bindings::Wrappable {
  37. public:
  38. using WrapperType = Bindings::NodeWrapper;
  39. using TreeNode<Node>::ref;
  40. using TreeNode<Node>::unref;
  41. ParentNode* parent_or_shadow_host();
  42. const ParentNode* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
  43. // ^EventTarget
  44. virtual void ref_event_target() final { ref(); }
  45. virtual void unref_event_target() final { unref(); }
  46. virtual bool dispatch_event(NonnullRefPtr<Event>) final;
  47. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  48. virtual ~Node();
  49. void removed_last_ref();
  50. NodeType type() const { return m_type; }
  51. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  52. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  53. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  54. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  55. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  56. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  57. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  58. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  59. bool is_slottable() const { return is_element() || is_text(); }
  60. virtual bool requires_svg_container() const { return false; }
  61. virtual bool is_svg_container() const { return false; }
  62. // NOTE: This is intended for the JS bindings.
  63. u16 node_type() const { return (u16)m_type; }
  64. virtual bool is_editable() const;
  65. ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
  66. ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);
  67. ExceptionOr<NonnullRefPtr<Node>> append_child(NonnullRefPtr<Node>);
  68. void insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers = false);
  69. void remove(bool suppress_observers = false);
  70. void remove_all_children(bool suppress_observers = false);
  71. u16 compare_document_position(RefPtr<Node> other);
  72. ExceptionOr<NonnullRefPtr<Node>> replace_child(NonnullRefPtr<Node> node, NonnullRefPtr<Node> child);
  73. NonnullRefPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
  74. ExceptionOr<NonnullRefPtr<Node>> clone_node_binding(bool deep);
  75. // NOTE: This is intended for the JS bindings.
  76. bool has_child_nodes() const { return has_children(); }
  77. NonnullRefPtrVector<Node> child_nodes() const;
  78. virtual RefPtr<Layout::Node> create_layout_node();
  79. virtual FlyString node_name() const = 0;
  80. virtual String text_content() const;
  81. void set_text_content(const String&);
  82. Document& document() { return *m_document; }
  83. const Document& document() const { return *m_document; }
  84. RefPtr<Document> owner_document() const;
  85. const HTML::HTMLAnchorElement* enclosing_link_element() const;
  86. const HTML::HTMLElement* enclosing_html_element() const;
  87. const HTML::HTMLElement* enclosing_html_element_with_attribute(const FlyString&) const;
  88. String child_text_content() const;
  89. Node* root();
  90. const Node* root() const
  91. {
  92. return const_cast<Node*>(this)->root();
  93. }
  94. Node* shadow_including_root();
  95. const Node* shadow_including_root() const
  96. {
  97. return const_cast<Node*>(this)->shadow_including_root();
  98. }
  99. bool is_connected() const;
  100. Node* parent_node() { return parent(); }
  101. const Node* parent_node() const { return parent(); }
  102. Element* parent_element();
  103. const Element* parent_element() const;
  104. virtual void inserted();
  105. virtual void removed_from(Node*) { }
  106. virtual void children_changed() { }
  107. virtual void adopted_from(Document&) { }
  108. virtual void cloned(Node&, bool) {};
  109. const Layout::Node* layout_node() const { return m_layout_node; }
  110. Layout::Node* layout_node() { return m_layout_node; }
  111. void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
  112. virtual bool is_child_allowed(const Node&) const { return true; }
  113. bool needs_style_update() const { return m_needs_style_update; }
  114. void set_needs_style_update(bool);
  115. bool child_needs_style_update() const { return m_child_needs_style_update; }
  116. void set_child_needs_style_update(bool b) { m_child_needs_style_update = b; }
  117. void invalidate_style();
  118. bool is_link() const;
  119. void set_document(Badge<Document>, Document&);
  120. virtual EventTarget* get_parent(const Event&) override;
  121. template<typename T>
  122. bool fast_is() const = delete;
  123. ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
  124. bool is_host_including_inclusive_ancestor_of(const Node&) const;
  125. bool is_scripting_disabled() const;
  126. bool contains(RefPtr<Node>) const;
  127. // Used for dumping the DOM Tree
  128. void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const;
  129. protected:
  130. Node(Document&, NodeType);
  131. Document* m_document { nullptr };
  132. mutable WeakPtr<Layout::Node> m_layout_node;
  133. NodeType m_type { NodeType::INVALID };
  134. bool m_needs_style_update { false };
  135. bool m_child_needs_style_update { false };
  136. };
  137. }