Skip to content

Truly independent web browser

LibWeb: Skip collecting post-connection nodes when none have steps

Andreas Kling692de37cd6b1
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index 0cbd486e49d..24e37ba63c7 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -1056,6 +1056,11 @@ void Node::insert_nodes_before(ReadonlySpan<GC::Ref<Node>> nodes, GC::Ptr<Node>
else
previous_sibling = last_child();
+ // NB: Set by the insertion steps loop below. Any later change to the tree, such as by script run from the
+ // children changed steps, can add other nodes to the ones the post-connection steps are collected from.
+ auto any_node_has_post_connection_steps = false;
+ auto tree_version_before_insertion_steps = s_last_tree_version;
+
// 7. For each node in nodes, in tree order:
// FIXME: In tree order
for (auto& node_to_insert : nodes) {
@@ -1073,6 +1078,8 @@ void Node::insert_nodes_before(ReadonlySpan<GC::Ref<Node>> nodes, GC::Ptr<Node>
node_to_insert->for_each_shadow_including_inclusive_descendant([&](Node& inclusive_descendant) {
// 1. Run the insertion steps with inclusiveDescendant.
inclusive_descendant.inserted();
+ if (!any_node_has_post_connection_steps)
+ any_node_has_post_connection_steps = inclusive_descendant.has_post_connection_steps();
// 2. If inclusiveDescendant is not connected, then continue.
if (!inclusive_descendant.is_connected())
@@ -1123,7 +1130,11 @@ void Node::insert_nodes_before(ReadonlySpan<GC::Ref<Node>> nodes, GC::Ptr<Node>
// OPTIMIZATION: Disconnected subtrees cannot have post-connection steps to run. If any root is connected,
// collect all nodes, since a callback could connect one of the initially detached subtrees.
- if (any_of(nodes, [](auto const& node) { return node->is_connected(); }))
+ // Unless some tree changed since the insertion steps began, the nodes to collect are the ones those
+ // steps ran for, so without one that has post-connection steps there is nothing to run.
+ auto tree_changed_since_insertion_steps = s_last_tree_version != tree_version_before_insertion_steps;
+ if ((any_node_has_post_connection_steps || tree_changed_since_insertion_steps)
+ && any_of(nodes, [](auto const& node) { return node->is_connected(); }))
run_post_connection_steps(nodes);
auto is_boxless_style_element = (is_html_style_element() || is_svg_style_element()) && !has_layout_box();
diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h
index 5318689ac7d..0aef97d0a21 100644
--- a/Libraries/LibWeb/DOM/Node.h
+++ b/Libraries/LibWeb/DOM/Node.h
@@ -412,6 +412,9 @@ public:
MUST_UPCALL virtual void inserted();
virtual void post_connection();
+ // Whether this node overrides post_connection(). Every override of post_connection() must also override this to
+ // return true, since insertions skip the post-connection steps when no inserted node has them.
+ virtual bool has_post_connection_steps() const { return false; }
enum class IsSubtreeRoot : u8 {
No,
Yes,
diff --git a/Libraries/LibWeb/HTML/HTMLFrameElement.h b/Libraries/LibWeb/HTML/HTMLFrameElement.h
index 689ebe242c7..a8d95f78cb4 100644
--- a/Libraries/LibWeb/HTML/HTMLFrameElement.h
+++ b/Libraries/LibWeb/HTML/HTMLFrameElement.h
@@ -24,6 +24,7 @@ private:
// ^DOM::Element
virtual void post_connection() override;
+ virtual bool has_post_connection_steps() const override { return true; }
virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;
virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
virtual i32 default_tab_index_value() const override;
diff --git a/Libraries/LibWeb/HTML/HTMLIFrameElement.h b/Libraries/LibWeb/HTML/HTMLIFrameElement.h
index 5013aaa32d8..2d7c79e4148 100644
--- a/Libraries/LibWeb/HTML/HTMLIFrameElement.h
+++ b/Libraries/LibWeb/HTML/HTMLIFrameElement.h
@@ -61,6 +61,7 @@ private:
// ^DOM::Element
virtual void post_connection() override;
+ virtual bool has_post_connection_steps() const override { return true; }
virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;
virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
virtual i32 default_tab_index_value() const override;
diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Libraries/LibWeb/HTML/HTMLScriptElement.h
index b961ae9da5e..dd393272ad9 100644
--- a/Libraries/LibWeb/HTML/HTMLScriptElement.h
+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.h
@@ -45,6 +45,7 @@ public:
virtual void children_changed(ChildrenChangedMetadata const&) override;
virtual void post_connection() override;
+ virtual bool has_post_connection_steps() const override { return true; }
// https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports
static bool supports(Utf16String const& type)
diff --git a/Libraries/LibWeb/HTML/HTMLSelectedContentElement.h b/Libraries/LibWeb/HTML/HTMLSelectedContentElement.h
index e5c75f8a9fc..72957c372d8 100644
--- a/Libraries/LibWeb/HTML/HTMLSelectedContentElement.h
+++ b/Libraries/LibWeb/HTML/HTMLSelectedContentElement.h
@@ -32,6 +32,7 @@ private:
HTMLSelectedContentElement(DOM::Document&, DOM::QualifiedName);
virtual void post_connection() override;
+ virtual bool has_post_connection_steps() const override { return true; }
virtual void removed_from(IsSubtreeRoot, DOM::Node* old_ancestor, DOM::Node& old_root) override;
// https://html.spec.whatwg.org/multipage/form-elements.html#selectedcontent-disabled