LibWeb: Avoid subtree iteration when assigning slottables

This change introduces SlotRegistry to track slot elements per shadow
root. This allows us to iterate slots directly when assigning
slottables for a tree instead of walking an entire subtree.
This commit is contained in:
Tim Ledbetter
2026-01-28 13:12:46 +00:00
committed by Shannon Booth
parent 2017347a77
commit 18b8ba1fd3
Notes: github-actions[bot] 2026-02-06 10:52:20 +00:00
9 changed files with 213 additions and 21 deletions

View File

@@ -9,6 +9,7 @@
#include <LibWeb/Bindings/HTMLSlotElementPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
@@ -151,6 +152,11 @@ void HTMLSlotElement::attribute_changed(FlyString const& local_name, Optional<St
if (value == String {} && !old_value.has_value())
return;
// OPTIMIZATION: Update the slot registry before changing the name.
auto* shadow_root = as_if<DOM::ShadowRoot>(root());
if (shadow_root)
shadow_root->unregister_slot(*this);
// 4. If value is null or the empty string, then set elements name to the empty string.
if (!value.has_value())
set_slot_name({});
@@ -158,6 +164,10 @@ void HTMLSlotElement::attribute_changed(FlyString const& local_name, Optional<St
else
set_slot_name(*value);
// OPTIMIZATION: Register the slot with its new name.
if (shadow_root)
shadow_root->register_slot(*this);
// 6. Run assign slottables for a tree with elements root.
DOM::assign_slottables_for_a_tree(root());
}