Node.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/RefPtr.h>
  29. #include <AK/String.h>
  30. #include <AK/TypeCasts.h>
  31. #include <AK/Vector.h>
  32. #include <LibWeb/Bindings/Wrappable.h>
  33. #include <LibWeb/DOM/EventTarget.h>
  34. #include <LibWeb/DOM/ExceptionOr.h>
  35. #include <LibWeb/TreeNode.h>
  36. namespace Web::DOM {
  37. enum class NodeType : u16 {
  38. INVALID = 0,
  39. ELEMENT_NODE = 1,
  40. ATTRIBUTE_NODE = 2,
  41. TEXT_NODE = 3,
  42. CDATA_SECTION_NODE = 4,
  43. ENTITY_REFERENCE_NODE = 5,
  44. ENTITY_NODE = 6,
  45. PROCESSING_INSTRUCTION_NODE = 7,
  46. COMMENT_NODE = 8,
  47. DOCUMENT_NODE = 9,
  48. DOCUMENT_TYPE_NODE = 10,
  49. DOCUMENT_FRAGMENT_NODE = 11,
  50. NOTATION_NODE = 12
  51. };
  52. class Node
  53. : public TreeNode<Node>
  54. , public EventTarget
  55. , public Bindings::Wrappable {
  56. public:
  57. using WrapperType = Bindings::NodeWrapper;
  58. using TreeNode<Node>::ref;
  59. using TreeNode<Node>::unref;
  60. ParentNode* parent_or_shadow_host();
  61. const ParentNode* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
  62. // ^EventTarget
  63. virtual void ref_event_target() final { ref(); }
  64. virtual void unref_event_target() final { unref(); }
  65. virtual bool dispatch_event(NonnullRefPtr<Event>) final;
  66. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  67. virtual ~Node();
  68. void removed_last_ref();
  69. NodeType type() const { return m_type; }
  70. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  71. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  72. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  73. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  74. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  75. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  76. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  77. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  78. bool is_slottable() const { return is_element() || is_text(); }
  79. // NOTE: This is intended for the JS bindings.
  80. u16 node_type() const { return (u16)m_type; }
  81. virtual bool is_editable() const;
  82. NonnullRefPtr<Node> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
  83. NonnullRefPtr<Node> pre_remove(NonnullRefPtr<Node>);
  84. NonnullRefPtr<Node> append_child(NonnullRefPtr<Node>);
  85. void insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers = false);
  86. void remove(bool suppress_observers = false);
  87. void remove_all_children(bool suppress_observers = false);
  88. // NOTE: This is intended for the JS bindings.
  89. bool has_child_nodes() const { return has_children(); }
  90. NonnullRefPtrVector<Node> child_nodes() const;
  91. virtual RefPtr<Layout::Node> create_layout_node();
  92. virtual FlyString node_name() const = 0;
  93. virtual String text_content() const;
  94. void set_text_content(const String&);
  95. Document& document() { return *m_document; }
  96. const Document& document() const { return *m_document; }
  97. const HTML::HTMLAnchorElement* enclosing_link_element() const;
  98. const HTML::HTMLElement* enclosing_html_element() const;
  99. const HTML::HTMLElement* enclosing_html_element_with_attribute(const FlyString&) const;
  100. String child_text_content() const;
  101. Node* root();
  102. const Node* root() const
  103. {
  104. return const_cast<Node*>(this)->root();
  105. }
  106. Node* shadow_including_root();
  107. const Node* shadow_including_root() const
  108. {
  109. return const_cast<Node*>(this)->shadow_including_root();
  110. }
  111. bool is_connected() const;
  112. Node* parent_node() { return parent(); }
  113. const Node* parent_node() const { return parent(); }
  114. Element* parent_element();
  115. const Element* parent_element() const;
  116. virtual void inserted();
  117. virtual void removed_from(Node*) { }
  118. virtual void children_changed() { }
  119. virtual void adopted_from(const Document&) { }
  120. const Layout::Node* layout_node() const { return m_layout_node; }
  121. Layout::Node* layout_node() { return m_layout_node; }
  122. void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
  123. virtual bool is_child_allowed(const Node&) const { return true; }
  124. bool needs_style_update() const { return m_needs_style_update; }
  125. void set_needs_style_update(bool);
  126. bool child_needs_style_update() const { return m_child_needs_style_update; }
  127. void set_child_needs_style_update(bool b) { m_child_needs_style_update = b; }
  128. void invalidate_style();
  129. bool is_link() const;
  130. void set_document(Badge<Document>, Document&);
  131. virtual EventTarget* get_parent(const Event&) override;
  132. template<typename T>
  133. bool fast_is() const = delete;
  134. ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
  135. bool is_host_including_inclusive_ancestor_of(const Node&) const;
  136. size_t element_child_count() const;
  137. protected:
  138. Node(Document&, NodeType);
  139. Document* m_document { nullptr };
  140. mutable WeakPtr<Layout::Node> m_layout_node;
  141. NodeType m_type { NodeType::INVALID };
  142. bool m_needs_style_update { false };
  143. bool m_child_needs_style_update { false };
  144. };
  145. }