Truly independent web browser
LibWeb: Let an animation frame callback cancel a later one
Andreas Kling3aeea8670cae
diff --git a/Libraries/LibWeb/HTML/AnimationFrameCallbackDriver.cpp b/Libraries/LibWeb/HTML/AnimationFrameCallbackDriver.cppindex 44491b5e1f3..1f81b2f4948 100644--- a/Libraries/LibWeb/HTML/AnimationFrameCallbackDriver.cpp+++ b/Libraries/LibWeb/HTML/AnimationFrameCallbackDriver.cpp@@ -44,7 +44,7 @@ WebIDL::UnsignedLong AnimationFrameCallbackDriver::add_after_callbacks(Callbackbool AnimationFrameCallbackDriver::remove(WebIDL::UnsignedLong id){- return m_callbacks.remove(id) || m_after_callbacks.remove(id);+ return m_callbacks.remove(id) || m_executing_callbacks.remove(id) || m_after_callbacks.remove(id) || m_executing_after_callbacks.remove(id);}bool AnimationFrameCallbackDriver::has_callbacks() const@@ -59,15 +59,16 @@ void AnimationFrameCallbackDriver::run(double now)// NB: Work added while callbacks run waits for the next invocation of the algorithm.m_executing_after_callbacks = move(m_after_callbacks);- for (auto& [id, callback] : m_executing_callbacks)- callback->function()(now);+ // NB: A callback may cancel a callback that comes after it.+ while (!m_executing_callbacks.is_empty())+ m_executing_callbacks.take_first()->function()(now);}void AnimationFrameCallbackDriver::run_after_callbacks(double now){AK::ScopeGuard guard { [&]() { m_executing_after_callbacks.clear(); } };- for (auto& [id, callback] : m_executing_after_callbacks)- callback->function()(now);+ while (!m_executing_after_callbacks.is_empty())+ m_executing_after_callbacks.take_first()->function()(now);}}diff --git a/Tests/LibWeb/Text/expected/HTML/cancelAnimationFrame-from-earlier-callback.txt b/Tests/LibWeb/Text/expected/HTML/cancelAnimationFrame-from-earlier-callback.txtnew file mode 100644index 00000000000..470a85da7d8--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/cancelAnimationFrame-from-earlier-callback.txt@@ -0,0 +1,2 @@+first callback runs+last callback runsdiff --git a/Tests/LibWeb/Text/input/HTML/cancelAnimationFrame-from-earlier-callback.html b/Tests/LibWeb/Text/input/HTML/cancelAnimationFrame-from-earlier-callback.htmlnew file mode 100644index 00000000000..a67c091d3aa--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/cancelAnimationFrame-from-earlier-callback.html@@ -0,0 +1,16 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ let cancelled;+ requestAnimationFrame(() => {+ println("first callback runs");+ cancelAnimationFrame(cancelled);+ });+ cancelled = requestAnimationFrame(() => println("FAIL: cancelled callback runs"));+ requestAnimationFrame(() => {+ println("last callback runs");+ done();+ });+ });+</script>