| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | #include <AK/Badge.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:45:35 +02:00
										 |  |  | #include <AK/RefPtr.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 22:59:16 +02:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:15:11 +01:00
										 |  |  | #include <LibWeb/Bindings/Wrappable.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/TreeNode.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | enum class NodeType : unsigned { | 
					
						
							|  |  |  |     INVALID = 0, | 
					
						
							|  |  |  |     ELEMENT_NODE = 1, | 
					
						
							|  |  |  |     TEXT_NODE = 3, | 
					
						
							| 
									
										
										
										
											2019-10-12 23:26:47 +02:00
										 |  |  |     COMMENT_NODE = 8, | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     DOCUMENT_NODE = 9, | 
					
						
							| 
									
										
										
										
											2019-10-09 20:17:01 +02:00
										 |  |  |     DOCUMENT_TYPE_NODE = 10, | 
					
						
							| 
									
										
										
										
											2019-11-06 20:27:53 +01:00
										 |  |  |     DOCUMENT_FRAGMENT_NODE = 11, | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 11:43:07 +02:00
										 |  |  | class Document; | 
					
						
							| 
									
										
										
										
											2019-10-06 19:54:50 +02:00
										 |  |  | class Element; | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  | class HTMLElement; | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  | class HTMLAnchorElement; | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | class ParentNode; | 
					
						
							| 
									
										
										
										
											2019-09-25 12:17:29 +03:00
										 |  |  | class LayoutNode; | 
					
						
							|  |  |  | class StyleResolver; | 
					
						
							|  |  |  | class StyleProperties; | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:15:11 +01:00
										 |  |  | class Node | 
					
						
							|  |  |  |     : public TreeNode<Node> | 
					
						
							|  |  |  |     , public Bindings::Wrappable { | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-14 13:24:15 +01:00
										 |  |  |     using WrapperType = Bindings::NodeWrapper; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     virtual ~Node(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NodeType type() const { return m_type; } | 
					
						
							|  |  |  |     bool is_element() const { return type() == NodeType::ELEMENT_NODE; } | 
					
						
							|  |  |  |     bool is_text() const { return type() == NodeType::TEXT_NODE; } | 
					
						
							|  |  |  |     bool is_document() const { return type() == NodeType::DOCUMENT_NODE; } | 
					
						
							| 
									
										
										
										
											2019-10-09 20:17:01 +02:00
										 |  |  |     bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; } | 
					
						
							| 
									
										
										
										
											2019-10-12 23:26:47 +02:00
										 |  |  |     bool is_comment() const { return type() == NodeType::COMMENT_NODE; } | 
					
						
							|  |  |  |     bool is_character_data() const { return type() == NodeType::TEXT_NODE || type() == NodeType::COMMENT_NODE; } | 
					
						
							| 
									
										
										
										
											2019-11-06 20:27:53 +01:00
										 |  |  |     bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; } | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     bool is_parent_node() const { return is_element() || is_document(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 15:06:16 +02:00
										 |  |  |     virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const; | 
					
						
							| 
									
										
										
										
											2019-09-25 12:17:29 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 22:59:16 +02:00
										 |  |  |     virtual String tag_name() const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 16:22:15 +02:00
										 |  |  |     virtual String text_content() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 11:43:07 +02:00
										 |  |  |     Document& document() { return m_document; } | 
					
						
							|  |  |  |     const Document& document() const { return m_document; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  |     const HTMLAnchorElement* enclosing_link_element() const; | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  |     const HTMLElement* enclosing_html_element() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual bool is_html_element() const { return false; } | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 20:47:57 +02:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     const T* first_child_of_type() const; | 
					
						
							| 
									
										
										
										
											2019-09-29 16:24:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 21:07:36 +02:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     const T* first_ancestor_of_type() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 17:40:39 +02:00
										 |  |  |     virtual void inserted_into(Node&) {} | 
					
						
							|  |  |  |     virtual void removed_from(Node&) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 21:05:52 +02:00
										 |  |  |     const LayoutNode* layout_node() const { return m_layout_node; } | 
					
						
							|  |  |  |     LayoutNode* layout_node() { return m_layout_node; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 19:54:50 +02:00
										 |  |  |     const Element* previous_element_sibling() const; | 
					
						
							|  |  |  |     const Element* next_element_sibling() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-12 23:26:47 +02:00
										 |  |  |     virtual bool is_child_allowed(const Node&) const { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 18:57:02 +02:00
										 |  |  |     bool needs_style_update() const { return m_needs_style_update; } | 
					
						
							|  |  |  |     void set_needs_style_update(bool value) { m_needs_style_update = value; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  |     void invalidate_style(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 21:21:29 +02:00
										 |  |  |     bool is_link() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2019-09-29 11:43:07 +02:00
										 |  |  |     Node(Document&, NodeType); | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 11:43:07 +02:00
										 |  |  |     Document& m_document; | 
					
						
							| 
									
										
										
										
											2019-10-04 21:05:52 +02:00
										 |  |  |     mutable LayoutNode* m_layout_node { nullptr }; | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     NodeType m_type { NodeType::INVALID }; | 
					
						
							| 
									
										
										
										
											2019-10-19 18:57:02 +02:00
										 |  |  |     bool m_needs_style_update { false }; | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-10-06 20:37:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | inline bool is(const Node&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | inline bool is(const Node* node) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-15 16:48:38 +02:00
										 |  |  |     return !node || is<T>(*node); | 
					
						
							| 
									
										
										
										
											2019-10-06 20:37:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | inline bool is<Node>(const Node&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | inline bool is<ParentNode>(const Node& node) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return node.is_parent_node(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | inline const T& to(const Node& node) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(is<T>(node)); | 
					
						
							|  |  |  |     return static_cast<const T&>(node); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 20:47:57 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | inline T* to(Node* node) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(is<T>(node)); | 
					
						
							|  |  |  |     return static_cast<T*>(node); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | inline const T* to(const Node* node) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(is<T>(node)); | 
					
						
							|  |  |  |     return static_cast<const T*>(node); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-06 20:37:39 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | inline T& to(Node& node) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(is<T>(node)); | 
					
						
							|  |  |  |     return static_cast<T&>(node); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-06 20:47:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | inline const T* Node::first_child_of_type() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (auto* child = first_child(); child; child = child->next_sibling()) { | 
					
						
							|  |  |  |         if (is<T>(*child)) | 
					
						
							|  |  |  |             return to<T>(child); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-06 21:07:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | inline const T* Node::first_ancestor_of_type() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { | 
					
						
							|  |  |  |         if (is<T>(*ancestor)) | 
					
						
							|  |  |  |             return to<T>(ancestor); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |