LibWeb: Move slotted style invalidation into a helper

Slottable.cpp still handled the style invalidation fallout from slot
assignment changes directly. Move that ::slotted()-related policy into
CSS::Invalidation::SlotInvalidator.

Slot assignment remains DOM bookkeeping. The helper now owns the
choice to dirty element slottables when they gain or lose assignment
to a slot.
This commit is contained in:
Andreas Kling 2026-04-29 12:10:26 +02:00 committed by Alexander Kalenik
parent bc0059cfd5
commit c191d51af1
4 changed files with 42 additions and 10 deletions

View file

@ -186,6 +186,7 @@ set(SOURCES
CSS/Invalidation/NodeInvalidator.cpp
CSS/Invalidation/PartInvalidator.cpp
CSS/Invalidation/PseudoClassInvalidator.cpp
CSS/Invalidation/SlotInvalidator.cpp
CSS/Invalidation/StyleInvalidator.cpp
CSS/Invalidation/StructuralMutationInvalidator.cpp
CSS/Interpolation.cpp

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2026-present, the Ladybird developers
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/Invalidation/SlotInvalidator.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Slottable.h>
#include <LibWeb/DOM/Text.h>
namespace Web::CSS::Invalidation {
void invalidate_style_after_slottable_assignment_change(DOM::Slottable const& slottable)
{
slottable.visit(
[](GC::Ref<DOM::Element> const& element) {
element->set_needs_style_update(true);
},
[](GC::Ref<DOM::Text> const&) {});
}
}

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2026-present, the Ladybird developers
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/DOM/Slottable.h>
namespace Web::CSS::Invalidation {
void invalidate_style_after_slottable_assignment_change(DOM::Slottable const&);
}

View file

@ -6,6 +6,7 @@
*/
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/CSS/Invalidation/SlotInvalidator.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/ShadowRoot.h>
@ -202,14 +203,6 @@ void assign_slottables(GC::Ref<HTML::HTMLSlotElement> slot)
if (slottables != slot->assigned_nodes_internal())
signal_a_slot_change(slot);
auto invalidate_for_slottable_change = [](Slottable const& slottable) {
slottable.visit(
[](GC::Ref<Element> const& element) {
element->set_needs_style_update(true);
},
[](GC::Ref<Text> const&) {});
};
// AD-HOC: Clear the assigned slot for slottables that are no longer assigned to this slot.
// This must happen before setting the new assigned slots to avoid stale references
// during style computation.
@ -219,7 +212,7 @@ void assign_slottables(GC::Ref<HTML::HTMLSlotElement> slot)
});
// ::slotted(...) rules in the slot's shadow scope no longer apply to a slottable that just lost its
// assignment, so its style must be recomputed.
invalidate_for_slottable_change(old_slottable);
CSS::Invalidation::invalidate_style_after_slottable_assignment_change(old_slottable);
}
// 4. For each slottable in slottables, set slottables assigned slot to slot.
@ -229,7 +222,7 @@ void assign_slottables(GC::Ref<HTML::HTMLSlotElement> slot)
});
// Newly-assigned slottables become subject to ::slotted(...) rules in the slot's shadow scope, so their style
// needs to be recomputed.
invalidate_for_slottable_change(slottable);
CSS::Invalidation::invalidate_style_after_slottable_assignment_change(slottable);
}
// 3. Set slots assigned nodes to slottables.