Node.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/TreeNode.h>
  35. namespace Web::DOM {
  36. enum class NodeType : unsigned {
  37. INVALID = 0,
  38. ELEMENT_NODE = 1,
  39. TEXT_NODE = 3,
  40. COMMENT_NODE = 8,
  41. DOCUMENT_NODE = 9,
  42. DOCUMENT_TYPE_NODE = 10,
  43. DOCUMENT_FRAGMENT_NODE = 11,
  44. };
  45. class Node
  46. : public TreeNode<Node>
  47. , public EventTarget
  48. , public Bindings::Wrappable {
  49. public:
  50. using WrapperType = Bindings::NodeWrapper;
  51. using TreeNode<Node>::ref;
  52. using TreeNode<Node>::unref;
  53. // ^EventTarget
  54. virtual void ref_event_target() final { ref(); }
  55. virtual void unref_event_target() final { unref(); }
  56. virtual void dispatch_event(NonnullRefPtr<Event>) final;
  57. virtual ~Node();
  58. NodeType type() const { return m_type; }
  59. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  60. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  61. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  62. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  63. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  64. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  65. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  66. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  67. virtual bool is_svg_element() const { return false; }
  68. RefPtr<Node> append_child(NonnullRefPtr<Node>, bool notify = true);
  69. RefPtr<Node> insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify = true);
  70. virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style);
  71. virtual FlyString node_name() const = 0;
  72. virtual String text_content() const;
  73. Document& document() { return *m_document; }
  74. const Document& document() const { return *m_document; }
  75. const HTMLAnchorElement* enclosing_link_element() const;
  76. const HTMLElement* enclosing_html_element() const;
  77. String child_text_content() const;
  78. virtual bool is_html_element() const { return false; }
  79. const Node* root() const;
  80. bool is_connected() const;
  81. Node* parent_node() { return parent(); }
  82. const Node* parent_node() const { return parent(); }
  83. Element* parent_element();
  84. const Element* parent_element() const;
  85. template<typename T>
  86. const T* first_child_of_type() const;
  87. template<typename T>
  88. const T* first_ancestor_of_type() const;
  89. virtual void inserted_into(Node&) { }
  90. virtual void removed_from(Node&) { }
  91. virtual void children_changed() { }
  92. const LayoutNode* layout_node() const { return m_layout_node; }
  93. LayoutNode* layout_node() { return m_layout_node; }
  94. void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
  95. const Element* previous_element_sibling() const;
  96. const Element* next_element_sibling() const;
  97. virtual bool is_child_allowed(const Node&) const { return true; }
  98. bool needs_style_update() const { return m_needs_style_update; }
  99. void set_needs_style_update(bool value) { m_needs_style_update = value; }
  100. void invalidate_style();
  101. bool is_link() const;
  102. virtual void document_did_attach_to_frame(Frame&) { }
  103. virtual void document_will_detach_from_frame(Frame&) { }
  104. void set_document(Badge<Document>, Document&);
  105. protected:
  106. Node(Document&, NodeType);
  107. Document* m_document { nullptr };
  108. mutable LayoutNode* m_layout_node { nullptr };
  109. NodeType m_type { NodeType::INVALID };
  110. bool m_needs_style_update { true };
  111. };
  112. template<typename T>
  113. inline const T* Node::first_child_of_type() const
  114. {
  115. for (auto* child = first_child(); child; child = child->next_sibling()) {
  116. if (is<T>(*child))
  117. return downcast<T>(child);
  118. }
  119. return nullptr;
  120. }
  121. template<typename T>
  122. inline const T* Node::first_ancestor_of_type() const
  123. {
  124. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  125. if (is<T>(*ancestor))
  126. return downcast<T>(ancestor);
  127. }
  128. return nullptr;
  129. }
  130. }