Skip to content

Truly independent web browser

LibWeb: Clear just the styles a hidden element’s update cleared

sideshowbarker2786e0b1cb24
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index afe7d6cb6f4..6ebaa50813e 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -278,8 +278,8 @@ StyleEngineFFI::FfiReactionElement StyleEngineFFI::web_css_style_inheritance_par
// Swaps in the styles of a style installation, which the render side installed in the layout arena, and performs
// the main-side effects of the elements whose styles move where they have any. Both are in tree order, and each
// element's effects follow its swaps, which they name: an element's effects can clear the styles of its descendants,
-// whose swaps come after them.
-void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* application, StyleSwap const* swaps, size_t swap_count, InstalledElementEffects const* effects, size_t effect_count, u32 document_damage)
+// which they name too, and whose swaps come after them.
+void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* application, StyleSwap const* swaps, size_t swap_count, InstalledElementEffects const* effects, size_t effect_count, u32 const* cleared_descendants, size_t cleared_descendant_count, u32 document_damage)
{
auto const& read = application->read;
auto& document = *application->document;
@@ -294,6 +294,7 @@ void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* applicatio
ScopeGuard end_style_installation = [&] { style_computer.end_style_installation(); };
ReadonlySpan<StyleSwap> all_swaps { swaps, swap_count };
+ ReadonlySpan<u32> all_cleared_descendants { cleared_descendants, cleared_descendant_count };
size_t next_swap = 0;
// Swaps in the styles up to the swap `end`, each element's at once.
auto swap_in_styles_until = [&](size_t end) {
@@ -414,9 +415,17 @@ void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* applicatio
if (element->installed_style().view().animated_overlay || element->custom_property_data_is_animation_overlay({}))
sample_animations_for_installed_record(DOM::AbstractElement { *element });
}
- // The render side decided whether the element's descendants hold styles while it is hidden.
- if (has_flag(main_effects, CSS::MainEffects::ClearHiddenDescendants))
- element->clear_computed_styles_from_display_none_descendants(read);
+ // The render side decided whether the element's descendants hold styles while it is hidden, and which of them
+ // do: those it found below the element in the tree the transaction was computed from. That can be another
+ // tree than the document's now, where a write made beside a transaction that flew moved an element in it.
+ if (has_flag(main_effects, CSS::MainEffects::ClearHiddenDescendants)) {
+ VERIFY(element_effects.cleared_descendants_begin <= element_effects.cleared_descendants_end);
+ VERIFY(element_effects.cleared_descendants_end <= all_cleared_descendants.size());
+ for (auto style_node : all_cleared_descendants.slice(element_effects.cleared_descendants_begin, element_effects.cleared_descendants_end - element_effects.cleared_descendants_begin)) {
+ if (auto* descendant = element_for(style_node))
+ descendant->clear_computed_style_below_display_none(read);
+ }
+ }
if (has_flag(main_effects, CSS::MainEffects::VisibilityFlip))
document.throttled_animation_visibility_changed();
}
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 2eed12351eb..68aaebc9676 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -2070,39 +2070,33 @@ void Element::settle_animations_of_flat_tree_subtree(Layout::BegunRead const& re
});
}
-void Element::clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read)
+void Element::clear_computed_style_below_display_none(Layout::BegunRead const& read)
{
- for_each_inclusive_descendant_element_in_flat_tree(*this, [this, &read](Element& descendant) {
- if (&descendant == this)
- return TraversalDecision::Continue;
- auto* element = &descendant;
- if (!element->has_style())
- return TraversalDecision::SkipChildrenAndContinue;
+ if (!has_style())
+ return;
- // Anchor names are registered outside the style record, so unregister them before discarding the only record
- // that identifies them. Rematerializing the element's style will register its current names again.
- if (element->is_connected() && unregister_current_anchor_names(*element, element->root()))
- element->document().record_partial_relayout_escape(PartialRelayoutEscapeReason::AnchorNamesUnregisteredByStyleChange);
-
- // The layout tree is torn down after the style update. Keep its style alive until then without
- // retaining the record as the descendant's current computed style.
- if (auto* layout_node = element->unsafe_layout_node(read))
- layout_node->pin_style_record_for_detachment();
- element->m_installed_style = {};
-
- // NB: SVG resources can still affect rendering when a DOM ancestor has display:none.
- // Restore their styles in this style update, so painting never needs to materialize
- // styles for referenced resources. The engine still holds their records and settles
- // the reaction from them.
- if (auto const* svg_element = as_if<SVG::SVGElement>(*element); svg_element && svg_element->is_referenceable_in_display_none_subtree())
- element->document().style_computer().style_engine().record_derived_element_style_input_change(element->style_node_id(), CSS::StyleEngine::PublishedStyle | CSS::StyleEngine::RecomputeStyle);
-
- element->for_each_synthetic_pseudo_element([&](CSS::PseudoElement, SyntheticPseudoElement& pseudo_element) {
- pseudo_element.clear_computed_style();
- });
- element->clear_custom_highlight_styles();
- return TraversalDecision::Continue;
+ // Anchor names are registered outside the style record, so unregister them before discarding the only record
+ // that identifies them. Rematerializing the element's style will register its current names again.
+ if (is_connected() && unregister_current_anchor_names(*this, root()))
+ document().record_partial_relayout_escape(PartialRelayoutEscapeReason::AnchorNamesUnregisteredByStyleChange);
+
+ // The layout tree is torn down after the style update. Keep its style alive until then without
+ // retaining the record as the descendant's current computed style.
+ if (auto* layout_node = unsafe_layout_node(read))
+ layout_node->pin_style_record_for_detachment();
+ m_installed_style = {};
+
+ // NB: SVG resources can still affect rendering when a DOM ancestor has display:none.
+ // Restore their styles in this style update, so painting never needs to materialize
+ // styles for referenced resources. The engine still holds their records and settles
+ // the reaction from them.
+ if (auto const* svg_element = as_if<SVG::SVGElement>(*this); svg_element && svg_element->is_referenceable_in_display_none_subtree())
+ document().style_computer().style_engine().record_derived_element_style_input_change(style_node_id(), CSS::StyleEngine::PublishedStyle | CSS::StyleEngine::RecomputeStyle);
+
+ for_each_synthetic_pseudo_element([&](CSS::PseudoElement, SyntheticPseudoElement& pseudo_element) {
+ pseudo_element.clear_computed_style();
});
+ clear_custom_highlight_styles();
}
void Element::invalidate_descendant_styles_depending_on_style_container_query()
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index a7cbcf57a8b..e158b4d3870 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -402,9 +402,9 @@ public:
// As above, for a subtree that may have entered the flat tree, whose animations a record that did not move has yet
// to plan.
void settle_animations_of_flat_tree_subtree(Layout::BegunRead const&, Function<void(Element&)> const& compose_element);
- // Clears the styles of the descendants of a display:none element, which have no boxes: a CSSOM read or the subtree's
- // reveal materializes them again. The styles of the SVG elements visible content can reference are computed again.
- void clear_computed_styles_from_display_none_descendants(Layout::BegunRead const& read);
+ // Clears the style of a descendant of a display:none element, which has no box: a CSSOM read or the subtree's
+ // reveal materializes it again. The style of an SVG element visible content can reference is computed again.
+ void clear_computed_style_below_display_none(Layout::BegunRead const& read);
// The custom-property environment an engine-computed record was published with: the one the
// element inherits, or one the engine resolved over it. Nothing when it cannot be installed.
[[nodiscard]] RefPtr<CSS::CustomPropertyData const> custom_property_environment_of_engine_record(Layout::BegunRead const& read, u64 environment, bool& installable) const;
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs
index 8bac08c791e..ca11e490e05 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs
@@ -97,6 +97,10 @@ pub(crate) struct StyleInstallation {
/// The elements taking their first style whose parent, its layout tree parent as well, takes its first style in
/// this installation too, in the order of their effects, each with the index of those and that parent.
first_styles_below_first_styles: Vec<(u32, StyleNodeID)>,
+ /// The descendants whose styles the effects of hidden elements clear, in the order of those effects, which name
+ /// theirs. The host clears just these, rather than those its own tree has below each hidden element: a transaction
+ /// that flew decided them from the tree as it was sealed, which the writes made beside it may have moved since.
+ pub(crate) cleared_descendants: Vec<u32>,
}
impl StyleInstallation {
@@ -121,8 +125,9 @@ impl StyleInstallation {
/// Notes in `state` what the host holds once it took the installation, as it swaps in the styles and clears those
/// of hidden descendants without telling the render side: in the order the host takes them, each element's swaps,
- /// then the styles its effects clear, which the swaps of its descendants after them install again.
- pub(crate) fn note_held_by_the_host(&self, state: &mut RetainedState) {
+ /// then the styles its effects clear, which the swaps of its descendants after them install again. Names the
+ /// descendants each element's effects clear in those effects, for the host to clear.
+ pub(crate) fn note_held_by_the_host(&mut self, state: &mut RetainedState) {
let note_swaps = |state: &mut RetainedState, swaps: &[StyleSwap]| {
for swap in swaps {
let node = StyleNodeID::from_raw(swap.style_node).expect("a swap names an element");
@@ -141,17 +146,22 @@ impl StyleInstallation {
}
}
};
+ let index =
+ |cleared: usize| u32::try_from(cleared).expect("an installation's cleared descendants fit their index");
+ self.cleared_descendants.clear();
let mut next_swap = 0;
- for effects in &self.effects {
+ for effects in &mut self.effects {
let swaps_end = effects.swaps_end as usize;
note_swaps(state, &self.swaps[next_swap..swaps_end.max(next_swap)]);
next_swap = next_swap.max(swaps_end);
+ effects.cleared_descendants_begin = index(self.cleared_descendants.len());
if effects
.effects
.intersects(bridge::MainEffects::CLEAR_HIDDEN_DESCENDANTS)
{
- state.clear_hidden_descendants(effects.node());
+ state.clear_hidden_descendants(effects.node(), &mut self.cleared_descendants);
}
+ effects.cleared_descendants_end = index(self.cleared_descendants.len());
}
note_swaps(state, &self.swaps[next_swap..]);
}
@@ -821,16 +831,19 @@ impl RetainedState {
found
}
- /// Notes that the host cleared the styles of `node`'s descendants that hold one, in the flat tree, as it does for
- /// the descendants of a hidden element.
- pub(crate) fn clear_hidden_descendants(&mut self, node: StyleNodeID) {
- let mut cleared = Vec::new();
+ /// Notes that the host clears the styles of `node`'s descendants that hold one, in the flat tree, as it does for
+ /// the descendants of a hidden element, and appends them to `cleared`, the host's list of the elements to clear.
+ pub(crate) fn clear_hidden_descendants(&mut self, node: StyleNodeID, cleared: &mut Vec<u32>) {
+ let first = cleared.len();
self.for_each_held_flat_tree_descendant(node, |_, element| {
- cleared.push(element);
+ cleared.push(element.raw());
true
});
- for element in cleared {
- self.set_held_style_record(element, None);
+ for &element in &cleared[first..] {
+ self.set_held_style_record(
+ StyleNodeID::from_raw(element).expect("a cleared descendant is an element"),
+ None,
+ );
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
index 99ccd75453d..86fbc2a5e9f 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
@@ -43,6 +43,10 @@ pub struct InstalledElementEffects {
/// last, by index among the swaps of the installation.
pub(crate) swaps_begin: u32,
pub(crate) swaps_end: u32,
+ /// The descendants whose styles the element's effects clear, from the first to one past the last, by index among
+ /// the cleared descendants of the installation.
+ pub(crate) cleared_descendants_begin: u32,
+ pub(crate) cleared_descendants_end: u32,
}
// SAFETY: The pointer only borrows the immutable record the installation pins until the host ended the transaction.
@@ -388,6 +392,8 @@ impl RetainedState {
pseudo_element_transition_steps: facts.pseudo_elements.transition_steps,
swaps_begin: 0,
swaps_end: 0,
+ cleared_descendants_begin: 0,
+ cleared_descendants_end: 0,
}
}
@@ -506,7 +512,7 @@ mod tests {
add_class(&mut engine, nodes[1], "hidden");
run_style_update(&mut engine, nodes[0]);
- let installation = engine.take_installation();
+ let mut installation = engine.take_installation();
assert!(
installation
.effects_of(nodes[1])
diff --git a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
index e0986571a6e..fd67f08859a 100644
--- a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
@@ -122,6 +122,8 @@ unsafe extern "C" {
swap_count: usize,
effects: *const InstalledElementEffects,
effect_count: usize,
+ cleared_descendants: *const u32,
+ cleared_descendant_count: usize,
document_damage: u32,
);
}
@@ -181,6 +183,8 @@ impl Application<'_> {
installation.swaps.len(),
installation.effects.as_ptr(),
installation.effects.len(),
+ installation.cleared_descendants.as_ptr(),
+ installation.cleared_descendants.len(),
installation.document_damage,
);
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/style_job.rs b/Libraries/LibWeb/Rust/src/css/style/style_job.rs
index 0a057672f14..be8977aeb81 100644
--- a/Libraries/LibWeb/Rust/src/css/style/style_job.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/style_job.rs
@@ -412,7 +412,7 @@ impl StyleJob {
.filter_map(|row| crate::css::transition::DecidedTransitionStep::of_row(&mut *engine, row))
.collect();
decided_transition_steps.sort_unstable_by_key(crate::css::transition::DecidedTransitionStep::node);
- let installation_for_host = engine.take_installation();
+ let mut installation_for_host = engine.take_installation();
// The render side knows what it publishes: the host swaps the installation's records in without echoing them.
installation_for_host.note_held_by_the_host(engine);
let installed_styles = engine.computed_group_sets.publish_installed_styles();
diff --git a/Tests/LibWeb/Text/expected/HTML/frame-in-flight/slot-assignment-changed-beside-a-style-update-that-hides-the-slot.txt b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/slot-assignment-changed-beside-a-style-update-that-hides-the-slot.txt
new file mode 100644
index 00000000000..667cd87ab03
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/frame-in-flight/slot-assignment-changed-beside-a-style-update-that-hides-the-slot.txt
@@ -0,0 +1,6 @@
+into the hidden slot, slot attribute: offsetParent: body, display: block
+into the hidden slot, slot name: offsetParent: body, display: block
+into the hidden slot, manual assignment: offsetParent: body, display: block
+out of the hidden slot, slot attribute: offsetParent: body, display: block
+out of the hidden slot, slot name: offsetParent: body, display: block
+out of the hidden slot, manual assignment: offsetParent: body, display: block
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/slot-assignment-changed-beside-a-style-update-that-hides-the-slot.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/slot-assignment-changed-beside-a-style-update-that-hides-the-slot.html
new file mode 100644
index 00000000000..2cbc6cd6de3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/slot-assignment-changed-beside-a-style-update-that-hides-the-slot.html
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="frame-in-flight.js"></script>
+<script>
+ // A style update that hides a slot's container clears the styles of the elements it found assigned to that slot,
+ // as the tree was when the update flew. An element assigned to the slot beside it keeps its style, and one assigned
+ // away from the slot gets its style back.
+ async function check(name, slotAssignment, startsInHiddenSlot, reassign) {
+ const host = document.createElement("x-host");
+ host.innerHTML = `<div slot="${startsInHiddenSlot ? "a" : "b"}"><div>x</div></div>`;
+ document.body.append(host);
+ const slotted = host.firstElementChild;
+ const inner = slotted.firstElementChild;
+ const root = host.attachShadow({ mode: "open", slotAssignment });
+ root.innerHTML = `<div><slot name="a"></slot></div><slot name="b"></slot>`;
+ const wrapper = root.firstElementChild;
+ const [hiddenSlot, shownSlot] = root.querySelectorAll("slot");
+ if (slotAssignment === "manual")
+ (startsInHiddenSlot ? hiddenSlot : shownSlot).assign(slotted);
+ await twoFramesLanded();
+ void inner.offsetTop;
+
+ await whileRecordingInFlight(() => {
+ wrapper.style.display = "none";
+ }, () => {
+ reassign(slotted, hiddenSlot, shownSlot);
+ }, "style");
+ await twoFramesLanded();
+ wrapper.style.display = "";
+
+ println(`${name}: offsetParent: ${inner.offsetParent.localName}, display: ${getComputedStyle(slotted).display}`);
+ host.remove();
+ }
+
+ asyncTest(async done => {
+ await check("into the hidden slot, slot attribute", "named", false, slotted => {
+ slotted.slot = "a";
+ });
+ await check("into the hidden slot, slot name", "named", false, (slotted, hiddenSlot, shownSlot) => {
+ shownSlot.name = "c";
+ hiddenSlot.name = "b";
+ });
+ await check("into the hidden slot, manual assignment", "manual", false, (slotted, hiddenSlot) => {
+ hiddenSlot.assign(slotted);
+ });
+ await check("out of the hidden slot, slot attribute", "named", true, slotted => {
+ slotted.slot = "b";
+ });
+ await check("out of the hidden slot, slot name", "named", true, (slotted, hiddenSlot, shownSlot) => {
+ hiddenSlot.name = "c";
+ shownSlot.name = "a";
+ });
+ await check("out of the hidden slot, manual assignment", "manual", true, (slotted, hiddenSlot, shownSlot) => {
+ shownSlot.assign(slotted);
+ });
+ done();
+ });
+</script>