Skip to content

Truly independent web browser

LibWeb: Update the slots that a node is moved away from by assign()

Andreas Kling0379dd9436d6
diff --git a/Libraries/LibWeb/HTML/HTMLSlotElement.cpp b/Libraries/LibWeb/HTML/HTMLSlotElement.cpp
index cf652938c72..abbaea0ac4a 100644
--- a/Libraries/LibWeb/HTML/HTMLSlotElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLSlotElement.cpp
@@ -126,6 +126,11 @@ void HTMLSlotElement::assign(GC::ConservativeVector<SlottableHandle> nodes)
GC::ConservativeVector<DOM::Slottable> nodes_set;
GC::ConservativeHashTable<GC::Ref<DOM::Node const>> nodes_in_nodes_set;
+ // INTEROP: The spec only assigns slottables for this's root below, so a slot in another tree that loses a node
+ // to this slot keeps it as an assigned node and fires no slotchange event. Browsers also assign
+ // slottables for the trees of those slots, and so do we.
+ GC::ConservativeVector<GC::Ref<DOM::Node>> roots_of_previous_slots;
+
// 3. For each node of nodes:
for (auto& node_handle : nodes) {
auto& node = node_handle.visit([](auto& node) -> DOM::SlottableMixin& { return *node; });
@@ -133,9 +138,14 @@ void HTMLSlotElement::assign(GC::ConservativeVector<SlottableHandle> nodes)
// 1. If node's manual slot assignment refers to a slot, then remove node from that slot's manually assigned nodes.
if (node.manual_slot_assignment() != nullptr) {
- node.manual_slot_assignment()->m_manually_assigned_nodes.remove_all_matching([&](auto const& manually_assigned_node) {
+ auto& previous_slot = *node.manual_slot_assignment();
+ previous_slot.m_manually_assigned_nodes.remove_all_matching([&](auto const& manually_assigned_node) {
return slottable == manually_assigned_node;
});
+
+ auto& previous_slot_root = previous_slot.root();
+ if (&previous_slot_root != &root() && !roots_of_previous_slots.contains_slow(GC::Ref { previous_slot_root }))
+ roots_of_previous_slots.append(previous_slot_root);
}
// 2. Set node's manual slot assignment to this.
@@ -152,6 +162,10 @@ void HTMLSlotElement::assign(GC::ConservativeVector<SlottableHandle> nodes)
// 5. Run assign slottables for a tree for this's root.
assign_slottables_for_a_tree(root());
+
+ // INTEROP: See roots_of_previous_slots above.
+ for (auto const& previous_slot_root : roots_of_previous_slots)
+ assign_slottables_for_a_tree(previous_slot_root);
}
// https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-change-ext
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/imperative-slot-api-disconnected.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/imperative-slot-api-disconnected.txt
index 54a8fac482b..992c49c5927 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/imperative-slot-api-disconnected.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/imperative-slot-api-disconnected.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Assigning to disconnected slot after connected slot should not crash and should trigger slotchange on previous slot
\ No newline at end of file
+1 Pass
+Pass Assigning to disconnected slot after connected slot should not crash and should trigger slotchange on previous slot
\ No newline at end of file