Truly independent web browser
LibWeb: Throttle infinite animations in box-less iframes
sideshowbarkerd3f1b572cb9a
diff --git a/Libraries/LibWeb/Animations/KeyframeEffect.cpp b/Libraries/LibWeb/Animations/KeyframeEffect.cppindex 28e83e204e8..deee57b7c2a 100644--- a/Libraries/LibWeb/Animations/KeyframeEffect.cpp+++ b/Libraries/LibWeb/Animations/KeyframeEffect.cpp@@ -25,6 +25,8 @@#include <LibWeb/DOM/Slot.h>#include <LibWeb/HTML/EventNames.h>#include <LibWeb/HTML/HTMLSlotElement.h>+#include <LibWeb/HTML/LocalNavigable.h>+#include <LibWeb/HTML/NavigableContainer.h>#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/HTML/Window.h>#include <LibWeb/Layout/Node.h>@@ -1080,6 +1082,21 @@ bool KeyframeEffect::can_skip_per_frame_style_update() constauto target = this->target();if (!target)return false;++ // An animation inside a box-less iframe can't affect its embedding document's rendering. Check the containers+ // before the target-local cache, since their layout can change without changing the target's style or layout box.+ // Reverse playback still needs ticks to finish when it reaches the start, even with an infinite iteration count.+ if (auto animation = associated_animation(); animation && animation->playback_rate() > 0 && isinf(iteration_count())) {+ for (auto navigable = target->document().navigable(); navigable;) {+ auto container = navigable->container();+ if (!container)+ break;+ if (container->document().layout_is_up_to_date() && !container->has_layout_box())+ return true;+ navigable = container->document().navigable();+ }+ }+Layout::ForcedReadScope read { target->document() };auto cache_result = [&](bool result) {if (target->document().layout_is_up_to_date()) {@@ -1107,8 +1124,8 @@ bool KeyframeEffect::can_skip_per_frame_style_update() const++target->document().style_invalidation_counters().animation_style_skip_cache_misses;}- // INTEROP: Other engines suppress main-thread style sampling for invisible transform animations. Restrict this- // optimization to the infinite transform-only case until animation overflow updates can also be throttled safely.+ // Restrict throttling within a rendered document to infinite transform-only effects until animation overflow+ // updates can also be throttled safely.if (!isinf(iteration_count()) || pseudo_element_type().has_value())return cache_result(false);diff --git a/Tests/LibWeb/Ref/expected/showing-iframe-with-svg-animation-ref.html b/Tests/LibWeb/Ref/expected/showing-iframe-with-svg-animation-ref.htmlnew file mode 100644index 00000000000..229577dc067--- /dev/null+++ b/Tests/LibWeb/Ref/expected/showing-iframe-with-svg-animation-ref.html@@ -0,0 +1,5 @@+<!DOCTYPE html>+<iframe style="width: 120px; height: 120px; border: 0" srcdoc='<!DOCTYPE html><body style="margin: 0">+ <svg width="100" height="100">+ <circle cx="50" cy="50" r="40" fill="none" stroke="green" stroke-width="10" stroke-dasharray="30 20" stroke-dashoffset="75" />+ </svg>'></iframe>diff --git a/Tests/LibWeb/Ref/input/showing-iframe-with-svg-animation.html b/Tests/LibWeb/Ref/input/showing-iframe-with-svg-animation.htmlnew file mode 100644index 00000000000..8dd2df47073--- /dev/null+++ b/Tests/LibWeb/Ref/input/showing-iframe-with-svg-animation.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<html class="reftest-wait">+<link rel="match" href="../expected/showing-iframe-with-svg-animation-ref.html">+<div id="container" style="display: none"></div>+<script>+window.onload = async () => {+ const frame = document.createElement("iframe");+ frame.style.cssText = "width: 120px; height: 120px; border: 0";+ frame.srcdoc = `<!DOCTYPE html><body style="margin: 0">+ <svg width="100" height="100">+ <circle cx="50" cy="50" r="40" fill="none" stroke="green" stroke-width="10" stroke-dasharray="30 20" />+ </svg>`;+ const loaded = new Promise(resolve => frame.onload = resolve);+ container.append(frame);+ await loaded;+ const child = frame.contentWindow;+ const circle = child.document.querySelector("circle");+ const timeline = child.internals.createInternalAnimationTimeline();+ const animation = new child.Animation(new child.KeyframeEffect(circle,+ { strokeDashoffset: ["0", "100"] }, { duration: 100, iterations: Infinity }), timeline);+ timeline.setTime(0);+ animation.play();+ animation.startTime = 0;+ const frames = async () => {+ for (let i = 0; i < 4; ++i)+ await new Promise(requestAnimationFrame);+ };+ await frames();+ timeline.setTime(75);+ await frames();+ container.style.display = "block";+ await frames();+ document.documentElement.classList.remove("reftest-wait");+};+</script>+</html>diff --git a/Tests/LibWeb/Text/expected/css/animation-in-display-none-iframe.txt b/Tests/LibWeb/Text/expected/css/animation-in-display-none-iframe.txtnew file mode 100644index 00000000000..7e7083e3252--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/animation-in-display-none-iframe.txt@@ -0,0 +1,11 @@+hidden iframe skips animation frame requests: true+hidden iframe skips animated style builds: true+hidden iframe style read samples animation: true+showing iframe resumes animation frame requests: true+showing iframe resumes animated style builds: true+hiding iframe stops animation frame requests: true+hidden iframe iteration listener uses wake-up timer: true+hidden iframe dispatches iteration event: true+hidden ancestor iframe stops nested animation frame requests: true+showing ancestor iframe resumes nested animation: true+reversed infinite animation can finish while hidden: truediff --git a/Tests/LibWeb/Text/input/css/animation-in-display-none-iframe.html b/Tests/LibWeb/Text/input/css/animation-in-display-none-iframe.htmlnew file mode 100644index 00000000000..cde0e3c04f8--- /dev/null+++ b/Tests/LibWeb/Text/input/css/animation-in-display-none-iframe.html@@ -0,0 +1,91 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<div id="container" style="display: none"></div>+<script>+asyncTest(async done => {+ const frame = document.createElement("iframe");+ frame.srcdoc = `<!DOCTYPE html>+ <style>+ @keyframes spin { from { stroke-dashoffset: 0; } to { stroke-dashoffset: 100; } }+ circle { animation: spin 100s linear infinite; }+ </style>+ <svg width="100" height="100">+ <circle cx="50" cy="50" r="40" fill="none" stroke="red" stroke-dasharray="10 20" />+ </svg>`;+ const loaded = new Promise(resolve => frame.onload = resolve);+ container.append(frame);+ await loaded;++ const child = frame.contentWindow;+ const circle = frame.contentDocument.querySelector("circle");+ child.getComputedStyle(circle).strokeDashoffset;+ const animation = circle.getAnimations()[0];+ const frames = async () => {+ for (let i = 0; i < 4; ++i)+ await new Promise(requestAnimationFrame);+ };+ await frames();+ child.internals.resetStyleInvalidationCounters();+ await frames();+ const hidden = child.internals.getStyleInvalidationCounters();+ println(`hidden iframe skips animation frame requests: ${hidden.animationFramePumpRequests === 0}`);+ println(`hidden iframe skips animated style builds: ${hidden.animatedStyleOverlayBuilds === 0}`);++ animation.currentTime = 50000;+ println(`hidden iframe style read samples animation: ${Math.abs(parseFloat(child.getComputedStyle(circle).strokeDashoffset) - 50) < 1}`);+ await frames();+ child.internals.resetStyleInvalidationCounters();+ container.style.display = "block";+ await new Promise(resolve => setTimeout(resolve, 100));+ const shown = child.internals.getStyleInvalidationCounters();+ println(`showing iframe resumes animation frame requests: ${shown.animationFramePumpRequests > 0}`);+ println(`showing iframe resumes animated style builds: ${shown.animatedStyleOverlayBuilds > 0}`);++ container.style.display = "none";+ await frames();+ child.internals.resetStyleInvalidationCounters();+ await frames();+ println(`hiding iframe stops animation frame requests: ${child.internals.getStyleInvalidationCounters().animationFramePumpRequests === 0}`);++ const listener = () => {};+ circle.addEventListener("animationiteration", listener);+ await frames();+ child.internals.resetStyleInvalidationCounters();+ await frames();+ const listening = child.internals.getStyleInvalidationCounters();+ println(`hidden iframe iteration listener uses wake-up timer: ${listening.animationFramePumpRequests === 0 && listening.compositorAnimationWakeupTimerActive}`);+ circle.removeEventListener("animationiteration", listener);+ const iteration = new Promise(resolve => circle.addEventListener("animationiteration", () => resolve(true), { once: true }));+ animation.effect.updateTiming({ duration: 100 });+ animation.currentTime = 0;+ println(`hidden iframe dispatches iteration event: ${await Promise.race([iteration, new Promise(resolve => setTimeout(() => resolve(false), 2000))])}`);+ animation.cancel();++ const nested = child.document.createElement("iframe");+ nested.srcdoc = frame.srcdoc;+ const nestedLoaded = new Promise(resolve => nested.onload = resolve);+ child.document.body.append(nested);+ await nestedLoaded;+ const nestedChild = nested.contentWindow;+ const nestedCircle = nestedChild.document.querySelector("circle");+ nestedChild.getComputedStyle(nestedCircle).strokeDashoffset;+ await frames();+ nestedChild.internals.resetStyleInvalidationCounters();+ await frames();+ println(`hidden ancestor iframe stops nested animation frame requests: ${nestedChild.internals.getStyleInvalidationCounters().animationFramePumpRequests === 0}`);+ container.style.display = "block";+ await new Promise(resolve => setTimeout(resolve, 100));+ println(`showing ancestor iframe resumes nested animation: ${nestedChild.internals.getStyleInvalidationCounters().animationFramePumpRequests > 0}`);+ nestedCircle.getAnimations()[0].cancel();+ container.style.display = "none";+ await frames();+ const reversed = circle.animate({ strokeDashoffset: ["0", "100"] }, { duration: 100000, iterations: Infinity });+ reversed.currentTime = 500;+ reversed.playbackRate = -1;+ const finished = reversed.finished.then(() => true);+ println(`reversed infinite animation can finish while hidden: ${await Promise.race([finished, new Promise(resolve => setTimeout(() => resolve(false), 2000))])}`);+ reversed.cancel();+ frame.remove();+ done();+});+</script>