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-18 17:22:31 +03:00
|
|
|
#include <LibWeb/DOM/EventTarget.h>
|
2021-04-06 21:34:49 +03:00
|
|
|
#include <LibWeb/DOM/ExceptionOr.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 };
|
|
|
|
};
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
class Node : public EventTarget {
|
|
|
|
WEB_PLATFORM_OBJECT(Node, EventTarget);
|
2020-03-18 17:22:31 +03:00
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
public:
|
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
|
|
|
|
2019-06-15 19:55:47 +03:00
|
|
|
virtual ~Node();
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
// FIXME: Move cleanup to the regular destructor.
|
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
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
ExceptionOr<JS::NonnullGCPtr<Node>> pre_insert(JS::NonnullGCPtr<Node>, JS::GCPtr<Node>);
|
|
|
|
ExceptionOr<JS::NonnullGCPtr<Node>> pre_remove(JS::NonnullGCPtr<Node>);
|
2021-04-06 21:34:49 +03:00
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
ExceptionOr<JS::NonnullGCPtr<Node>> append_child(JS::NonnullGCPtr<Node>);
|
|
|
|
ExceptionOr<JS::NonnullGCPtr<Node>> remove_child(JS::NonnullGCPtr<Node>);
|
2022-02-22 00:21:59 +03:00
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
void insert_before(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child, bool suppress_observers = false);
|
2021-04-06 21:34:49 +03:00
|
|
|
void remove(bool suppress_observers = false);
|
|
|
|
void remove_all_children(bool suppress_observers = false);
|
2022-08-28 14:42:07 +03:00
|
|
|
u16 compare_document_position(JS::GCPtr<Node> other);
|
2020-06-21 02:00:58 +03:00
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
ExceptionOr<JS::NonnullGCPtr<Node>> replace_child(JS::NonnullGCPtr<Node> node, JS::NonnullGCPtr<Node> child);
|
2021-05-07 02:52:23 +03:00
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
JS::NonnullGCPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
|
|
|
|
ExceptionOr<JS::NonnullGCPtr<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(); }
|
2022-09-01 17:30:26 +03:00
|
|
|
JS::NonnullGCPtr<NodeList> child_nodes();
|
2022-08-28 14:42:07 +03:00
|
|
|
Vector<JS::Handle<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
|
|
|
|
2022-09-01 17:30:26 +03:00
|
|
|
HTML::Window& window() const;
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
JS::GCPtr<Document> owner_document() const;
|
2021-05-02 23:03:50 +03:00
|
|
|
|
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();
|
|
|
|
|
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;
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
ExceptionOr<void> ensure_pre_insertion_validity(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child) const;
|
2021-04-06 21:34:49 +03:00
|
|
|
|
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;
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
bool contains(JS::GCPtr<Node>) const;
|
2021-07-05 07:55:02 +03:00
|
|
|
|
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;
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
void replace_all(JS::GCPtr<Node>);
|
2021-09-06 03:25:58 +03:00
|
|
|
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
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
JS::NonnullGCPtr<Node> get_root_node(GetRootNodeOptions const& options = {});
|
2021-10-16 05:04:55 +03:00
|
|
|
|
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-09-01 18:59:48 +03:00
|
|
|
auto& registered_observers_list() { return m_registered_observer_list; }
|
|
|
|
auto const& registered_observers_list() const { return m_registered_observer_list; }
|
2022-07-11 18:37:51 +03:00
|
|
|
|
|
|
|
void add_registered_observer(RegisteredObserver& registered_observer) { m_registered_observer_list.append(registered_observer); }
|
|
|
|
|
2022-09-01 17:30:26 +03:00
|
|
|
void queue_mutation_record(FlyString const& type, String attribute_name, String attribute_namespace, String old_value, JS::NonnullGCPtr<NodeList> added_nodes, JS::NonnullGCPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
|
2022-07-11 18:39:14 +03:00
|
|
|
|
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);
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
Node* parent() { return m_parent.ptr(); }
|
|
|
|
Node const* parent() const { return m_parent.ptr(); }
|
|
|
|
|
|
|
|
bool has_children() const { return m_first_child; }
|
|
|
|
Node* next_sibling() { return m_next_sibling.ptr(); }
|
|
|
|
Node* previous_sibling() { return m_previous_sibling.ptr(); }
|
|
|
|
Node* first_child() { return m_first_child.ptr(); }
|
|
|
|
Node* last_child() { return m_last_child.ptr(); }
|
|
|
|
Node const* next_sibling() const { return m_next_sibling.ptr(); }
|
|
|
|
Node const* previous_sibling() const { return m_previous_sibling.ptr(); }
|
|
|
|
Node const* first_child() const { return m_first_child.ptr(); }
|
|
|
|
Node const* last_child() const { return m_last_child.ptr(); }
|
|
|
|
|
|
|
|
size_t child_count() const
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling())
|
|
|
|
++count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* child_at_index(int index)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (count == index)
|
|
|
|
return child;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node const* child_at_index(int index) const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->child_at_index(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-tree-index
|
|
|
|
size_t index() const
|
|
|
|
{
|
|
|
|
// The index of an object is its number of preceding siblings, or 0 if it has none.
|
|
|
|
size_t index = 0;
|
|
|
|
for (auto* node = previous_sibling(); node; node = node->previous_sibling())
|
|
|
|
++index;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<size_t> index_of_child(Node const& search_child)
|
|
|
|
{
|
|
|
|
VERIFY(search_child.parent() == this);
|
|
|
|
size_t index = 0;
|
|
|
|
auto* child = first_child();
|
|
|
|
VERIFY(child);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (child == &search_child)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
} while (child && (child = child->next_sibling()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ChildType>
|
|
|
|
Optional<size_t> index_of_child(Node const& search_child)
|
|
|
|
{
|
|
|
|
VERIFY(search_child.parent() == this);
|
|
|
|
size_t index = 0;
|
|
|
|
auto* child = first_child();
|
|
|
|
VERIFY(child);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!is<ChildType>(child))
|
|
|
|
continue;
|
|
|
|
if (child == &search_child)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
} while (child && (child = child->next_sibling()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_ancestor_of(Node const&) const;
|
|
|
|
bool is_inclusive_ancestor_of(Node const&) const;
|
|
|
|
bool is_descendant_of(Node const&) const;
|
|
|
|
bool is_inclusive_descendant_of(Node const&) const;
|
|
|
|
|
|
|
|
bool is_following(Node const&) const;
|
|
|
|
|
|
|
|
void prepend_child(JS::NonnullGCPtr<Node> node);
|
|
|
|
|
|
|
|
Node* next_in_pre_order()
|
|
|
|
{
|
|
|
|
if (first_child())
|
|
|
|
return first_child();
|
|
|
|
Node* node;
|
|
|
|
if (!(node = next_sibling())) {
|
|
|
|
node = parent();
|
|
|
|
while (node && !node->next_sibling())
|
|
|
|
node = node->parent();
|
|
|
|
if (node)
|
|
|
|
node = node->next_sibling();
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* next_in_pre_order(Node const* stay_within)
|
|
|
|
{
|
|
|
|
if (first_child())
|
|
|
|
return first_child();
|
|
|
|
|
|
|
|
Node* node = static_cast<Node*>(this);
|
|
|
|
Node* next = nullptr;
|
|
|
|
while (!(next = node->next_sibling())) {
|
|
|
|
node = node->parent();
|
|
|
|
if (!node || node == stay_within)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node const* next_in_pre_order() const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->next_in_pre_order();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node const* next_in_pre_order(Node const* stay_within) const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->next_in_pre_order(stay_within);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* previous_in_pre_order()
|
|
|
|
{
|
|
|
|
if (auto* node = previous_sibling()) {
|
|
|
|
while (node->last_child())
|
|
|
|
node = node->last_child();
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node const* previous_in_pre_order() const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->previous_in_pre_order();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_before(Node const& other) const
|
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return false;
|
|
|
|
for (auto* node = this; node; node = node->next_in_pre_order()) {
|
|
|
|
if (node == &other)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-tree-preceding (Object A is 'typename U' and Object B is 'this')
|
|
|
|
template<typename U>
|
|
|
|
bool has_preceding_node_of_type_in_tree_order() const
|
|
|
|
{
|
|
|
|
for (auto* node = previous_in_pre_order(); node; node = node->previous_in_pre_order()) {
|
|
|
|
if (is<U>(node))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#concept-tree-following (Object A is 'typename U' and Object B is 'this')
|
|
|
|
template<typename U>
|
|
|
|
bool has_following_node_of_type_in_tree_order() const
|
|
|
|
{
|
|
|
|
for (auto* node = next_in_pre_order(); node; node = node->next_in_pre_order()) {
|
|
|
|
if (is<U>(node))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
IterationDecision for_each_in_inclusive_subtree(Callback callback) const
|
|
|
|
{
|
|
|
|
if (callback(static_cast<Node const&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
IterationDecision for_each_in_inclusive_subtree(Callback callback)
|
|
|
|
{
|
|
|
|
if (callback(static_cast<Node&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
|
|
|
|
{
|
|
|
|
if (is<U>(static_cast<Node const&>(*this))) {
|
|
|
|
if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
|
|
|
|
{
|
|
|
|
if (is<U>(static_cast<Node const&>(*this))) {
|
|
|
|
if (callback(static_cast<U const&>(*this)) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree_of_type(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
IterationDecision for_each_in_subtree_of_type(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child(Callback callback) const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->template for_each_child(move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling())
|
|
|
|
callback(*node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
void for_each_child_of_type(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
|
|
|
if (is<U>(node))
|
|
|
|
callback(verify_cast<U>(*node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U, typename Callback>
|
|
|
|
void for_each_child_of_type(Callback callback) const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->template for_each_child_of_type<U>(move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U const* next_sibling_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->template next_sibling_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
inline U* next_sibling_of_type()
|
|
|
|
{
|
|
|
|
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
|
|
|
|
if (is<U>(*sibling))
|
|
|
|
return &verify_cast<U>(*sibling);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U const* previous_sibling_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->template previous_sibling_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* previous_sibling_of_type()
|
|
|
|
{
|
|
|
|
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
|
|
|
|
if (is<U>(*sibling))
|
|
|
|
return &verify_cast<U>(*sibling);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U const* first_child_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->template first_child_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U const* last_child_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->template last_child_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* first_child_of_type()
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (is<U>(*child))
|
|
|
|
return &verify_cast<U>(*child);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* last_child_of_type()
|
|
|
|
{
|
|
|
|
for (auto* child = last_child(); child; child = child->previous_sibling()) {
|
|
|
|
if (is<U>(*child))
|
|
|
|
return &verify_cast<U>(*child);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
bool has_child_of_type() const
|
|
|
|
{
|
|
|
|
return first_child_of_type<U>() != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U const* first_ancestor_of_type() const
|
|
|
|
{
|
|
|
|
return const_cast<Node*>(this)->template first_ancestor_of_type<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* first_ancestor_of_type()
|
|
|
|
{
|
|
|
|
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (is<U>(*ancestor))
|
|
|
|
return &verify_cast<U>(*ancestor);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_parent_of(Node const& other) const
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (&other == child)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-15 19:55:47 +03:00
|
|
|
protected:
|
2022-08-28 14:42:07 +03:00
|
|
|
Node(JS::Realm&, Document&, NodeType);
|
2019-09-29 12:43:07 +03:00
|
|
|
Node(Document&, NodeType);
|
2019-06-15 19:55:47 +03:00
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
|
|
JS::GCPtr<Document> m_document;
|
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
|
2022-09-01 18:59:48 +03:00
|
|
|
Vector<RegisteredObserver&> m_registered_observer_list;
|
2022-07-11 18:39:14 +03:00
|
|
|
|
|
|
|
private:
|
2022-09-01 17:30:26 +03:00
|
|
|
void queue_tree_mutation_record(JS::NonnullGCPtr<NodeList> added_nodes, JS::NonnullGCPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
|
2022-08-28 14:42:07 +03:00
|
|
|
|
|
|
|
void insert_before_impl(JS::NonnullGCPtr<Node>, JS::GCPtr<Node> child);
|
|
|
|
void append_child_impl(JS::NonnullGCPtr<Node>);
|
|
|
|
void remove_child_impl(JS::NonnullGCPtr<Node>);
|
|
|
|
|
|
|
|
JS::GCPtr<Node> m_parent;
|
|
|
|
JS::GCPtr<Node> m_first_child;
|
|
|
|
JS::GCPtr<Node> m_last_child;
|
|
|
|
JS::GCPtr<Node> m_next_sibling;
|
|
|
|
JS::GCPtr<Node> m_previous_sibling;
|
2022-09-21 14:49:31 +03:00
|
|
|
|
|
|
|
JS::GCPtr<NodeList> m_child_nodes;
|
2019-06-15 19:55:47 +03:00
|
|
|
};
|
2019-10-06 21:37:39 +03:00
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
}
|