2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2022-02-05 15:17:01 +03:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-15 19:55:47 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
#include <AK/Badge.h>
|
2021-06-07 18:32:24 +03:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2019-09-28 23:59:16 +03:00
|
|
|
#include <AK/String.h>
|
2020-07-26 18:16:18 +03:00
|
|
|
#include <AK/TypeCasts.h>
|
2019-06-15 19:55:47 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-03-14 15:15:11 +03:00
|
|
|
#include <LibWeb/Bindings/Wrappable.h>
|
2020-03-18 17:22:31 +03:00
|
|
|
#include <LibWeb/DOM/EventTarget.h>
|
2021-04-06 21:34:49 +03:00
|
|
|
#include <LibWeb/DOM/ExceptionOr.h>
|
2022-07-11 18:37:51 +03:00
|
|
|
#include <LibWeb/DOM/MutationObserver.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/TreeNode.h>
|
2019-06-15 19:55:47 +03:00
|
|
|
|
2020-07-26 20:37:56 +03:00
|
|
|
namespace Web::DOM {
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2021-03-06 20:06:25 +03:00
|
|
|
enum class NodeType : u16 {
|
2019-06-15 19:55:47 +03:00
|
|
|
INVALID = 0,
|
|
|
|
ELEMENT_NODE = 1,
|
2021-03-06 20:06:25 +03:00
|
|
|
ATTRIBUTE_NODE = 2,
|
2019-06-15 19:55:47 +03:00
|
|
|
TEXT_NODE = 3,
|
2021-03-06 20:06:25 +03:00
|
|
|
CDATA_SECTION_NODE = 4,
|
|
|
|
ENTITY_REFERENCE_NODE = 5,
|
|
|
|
ENTITY_NODE = 6,
|
|
|
|
PROCESSING_INSTRUCTION_NODE = 7,
|
2019-10-13 00:26:47 +03:00
|
|
|
COMMENT_NODE = 8,
|
2019-06-15 19:55:47 +03:00
|
|
|
DOCUMENT_NODE = 9,
|
2019-10-09 21:17:01 +03:00
|
|
|
DOCUMENT_TYPE_NODE = 10,
|
2019-11-06 22:27:53 +03:00
|
|
|
DOCUMENT_FRAGMENT_NODE = 11,
|
2021-03-06 20:06:25 +03:00
|
|
|
NOTATION_NODE = 12
|
2019-06-15 19:55:47 +03:00
|
|
|
};
|
|
|
|
|
2021-10-16 05:04:55 +03:00
|
|
|
struct GetRootNodeOptions {
|
|
|
|
bool composed { false };
|
|
|
|
};
|
|
|
|
|
2020-03-14 15:15:11 +03:00
|
|
|
class Node
|
|
|
|
: public TreeNode<Node>
|
2020-03-18 17:22:31 +03:00
|
|
|
, public EventTarget
|
2020-03-14 15:15:11 +03:00
|
|
|
, public Bindings::Wrappable {
|
2019-06-15 19:55:47 +03:00
|
|
|
public:
|
2020-03-14 15:24:15 +03:00
|
|
|
using WrapperType = Bindings::NodeWrapper;
|
|
|
|
|
2020-03-18 17:22:31 +03:00
|
|
|
using TreeNode<Node>::ref;
|
|
|
|
using TreeNode<Node>::unref;
|
|
|
|
|
2021-02-10 20:22:20 +03:00
|
|
|
ParentNode* parent_or_shadow_host();
|
2022-04-01 20:58:27 +03:00
|
|
|
ParentNode const* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
|
2021-02-10 20:22:20 +03:00
|
|
|
|
2020-03-18 17:22:31 +03:00
|
|
|
// ^EventTarget
|
|
|
|
virtual void ref_event_target() final { ref(); }
|
|
|
|
virtual void unref_event_target() final { unref(); }
|
2022-08-22 20:31:08 +03:00
|
|
|
virtual JS::Object* create_wrapper(JS::Realm&) override;
|
2020-03-18 17:22:31 +03:00
|
|
|
|
2019-06-15 19:55:47 +03:00
|
|
|
virtual ~Node();
|
|
|
|
|
2020-10-11 22:52:59 +03:00
|
|
|
void removed_last_ref();
|
|
|
|
|
2019-06-15 19:55:47 +03: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 21:17:01 +03:00
|
|
|
bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
|
2019-10-13 00:26:47 +03: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 22:27:53 +03:00
|
|
|
bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
|
2020-03-25 20:48:10 +03:00
|
|
|
bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
|
2020-11-21 21:32:39 +03:00
|
|
|
bool is_slottable() const { return is_element() || is_text(); }
|
2022-01-31 21:05:54 +03:00
|
|
|
bool is_attribute() const { return type() == NodeType::ATTRIBUTE_NODE; }
|
2022-06-27 23:20:59 +03:00
|
|
|
bool is_cdata_section() const { return type() == NodeType::CDATA_SECTION_NODE; }
|
2022-03-13 19:21:27 +03:00
|
|
|
virtual bool is_shadow_root() const { return false; }
|
2019-06-15 19:55:47 +03:00
|
|
|
|
2021-08-05 11:26:09 +03:00
|
|
|
virtual bool requires_svg_container() const { return false; }
|
|
|
|
virtual bool is_svg_container() const { return false; }
|
2022-03-12 18:08:40 +03:00
|
|
|
virtual bool is_svg_svg_element() const { return false; }
|
2021-08-05 11:26:09 +03:00
|
|
|
|
2021-09-29 23:23:28 +03:00
|
|
|
bool in_a_document_tree() const;
|
|
|
|
|
2021-03-06 20:06:25 +03:00
|
|
|
// NOTE: This is intended for the JS bindings.
|
|
|
|
u16 node_type() const { return (u16)m_type; }
|
|
|
|
|
2020-08-02 17:05:59 +03:00
|
|
|
virtual bool is_editable() const;
|
|
|
|
|
2022-07-27 17:04:31 +03:00
|
|
|
virtual bool is_html_element() const { return false; }
|
2021-10-27 18:58:57 +03:00
|
|
|
virtual bool is_html_html_element() const { return false; }
|
2022-03-02 17:14:38 +03:00
|
|
|
virtual bool is_html_anchor_element() const { return false; }
|
2022-07-27 17:03:43 +03:00
|
|
|
virtual bool is_html_base_element() const { return false; }
|
2021-09-16 01:52:10 +03:00
|
|
|
virtual bool is_html_template_element() const { return false; }
|
2021-11-24 19:15:04 +03:00
|
|
|
virtual bool is_browsing_context_container() const { return false; }
|
2021-09-16 01:52:10 +03:00
|
|
|
|
2021-04-13 21:33:48 +03:00
|
|
|
ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
|
|
|
|
ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);
|
2021-04-06 21:34:49 +03:00
|
|
|
|
2021-04-13 21:33:48 +03:00
|
|
|
ExceptionOr<NonnullRefPtr<Node>> append_child(NonnullRefPtr<Node>);
|
2022-02-22 00:21:59 +03:00
|
|
|
ExceptionOr<NonnullRefPtr<Node>> remove_child(NonnullRefPtr<Node>);
|
|
|
|
|
2021-04-06 21:34:49 +03:00
|
|
|
void insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers = false);
|
|
|
|
void remove(bool suppress_observers = false);
|
|
|
|
void remove_all_children(bool suppress_observers = false);
|
2021-04-11 03:21:22 +03:00
|
|
|
u16 compare_document_position(RefPtr<Node> other);
|
2020-06-21 02:00:58 +03:00
|
|
|
|
2021-05-07 02:52:23 +03:00
|
|
|
ExceptionOr<NonnullRefPtr<Node>> replace_child(NonnullRefPtr<Node> node, NonnullRefPtr<Node> child);
|
|
|
|
|
2021-07-05 07:30:24 +03:00
|
|
|
NonnullRefPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
|
|
|
|
ExceptionOr<NonnullRefPtr<Node>> clone_node_binding(bool deep);
|
2021-04-14 02:25:10 +03:00
|
|
|
|
2021-03-06 20:06:25 +03:00
|
|
|
// NOTE: This is intended for the JS bindings.
|
|
|
|
bool has_child_nodes() const { return has_children(); }
|
2021-10-02 22:37:45 +03:00
|
|
|
NonnullRefPtr<NodeList> child_nodes();
|
|
|
|
NonnullRefPtrVector<Node> children_as_vector() const;
|
2021-03-06 20:06:25 +03:00
|
|
|
|
2020-06-16 20:09:14 +03:00
|
|
|
virtual FlyString node_name() const = 0;
|
2019-09-28 23:59:16 +03:00
|
|
|
|
2022-04-12 19:20:13 +03:00
|
|
|
String base_uri() const;
|
|
|
|
|
2021-09-06 02:21:59 +03:00
|
|
|
String descendant_text_content() const;
|
|
|
|
String text_content() const;
|
|
|
|
void set_text_content(String const&);
|
2019-09-29 17:22:15 +03:00
|
|
|
|
2022-02-19 01:11:43 +03:00
|
|
|
String node_value() const;
|
|
|
|
void set_node_value(String const&);
|
|
|
|
|
2020-06-26 00:42:08 +03:00
|
|
|
Document& document() { return *m_document; }
|
2022-04-01 20:58:27 +03:00
|
|
|
Document const& document() const { return *m_document; }
|
2019-09-29 12:43:07 +03:00
|
|
|
|
2021-05-02 23:03:50 +03:00
|
|
|
RefPtr<Document> owner_document() const;
|
|
|
|
|
2020-07-28 19:20:36 +03:00
|
|
|
const HTML::HTMLAnchorElement* enclosing_link_element() const;
|
|
|
|
const HTML::HTMLElement* enclosing_html_element() const;
|
2022-04-01 20:58:27 +03:00
|
|
|
const HTML::HTMLElement* enclosing_html_element_with_attribute(FlyString const&) const;
|
2019-09-29 13:24:36 +03:00
|
|
|
|
2020-05-24 22:59:24 +03:00
|
|
|
String child_text_content() const;
|
|
|
|
|
2021-09-02 21:27:42 +03:00
|
|
|
Node& root();
|
2022-04-01 20:58:27 +03:00
|
|
|
Node const& root() const
|
2020-11-21 21:32:39 +03:00
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->root();
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:27:42 +03:00
|
|
|
Node& shadow_including_root();
|
2022-04-01 20:58:27 +03:00
|
|
|
Node const& shadow_including_root() const
|
2020-11-21 21:32:39 +03:00
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->shadow_including_root();
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:59:24 +03:00
|
|
|
bool is_connected() const;
|
|
|
|
|
2020-06-20 23:26:54 +03:00
|
|
|
Node* parent_node() { return parent(); }
|
2022-04-01 20:58:27 +03:00
|
|
|
Node const* parent_node() const { return parent(); }
|
2020-06-20 23:26:54 +03:00
|
|
|
|
|
|
|
Element* parent_element();
|
2022-04-01 20:58:27 +03:00
|
|
|
Element const* parent_element() const;
|
2020-06-20 23:26:54 +03:00
|
|
|
|
2021-04-06 19:58:20 +03:00
|
|
|
virtual void inserted();
|
|
|
|
virtual void removed_from(Node*) { }
|
2020-07-26 18:16:18 +03:00
|
|
|
virtual void children_changed() { }
|
2021-07-05 07:33:35 +03:00
|
|
|
virtual void adopted_from(Document&) { }
|
2021-07-05 07:30:24 +03:00
|
|
|
virtual void cloned(Node&, bool) {};
|
2019-09-29 18:40:39 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Layout::Node const* layout_node() const { return m_layout_node; }
|
2020-11-22 17:53:01 +03:00
|
|
|
Layout::Node* layout_node() { return m_layout_node; }
|
2019-10-04 22:05:52 +03:00
|
|
|
|
2022-03-10 17:50:57 +03:00
|
|
|
Painting::PaintableBox const* paint_box() const;
|
2022-03-11 00:46:35 +03:00
|
|
|
Painting::Paintable const* paintable() const;
|
2022-03-10 01:53:41 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
|
2019-10-04 22:05:52 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool is_child_allowed(Node const&) const { return true; }
|
2019-10-13 00:26:47 +03:00
|
|
|
|
2019-10-19 19:57:02 +03:00
|
|
|
bool needs_style_update() const { return m_needs_style_update; }
|
2020-12-13 17:19:42 +03:00
|
|
|
void set_needs_style_update(bool);
|
2019-10-19 19:57:02 +03:00
|
|
|
|
2020-12-14 14:04:30 +03: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 19:32:02 +03:00
|
|
|
void invalidate_style();
|
|
|
|
|
2019-10-19 22:21:29 +03:00
|
|
|
bool is_link() const;
|
|
|
|
|
2020-06-26 00:42:08 +03:00
|
|
|
void set_document(Badge<Document>, Document&);
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual EventTarget* get_parent(Event const&) override;
|
2020-11-21 21:32:39 +03:00
|
|
|
|
2021-01-17 11:34:01 +03:00
|
|
|
template<typename T>
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
2021-04-06 21:34:49 +03:00
|
|
|
ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool is_host_including_inclusive_ancestor_of(Node const&) const;
|
2021-04-06 21:34:49 +03:00
|
|
|
|
2022-03-31 00:28:28 +03:00
|
|
|
bool is_scripting_enabled() const;
|
2021-07-05 05:59:47 +03:00
|
|
|
bool is_scripting_disabled() const;
|
|
|
|
|
2021-07-05 07:55:02 +03:00
|
|
|
bool contains(RefPtr<Node>) const;
|
|
|
|
|
2021-06-07 18:32:24 +03:00
|
|
|
// Used for dumping the DOM Tree
|
|
|
|
void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const;
|
|
|
|
|
2021-09-02 04:17:13 +03:00
|
|
|
bool is_shadow_including_descendant_of(Node const&) const;
|
|
|
|
bool is_shadow_including_inclusive_descendant_of(Node const&) const;
|
|
|
|
bool is_shadow_including_ancestor_of(Node const&) const;
|
|
|
|
bool is_shadow_including_inclusive_ancestor_of(Node const&) const;
|
|
|
|
|
2021-08-30 17:52:08 +03:00
|
|
|
i32 id() const { return m_id; }
|
|
|
|
static Node* from_id(i32 node_id);
|
|
|
|
|
2021-09-14 00:42:15 +03:00
|
|
|
String serialize_fragment() const;
|
|
|
|
|
2021-09-06 03:25:58 +03:00
|
|
|
void replace_all(RefPtr<Node>);
|
|
|
|
void string_replace_all(String const&);
|
|
|
|
|
2021-09-13 13:49:23 +03:00
|
|
|
bool is_same_node(Node const*) const;
|
2021-09-13 13:54:24 +03:00
|
|
|
bool is_equal_node(Node const*) const;
|
2021-09-13 13:49:23 +03:00
|
|
|
|
2021-10-16 05:04:55 +03:00
|
|
|
NonnullRefPtr<Node> get_root_node(GetRootNodeOptions const& options = {});
|
|
|
|
|
2021-11-02 21:20:57 +03:00
|
|
|
bool is_uninteresting_whitespace_node() const;
|
|
|
|
|
2022-02-26 10:18:14 +03:00
|
|
|
String debug_description() const;
|
|
|
|
|
2022-01-31 21:05:54 +03:00
|
|
|
size_t length() const;
|
|
|
|
|
2022-07-11 18:37:51 +03:00
|
|
|
NonnullRefPtrVector<RegisteredObserver>& registered_observers_list() { return m_registered_observer_list; }
|
|
|
|
NonnullRefPtrVector<RegisteredObserver> const& registered_observers_list() const { return m_registered_observer_list; }
|
|
|
|
|
|
|
|
void add_registered_observer(RegisteredObserver& registered_observer) { m_registered_observer_list.append(registered_observer); }
|
|
|
|
|
2022-07-11 18:39:14 +03:00
|
|
|
void queue_mutation_record(FlyString const& type, String attribute_name, String attribute_namespace, String old_value, NonnullRefPtr<NodeList> added_nodes, NonnullRefPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
|
|
|
|
|
2022-07-13 00:13:57 +03:00
|
|
|
// https://dom.spec.whatwg.org/#concept-shadow-including-descendant
|
|
|
|
template<typename Callback>
|
|
|
|
IterationDecision for_each_shadow_including_descendant(Callback);
|
|
|
|
|
2019-06-15 19:55:47 +03:00
|
|
|
protected:
|
2019-09-29 12:43:07 +03:00
|
|
|
Node(Document&, NodeType);
|
2019-06-15 19:55:47 +03:00
|
|
|
|
2020-06-26 00:42:08 +03:00
|
|
|
Document* m_document { nullptr };
|
2020-11-22 17:53:01 +03:00
|
|
|
mutable WeakPtr<Layout::Node> m_layout_node;
|
2019-06-15 19:55:47 +03:00
|
|
|
NodeType m_type { NodeType::INVALID };
|
2020-12-14 14:04:30 +03:00
|
|
|
bool m_needs_style_update { false };
|
|
|
|
bool m_child_needs_style_update { false };
|
2021-08-30 17:52:08 +03:00
|
|
|
|
|
|
|
i32 m_id;
|
2022-07-11 18:37:51 +03:00
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#registered-observer-list
|
|
|
|
// "Nodes have a strong reference to registered observers in their registered observer list." https://dom.spec.whatwg.org/#garbage-collection
|
|
|
|
NonnullRefPtrVector<RegisteredObserver> m_registered_observer_list;
|
2022-07-11 18:39:14 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
void queue_tree_mutation_record(NonnullRefPtr<NodeList> added_nodes, NonnullRefPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
|
2019-06-15 19:55:47 +03:00
|
|
|
};
|
2019-10-06 21:37:39 +03:00
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
}
|