Serenity Operating System
at hosted 207 lines 6.0 kB view raw
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 27#pragma once 28 29#include <AK/Badge.h> 30#include <AK/RefPtr.h> 31#include <AK/String.h> 32#include <AK/Vector.h> 33#include <LibWeb/Bindings/Wrappable.h> 34#include <LibWeb/DOM/EventTarget.h> 35#include <LibWeb/TreeNode.h> 36 37namespace Web { 38 39enum class NodeType : unsigned { 40 INVALID = 0, 41 ELEMENT_NODE = 1, 42 TEXT_NODE = 3, 43 COMMENT_NODE = 8, 44 DOCUMENT_NODE = 9, 45 DOCUMENT_TYPE_NODE = 10, 46 DOCUMENT_FRAGMENT_NODE = 11, 47}; 48 49class Document; 50class Element; 51class HTMLElement; 52class HTMLAnchorElement; 53class ParentNode; 54class LayoutNode; 55class StyleResolver; 56class StyleProperties; 57 58class Node 59 : public TreeNode<Node> 60 , public EventTarget 61 , public Bindings::Wrappable { 62public: 63 using WrapperType = Bindings::NodeWrapper; 64 65 using TreeNode<Node>::ref; 66 using TreeNode<Node>::unref; 67 68 // ^EventTarget 69 virtual void ref_event_target() final { ref(); } 70 virtual void unref_event_target() final { unref(); } 71 virtual void dispatch_event(NonnullRefPtr<Event>) final; 72 73 virtual ~Node(); 74 75 NodeType type() const { return m_type; } 76 bool is_element() const { return type() == NodeType::ELEMENT_NODE; } 77 bool is_text() const { return type() == NodeType::TEXT_NODE; } 78 bool is_document() const { return type() == NodeType::DOCUMENT_NODE; } 79 bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; } 80 bool is_comment() const { return type() == NodeType::COMMENT_NODE; } 81 bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; } 82 bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; } 83 bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); } 84 85 virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const; 86 87 virtual FlyString tag_name() const = 0; 88 89 virtual String text_content() const; 90 91 Document& document() { return m_document; } 92 const Document& document() const { return m_document; } 93 94 const HTMLAnchorElement* enclosing_link_element() const; 95 const HTMLElement* enclosing_html_element() const; 96 97 virtual bool is_html_element() const { return false; } 98 99 template<typename T> 100 const T* first_child_of_type() const; 101 102 template<typename T> 103 const T* first_ancestor_of_type() const; 104 105 virtual void inserted_into(Node&) {} 106 virtual void removed_from(Node&) {} 107 virtual void children_changed() {} 108 109 const LayoutNode* layout_node() const { return m_layout_node; } 110 LayoutNode* layout_node() { return m_layout_node; } 111 112 void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; } 113 114 const Element* previous_element_sibling() const; 115 const Element* next_element_sibling() const; 116 117 virtual bool is_child_allowed(const Node&) const { return true; } 118 119 bool needs_style_update() const { return m_needs_style_update; } 120 void set_needs_style_update(bool value) { m_needs_style_update = value; } 121 122 void invalidate_style(); 123 124 bool is_link() const; 125 126protected: 127 Node(Document&, NodeType); 128 129 Document& m_document; 130 mutable LayoutNode* m_layout_node { nullptr }; 131 NodeType m_type { NodeType::INVALID }; 132 bool m_needs_style_update { false }; 133}; 134 135template<typename T> 136inline bool is(const Node&) 137{ 138 return false; 139} 140 141template<typename T> 142inline bool is(const Node* node) 143{ 144 return !node || is<T>(*node); 145} 146 147template<> 148inline bool is<Node>(const Node&) 149{ 150 return true; 151} 152 153template<> 154inline bool is<ParentNode>(const Node& node) 155{ 156 return node.is_parent_node(); 157} 158 159template<typename T> 160inline const T& to(const Node& node) 161{ 162 ASSERT(is<T>(node)); 163 return static_cast<const T&>(node); 164} 165 166template<typename T> 167inline T* to(Node* node) 168{ 169 ASSERT(is<T>(node)); 170 return static_cast<T*>(node); 171} 172 173template<typename T> 174inline const T* to(const Node* node) 175{ 176 ASSERT(is<T>(node)); 177 return static_cast<const T*>(node); 178} 179 180template<typename T> 181inline T& to(Node& node) 182{ 183 ASSERT(is<T>(node)); 184 return static_cast<T&>(node); 185} 186 187template<typename T> 188inline const T* Node::first_child_of_type() const 189{ 190 for (auto* child = first_child(); child; child = child->next_sibling()) { 191 if (is<T>(*child)) 192 return to<T>(child); 193 } 194 return nullptr; 195} 196 197template<typename T> 198inline const T* Node::first_ancestor_of_type() const 199{ 200 for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { 201 if (is<T>(*ancestor)) 202 return to<T>(ancestor); 203 } 204 return nullptr; 205} 206 207}