Skip to content

Truly independent web browser

LibWeb: Stop a cancelled CSS transition from restyling forever

Andreas Klinga19a2ca412db
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index 7af47a24899..5027d4dce2e 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -381,6 +381,7 @@ void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* applicatio
style_computer.apply_settled_animation_plan(read, abstract_element);
if (old_style && document.is_in_style_stabilization_epoch())
style_computer.record_transition_stabilization_baseline(abstract_element, old_style);
+ StyleEngineFFI::style_engine_begin_composition_settling(style_computer.style_engine().host(), &read, element->style_node_id().value());
if (style_computer.has_animations_to_compose(abstract_element))
sample_animations_for_installed_record(abstract_element);
else
@@ -390,6 +391,7 @@ void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* applicatio
apply_element_style_invalidation_after_style_change(read, *element, step_invalidation);
application->invalidation |= step_invalidation;
}
+ StyleEngineFFI::style_engine_end_composition_settling(style_computer.style_engine().host(), &read);
}
// The host composes the records swapped in for the element's pseudo-elements that owe the plan of their
// animations or the transition step, or whose element has animations, as it composes those it settles.
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index 3ebed3ae621..4d978233a9f 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -1289,6 +1289,17 @@ pub extern "C" fn style_engine_for_each_element_custom_property_environment(
}
}
+#[unsafe(no_mangle)]
+pub extern "C" fn style_engine_begin_composition_settling(host: &DocumentHost, read: &BegunRead, node: u32) {
+ let node = StyleNodeID::from_raw(node).expect("a composition settles for an element");
+ with_engine(read, host, |engine| engine.begin_composition_settling(node));
+}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn style_engine_end_composition_settling(host: &DocumentHost, read: &BegunRead) {
+ with_engine(read, host, |engine| engine.end_composition_settling());
+}
+
#[unsafe(no_mangle)]
pub extern "C" fn style_engine_has_pending_transaction(host: &DocumentHost, read: &BegunRead) -> bool {
match host.known_engine_facts() {
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index e01d16a8ed0..4aed3b98015 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -1635,6 +1635,7 @@ impl StyleEngine {
host: HostState {
hover_seal: Default::default(),
font_resolver: None,
+ settling_composition: None,
element_inputs: PendingElementInputs::default(),
deferred_element_style_inputs: Default::default(),
deferred_element_style_inputs_moved: false,
diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rs
index 8c8658bc86c..c128b9bee02 100644
--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs
@@ -400,6 +400,8 @@ pub struct HostState {
/// The host's synchronous font resolver. A step that misses the cache suspends its element;
/// the round outside the step calls this and the element is retried.
font_resolver: Option<font_resolution::FontResolverHost>,
+ /// The element whose composition the host is settling, whose moves are told once it has.
+ settling_composition: Option<pipeline::SettlingComposition>,
/// The element inputs the next transaction takes beside the projection's changes.
element_inputs: PendingElementInputs,
/// Exact element reactions retained across rootless flushes until a style root can consume them.
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
index e6ffd79eabe..623f9dfabd3 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/live.rs
@@ -4284,9 +4284,37 @@ impl StyleEngine {
.is_some_and(|live| live.released_compositions.iter().any(|(released, _)| *released == node))
}
+ /// The host goes on to sample the animations of `node` and run its transition step, which composes a transition and
+ /// then takes it out again where the step ends it. What they compose is told as one move, once they are over, so
+ /// that a pair that leaves the composition as it found it restyles nothing.
+ pub(in super::super) fn begin_composition_settling(&mut self, node: StyleNodeID) {
+ debug_assert!(self.host.settling_composition.is_none());
+ self.host.settling_composition = Some(SettlingComposition { node, held: None });
+ }
+
+ pub(in super::super) fn end_composition_settling(&mut self) {
+ let settling = self
+ .host
+ .settling_composition
+ .take()
+ .expect("a composition settling ends after it began");
+ if let Some(held) = settling.held {
+ self.note_composition_moved(settling.node, held);
+ }
+ }
+
/// Restyle the children of `node` that compute from what the composition of its animations
/// moved since `held`, in the next pass of the engine.
pub(in super::super) fn note_composition_moved(&mut self, node: StyleNodeID, held: HeldComposition) {
+ if let Some(settling) = self
+ .host
+ .settling_composition
+ .as_mut()
+ .filter(|settling| settling.node == node)
+ {
+ settling.held.get_or_insert(held);
+ return;
+ }
let Some(view) = DriveRecords::assigned_record_view(&self.retained, node) else {
return;
};
@@ -4551,6 +4579,14 @@ fn resolve_waits(
/// What the children of an element compute from of the view it held before the composition of
/// its animations moved: its groups, each holding a reference, and the environment its animations
/// sampled custom properties into, where they do.
+#[derive(Clone)]
+pub(in super::super) struct SettlingComposition {
+ node: StyleNodeID,
+ /// What the element's children computed from before the first move.
+ held: Option<HeldComposition>,
+}
+
+#[derive(Clone)]
pub(in super::super) struct HeldComposition {
payloads: crate::css::computed_values::StyleGroups,
custom_properties: Option<u64>,
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/mod.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/mod.rs
index eb88a2a63ab..8a219018b77 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/mod.rs
@@ -57,6 +57,7 @@ use super::*;
use bloom::{attribute_hash, class_hash, id_hash, tag_hash};
pub(super) use cascade::DeclaredOnly;
pub(super) use live::Live;
+pub(super) use live::SettlingComposition;
#[cfg(test)]
pub(super) use records::PseudoElementStyles;
pub(super) use records::PseudoElementTarget;
diff --git a/Tests/LibWeb/Crash/CSS/cancelled-transition-on-element-with-pseudo-element.html b/Tests/LibWeb/Crash/CSS/cancelled-transition-on-element-with-pseudo-element.html
new file mode 100644
index 00000000000..5be871ed369
--- /dev/null
+++ b/Tests/LibWeb/Crash/CSS/cancelled-transition-on-element-with-pseudo-element.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<style>
+ @property --color {
+ syntax: "<color>";
+ inherits: false;
+ initial-value: red;
+ }
+ #container {
+ container-type: inline-size;
+ width: 200px;
+ }
+ @container (width > 0px) {
+ #target, #custom {
+ color: green;
+ }
+ }
+ #target {
+ background-color: red;
+ transition: background-color 1s 100s;
+ }
+ #target.moved {
+ background-color: blue;
+ }
+ #custom {
+ --color: red;
+ transition: --color 1s 100s;
+ }
+ #custom.moved {
+ --color: blue;
+ }
+ #target::before, #custom::before {
+ content: "x";
+ }
+</style>
+<div id="container"><span id="target"></span><span id="custom"></span></div>
+<script>
+ for (const element of [target, custom]) {
+ getComputedStyle(element).color;
+ element.className = "moved";
+ getComputedStyle(element).color;
+ element.className = "";
+ getComputedStyle(element).color;
+ }
+</script>
diff --git a/Tests/LibWeb/Text/expected/css/transition-decision-superseded-within-layout-update.txt b/Tests/LibWeb/Text/expected/css/transition-decision-superseded-within-layout-update.txt
new file mode 100644
index 00000000000..52b641bf27e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/transition-decision-superseded-within-layout-update.txt
@@ -0,0 +1,7 @@
+kept before: rgb(191, 0, 64)
+replaced before: rgb(191, 0, 64)
+kept transition survives: true
+kept after: rgb(191, 0, 64)
+old transition replaced: true
+has replacement: true
+replacement starts from: rgb(191, 0, 64)
diff --git a/Tests/LibWeb/Text/input/css/transition-decision-superseded-within-layout-update.html b/Tests/LibWeb/Text/input/css/transition-decision-superseded-within-layout-update.html
new file mode 100644
index 00000000000..d459c0f028e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/transition-decision-superseded-within-layout-update.html
@@ -0,0 +1,75 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ #outer {
+ container: outer / inline-size;
+ width: 200px;
+ }
+ #inner {
+ container: inner / inline-size;
+ width: 100px;
+ }
+ #kept, #replaced {
+ background-color: red;
+ transition: background-color 1000s linear;
+ }
+ #kept.moved, #replaced.moved {
+ background-color: blue;
+ }
+ @container outer (width < 150px) {
+ #inner {
+ width: 200px;
+ }
+ #kept.moved, #replaced.moved {
+ transition-property: none;
+ }
+ }
+ @container inner (width > 150px) {
+ #kept.moved, #replaced.moved {
+ transition-property: background-color;
+ }
+ #replaced.moved {
+ background-color: lime;
+ }
+ }
+</style>
+<div id="outer">
+ <div id="inner">
+ <div id="kept">kept</div>
+ <div id="replaced">replaced</div>
+ </div>
+</div>
+<script>
+ test(() => {
+ document.body.getBoundingClientRect();
+ kept.className = "moved";
+ replaced.className = "moved";
+ document.body.getBoundingClientRect();
+ for (const element of [kept, replaced]) {
+ const transition = element.getAnimations()[0];
+ transition.pause();
+ transition.currentTime = 250000;
+ }
+ println(`kept before: ${getComputedStyle(kept).backgroundColor}`);
+ println(`replaced before: ${getComputedStyle(replaced).backgroundColor}`);
+ const keptTransition = kept.getAnimations()[0];
+ const replacedTransition = replaced.getAnimations()[0];
+
+ // Layout makes the outer query match, which cancels both transitions in one pass, and then the inner query
+ // match, which brings their transition-property back in a later pass of the same update.
+ outer.style.width = "100px";
+ document.body.getBoundingClientRect();
+
+ println(`kept transition survives: ${kept.getAnimations()[0] === keptTransition}`);
+ println(`kept after: ${getComputedStyle(kept).backgroundColor}`);
+
+ println(`old transition replaced: ${replacedTransition.playState === "idle"}`);
+ const replacement = replaced.getAnimations()[0];
+ println(`has replacement: ${replacement !== undefined}`);
+ if (replacement) {
+ replacement.pause();
+ replacement.currentTime = 0;
+ println(`replacement starts from: ${getComputedStyle(replaced).backgroundColor}`);
+ }
+ });
+</script>