Node.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Bindings::EventTargetWrapper* create_wrapper(JS::GlobalObject&) override;
  58. virtual ~Node();
  59. void removed_last_ref();
  60. NodeType type() const { return m_type; }
  61. bool is_element() const { return type() == NodeType::ELEMENT_NODE; }
  62. bool is_text() const { return type() == NodeType::TEXT_NODE; }
  63. bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
  64. bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
  65. bool is_comment() const { return type() == NodeType::COMMENT_NODE; }
  66. bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; }
  67. bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
  68. bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
  69. virtual bool is_svg_element() const { return false; }
  70. virtual bool is_editable() const;
  71. RefPtr<Node> append_child(NonnullRefPtr<Node>, bool notify = true);
  72. RefPtr<Node> insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify = true);
  73. void remove_all_children();
  74. virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style);
  75. virtual FlyString node_name() const = 0;
  76. virtual String text_content() const;
  77. void set_text_content(const String&);
  78. Document& document() { return *m_document; }
  79. const Document& document() const { return *m_document; }
  80. const HTML::HTMLAnchorElement* enclosing_link_element() const;
  81. const HTML::HTMLElement* enclosing_html_element() const;
  82. String child_text_content() const;
  83. virtual bool is_html_element() const { return false; }
  84. virtual bool is_unknown_html_element() const { return false; }
  85. const Node* root() const;
  86. bool is_connected() const;
  87. Node* parent_node() { return parent(); }
  88. const Node* parent_node() const { return parent(); }
  89. Element* parent_element();
  90. const Element* parent_element() const;
  91. virtual void inserted_into(Node&) { }
  92. virtual void removed_from(Node&) { }
  93. virtual void children_changed() { }
  94. const LayoutNode* layout_node() const { return m_layout_node; }
  95. LayoutNode* layout_node() { return m_layout_node; }
  96. void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
  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. }