Ver Fonte

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.
Matthew Olsson há 1 ano atrás
pai
commit
15a8baee03

+ 1 - 0
Tests/LibWeb/Text/expected/css/sending-animationcancel-event-crash.txt

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

+ 27 - 0
Tests/LibWeb/Text/input/css/sending-animationcancel-event-crash.html

@@ -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>

+ 4 - 0
Userland/Libraries/LibWeb/Animations/Animation.cpp

@@ -470,6 +470,10 @@ void Animation::cancel(ShouldInvalidate should_invalidate)
     // 3. Make animation’s start time unresolved.
     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)
         invalidate_effect();
 }

+ 3 - 0
Userland/Libraries/LibWeb/Animations/Animation.h

@@ -108,6 +108,8 @@ public:
 
     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:
     Animation(JS::Realm&);
 
@@ -194,6 +196,7 @@ private:
 
     Optional<double> m_saved_play_time;
     Optional<double> m_saved_pause_time;
+    Optional<double> m_saved_cancel_time;
 };
 
 }

+ 3 - 2
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -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) {
-            // FIXME: Use the active time "at the moment it was cancelled"
-            dispatch_event(HTML::EventNames::animationcancel, effect->active_time_using_fill(Bindings::FillMode::Both).value());
+            // FIXME: Calculate a non-zero time when the animation is cancelled by means other than calling cancel()
+            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);