Skip to content

Truly independent web browser

LibWeb: Preserve later effects after cyclic registered intervals

Callum Law280b3f6fd971
diff --git a/Libraries/LibWeb/Rust/src/css/animation.rs b/Libraries/LibWeb/Rust/src/css/animation.rs
index 91c3845f64c..180d422f417 100644
--- a/Libraries/LibWeb/Rust/src/css/animation.rs
+++ b/Libraries/LibWeb/Rust/src/css/animation.rs
@@ -8618,7 +8618,10 @@ impl KeyframeConversionCache {
/// The endpoints selected for a custom property's current animation sample.
pub(crate) struct CustomPropertyInterval {
interval_span: [CustomPropertyKeyframeLookup; 2],
+ /// Index of the effect in the sample's composite order.
+ pub(crate) effect_index: usize,
pub(crate) custom_name_id: u32,
+ pub(crate) cyclic: [bool; 2],
pub(crate) name: crate::css::retained_fly_string::RetainedUtf16FlyString,
pub(crate) declared_values: [RetainedStyleValueData; 2],
}
@@ -8881,7 +8884,7 @@ fn select_contributing_custom_property_intervals(
// NB: A replacing interval hides lower effects for its property. Neutral and additive
// endpoints keep the intervals whose underlying values they need.
let mut replaced_names = HashSet::new();
- for selection in selected.iter().rev() {
+ for (effect_index, selection) in selected.iter().enumerate().rev() {
let effect = selection.effect;
let mut intervals = Vec::new();
for (name_id, keyframe_lookups) in build_custom_property_keyframe_lookups(effect, custom_properties) {
@@ -8908,7 +8911,9 @@ fn select_contributing_custom_property_intervals(
}
intervals.push(CustomPropertyInterval {
interval_span,
+ effect_index,
custom_name_id: name_id,
+ cyclic: [false; 2],
name: custom_properties.name(name_id).clone(),
declared_values: interval_span.map(|lookup| {
match &effect.custom_declaration_at(lookup.declaration_index).value {
@@ -9233,7 +9238,51 @@ pub(crate) struct PreparedAnimationBatch {
context: FfiAnimationContext<'static>,
}
-// SAFETY: A prepared batch is built once and never mutated: sampling reads its plans and copies
+impl PreparedAnimationBatch {
+ /// Replace cyclic intervals' keyframe values and report whether recomposition is needed.
+ pub(crate) fn replace_cyclic_custom_property_endpoints(
+ &mut self,
+ intervals: &[CustomPropertyInterval],
+ registry: &Registry,
+ cyclic_declarations: &mut Vec<crate::css::retained_fly_string::RetainedUtf16FlyString>,
+ ) -> bool {
+ let mut has_cyclic_interval = false;
+ for interval in intervals {
+ if !interval.cyclic.contains(&true) {
+ continue;
+ }
+ // SAFETY: The interval retains the name while it is borrowed for the lookup.
+ let name = unsafe { ak::Utf16FlyString::from_raw_borrowed(interval.name.raw()) };
+ let has_neutral_endpoint = interval
+ .declared_values
+ .iter()
+ .any(|value| value.optional_data().is_none());
+ let registered = registry.is_registered(&name);
+ if !registered || has_neutral_endpoint {
+ cyclic_declarations.push(interval.name.clone());
+ continue;
+ }
+ for (plan, keyframes) in self.resolved.value_plans.iter().zip(&mut self.keyframes_by_value) {
+ if plan.custom_name_id != interval.custom_name_id || plan.effect_index != interval.effect_index {
+ continue;
+ }
+ // NB: Custom-property plans contain only the selected interval's two endpoints.
+ assert_eq!(keyframes.len(), interval.cyclic.len());
+ // AD-HOC: Compose a registered custom property's selected interval as unset if it has a cyclic endpoint
+ // and two explicit keyframe values, matching Chromium. Keep unset as a marker until all effects
+ // are composed so later additive effects do not add the inherited or initial value.
+ let fallback = RetainedStyleValueData::shared_keyword(crate::css::css_enums::keyword::UNSET);
+ for keyframe in keyframes {
+ keyframe.value = fallback.clone();
+ }
+ }
+ has_cyclic_interval = true;
+ }
+ has_cyclic_interval
+ }
+}
+
+// SAFETY: A prepared batch is immutable while shared: sampling reads its plans and copies
// its context. The raw pointers its resolved declarations hold name the base URLs and random
// sharing values those declarations retain for the batch's lifetime. An animated overlay caches
// one behind an `Arc`, and an installed overlay is reachable from the computed catalog that every
diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
index 7fe406570a3..9bb724dc354 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
@@ -824,14 +824,15 @@ impl RetainedState {
.then_some(previous)
}
- /// The custom properties in a dependency cycle through the selected animation intervals, as
+ /// Mark cyclic endpoints in `intervals` and return the names of cyclic element declarations.
+ /// Cycles can pass through the selected animation intervals, as with
/// `--p: var(--q)` in a keyframe beside `--q: var(--p)` on the element. An animated value wins
/// the cascade over a declared one, so a name it animates reads as its selected endpoints do. What a value
/// reads is what substituting it looks up, over `base`, the element's environment beneath its
/// animations, with their values `animated`, and `inheritance`, the environment it inherits:
/// which branch of an `if()` or fallback that takes depends on the values, and what decides that
/// is itself read, so that a cycle through it is found whichever way it went.
- pub(crate) fn custom_properties_in_an_animated_cycle(
+ pub(crate) fn check_custom_property_animation_cycles(
&self,
node: StyleNodeID,
pseudo: Option<u8>,
@@ -841,17 +842,18 @@ impl RetainedState {
crate::css::retained_fly_string::RetainedUtf16FlyString,
RetainedStyleValueData,
)],
- intervals: &[crate::css::animation::CustomPropertyInterval],
+ intervals: &mut [crate::css::animation::CustomPropertyInterval],
) -> Vec<crate::css::retained_fly_string::RetainedUtf16FlyString> {
type Name = crate::css::retained_fly_string::RetainedUtf16FlyString;
let mut written: Vec<(Name, &StyleValueData)> = Vec::new();
- for interval in intervals {
+ for interval in intervals.iter() {
for value in &interval.declared_values {
if let Some(value) = value.optional_data() {
written.push((interval.name.clone(), value));
}
}
}
+ let keyframe_declaration_count = written.len();
if !written.iter().any(|(_, value)| substitutes(value)) {
return Vec::new();
}
@@ -920,13 +922,9 @@ impl RetainedState {
);
let declaration_count = written.len();
let mut property_vertices: HashMap<usize, usize> = HashMap::default();
- let mut property_names = Vec::new();
for (name, _) in &written {
let property_vertex = declaration_count + property_vertices.len();
- property_vertices.entry(name.raw()).or_insert_with(|| {
- property_names.push(name);
- property_vertex
- });
+ property_vertices.entry(name.raw()).or_insert(property_vertex);
}
// NB: Declaration vertices come first, followed by one vertex per property.
// A property points to its declarations, which point to the properties they read.
@@ -954,10 +952,26 @@ impl RetainedState {
.filter_map(|name| property_vertices.get(&name.raw_identity()).copied()),
);
}
+ let declaration_names: Vec<_> = written.into_iter().map(|(name, _)| name).collect();
let on_a_cycle = nodes_on_a_cycle(&edges);
- property_names
- .into_iter()
- .zip(on_a_cycle.into_iter().skip(declaration_count))
+ let (keyframe_declaration_cycles, element_declaration_cycles) =
+ on_a_cycle[..declaration_count].split_at(keyframe_declaration_count);
+ let mut keyframe_declaration_cycles = keyframe_declaration_cycles.iter().copied();
+ // NB: Neutral endpoints have no declaration vertex, so they do not consume a cycle flag.
+ for interval in intervals {
+ interval.cyclic = interval.declared_values.each_ref().map(|value| {
+ if value.optional_data().is_some() {
+ keyframe_declaration_cycles
+ .next()
+ .expect("declared endpoints have cycle vertices")
+ } else {
+ false
+ }
+ });
+ }
+ declaration_names[keyframe_declaration_count..]
+ .iter()
+ .zip(element_declaration_cycles.iter().copied())
.filter(|(_, on_a_cycle)| *on_a_cycle)
.map(|(name, _)| name.clone())
.collect()
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 6e407036f6a..2048c45a1af 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -7200,7 +7200,7 @@ fn finish_animation_sample(
node,
composed,
resolved,
- custom_properties,
+ mut custom_properties,
mut result,
} = sample;
let table = input.longhands;
@@ -7280,7 +7280,7 @@ fn finish_animation_sample(
let has_non_custom_property_animation = resolved.properties.iter().any(|property| property.custom_name_id == 0);
- let custom_results = anim::evaluate_animations(anim::ComputedAnimationBatch {
+ let mut batch = anim::ComputedAnimationBatch {
context: input.animation_context(Some(&length_contexts.remaining), reference_box),
device_pixels_per_css_pixel: input.environment.device_pixels_per_css_pixel,
sampled_effects: &composed,
@@ -7295,7 +7295,9 @@ fn finish_animation_sample(
overlay: host.overlay_for_mutation(),
custom_underlying_values: &underlying_values,
custom_initial_values: &initial_values,
- });
+ };
+ let mut preparation = anim::prepare_animations(&mut batch);
+ let custom_results = anim::evaluate_prepared_animations(&preparation, &mut batch);
result.outcome = FfiHostAnimationSampleOutcome::Evaluated;
if !custom_results.is_empty() {
let mut retained = custom_results
@@ -7316,15 +7318,35 @@ fn finish_animation_sample(
.cast::<crate::css::custom_properties::CustomPropertyStore>()
.as_ref()
};
- let names_in_a_cycle = engine.custom_properties_in_an_animated_cycle(
+ let mut cyclic_declarations = engine.check_custom_property_animation_cycles(
node,
cycle_pseudo,
input.stores.base,
input.stores.inheritance,
&retained,
- &custom_properties.intervals,
+ &mut custom_properties.intervals,
);
- for name in names_in_a_cycle {
+ // NB: Preparations containing custom properties are not cached, so this sample owns its preparation.
+ let preparation =
+ std::sync::Arc::get_mut(&mut preparation).expect("custom-property preparations are not shared");
+ if preparation.replace_cyclic_custom_property_endpoints(
+ &custom_properties.intervals,
+ registry,
+ &mut cyclic_declarations,
+ ) {
+ retained = anim::evaluate_prepared_animations(preparation, &mut batch)
+ .into_iter()
+ .map(|(custom_name_id, value)| (custom_properties.name(custom_name_id).clone(), value))
+ .collect();
+ // NB: Resolve surviving unset markers to the inherited or initial value. See the AD-HOC comment
+ // in PreparedAnimationBatch::replace_cyclic_custom_property_endpoints for why this is deferred.
+ for (name, value) in &mut retained {
+ if matches!(value.data(), StyleValueData::Keyword { keyword } if *keyword == keyword::UNSET) {
+ *value = registry.invalid_at_computed_value_time(name, inherited);
+ }
+ }
+ }
+ for name in cyclic_declarations {
let value = registry.invalid_at_computed_value_time(&name, inherited);
match retained.iter_mut().find(|(animated, _)| *animated == name) {
Some((_, animated)) => *animated = value,
diff --git a/Tests/LibWeb/Text/expected/css/registered-custom-property-animation-cycle-composition.txt b/Tests/LibWeb/Text/expected/css/registered-custom-property-animation-cycle-composition.txt
new file mode 100644
index 00000000000..7729367658f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/registered-custom-property-animation-cycle-composition.txt
@@ -0,0 +1,3 @@
+replace preserves a later additive animation over a cyclic registered interval: true
+add preserves a later additive animation over a cyclic registered interval: true
+accumulate preserves a later additive animation over a cyclic registered interval: true
diff --git a/Tests/LibWeb/Text/input/css/registered-custom-property-animation-cycle-composition.html b/Tests/LibWeb/Text/input/css/registered-custom-property-animation-cycle-composition.html
new file mode 100644
index 00000000000..d1153da08e2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/registered-custom-property-animation-cycle-composition.html
@@ -0,0 +1,41 @@
+<!doctype html>
+<style>
+ @property --number {
+ syntax: "<number>";
+ inherits: true;
+ initial-value: 0;
+ }
+ @keyframes mixed {
+ from {
+ --number: var(--reader);
+ }
+ to {
+ --number: 10;
+ }
+ }
+ #target {
+ --number: 10;
+ --reader: var(--number);
+ animation: mixed 1s linear paused;
+ }
+</style>
+<div style="--number: 7">
+ <div id="target"></div>
+</div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const target = document.getElementById("target");
+ const animation = target.getAnimations()[0];
+ animation.currentTime = 900;
+ for (const composite of ["replace", "add", "accumulate"]) {
+ animation.effect.composite = composite;
+ const later = target.animate({ "--number": [2, 2] }, { duration: 1000, composite: "add" });
+ later.pause();
+ println(
+ `${composite} preserves a later additive animation over a cyclic registered interval: ${getComputedStyle(target).getPropertyValue("--number") === "2"}`
+ );
+ later.cancel();
+ }
+ });
+</script>