Skip to content

Truly independent web browser

LibWeb: Resolve custom properties reading an animated one against it

Andreas Kling24ba19374431
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 544c517a619..69bc782e871 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
@@ -480,6 +480,26 @@ pub(super) fn value_reads_any_of_custom_properties(value: &StyleValueData, names
.any(|reference| names.binary_search_by(|name| name.as_ref().cmp(reference)).is_ok())
}
+/// The custom properties a written value reads by name, or `None` when it may read others: an
+/// `if()` condition, a custom function or an attribute's value can read any.
+fn named_custom_property_reads(value: &StyleValueData) -> Option<Vec<&[u16]>> {
+ let value = substituted_tokens(value);
+ if let StyleValueData::Unresolved {
+ presence_attr,
+ presence_dashed_function,
+ presence_if,
+ ..
+ } = value
+ && (*presence_attr || *presence_dashed_function || *presence_if)
+ {
+ return None;
+ }
+ let Some((references, all_references_visible)) = custom_property_references(value) else {
+ return Some(Vec::new());
+ };
+ all_references_visible.then_some(references)
+}
+
pub(super) fn sorted_custom_property_names(mut names: Vec<Arc<[u16]>>) -> CustomPropertyNames {
names.sort_unstable();
names.dedup();
@@ -1947,6 +1967,169 @@ impl RetainedState {
reads
}
+ /// What the custom properties an element declares resolve to once its animations sampled
+ /// `animated` into its custom properties. An animated value wins the cascade over a declared
+ /// one, so a declaration reading an animated name, or reading a declaration that does, resolves
+ /// against the animated values. Answers each such declaration whose value moved from the one
+ /// it holds in `base`, the element's environment beneath its animations. `inheritance` is the
+ /// environment the element inherits, and `finalization` computes registered values against the
+ /// element's lengths.
+ pub(crate) fn resolve_custom_properties_reading_animated_values(
+ &mut self,
+ node: StyleNodeID,
+ pseudo: Option<u8>,
+ base: *const c_void,
+ inheritance: *const c_void,
+ animated: &[(
+ crate::css::retained_fly_string::RetainedUtf16FlyString,
+ RetainedStyleValueData,
+ )],
+ finalization: CustomPropertyFinalization<'_>,
+ ) -> Vec<(
+ crate::css::retained_fly_string::RetainedUtf16FlyString,
+ RetainedStyleValueData,
+ )> {
+ if animated.is_empty() || !self.any_custom_property_is_declared() {
+ return Vec::new();
+ }
+ let Some(cascaded) = self.cascaded_custom_declarations_of(node, pseudo) else {
+ return Vec::new();
+ };
+ let mut moved: Vec<Arc<[u16]>> = animated
+ .iter()
+ .map(|(name, _)| Arc::from(name.to_utf16().as_ref()))
+ .collect();
+ let mut candidates: Vec<_> = cascaded
+ .iter()
+ .filter(|(declared, _)| declared.operator == CascadeOperator::Declared)
+ .filter_map(|(declared, value)| {
+ let name = self.declared_custom_property_name(declared.name)?;
+ Some((name, declared, value, named_custom_property_reads(value.data())))
+ })
+ .filter(|(name, _, _, _)| !moved.contains(&name.text))
+ .collect();
+ // What a declaration reads moves where any name it reads moved, and a read the scan
+ // cannot name may read any.
+ let mut readers = Vec::new();
+ loop {
+ let reads_moved = |reads: &Option<Vec<&[u16]>>| {
+ reads
+ .as_ref()
+ .is_none_or(|reads| reads.iter().any(|read| moved.iter().any(|name| name[..] == **read)))
+ };
+ let Some(index) = candidates.iter().position(|(_, _, _, reads)| reads_moved(reads)) else {
+ break;
+ };
+ let (name, declared, value, _) = candidates.swap_remove(index);
+ moved.push(name.text.clone());
+ readers.push((name.clone(), declared, value));
+ }
+ if readers.is_empty() {
+ return Vec::new();
+ }
+
+ let functions = if readers
+ .iter()
+ .any(|(_, _, value)| value_calls_custom_functions(value.data()))
+ {
+ // A container condition the engine cannot decide leaves the declarations as they resolved.
+ let Some(functions) = self.prepare_custom_functions(node, pseudo) else {
+ return Vec::new();
+ };
+ Some(functions)
+ } else {
+ None
+ };
+ let reads_attributes = functions.as_ref().is_some_and(|functions| functions.reads_attributes)
+ || readers.iter().any(|(_, _, value)| value_reads_attributes(value.data()));
+ let attribute_element = self.substitution_attribute_element(node, pseudo);
+ let attributes = reads_attributes
+ .then(|| SubstitutionAttributes::of(&self.facts, attribute_element, self.html_element_namespace));
+ let reads_conditions =
+ functions.is_some() || readers.iter().any(|(_, _, value)| value_reads_conditions(value.data()));
+ let style_query = reads_conditions
+ .then(|| self.style_query_inputs(computed::ComputedStyleTarget::new(node, pseudo.unwrap_or(u8::MAX))))
+ .flatten();
+
+ // The readers resolve as declared, over the element's environment, beside the animated values.
+ let values = readers
+ .iter()
+ .map(|(name, declared, value)| {
+ (
+ name.raw.raw(),
+ name.text.clone(),
+ declared.important,
+ value.pointer().cast(),
+ )
+ })
+ .chain(animated.iter().map(|(name, value)| {
+ (
+ name.raw(),
+ Arc::from(name.to_utf16().as_ref()),
+ false,
+ value.pointer().cast(),
+ )
+ }))
+ .collect();
+ // SAFETY: The base store is live for the sample, and the values are live for the call.
+ let cascaded_store = unsafe { Arc::from_raw(CustomPropertyStore::cascaded_child(base, values).cast()) };
+ let registry = self.document_style_computation_inputs.custom_property_registry();
+ let mut random_function_index = 0_usize;
+ let mut parse_context = registry.parse_context(&mut random_function_index);
+ parse_context.in_quirks_mode = self.document_style_computation_inputs.in_quirks_mode;
+ let media_environment = self.document_media.environment();
+ // The `style()` queries the declarations read were noted as they resolved beneath the animations.
+ let mut style_query_references = None;
+ // SAFETY: The store the element inherits from is live for the sample.
+ let inheritance_store = unsafe { inheritance.cast::<CustomPropertyStore>().as_ref() };
+ let mut resolution_context = engine_resolution_context(
+ &parse_context,
+ Some(&cascaded_store),
+ inheritance_store,
+ registry,
+ attributes.as_ref(),
+ &media_environment,
+ style_query.as_ref(),
+ Some(&mut style_query_references),
+ functions.as_ref(),
+ );
+ // A registered reader's random() takes the base value drawn for the element as its
+ // declarations resolved, not one the keyframes share.
+ let random_base_values = std::cell::RefCell::new(&mut self.random_base_values);
+ let draw_random_base_value = |name: &[u16], element_shared: bool| {
+ random_base_values.borrow_mut().ensure(Some(node), name, element_shared)
+ };
+ let finalization = CustomPropertyFinalization {
+ random_base_values: &[],
+ draw_random_base_value: Some(&draw_random_base_value),
+ ..finalization
+ };
+ // SAFETY: As above.
+ let (resolved, _) = unsafe {
+ resolve_declared_custom_properties(&cascaded_store, base, true, &mut resolution_context, finalization)
+ };
+ if resolved.is_null() {
+ return Vec::new();
+ }
+ // SAFETY: The resolution hands over one strong reference to the store.
+ let resolved = unsafe { Arc::from_raw(resolved.cast::<CustomPropertyStore>()) };
+ readers
+ .iter()
+ .filter_map(|(name, _, _)| {
+ let name_raw = name.raw.raw();
+ if !resolved.declared_names.contains(&name_raw) {
+ return None;
+ }
+ let value = resolved.own_values.get(&name_raw)?.value.clone();
+ // SAFETY: The name is live while the engine notes it.
+ Some((
+ unsafe { crate::css::retained_fly_string::RetainedUtf16FlyString::from_borrowed_raw(name_raw) },
+ value,
+ ))
+ })
+ .collect()
+ }
+
/// What a keyframe's written value substitutes to on the element being sampled, against the
/// custom-property store the element holds and the one it inherits from, which the host hands
/// over: what the host's `resolve_unresolved_style_value` makes of it.
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 8ec16fd04a6..ae403fc3328 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -6168,10 +6168,23 @@ fn finish_animation_sample(
});
result.outcome = FfiHostAnimationSampleOutcome::Evaluated;
if !custom_results.is_empty() {
- let retained = custom_results
+ let mut retained = custom_results
.into_iter()
.map(|(custom_name_id, value)| (custom_properties.name(custom_name_id).clone(), value))
.collect::<Vec<_>>();
+ // The element's own custom properties that read an animated one move with it, whether or
+ // not they resolved to what it inherits.
+ let pseudo =
+ (input.pseudo_kind != crate::css::cascaded_properties::NO_PSEUDO_ELEMENT).then_some(input.pseudo_kind);
+ let readers = engine.resolve_custom_properties_reading_animated_values(
+ node,
+ pseudo,
+ input.stores.base,
+ input.stores.inheritance,
+ &retained,
+ finalization,
+ );
+ retained.extend(readers);
let rows = retained
.iter()
.map(|(name, value)| FfiAnimatedCustomPropertyResult {
diff --git a/Tests/LibWeb/Text/expected/css/animated-custom-property-read-by-own-custom-properties.txt b/Tests/LibWeb/Text/expected/css/animated-custom-property-read-by-own-custom-properties.txt
index c8f05ab8aa3..1a8ce563a2d 100644
--- a/Tests/LibWeb/Text/expected/css/animated-custom-property-read-by-own-custom-properties.txt
+++ b/Tests/LibWeb/Text/expected/css/animated-custom-property-read-by-own-custom-properties.txt
@@ -1,23 +1,23 @@
an eighth of a turn:
- --gradient: linear-gradient(0deg, red, blue)
- --rotations: rotate(0deg) rotate(0deg)
- --double-turn: 0deg
+ --gradient: linear-gradient(45deg, red, blue)
+ --rotations: rotate(45deg) rotate(45deg)
+ --double-turn: 90deg
--unrelated: 1px
- background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
+ background-image: linear-gradient(45deg, rgb(255, 0, 0), rgb(0, 0, 255))
::before rotate: 0deg
::before background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
- child background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
- child --rotations: rotate(0deg) rotate(0deg)
+ child background-image: linear-gradient(45deg, rgb(255, 0, 0), rgb(0, 0, 255))
+ child --rotations: rotate(45deg) rotate(45deg)
three eighths of a turn:
- --gradient: linear-gradient(0deg, red, blue)
- --rotations: rotate(0deg) rotate(0deg)
- --double-turn: 0deg
+ --gradient: linear-gradient(135deg, red, blue)
+ --rotations: rotate(135deg) rotate(135deg)
+ --double-turn: 270deg
--unrelated: 1px
- background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
+ background-image: linear-gradient(135deg, rgb(255, 0, 0), rgb(0, 0, 255))
::before rotate: 0deg
::before background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
- child background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
- child --rotations: rotate(0deg) rotate(0deg)
+ child background-image: linear-gradient(135deg, rgb(255, 0, 0), rgb(0, 0, 255))
+ child --rotations: rotate(135deg) rotate(135deg)
cancelled:
--gradient: linear-gradient(0deg, red, blue)
--rotations: rotate(0deg) rotate(0deg)
@@ -28,3 +28,7 @@ cancelled:
::before background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
child background-image: linear-gradient(0deg, rgb(255, 0, 0), rgb(0, 0, 255))
child --rotations: rotate(0deg) rotate(0deg)
+if() and custom function at 125ms: rgb(0, 0, 255), 90deg
+if() and custom function at 375ms: rgb(255, 0, 0), 270deg
+declaration its parent shares at 125ms: rotate(45deg), matrix(0.707107, 0.707107, -0.707107, 0.707107, 0, 0)
+declaration its parent shares at 375ms: rotate(135deg), matrix(-0.707107, 0.707107, -0.707107, -0.707107, 0, 0)
diff --git a/Tests/LibWeb/Text/expected/css/animated-custom-property-read-by-registered-random-value.txt b/Tests/LibWeb/Text/expected/css/animated-custom-property-read-by-registered-random-value.txt
new file mode 100644
index 00000000000..02a020f8e1a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/animated-custom-property-read-by-registered-random-value.txt
@@ -0,0 +1,3 @@
+random value in range: true
+at 250ms: 100.00px over the random value
+at 500ms: 200.00px over the random value
diff --git a/Tests/LibWeb/Text/input/css/animated-custom-property-read-by-own-custom-properties.html b/Tests/LibWeb/Text/input/css/animated-custom-property-read-by-own-custom-properties.html
index 23b250b4e55..864c375c0bf 100644
--- a/Tests/LibWeb/Text/input/css/animated-custom-property-read-by-own-custom-properties.html
+++ b/Tests/LibWeb/Text/input/css/animated-custom-property-read-by-own-custom-properties.html
@@ -32,8 +32,26 @@
#child {
background-image: var(--gradient);
}
+ @function --double() returns <angle> {
+ result: calc(var(--turn) * 2);
+ }
+ #conditional {
+ animation: turn 1s linear infinite paused;
+ --color: if(style(--turn >= 90deg): red; else: blue);
+ --doubled: --double();
+ color: var(--color);
+ }
+ #sharing-parent, #sharing {
+ --shared-rotation: rotate(var(--turn));
+ }
+ #sharing {
+ animation: turn 1s linear infinite paused;
+ transform: var(--shared-rotation);
+ }
</style>
<div id="target"><div id="child"></div></div>
+<div id="conditional"></div>
+<div id="sharing-parent"><div id="sharing"></div></div>
<script>
test(() => {
const target = document.getElementById("target");
@@ -60,5 +78,21 @@
print("three eighths of a turn");
animation.cancel();
print("cancelled");
+
+ // Names an if() condition or a custom function reads are not written as var().
+ const conditional = document.getElementById("conditional");
+ for (const currentTime of [125, 375]) {
+ conditional.getAnimations()[0].currentTime = currentTime;
+ const style = getComputedStyle(conditional);
+ println(`if() and custom function at ${currentTime}ms: ${style.color}, ${style.getPropertyValue("--doubled")}`);
+ }
+
+ // An element whose declarations resolve to what it inherits shares its parent's environment.
+ const sharing = document.getElementById("sharing");
+ for (const currentTime of [125, 375]) {
+ sharing.getAnimations()[0].currentTime = currentTime;
+ const style = getComputedStyle(sharing);
+ println(`declaration its parent shares at ${currentTime}ms: ${style.getPropertyValue("--shared-rotation")}, ${style.transform}`);
+ }
});
</script>
diff --git a/Tests/LibWeb/Text/input/css/animated-custom-property-read-by-registered-random-value.html b/Tests/LibWeb/Text/input/css/animated-custom-property-read-by-registered-random-value.html
new file mode 100644
index 00000000000..b2025e0c2a1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/animated-custom-property-read-by-registered-random-value.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @property --length {
+ syntax: "<length>";
+ inherits: true;
+ initial-value: 0px;
+ }
+ @property --size {
+ syntax: "<length>";
+ inherits: true;
+ initial-value: 0px;
+ }
+ @keyframes grow {
+ from { --length: 0px; }
+ to { --length: 400px; }
+ }
+ #target {
+ --size: calc(random(10px, 20px) + var(--length));
+ }
+ #target.animated {
+ animation: grow 1s linear infinite paused;
+ }
+</style>
+<div id="target"></div>
+<script>
+ test(() => {
+ const target = document.getElementById("target");
+ const size = () => parseFloat(getComputedStyle(target).getPropertyValue("--size"));
+ const random = size();
+ println(`random value in range: ${random >= 10 && random <= 20}`);
+
+ // The animation moves the value its random() adds to, not the random value.
+ target.className = "animated";
+ for (const currentTime of [250, 500]) {
+ target.getAnimations()[0].currentTime = currentTime;
+ println(`at ${currentTime}ms: ${(size() - random).toFixed(2)}px over the random value`);
+ }
+ });
+</script>