Skip to content

Truly independent web browser

LibWeb: Iterate a snapshot of a timeline's animations when updating

Shannon Boothcb62d10a1526
diff --git a/Libraries/LibWeb/Animations/AnimationTimeline.cpp b/Libraries/LibWeb/Animations/AnimationTimeline.cpp
index 8863baac4db..1f07959c631 100644
--- a/Libraries/LibWeb/Animations/AnimationTimeline.cpp
+++ b/Libraries/LibWeb/Animations/AnimationTimeline.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibGC/RootVector.h>
#include <LibWeb/Animations/Animation.h>
#include <LibWeb/Animations/AnimationTimeline.h>
#include <LibWeb/Animations/KeyframeEffect.h>
@@ -109,8 +110,15 @@ void AnimationTimeline::update_associated_animations()
// - Queueing animation events for any such animations.
// NB: Since we dispatch events for all animations regardless of whether they have a timeline we handle them all together in Document::update_animations_and_send_events()
++associated_document()->style_invalidation_counters().animation_timeline_associated_animation_updates;
+ // NB: Updating an animation can run script that changes which animations are associated with this timeline. Skip
+ // the ones it disassociates.
+ GC::RootVector<GC::Ref<Animation>> animations;
for (auto& animation : m_associated_animations)
- animation.update();
+ animations.append(animation);
+ for (auto& animation : animations) {
+ if (m_associated_animations.contains(*animation))
+ animation->update();
+ }
}
Optional<TimeValue> AnimationTimeline::effective_current_time() const
diff --git a/Tests/LibWeb/Text/expected/WebAnimations/animation-ready-then-getter-changes-timeline-animations.txt b/Tests/LibWeb/Text/expected/WebAnimations/animation-ready-then-getter-changes-timeline-animations.txt
new file mode 100644
index 00000000000..d360dde1b5e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebAnimations/animation-ready-then-getter-changes-timeline-animations.txt
@@ -0,0 +1 @@
+then getter ran: true
diff --git a/Tests/LibWeb/Text/input/WebAnimations/animation-ready-then-getter-changes-timeline-animations.html b/Tests/LibWeb/Text/input/WebAnimations/animation-ready-then-getter-changes-timeline-animations.html
new file mode 100644
index 00000000000..8e492bcea59
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebAnimations/animation-ready-then-getter-changes-timeline-animations.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="target"></div>
+<script>
+ promiseTest(async () => {
+ const target = document.getElementById("target");
+ const timeline = document.timeline;
+ const animations = [];
+
+ let thenGetterRan = false;
+ function thenGetter() {
+ if (thenGetterRan)
+ return;
+ thenGetterRan = true;
+ for (const animation of animations) {
+ animation.timeline = null;
+ animation.timeline = timeline;
+ }
+ for (let i = 0; i < 256; ++i)
+ new Animation(null, timeline);
+ }
+
+ for (let i = 0; i < 16; ++i) {
+ const effect = new KeyframeEffect(target, [{ opacity: 0 }, { opacity: 1 }], { duration: 100000 });
+ const animation = new Animation(effect, timeline);
+ Object.defineProperty(animation, "then", { get: thenGetter });
+ animations.push(animation);
+ animation.play();
+ }
+
+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ println(`then getter ran: ${thenGetterRan}`);
+ });
+</script>