Truly independent web browser
LibWeb: Use one keyframe where several overlap at an end of an effect
Jelle Raaijmakers28245e21606d
diff --git a/Libraries/LibWeb/Rust/src/css/animation.rs b/Libraries/LibWeb/Rust/src/css/animation.rsindex 51621fbaf16..555b3bfff02 100644--- a/Libraries/LibWeb/Rust/src/css/animation.rs+++ b/Libraries/LibWeb/Rust/src/css/animation.rs@@ -6627,25 +6627,48 @@ fn evaluate_animation_value(// https://drafts.csswg.org/web-animations-1/#the-effect-value-of-a-keyframe-animation-effect// 10. Let interval endpoints be an empty sequence of keyframes.// 11. Populate interval endpoints by following the steps from the first matching condition from below:- // Otherwise,- // 1. Append to interval endpoints the last keyframe in property-specific keyframes whose computed keyframe offset is less than or equal- // to iteration progress and less than 1. If there is no such keyframe (because, for example, the iteration progress is negative),- // add the last keyframe whose computed keyframe offset is 0.- // 2. Append to interval endpoints the next keyframe in property-specific keyframes after the one added in the previous step.+ // NB: The first and the last property-specific keyframes are at the computed keyframe offsets 0 and 1.+ let last_index = keyframes.len() - 1;let mut start_index = 0;let mut end_index = 1;- for next_index in 2..keyframes.len() {- if input.current_key < plan(end_index).key as f64 {- break;+ if input.current_key < plan(0).key as f64 && plan(1).key == plan(0).key {+ // If iteration progress < 0 and there is more than one keyframe in property-specific keyframes with a computed+ // keyframe offset of 0,+ // Add the first keyframe in property-specific keyframes to interval endpoints.+ end_index = start_index;+ } else if input.current_key >= plan(last_index).key as f64 && plan(last_index - 1).key == plan(last_index).key {+ // If iteration progress ≥ 1 and there is more than one keyframe in property-specific keyframes with a computed+ // keyframe offset of 1,+ // Add the last keyframe in property-specific keyframes to interval endpoints.+ start_index = last_index;+ end_index = last_index;+ } else {+ // Otherwise,+ // 1. Append to interval endpoints the last keyframe in property-specific keyframes whose computed keyframe offset is less than or equal+ // to iteration progress and less than 1. If there is no such keyframe (because, for example, the iteration progress is negative),+ // add the last keyframe whose computed keyframe offset is 0.+ // 2. Append to interval endpoints the next keyframe in property-specific keyframes after the one added in the previous step.+ for next_index in 2..keyframes.len() {+ if input.current_key < plan(end_index).key as f64 {+ break;+ }+ start_index = end_index;+ end_index = next_index;}- start_index = end_index;- end_index = next_index;}let start_keyframe = &keyframes[start_index];let end_keyframe = &keyframes[end_index];let (start_plan, end_plan) = (plan(start_index), plan(end_index));- let interval_progress = (input.current_key - start_plan.key as f64) / (end_plan.key - start_plan.key) as f64;- let progress = start_plan.easing.evaluate_at(interval_progress, false) as f32;++ // 13. If there is only one keyframe in interval endpoints return the property value of target property on that+ // keyframe.+ // NB: Interpolating from that keyframe to itself returns its value, as step 12 composites it.+ let progress = if start_index == end_index {+ 0.0+ } else {+ let interval_progress = (input.current_key - start_plan.key as f64) / (end_plan.key - start_plan.key) as f64;+ start_plan.easing.evaluate_at(interval_progress, false) as f32+ };let Some(end) = end_keyframe.value.optional_data() else {if start_keyframe.value.optional_data().is_none() {return AnimatedProperty {@@ -7686,6 +7709,48 @@ mod tests {assert!(matches!(&*value, StyleValueData::Number { value } if *value == 9.0));}+ #[test]+ fn evaluates_overlapping_keyframes_at_the_ends() {+ let underlying = StyleValueData::Number { value: 0.0 };+ let plans = [0, 0, 100, 100].map(|key| AnimationKeyframePlan {+ key,+ easing: Easing::default(),+ composite: FfiCompositeOperation::Replace,+ });+ let keyframes = [1.0, 2.0, 3.0, 4.0]+ .into_iter()+ .enumerate()+ .map(|(keyframe, value)| AnimationKeyframeValue {+ keyframe,+ value: RetainedStyleValueData::from_arc(Arc::new(StyleValueData::Number { value })),+ })+ .collect::<Vec<_>>();+ let evaluate = |current_key| {+ let input = AnimationValueInput {+ property_id: crate::css::property_metadata::property_id::FLEX_GROW,+ custom_name_id: 0,+ result_of_transition: false,+ underlying: Some(&underlying),+ initial: Some(&underlying),+ current_key,+ keyframes: &keyframes,+ keyframe_plans: &plans,+ };+ let result = evaluate_animation_value(&animation_context(true), &input, None);+ assert!(result.apply);+ match &*result.value.unwrap().into_arc() {+ StyleValueData::Number { value } => *value,+ _ => panic!("expected a number"),+ }+ };++ assert_eq!(evaluate(-10.0), 1.0);+ assert_eq!(evaluate(0.0), 2.0);+ assert_eq!(evaluate(50.0), 2.5);+ assert_eq!(evaluate(100.0), 4.0);+ assert_eq!(evaluate(110.0), 4.0);+ }+#[test]fn evaluates_missing_keyframe_values() {let underlying = StyleValueData::Number { value: 2.0 };diff --git a/Tests/LibWeb/Text/expected/WebAnimations/misc/overlapping-keyframes-at-the-ends.txt b/Tests/LibWeb/Text/expected/WebAnimations/misc/overlapping-keyframes-at-the-ends.txtnew file mode 100644index 00000000000..b4b569d0d83--- /dev/null+++ b/Tests/LibWeb/Text/expected/WebAnimations/misc/overlapping-keyframes-at-the-ends.txt@@ -0,0 +1,3 @@+before: 0.2+halfway: 0.5+after: 0.9diff --git a/Tests/LibWeb/Text/input/WebAnimations/misc/overlapping-keyframes-at-the-ends.html b/Tests/LibWeb/Text/input/WebAnimations/misc/overlapping-keyframes-at-the-ends.htmlnew file mode 100644index 00000000000..d541f27b703--- /dev/null+++ b/Tests/LibWeb/Text/input/WebAnimations/misc/overlapping-keyframes-at-the-ends.html@@ -0,0 +1,21 @@+<!DOCTYPE html>+<div id="foo"></div>+<script src="../../include.js"></script>+<script>+ test(() => {+ const foo = document.getElementById("foo");+ const keyframes = [+ { offset: 0, opacity: 0.1 },+ { offset: 0, opacity: 0.2 },+ { offset: 1, opacity: 0.8 },+ { offset: 1, opacity: 0.9 },+ ];++ const animation = foo.animate(keyframes, { duration: 1000, fill: "both", delay: 1000 });+ println(`before: ${getComputedStyle(foo).opacity}`);+ animation.currentTime = 1500;+ println(`halfway: ${getComputedStyle(foo).opacity}`);+ animation.finish();+ println(`after: ${getComputedStyle(foo).opacity}`);+ });+</script>