LibWeb: Save time for animationcancel event before transitioning to idle

The if statement in the dispatch implies we are in the idle state, so of
course the active time will always be undefined. If this was cancelled
via a call to cancel(), we can save the time at that point. Otherwise,
just send 0.
This commit is contained in:
Matthew Olsson 2024-05-23 19:17:52 -07:00 committed by Andreas Kling
commit 15a8baee03
Notes: sideshowbarker 2024-07-17 10:16:43 +09:00
5 changed files with 38 additions and 2 deletions

View file

@ -0,0 +1 @@
PASS! (Didn't crash)

View file

@ -0,0 +1,27 @@
<!-- https://github.com/SerenityOS/serenity/issues/24424 -->
<style>
@keyframes anim {
to {
width: 200px;
}
}
div {
animation: anim 1s;
}
</style>
<div id="foo"></div>
<script src="../include.js"></script>
<script>
asyncTest(done => {
const foo = document.getElementById("foo");
const anim = foo.getAnimations()[0];
foo.addEventListener("animationcancel", () => {
println("PASS! (Didn't crash)");
done();
});
requestAnimationFrame(() => {
anim.cancel();
});
});
</script>

View file

@ -470,6 +470,10 @@ void Animation::cancel(ShouldInvalidate should_invalidate)
// 3. Make animations start time unresolved. // 3. Make animations start time unresolved.
m_start_time = {}; m_start_time = {};
// This time is needed for dispatching the animationcancel DOM event
if (auto effect = m_effect)
m_saved_cancel_time = effect->active_time_using_fill(Bindings::FillMode::Both);
if (should_invalidate == ShouldInvalidate::Yes) if (should_invalidate == ShouldInvalidate::Yes)
invalidate_effect(); invalidate_effect();
} }

View file

@ -108,6 +108,8 @@ public:
unsigned int global_animation_list_order() const { return m_global_animation_list_order; } unsigned int global_animation_list_order() const { return m_global_animation_list_order; }
auto release_saved_cancel_time() { return move(m_saved_cancel_time); }
protected: protected:
Animation(JS::Realm&); Animation(JS::Realm&);
@ -194,6 +196,7 @@ private:
Optional<double> m_saved_play_time; Optional<double> m_saved_play_time;
Optional<double> m_saved_pause_time; Optional<double> m_saved_pause_time;
Optional<double> m_saved_cancel_time;
}; };
} }

View file

@ -2103,8 +2103,9 @@ void Document::dispatch_events_for_animation_if_necessary(JS::NonnullGCPtr<Anima
} }
if (current_phase == Animations::AnimationEffect::Phase::Idle && previous_phase != Animations::AnimationEffect::Phase::Idle && previous_phase != Animations::AnimationEffect::Phase::After) { if (current_phase == Animations::AnimationEffect::Phase::Idle && previous_phase != Animations::AnimationEffect::Phase::Idle && previous_phase != Animations::AnimationEffect::Phase::After) {
// FIXME: Use the active time "at the moment it was cancelled" // FIXME: Calculate a non-zero time when the animation is cancelled by means other than calling cancel()
dispatch_event(HTML::EventNames::animationcancel, effect->active_time_using_fill(Bindings::FillMode::Both).value()); auto cancel_time = animation->release_saved_cancel_time().value_or(0.0);
dispatch_event(HTML::EventNames::animationcancel, cancel_time);
} }
} }
effect->set_previous_phase(current_phase); effect->set_previous_phase(current_phase);