mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 10:48:13 +00:00
LibWeb+LibJS: Make the EventTarget hierarchy (incl. DOM) GC-allocated
This is a monster patch that turns all EventTargets into GC-allocated PlatformObjects. Their C++ wrapper classes are removed, and the LibJS garbage collector is now responsible for their lifetimes. There's a fair amount of hacks and band-aids in this patch, and we'll have a lot of cleanup to do after this.
This commit is contained in:
parent
bb547ce1c4
commit
6f433c8656
445 changed files with 4797 additions and 4268 deletions
|
@ -16,7 +16,6 @@
|
|||
#include <LibWeb/DOM/EventTarget.h>
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/DOM/MutationObserver.h>
|
||||
#include <LibWeb/TreeNode.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
|
@ -40,26 +39,16 @@ struct GetRootNodeOptions {
|
|||
bool composed { false };
|
||||
};
|
||||
|
||||
class Node
|
||||
: public TreeNode<Node>
|
||||
, public EventTarget
|
||||
, public Bindings::Wrappable {
|
||||
class Node : public EventTarget {
|
||||
WEB_PLATFORM_OBJECT(Node, EventTarget);
|
||||
|
||||
public:
|
||||
using WrapperType = Bindings::NodeWrapper;
|
||||
|
||||
using TreeNode<Node>::ref;
|
||||
using TreeNode<Node>::unref;
|
||||
|
||||
ParentNode* parent_or_shadow_host();
|
||||
ParentNode const* parent_or_shadow_host() const { return const_cast<Node*>(this)->parent_or_shadow_host(); }
|
||||
|
||||
// ^EventTarget
|
||||
virtual void ref_event_target() final { ref(); }
|
||||
virtual void unref_event_target() final { unref(); }
|
||||
virtual JS::Object* create_wrapper(JS::Realm&) override;
|
||||
|
||||
virtual ~Node();
|
||||
|
||||
// FIXME: Move cleanup to the regular destructor.
|
||||
void removed_last_ref();
|
||||
|
||||
NodeType type() const { return m_type; }
|
||||
|
@ -94,26 +83,26 @@ public:
|
|||
virtual bool is_html_template_element() const { return false; }
|
||||
virtual bool is_browsing_context_container() const { return false; }
|
||||
|
||||
ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
|
||||
ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);
|
||||
ExceptionOr<JS::NonnullGCPtr<Node>> pre_insert(JS::NonnullGCPtr<Node>, JS::GCPtr<Node>);
|
||||
ExceptionOr<JS::NonnullGCPtr<Node>> pre_remove(JS::NonnullGCPtr<Node>);
|
||||
|
||||
ExceptionOr<NonnullRefPtr<Node>> append_child(NonnullRefPtr<Node>);
|
||||
ExceptionOr<NonnullRefPtr<Node>> remove_child(NonnullRefPtr<Node>);
|
||||
ExceptionOr<JS::NonnullGCPtr<Node>> append_child(JS::NonnullGCPtr<Node>);
|
||||
ExceptionOr<JS::NonnullGCPtr<Node>> remove_child(JS::NonnullGCPtr<Node>);
|
||||
|
||||
void insert_before(NonnullRefPtr<Node> node, RefPtr<Node> child, bool suppress_observers = false);
|
||||
void insert_before(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child, bool suppress_observers = false);
|
||||
void remove(bool suppress_observers = false);
|
||||
void remove_all_children(bool suppress_observers = false);
|
||||
u16 compare_document_position(RefPtr<Node> other);
|
||||
u16 compare_document_position(JS::GCPtr<Node> other);
|
||||
|
||||
ExceptionOr<NonnullRefPtr<Node>> replace_child(NonnullRefPtr<Node> node, NonnullRefPtr<Node> child);
|
||||
ExceptionOr<JS::NonnullGCPtr<Node>> replace_child(JS::NonnullGCPtr<Node> node, JS::NonnullGCPtr<Node> child);
|
||||
|
||||
NonnullRefPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
|
||||
ExceptionOr<NonnullRefPtr<Node>> clone_node_binding(bool deep);
|
||||
JS::NonnullGCPtr<Node> clone_node(Document* document = nullptr, bool clone_children = false);
|
||||
ExceptionOr<JS::NonnullGCPtr<Node>> clone_node_binding(bool deep);
|
||||
|
||||
// NOTE: This is intended for the JS bindings.
|
||||
bool has_child_nodes() const { return has_children(); }
|
||||
NonnullRefPtr<NodeList> child_nodes();
|
||||
NonnullRefPtrVector<Node> children_as_vector() const;
|
||||
Vector<JS::Handle<Node>> children_as_vector() const;
|
||||
|
||||
virtual FlyString node_name() const = 0;
|
||||
|
||||
|
@ -129,7 +118,7 @@ public:
|
|||
Document& document() { return *m_document; }
|
||||
Document const& document() const { return *m_document; }
|
||||
|
||||
RefPtr<Document> owner_document() const;
|
||||
JS::GCPtr<Document> owner_document() const;
|
||||
|
||||
const HTML::HTMLAnchorElement* enclosing_link_element() const;
|
||||
const HTML::HTMLElement* enclosing_html_element() const;
|
||||
|
@ -190,14 +179,14 @@ public:
|
|||
template<typename T>
|
||||
bool fast_is() const = delete;
|
||||
|
||||
ExceptionOr<void> ensure_pre_insertion_validity(NonnullRefPtr<Node> node, RefPtr<Node> child) const;
|
||||
ExceptionOr<void> ensure_pre_insertion_validity(JS::NonnullGCPtr<Node> node, JS::GCPtr<Node> child) const;
|
||||
|
||||
bool is_host_including_inclusive_ancestor_of(Node const&) const;
|
||||
|
||||
bool is_scripting_enabled() const;
|
||||
bool is_scripting_disabled() const;
|
||||
|
||||
bool contains(RefPtr<Node>) const;
|
||||
bool contains(JS::GCPtr<Node>) const;
|
||||
|
||||
// Used for dumping the DOM Tree
|
||||
void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const;
|
||||
|
@ -212,13 +201,13 @@ public:
|
|||
|
||||
String serialize_fragment() const;
|
||||
|
||||
void replace_all(RefPtr<Node>);
|
||||
void replace_all(JS::GCPtr<Node>);
|
||||
void string_replace_all(String const&);
|
||||
|
||||
bool is_same_node(Node const*) const;
|
||||
bool is_equal_node(Node const*) const;
|
||||
|
||||
NonnullRefPtr<Node> get_root_node(GetRootNodeOptions const& options = {});
|
||||
JS::NonnullGCPtr<Node> get_root_node(GetRootNodeOptions const& options = {});
|
||||
|
||||
bool is_uninteresting_whitespace_node() const;
|
||||
|
||||
|
@ -237,10 +226,407 @@ public:
|
|||
template<typename Callback>
|
||||
IterationDecision for_each_shadow_including_descendant(Callback);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected:
|
||||
Node(JS::Realm&, Document&, NodeType);
|
||||
Node(Document&, NodeType);
|
||||
|
||||
Document* m_document { nullptr };
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
JS::GCPtr<Document> m_document;
|
||||
mutable WeakPtr<Layout::Node> m_layout_node;
|
||||
NodeType m_type { NodeType::INVALID };
|
||||
bool m_needs_style_update { false };
|
||||
|
@ -254,6 +640,18 @@ protected:
|
|||
|
||||
private:
|
||||
void queue_tree_mutation_record(NonnullRefPtr<NodeList> added_nodes, NonnullRefPtr<NodeList> removed_nodes, Node* previous_sibling, Node* next_sibling);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
WRAPPER_HACK(Node, Web::DOM)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue