2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-06-15 13:55:47 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-06-15 17:49:44 -03:00
|
|
|
#include <AK/Badge.h>
|
2021-06-07 12:32:24 -03:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2019-06-21 13:45:35 -03:00
|
|
|
#include <AK/RefPtr.h>
|
2019-09-28 17:59:16 -03:00
|
|
|
#include <AK/String.h>
|
2020-07-26 12:16:18 -03:00
|
|
|
#include <AK/TypeCasts.h>
|
2019-06-15 13:55:47 -03:00
|
|
|
#include <AK/Vector.h>
|
2020-03-14 09:15:11 -03:00
|
|
|
#include <LibWeb/Bindings/Wrappable.h>
|
2020-03-18 11:22:31 -03:00
|
|
|
#include <LibWeb/DOM/EventTarget.h>
|
2021-04-06 15:34:49 -03:00
|
|
|
#include <LibWeb/DOM/ExceptionOr.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/TreeNode.h>
|
2019-06-15 13:55:47 -03:00
|
|
|
|
2020-07-26 14:37:56 -03:00
|
|
|
namespace Web::DOM {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2021-03-06 14:06:25 -03:00
|
|
|
enum class NodeType : u16 {
|
2019-06-15 13:55:47 -03:00
|
|
|
INVALID = 0,
|
|
|
|
|
ELEMENT_NODE = 1,
|
2021-03-06 14:06:25 -03:00
|
|
|
ATTRIBUTE_NODE = 2,
|
2019-06-15 13:55:47 -03:00
|
|
|
TEXT_NODE = 3,
|
2021-03-06 14:06:25 -03:00
|
|
|
CDATA_SECTION_NODE = 4,
|
|
|
|
|
ENTITY_REFERENCE_NODE = 5,
|
|
|
|
|
ENTITY_NODE = 6,
|
|
|
|
|
PROCESSING_INSTRUCTION_NODE = 7,
|
2019-10-12 18:26:47 -03:00
|
|
|
COMMENT_NODE = 8,
|
2019-06-15 13:55:47 -03:00
|
|
|
DOCUMENT_NODE = 9,
|
2019-10-09 15:17:01 -03:00
|
|
|
DOCUMENT_TYPE_NODE = 10,
|
2019-11-06 16:27:53 -03:00
|
|
|
DOCUMENT_FRAGMENT_NODE = 11,
|
2021-03-06 14:06:25 -03:00
|
|
|
NOTATION_NODE = 12
|
2019-06-15 13:55:47 -03:00
|
|
|
};
|
|
|
|
|
|
2020-03-14 09:15:11 -03:00
|
|
|
class Node
|
|
|
|
|
: public TreeNode<Node>
|
2020-03-18 11:22:31 -03:00
|
|
|
, public EventTarget
|
2020-03-14 09:15:11 -03:00
|
|
|
, public Bindings::Wrappable {
|
2019-06-15 13:55:47 -03:00
|
|
|
public:
|
2020-03-14 09:24:15 -03:00
|
|
|
using WrapperType = Bindings::NodeWrapper;
|
|
|
|
|
|
2020-03-18 11:22:31 -03:00
|
|
|
using TreeNode<Node>::ref;
|
|
|
|
|
using TreeNode<Node>::unref;
|
|
|
|
|
|
2021-02-10 14:22:20 -03: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 11:22:31 -03:00
|
|
|
// ^EventTarget
|
|
|
|
|
virtual void ref_event_target() final { ref(); }
|
|
|
|
|
virtual void unref_event_target() final { unref(); }
|
2020-11-21 15:32:39 -03:00
|
|
|
virtual bool dispatch_event(NonnullRefPtr<Event>) final;
|
2021-01-18 08:15:02 -03:00
|
|
|
virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
|
2020-03-18 11:22:31 -03:00
|
|
|
|
2019-06-15 13:55:47 -03:00
|
|
|
virtual ~Node();
|
|
|
|
|
|
2020-10-11 16:52:59 -03:00
|
|
|
void removed_last_ref();
|
|
|
|
|
|
2019-06-15 13: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 15:17:01 -03:00
|
|
|
bool is_document_type() const { return type() == NodeType::DOCUMENT_TYPE_NODE; }
|
2019-10-12 18: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 16:27:53 -03:00
|
|
|
bool is_document_fragment() const { return type() == NodeType::DOCUMENT_FRAGMENT_NODE; }
|
2020-03-25 14:48:10 -03:00
|
|
|
bool is_parent_node() const { return is_element() || is_document() || is_document_fragment(); }
|
2020-11-21 15:32:39 -03:00
|
|
|
bool is_slottable() const { return is_element() || is_text(); }
|
2019-06-15 13:55:47 -03:00
|
|
|
|
2021-08-05 05:26:09 -03:00
|
|
|
virtual bool requires_svg_container() const { return false; }
|
|
|
|
|
virtual bool is_svg_container() const { return false; }
|
|
|
|
|
|
2021-03-06 14:06:25 -03:00
|
|
|
// NOTE: This is intended for the JS bindings.
|
|
|
|
|
u16 node_type() const { return (u16)m_type; }
|
|
|
|
|
|
2020-08-02 11:05:59 -03:00
|
|
|
virtual bool is_editable() const;
|
|
|
|
|
|
2021-09-15 19:52:10 -03:00
|
|
|
virtual bool is_html_template_element() const { return false; }
|
|
|
|
|
|
2021-04-13 15:33:48 -03:00
|
|
|
ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
|
|
|
|
|
ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);
|
2021-04-06 15:34:49 -03:00
|
|
|
|
2021-04-13 15:33:48 -03:00
|
|
|
ExceptionOr<NonnullRefPtr<Node>> append_child(NonnullRefPtr<Node>);
|
2021-04-06 15: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-10 21:21:22 -03:00
|
|
|
u16 compare_document_position(RefPtr<Node> other);
|
2020-06-20 20:00:58 -03:00
|
|
|
|
2021-05-06 20:52:23 -03:00
|
|
|
ExceptionOr<NonnullRefPtr<Node>> replace_child(NonnullRefPtr<Node> node, NonnullRefPtr<Node> child);
|
|
|
|
|
|
2021-07-05 01: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-13 20:25:10 -03:00
|
|
|
|
2021-03-06 14:06:25 -03: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 10:27:40 -03:00
|
|
|
virtual RefPtr<Layout::Node> create_layout_node();
|
2019-09-25 06:17:29 -03:00
|
|
|
|
2020-06-16 14:09:14 -03:00
|
|
|
virtual FlyString node_name() const = 0;
|
2019-09-28 17:59:16 -03:00
|
|
|
|
2021-09-05 20:21:59 -03:00
|
|
|
String descendant_text_content() const;
|
|
|
|
|
String text_content() const;
|
|
|
|
|
void set_text_content(String const&);
|
2019-09-29 11:22:15 -03:00
|
|
|
|
2020-06-25 18:42:08 -03:00
|
|
|
Document& document() { return *m_document; }
|
|
|
|
|
const Document& document() const { return *m_document; }
|
2019-09-29 06:43:07 -03:00
|
|
|
|
2021-05-02 17:03:50 -03:00
|
|
|
RefPtr<Document> owner_document() const;
|
|
|
|
|
|
2020-07-28 13:20:36 -03:00
|
|
|
const HTML::HTMLAnchorElement* enclosing_link_element() const;
|
|
|
|
|
const HTML::HTMLElement* enclosing_html_element() const;
|
2021-03-30 13:06:06 -03:00
|
|
|
const HTML::HTMLElement* enclosing_html_element_with_attribute(const FlyString&) const;
|
2019-09-29 07:24:36 -03:00
|
|
|
|
2020-05-24 16:59:24 -03:00
|
|
|
String child_text_content() const;
|
|
|
|
|
|
2021-09-02 15:27:42 -03:00
|
|
|
Node& root();
|
|
|
|
|
const Node& root() const
|
2020-11-21 15:32:39 -03:00
|
|
|
{
|
|
|
|
|
return const_cast<Node*>(this)->root();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 15:27:42 -03:00
|
|
|
Node& shadow_including_root();
|
|
|
|
|
const Node& shadow_including_root() const
|
2020-11-21 15:32:39 -03:00
|
|
|
{
|
|
|
|
|
return const_cast<Node*>(this)->shadow_including_root();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-24 16:59:24 -03:00
|
|
|
bool is_connected() const;
|
|
|
|
|
|
2020-06-20 17:26:54 -03:00
|
|
|
Node* parent_node() { return parent(); }
|
|
|
|
|
const Node* parent_node() const { return parent(); }
|
|
|
|
|
|
|
|
|
|
Element* parent_element();
|
|
|
|
|
const Element* parent_element() const;
|
|
|
|
|
|
2021-04-06 13:58:20 -03:00
|
|
|
virtual void inserted();
|
|
|
|
|
virtual void removed_from(Node*) { }
|
2020-07-26 12:16:18 -03:00
|
|
|
virtual void children_changed() { }
|
2021-07-05 01:33:35 -03:00
|
|
|
virtual void adopted_from(Document&) { }
|
2021-07-05 01:30:24 -03:00
|
|
|
virtual void cloned(Node&, bool) {};
|
2019-09-29 12:40:39 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
const Layout::Node* layout_node() const { return m_layout_node; }
|
|
|
|
|
Layout::Node* layout_node() { return m_layout_node; }
|
2019-10-04 16:05:52 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
void set_layout_node(Badge<Layout::Node>, Layout::Node*) const;
|
2019-10-04 16:05:52 -03:00
|
|
|
|
2019-10-12 18:26:47 -03:00
|
|
|
virtual bool is_child_allowed(const Node&) const { return true; }
|
|
|
|
|
|
2019-10-19 13:57:02 -03:00
|
|
|
bool needs_style_update() const { return m_needs_style_update; }
|
2020-12-13 11:19:42 -03:00
|
|
|
void set_needs_style_update(bool);
|
2019-10-19 13:57:02 -03:00
|
|
|
|
2020-12-14 08: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 13:32:02 -03:00
|
|
|
void invalidate_style();
|
|
|
|
|
|
2019-10-19 16:21:29 -03:00
|
|
|
bool is_link() const;
|
|
|
|
|
|
2020-06-25 18:42:08 -03:00
|
|
|
void set_document(Badge<Document>, Document&);
|
|
|
|
|
|
2020-11-21 15:32:39 -03:00
|
|
|
virtual EventTarget* get_parent(const Event&) override;
|
|
|
|
|
|
2021-01-17 05:34:01 -03:00
|
|
|
template<typename T>
|
|
|
|
|
bool fast_is() const = delete;
|
|
|
|
|
|
2021-04-06 15:34:49 -03:00
|
|
|
ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
|
|
|
|
|
|
|
|
|
|
bool is_host_including_inclusive_ancestor_of(const Node&) const;
|
|
|
|
|
|
2021-07-04 23:59:47 -03:00
|
|
|
bool is_scripting_disabled() const;
|
|
|
|
|
|
2021-07-05 01:55:02 -03:00
|
|
|
bool contains(RefPtr<Node>) const;
|
|
|
|
|
|
2021-06-07 12:32:24 -03:00
|
|
|
// Used for dumping the DOM Tree
|
|
|
|
|
void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const;
|
|
|
|
|
|
2021-09-01 22: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 11:52:08 -03:00
|
|
|
i32 id() const { return m_id; }
|
|
|
|
|
static Node* from_id(i32 node_id);
|
|
|
|
|
|
2021-09-13 18:42:15 -03:00
|
|
|
String serialize_fragment() const;
|
|
|
|
|
|
2021-09-05 21:25:58 -03:00
|
|
|
void replace_all(RefPtr<Node>);
|
|
|
|
|
void string_replace_all(String const&);
|
|
|
|
|
|
2021-09-13 07:49:23 -03:00
|
|
|
bool is_same_node(Node const*) const;
|
2021-09-13 07:54:24 -03:00
|
|
|
bool is_equal_node(Node const*) const;
|
2021-09-13 07:49:23 -03:00
|
|
|
|
2019-06-15 13:55:47 -03:00
|
|
|
protected:
|
2019-09-29 06:43:07 -03:00
|
|
|
Node(Document&, NodeType);
|
2019-06-15 13:55:47 -03:00
|
|
|
|
2020-06-25 18:42:08 -03:00
|
|
|
Document* m_document { nullptr };
|
2020-11-22 11:53:01 -03:00
|
|
|
mutable WeakPtr<Layout::Node> m_layout_node;
|
2019-06-15 13:55:47 -03:00
|
|
|
NodeType m_type { NodeType::INVALID };
|
2020-12-14 08:04:30 -03:00
|
|
|
bool m_needs_style_update { false };
|
|
|
|
|
bool m_child_needs_style_update { false };
|
2021-08-30 11:52:08 -03:00
|
|
|
|
|
|
|
|
i32 m_id;
|
2019-06-15 13:55:47 -03:00
|
|
|
};
|
2019-10-06 15:37:39 -03:00
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|