Skip to content

Truly independent web browser

LibWeb: Check custom property cycles only in selected intervals

Callum Law65c96ad64f88
diff --git a/Libraries/LibWeb/Rust/src/css/animation.rs b/Libraries/LibWeb/Rust/src/css/animation.rs
index 81022a5fdc0..17cd99e62d2 100644
--- a/Libraries/LibWeb/Rust/src/css/animation.rs
+++ b/Libraries/LibWeb/Rust/src/css/animation.rs
@@ -8615,6 +8615,19 @@ impl KeyframeConversionCache {
}
}
+/// The endpoints selected for a custom property's current animation sample.
+pub(crate) struct CustomPropertyInterval {
+ interval_span: [CustomPropertyKeyframeLookup; 2],
+ pub(crate) name: crate::css::retained_fly_string::RetainedUtf16FlyString,
+ pub(crate) declared_values: [RetainedStyleValueData; 2],
+}
+
+impl CustomPropertyInterval {
+ fn declaration_indices(&self) -> [usize; 2] {
+ self.interval_span.map(|lookup| lookup.declaration_index)
+ }
+}
+
/// The custom properties the effects an element samples declare, each minted into the number the
/// animation core keys it by, with what the element's environment and the document's registry say
/// of it.
@@ -8622,6 +8635,7 @@ pub(crate) struct AnimatedCustomProperties {
stores: KeyframeStores,
names: Vec<crate::css::retained_fly_string::RetainedUtf16FlyString>,
units: Vec<Vec<u16>>,
+ pub(crate) intervals: Vec<CustomPropertyInterval>,
/// Whether any interval values are resolved afresh rather than retained across samples.
pub(crate) has_uncached_interval_values: bool,
}
@@ -8879,6 +8893,7 @@ pub(crate) fn resolve_published_animation_declarations(
stores,
names: Vec::new(),
units: Vec::new(),
+ intervals: Vec::new(),
has_uncached_interval_values: false,
};
let substitution_marks = std::cell::Cell::new(0_u8);
@@ -9057,11 +9072,21 @@ pub(crate) fn resolve_published_animation_declarations(
}
let name = custom_properties.units(name_id);
let interval_span = select_custom_property_interval_span(effect, &keyframe_lookups, selection.current_key);
+ let interval = CustomPropertyInterval {
+ interval_span,
+ name: custom_properties.name(name_id).clone(),
+ declared_values: interval_span.map(|lookup| {
+ match &effect.custom_declaration_at(lookup.declaration_index).value {
+ PublishedValue::Declared(value) => value.clone(),
+ _ => RetainedStyleValueData::none(),
+ }
+ }),
+ };
let (interval_values, values_are_retained) = effect.resolve_custom_property_interval_values(
- interval_span.map(|lookup| lookup.declaration_index),
+ interval.declaration_indices(),
registry.name_has_interpolable_syntax(name),
|| {
- interval_span.map(|lookup| {
+ interval.interval_span.map(|lookup| {
let declaration = effect.custom_declaration_at(lookup.declaration_index);
match &declaration.value {
PublishedValue::ElementValue => custom_properties.underlying_value(registry, name_id),
@@ -9078,7 +9103,7 @@ pub(crate) fn resolve_published_animation_declarations(
// SAFETY: The sample keeps the name alive while it is retained for the lookup.
let interned_name = unsafe { ak::Utf16FlyString::from_raw_borrowed(custom_properties.name(name_id).raw()) };
let custom_is_inherited = registry.name_inherits(&interned_name);
- for (lookup, value) in interval_span.into_iter().zip(interval_values) {
+ for (lookup, value) in interval.interval_span.into_iter().zip(interval_values) {
let declaration = effect.custom_declaration_at(lookup.declaration_index);
declarations.push(AnimationDeclaration {
keyframe_index: first_keyframe_index + lookup.keyframe_index,
@@ -9093,6 +9118,7 @@ pub(crate) fn resolve_published_animation_declarations(
});
}
custom_properties.has_uncached_interval_values |= !values_are_retained;
+ custom_properties.intervals.push(interval);
}
effects.push(FfiAnimationEffect {
first_keyframe_index,
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 7b25468426e..63c5bd221f4 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
@@ -824,9 +824,9 @@ impl RetainedState {
.then_some(previous)
}
- /// The custom properties in a dependency cycle through what an element's animations declare, as
+ /// The custom properties in a dependency cycle through the selected animation intervals, as
/// `--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 keyframes do. What a value
+ /// 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
@@ -841,25 +841,21 @@ impl RetainedState {
crate::css::retained_fly_string::RetainedUtf16FlyString,
RetainedStyleValueData,
)],
+ intervals: &[crate::css::animation::CustomPropertyInterval],
) -> Vec<crate::css::retained_fly_string::RetainedUtf16FlyString> {
type Name = crate::css::retained_fly_string::RetainedUtf16FlyString;
- let slot = pseudo.map_or(0, |kind| kind + 1);
let mut written: Vec<(Name, &StyleValueData)> = Vec::new();
- for effect in self.element_animation_effects(node, slot) {
- for keyframe in effect.keyframes() {
- for declaration in effect.custom_declarations_of(keyframe) {
- let crate::css::style::effect_descriptions::PublishedValue::Declared(value) = &declaration.value
- else {
- continue;
- };
- written.push((declaration.name.clone(), value.data()));
+ for interval in intervals {
+ for value in &interval.declared_values {
+ if let Some(value) = value.optional_data() {
+ written.push((interval.name.clone(), value));
}
}
}
if !written.iter().any(|(_, value)| substitutes(value)) {
return Vec::new();
}
- let written_names: HashSet<usize> = written.iter().map(|(name, _)| name.raw()).collect();
+ let written_names: HashSet<usize> = intervals.iter().map(|interval| interval.name.raw()).collect();
let declarations = self
.live_style
.as_ref()
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 781fa0a15d2..6e407036f6a 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -7322,6 +7322,7 @@ fn finish_animation_sample(
input.stores.base,
input.stores.inheritance,
&retained,
+ &custom_properties.intervals,
);
for name in names_in_a_cycle {
let value = registry.invalid_at_computed_value_time(&name, inherited);
diff --git a/Tests/LibWeb/Text/expected/css/unregistered-custom-property-animation-cycle-selected-interval.txt b/Tests/LibWeb/Text/expected/css/unregistered-custom-property-animation-cycle-selected-interval.txt
new file mode 100644
index 00000000000..9949385409e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/unregistered-custom-property-animation-cycle-selected-interval.txt
@@ -0,0 +1,4 @@
+an unselected cyclic interval does not invalidate the safe interval: true
+a selected cyclic interval is invalid: true
+a retained cyclic interval stays invalid after a style change: true
+returning to the safe interval restores its value: true
diff --git a/Tests/LibWeb/Text/input/css/unregistered-custom-property-animation-cycle-selected-interval.html b/Tests/LibWeb/Text/input/css/unregistered-custom-property-animation-cycle-selected-interval.html
new file mode 100644
index 00000000000..bff84a0ecf0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/unregistered-custom-property-animation-cycle-selected-interval.html
@@ -0,0 +1,22 @@
+<!doctype html>
+<div id="interval"></div>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const interval = document.getElementById("interval");
+ const animation = interval.animate(
+ [{ "--saved": "safe" }, { "--saved": "middle" }, { "--saved": "var(--saved)" }],
+ 1000
+ );
+ animation.pause();
+ const intervalValue = () => getComputedStyle(interval).getPropertyValue("--saved");
+ animation.currentTime = 0;
+ println(`an unselected cyclic interval does not invalidate the safe interval: ${intervalValue() === "safe"}`);
+ animation.currentTime = 900;
+ println(`a selected cyclic interval is invalid: ${intervalValue() === ""}`);
+ interval.style.color = "red";
+ println(`a retained cyclic interval stays invalid after a style change: ${intervalValue() === ""}`);
+ animation.currentTime = 0;
+ println(`returning to the safe interval restores its value: ${intervalValue() === "safe"}`);
+ });
+</script>