| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 17:16:18 +02:00
										 |  |  | #include <AK/TypeCasts.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-18 15:22:31 +01:00
										 |  |  | #include <LibWeb/DOM/EventTarget.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/TreeNode.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  | namespace Web::DOM { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-06 17:06:25 +00:00
										 |  |  | enum class NodeType : u16 { | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     INVALID = 0, | 
					
						
							|  |  |  |     ELEMENT_NODE = 1, | 
					
						
							| 
									
										
										
										
											2021-03-06 17:06:25 +00:00
										 |  |  |     ATTRIBUTE_NODE = 2, | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     TEXT_NODE = 3, | 
					
						
							| 
									
										
										
										
											2021-03-06 17:06:25 +00:00
										 |  |  |     CDATA_SECTION_NODE = 4, | 
					
						
							|  |  |  |     ENTITY_REFERENCE_NODE = 5, | 
					
						
							|  |  |  |     ENTITY_NODE = 6, | 
					
						
							|  |  |  |     PROCESSING_INSTRUCTION_NODE = 7, | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2021-03-06 17:06:25 +00:00
										 |  |  |     NOTATION_NODE = 12 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:15:11 +01:00
										 |  |  | class Node | 
					
						
							|  |  |  |     : public TreeNode<Node> | 
					
						
							| 
									
										
										
										
											2020-03-18 15:22:31 +01:00
										 |  |  |     , public EventTarget | 
					
						
							| 
									
										
										
										
											2020-03-14 13:15:11 +01:00
										 |  |  |     , public Bindings::Wrappable { | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-14 13:24:15 +01:00
										 |  |  |     using WrapperType = Bindings::NodeWrapper; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 15:22:31 +01:00
										 |  |  |     using TreeNode<Node>::ref; | 
					
						
							|  |  |  |     using TreeNode<Node>::unref; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 18:22:20 +01:00
										 |  |  |     ParentNode* parent_or_shadow_host(); | 
					
						
							|  |  |  |     const ParentNode* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 15:22:31 +01:00
										 |  |  |     // ^EventTarget
 | 
					
						
							|  |  |  |     virtual void ref_event_target() final { ref(); } | 
					
						
							|  |  |  |     virtual void unref_event_target() final { unref(); } | 
					
						
							| 
									
										
										
										
											2020-11-21 18:32:39 +00:00
										 |  |  |     virtual bool dispatch_event(NonnullRefPtr<Event>) final; | 
					
						
							| 
									
										
										
										
											2021-01-18 12:15:02 +01:00
										 |  |  |     virtual JS::Object* create_wrapper(JS::GlobalObject&) override; | 
					
						
							| 
									
										
										
										
											2020-03-18 15:22:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     virtual ~Node(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-11 21:52:59 +02:00
										 |  |  |     void removed_last_ref(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     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; } | 
					
						
							| 
									
										
										
										
											2020-03-25 18:48:10 +01:00
										 |  |  |     bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); } | 
					
						
							| 
									
										
										
										
											2020-11-21 18:32:39 +00:00
										 |  |  |     bool is_slottable() const { return is_element() || is_text(); } | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-06 17:06:25 +00:00
										 |  |  |     // NOTE: This is intended for the JS bindings.
 | 
					
						
							|  |  |  |     u16 node_type() const { return (u16)m_type; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 16:05:59 +02:00
										 |  |  |     virtual bool is_editable() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 01:00:58 +02:00
										 |  |  |     RefPtr<Node> append_child(NonnullRefPtr<Node>, bool notify = true); | 
					
						
							| 
									
										
										
										
											2020-06-21 16:45:21 +02:00
										 |  |  |     RefPtr<Node> insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool notify = true); | 
					
						
							| 
									
										
										
										
											2021-01-28 08:57:37 +01:00
										 |  |  |     RefPtr<Node> remove_child(NonnullRefPtr<Node>); | 
					
						
							| 
									
										
										
										
											2020-06-21 01:00:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-06 17:06:25 +00:00
										 |  |  |     // NOTE: This is intended for the JS bindings.
 | 
					
						
							|  |  |  |     bool has_child_nodes() const { return has_children(); } | 
					
						
							|  |  |  |     NonnullRefPtrVector<Node> child_nodes() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-06 14:27:40 +01:00
										 |  |  |     virtual RefPtr<Layout::Node> create_layout_node(); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:17:29 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 19:09:14 +02:00
										 |  |  |     virtual FlyString node_name() const = 0; | 
					
						
							| 
									
										
										
										
											2019-09-28 22:59:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 16:22:15 +02:00
										 |  |  |     virtual String text_content() const; | 
					
						
							| 
									
										
										
										
											2020-08-17 12:36:00 -04:00
										 |  |  |     void set_text_content(const String&); | 
					
						
							| 
									
										
										
										
											2019-09-29 16:22:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 23:42:08 +02:00
										 |  |  |     Document& document() { return *m_document; } | 
					
						
							|  |  |  |     const Document& document() const { return *m_document; } | 
					
						
							| 
									
										
										
										
											2019-09-29 11:43:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 18:20:36 +02:00
										 |  |  |     const HTML::HTMLAnchorElement* enclosing_link_element() const; | 
					
						
							|  |  |  |     const HTML::HTMLElement* enclosing_html_element() const; | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 21:59:24 +02:00
										 |  |  |     String child_text_content() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 18:32:39 +00:00
										 |  |  |     Node* root(); | 
					
						
							|  |  |  |     const Node* root() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return const_cast<Node*>(this)->root(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Node* shadow_including_root(); | 
					
						
							|  |  |  |     const Node* shadow_including_root() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return const_cast<Node*>(this)->shadow_including_root(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 21:59:24 +02:00
										 |  |  |     bool is_connected() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 22:26:54 +02:00
										 |  |  |     Node* parent_node() { return parent(); } | 
					
						
							|  |  |  |     const Node* parent_node() const { return parent(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Element* parent_element(); | 
					
						
							|  |  |  |     const Element* parent_element() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 12:04:30 +01:00
										 |  |  |     virtual void inserted_into(Node&); | 
					
						
							| 
									
										
										
										
											2020-07-26 17:16:18 +02:00
										 |  |  |     virtual void removed_from(Node&) { } | 
					
						
							|  |  |  |     virtual void children_changed() { } | 
					
						
							| 
									
										
										
										
											2019-09-29 17:40:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     const Layout::Node* layout_node() const { return m_layout_node; } | 
					
						
							|  |  |  |     Layout::Node* layout_node() { return m_layout_node; } | 
					
						
							| 
									
										
										
										
											2019-10-04 21:05:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     void set_layout_node(Badge<Layout::Node>, Layout::Node*) const; | 
					
						
							| 
									
										
										
										
											2019-10-04 21:05:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; } | 
					
						
							| 
									
										
										
										
											2020-12-13 15:19:42 +01:00
										 |  |  |     void set_needs_style_update(bool); | 
					
						
							| 
									
										
										
										
											2019-10-19 18:57:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 12:04:30 +01:00
										 |  |  |     bool child_needs_style_update() const { return m_child_needs_style_update; } | 
					
						
							|  |  |  |     void set_child_needs_style_update(bool b) { m_child_needs_style_update = b; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-14 18:32:02 +02:00
										 |  |  |     void invalidate_style(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 21:21:29 +02:00
										 |  |  |     bool is_link() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 23:42:08 +02:00
										 |  |  |     void set_document(Badge<Document>, Document&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 18:32:39 +00:00
										 |  |  |     virtual EventTarget* get_parent(const Event&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 09:34:01 +01:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     bool fast_is() const = delete; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 23:42:08 +02:00
										 |  |  |     Document* m_document { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     mutable WeakPtr<Layout::Node> m_layout_node; | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  |     NodeType m_type { NodeType::INVALID }; | 
					
						
							| 
									
										
										
										
											2020-12-14 12:04:30 +01:00
										 |  |  |     bool m_needs_style_update { false }; | 
					
						
							|  |  |  |     bool m_child_needs_style_update { false }; | 
					
						
							| 
									
										
										
										
											2019-06-15 18:55:47 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-10-06 20:37:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |