Skip to content

Truly independent web browser

LibWeb: Make effect phase boundaries inclusive at a timeline's bounds

Andreas Kling3f0e81f35181
diff --git a/Libraries/LibWeb/Animations/AnimationEffect.cpp b/Libraries/LibWeb/Animations/AnimationEffect.cpp
index 70b39524bf2..5f4f3d3dac1 100644
--- a/Libraries/LibWeb/Animations/AnimationEffect.cpp
+++ b/Libraries/LibWeb/Animations/AnimationEffect.cpp
@@ -590,6 +590,26 @@ TimeValue AnimationEffect::after_active_boundary_time() const
return max(min(m_start_delay + active_duration(), end_time()), TimeValue::create_zero(associated_timeline()));
}
+// https://drafts.csswg.org/web-animations-2/#at-timeline-boundary
+bool AnimationEffect::is_at_timeline_boundary() const
+{
+ auto timeline = associated_timeline();
+ if (!timeline)
+ return false;
+ auto timeline_time = timeline->current_time();
+ if (!timeline_time.has_value())
+ return false;
+ auto matches = [&](Optional<TimeValue> const& time) {
+ return time.has_value() && fabs(timeline_time->value - time->value) < progress_timeline_tolerance;
+ };
+
+ // Determined based on the first matching condition:
+ // If the minimum timeline time matches the timeline current time: Return true
+ // If the maximum timeline time matches the timeline current time: Return true
+ // Otherwise: Return false
+ return matches(timeline->minimum_timeline_time()) || matches(timeline->maximum_timeline_time());
+}
+
AnimationEffect::Phase AnimationEffect::phase() const
{
return phase(local_time());
@@ -602,22 +622,25 @@ AnimationEffect::Phase AnimationEffect::phase(Optional<TimeValue> const& local_t
if (!local_time.has_value())
return Phase::Idle;
+ auto at_timeline_boundary = this->is_at_timeline_boundary();
+
auto before_active_boundary_time = this->before_active_boundary_time();
- // https://www.w3.org/TR/web-animations-1/#animation-effect-before-phase
+ // https://drafts.csswg.org/web-animations-2/#animation-effect-phases-and-states
// An animation effect is in the before phase if the animation effect’s local time is not unresolved and either of
// the following conditions are met:
- // - the local time is less than the before-active boundary time, or
- // - the animation direction is "backwards" and the local time is equal to the before-active boundary time.
- if (local_time.value() < before_active_boundary_time || (animation_direction() == AnimationDirection::Backwards && local_time.value() == before_active_boundary_time))
+ // 1. the local time is less than the before-active boundary time, or
+ // 2. the animation direction is "backwards" and the local time is equal to the before-active boundary time and
+ // not at timeline boundary.
+ if (local_time.value() < before_active_boundary_time || (animation_direction() == AnimationDirection::Backwards && local_time.value() == before_active_boundary_time && !at_timeline_boundary))
return Phase::Before;
auto after_active_boundary_time = this->after_active_boundary_time();
- // https://www.w3.org/TR/web-animations-1/#animation-effect-after-phase
// An animation effect is in the after phase if the animation effect’s local time is not unresolved and either of
// the following conditions are met:
- // - the local time is greater than the active-after boundary time, or
- // - the animation direction is "forwards" and the local time is equal to the active-after boundary time.
- if (local_time.value() > after_active_boundary_time || (animation_direction() == AnimationDirection::Forwards && local_time.value() == after_active_boundary_time))
+ // 1. the local time is greater than the active-after boundary time, or
+ // 2. the animation direction is "forwards" and the local time is equal to the active-after boundary time and not
+ // at timeline boundary.
+ if (local_time.value() > after_active_boundary_time || (animation_direction() == AnimationDirection::Forwards && local_time.value() == after_active_boundary_time && !at_timeline_boundary))
return Phase::After;
// https://www.w3.org/TR/web-animations-1/#animation-effect-active-phase
diff --git a/Libraries/LibWeb/Animations/AnimationEffect.h b/Libraries/LibWeb/Animations/AnimationEffect.h
index ababc477479..a33bcbe2e49 100644
--- a/Libraries/LibWeb/Animations/AnimationEffect.h
+++ b/Libraries/LibWeb/Animations/AnimationEffect.h
@@ -175,6 +175,7 @@ protected:
Optional<TimeValue> timeline_duration() const;
Phase phase(Optional<TimeValue> const& local_time) const;
+ bool is_at_timeline_boundary() const;
Optional<TimeValue> active_time_for_phase(Phase, Optional<TimeValue> const& local_time, Bindings::FillMode) const;
Optional<double> overall_progress_for_phase(Phase, Optional<TimeValue> const& active_time) const;
Optional<double> simple_iteration_progress(ResolvedTiming const&) const;
diff --git a/Libraries/LibWeb/Animations/AnimationTimeline.h b/Libraries/LibWeb/Animations/AnimationTimeline.h
index 23d0e7ad20f..c71e7586e3c 100644
--- a/Libraries/LibWeb/Animations/AnimationTimeline.h
+++ b/Libraries/LibWeb/Animations/AnimationTimeline.h
@@ -50,6 +50,13 @@ public:
bool is_monotonically_increasing() const { return m_is_monotonically_increasing; }
virtual bool is_progress_based() const { return false; }
+ // https://drafts.csswg.org/web-animations-2/#minimum-timeline-time
+ // NB: Empty for a monotonically increasing timeline, whose minimum timeline time is -infinity.
+ virtual Optional<TimeValue> minimum_timeline_time() const { return {}; }
+ // https://drafts.csswg.org/web-animations-2/#maximum-timeline-time
+ // NB: Empty for a monotonically increasing timeline, whose maximum timeline time is +infinity.
+ virtual Optional<TimeValue> maximum_timeline_time() const { return {}; }
+
// The timeline times of the start and the end of an attachment range on a progress-based timeline.
// https://drafts.csswg.org/scroll-animations-1/#animation-attachment-range
virtual AttachmentInterval interval_for_attachment_range(AnimationAttachmentRange const&) const
diff --git a/Libraries/LibWeb/Animations/ScrollTimeline.h b/Libraries/LibWeb/Animations/ScrollTimeline.h
index c6e159ef5e5..ffa2a437881 100644
--- a/Libraries/LibWeb/Animations/ScrollTimeline.h
+++ b/Libraries/LibWeb/Animations/ScrollTimeline.h
@@ -61,6 +61,10 @@ public:
virtual bool is_progress_based() const override { return true; }
virtual AttachmentInterval interval_for_attachment_range(AnimationAttachmentRange const&) const override;
+
+ // NB: The minimum and the maximum scroll offsets.
+ virtual Optional<TimeValue> minimum_timeline_time() const override { return TimeValue { TimeValue::Type::Percentage, 0 }; }
+ virtual Optional<TimeValue> maximum_timeline_time() const override { return TimeValue { TimeValue::Type::Percentage, 100 }; }
virtual bool can_convert_a_timeline_time_to_an_origin_relative_time() const override { return false; }
private:
diff --git a/Libraries/LibWeb/Animations/TimelineRange.h b/Libraries/LibWeb/Animations/TimelineRange.h
index 8013d99fff4..f5b9cde135d 100644
--- a/Libraries/LibWeb/Animations/TimelineRange.h
+++ b/Libraries/LibWeb/Animations/TimelineRange.h
@@ -44,6 +44,10 @@ struct AnimationAttachmentRange {
bool operator==(AnimationAttachmentRange const&) const = default;
};
+// How far apart two progress-based timeline times, in percent, may be and still be the same but for floating point error.
+// NB: Must match PROGRESS_TOLERANCE in the style engine.
+constexpr double progress_timeline_tolerance = 1e-6;
+
// The timeline times of the start and the end of an attachment range.
struct AttachmentInterval {
TimeValue start;
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index df647049641..2f44844032e 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -663,6 +663,11 @@ ComputedValuesFFI::FfiEffectTiming style_engine_effect_timing(Animations::Keyfra
timing.timeline_scroller_is_vertical = scroller.vertical;
timing.timeline_scroller = scroller.scroll_node.node_id.value();
}
+ if (auto minimum = timeline->minimum_timeline_time(), maximum = timeline->maximum_timeline_time(); minimum.has_value() && maximum.has_value()) {
+ timing.has_timeline_boundaries = true;
+ timing.minimum_timeline_time = minimum->value;
+ timing.maximum_timeline_time = maximum->value;
+ }
}
optional_duration(animation.start_time(), timing.has_start_time, timing.start_time);
optional_duration(animation.hold_time(), timing.has_hold_time, timing.hold_time);
diff --git a/Libraries/LibWeb/Rust/src/css/style/animations.rs b/Libraries/LibWeb/Rust/src/css/style/animations.rs
index c3dcb176f3b..2cf415ddd49 100644
--- a/Libraries/LibWeb/Rust/src/css/style/animations.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/animations.rs
@@ -399,6 +399,14 @@ fn smallest(a: f64, b: f64) -> f64 {
if b < a { b } else { a }
}
+/// Whether two progress-based timeline times, in percent, are the same but for floating point error.
+fn is_within_progress_tolerance(a: f64, b: f64) -> bool {
+ (a - b).abs() < PROGRESS_TOLERANCE
+}
+
+/// Must match `Animations::progress_timeline_tolerance` in the host.
+const PROGRESS_TOLERANCE: f64 = 1e-6;
+
/// The timing the host sampled each of an element's effects with, by the effect's identity.
pub(crate) type SampledEffectTimings = Box<[(u64, EffectTiming)]>;
@@ -466,18 +474,26 @@ impl EffectTiming {
false => unconstrained_time,
};
- // https://www.w3.org/TR/web-animations-1/#animation-effect-phases-and-states
+ // https://drafts.csswg.org/web-animations-2/#at-timeline-boundary
+ let at_timeline_boundary = timing.has_timeline_boundaries
+ && timeline_time.is_some_and(|time| {
+ is_within_progress_tolerance(time, timing.minimum_timeline_time)
+ || is_within_progress_tolerance(time, timing.maximum_timeline_time)
+ });
+
+ // https://drafts.csswg.org/web-animations-2/#animation-effect-phases-and-states
let backwards = timing.playback_rate < 0.0;
let phase = match local_time {
None => Phase::Idle,
Some(local_time) => {
let before_active_boundary_time = largest(smallest(timing.start_delay, end_time), 0.0);
let after_active_boundary_time = largest(smallest(timing.start_delay + active_duration, end_time), 0.0);
- if local_time < before_active_boundary_time || (backwards && local_time == before_active_boundary_time)
+ if local_time < before_active_boundary_time
+ || (backwards && local_time == before_active_boundary_time && !at_timeline_boundary)
{
Phase::Before
} else if local_time > after_active_boundary_time
- || (!backwards && local_time == after_active_boundary_time)
+ || (!backwards && local_time == after_active_boundary_time && !at_timeline_boundary)
{
Phase::After
} else {
@@ -773,6 +789,9 @@ mod tests {
has_timeline_scroller: true,
timeline_scroller_is_vertical: true,
timeline_scroller: 7,
+ has_timeline_boundaries: false,
+ minimum_timeline_time: 0.0,
+ maximum_timeline_time: 0.0,
iteration_duration: 100.0,
..timing_at(25.0).timing
},
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 99fd817e8cb..f70784e4415 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -5504,6 +5504,10 @@ pub struct FfiEffectTiming {
pub has_timeline_scroller: bool,
pub timeline_scroller_is_vertical: bool,
pub timeline_scroller: i64,
+ /// The effect's animation runs on a progress-based timeline, whose minimum and maximum timeline times these are.
+ pub has_timeline_boundaries: bool,
+ pub minimum_timeline_time: f64,
+ pub maximum_timeline_time: f64,
pub has_start_time: bool,
pub has_hold_time: bool,
/// The effect's animation is paused, which holds its time, as one whose play is pending does too.
diff --git a/Libraries/LibWeb/Rust/src/css/transition.rs b/Libraries/LibWeb/Rust/src/css/transition.rs
index 9a49444aee8..3c6f8f5bd80 100644
--- a/Libraries/LibWeb/Rust/src/css/transition.rs
+++ b/Libraries/LibWeb/Rust/src/css/transition.rs
@@ -1202,6 +1202,9 @@ fn started_transition_timing(
has_timeline_scroller: false,
timeline_scroller_is_vertical: false,
timeline_scroller: 0,
+ has_timeline_boundaries: false,
+ minimum_timeline_time: 0.0,
+ maximum_timeline_time: 0.0,
has_start_time: false,
has_hold_time: true,
paused: false,
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-animations/scroll-timelines/scroll-animation-effect-phases.tentative.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-animations/scroll-timelines/scroll-animation-effect-phases.tentative.txt
index 3222d1aabc8..ebb38830c37 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-animations/scroll-timelines/scroll-animation-effect-phases.tentative.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-animations/scroll-timelines/scroll-animation-effect-phases.tentative.txt
@@ -2,16 +2,15 @@ Harness status: OK
Found 30 tests
-26 Pass
-4 Fail
+30 Pass
Pass Current times and effect phase at start when delay = 0 and endDelay = 0 |
Pass Current times and effect phase in active range when delay = 0 and endDelay = 0 |
-Fail Current times and effect phase at effect end time when delay = 0 and endDelay = 0 |
+Pass Current times and effect phase at effect end time when delay = 0 and endDelay = 0 |
Pass Current times and effect phase at timeline start when delay = 500 and endDelay = 0 |
Pass Current times and effect phase before start delay when delay = 500 and endDelay = 0 |
Pass Current times and effect phase at start delay when delay = 500 and endDelay = 0 |
Pass Current times and effect phase in active range when delay = 500 and endDelay = 0 |
-Fail Current times and effect phase at effect end time when delay = 500 and endDelay = 0 |
+Pass Current times and effect phase at effect end time when delay = 500 and endDelay = 0 |
Pass Current times and effect phase at timeline start when delay = 0 and endDelay = 500 |
Pass Current times and effect phase in active range when delay = 0 and endDelay = 500 |
Pass Current times and effect phase at effect end time when delay = 0 and endDelay = 500 |
@@ -26,8 +25,8 @@ Pass Current times and effect phase after effect end time when delay = 250 and e
Pass Current times and effect phase at timeline boundary when delay = 250 and endDelay = 250 |
Pass Current times and effect phase at timeline start when delay = -125 and endDelay = -125 |
Pass Current times and effect phase in active range when delay = -125 and endDelay = -125 |
-Fail Current times and effect phase at timeline end when delay = -125 and endDelay = -125 |
-Fail Playback rate affects whether active phase boundary is inclusive.
+Pass Current times and effect phase at timeline end when delay = -125 and endDelay = -125 |
+Pass Playback rate affects whether active phase boundary is inclusive.
Pass Verify that (play -> pause -> play) doesn't change phase/progress.
Pass Pause in before phase, scroll timeline into active phase, animation should remain in the before phase
Pass Pause in before phase, set animation current time to be in active range, animation should become active. Scrolling should have no effect.