LibWeb: Narrow @keyframes insertRule() invalidation

Handle inline stylesheet @keyframes insertions without falling back to
broad owner invalidation. Recompute only elements whose computed
animation-name already references the inserted keyframes name.

Document-scoped insertions still walk the shadow-including tree so
existing shadow trees pick up inherited animations, and shadow-root
stylesheets fan out through the host root so :host combinators can
refresh host-side consumers as well. Also introduce the shared
ShadowRootStylesheetEffects analysis so later stylesheet mutation paths
can reuse the same per-scope escape classification.
This commit is contained in:
Andreas Kling
2026-04-22 22:32:20 +02:00
committed by Andreas Kling
parent b6559d3846
commit 4e92765211
Notes: github-actions[bot] 2026-04-23 14:49:46 +00:00
15 changed files with 421 additions and 1 deletions

View File

@@ -0,0 +1 @@
PASS: bare :host keyframes insertRule only invalidates the host (0 invalidations)

View File

@@ -0,0 +1,2 @@
opacity before keyframes insertRule: 1
opacity after keyframes insertRule: 0.25

View File

@@ -0,0 +1,2 @@
opacity before keyframes: 1
opacity after keyframes: 0.25

View File

@@ -0,0 +1,3 @@
before: 1
after: 1
keyframes: 0

View File

@@ -0,0 +1 @@
PASS: keyframes insertRule only invalidates animation users (1 invalidations)

View File

@@ -0,0 +1,6 @@
slotted before rule: rgb(0, 0, 0)
slotted after rule: rgb(0, 0, 255)
inherited before slot rule: rgb(0, 0, 0)
inherited after slot rule: rgb(255, 0, 0)
opacity before keyframes: 1
opacity after keyframes: 0.25

View File

@@ -0,0 +1,42 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<div id="host"></div>
<script>
function settleAndReset(triggerElement) {
getComputedStyle(triggerElement).animationName;
getComputedStyle(triggerElement).animationName;
internals.resetStyleInvalidationCounters();
}
function addBystanders(parent, count) {
for (let i = 0; i < count; ++i) {
const bystander = document.createElement("div");
bystander.textContent = `bystander ${i}`;
parent.appendChild(bystander);
}
}
test(() => {
const host = document.getElementById("host");
const shadowRoot = host.attachShadow({ mode: "open" });
const sheet = new CSSStyleSheet();
sheet.replaceSync(`
:host {
animation-name: fade;
animation-duration: 1s;
}
`);
shadowRoot.adoptedStyleSheets = [sheet];
addBystanders(document.body, 25);
settleAndReset(host);
sheet.insertRule("@keyframes fade { from { opacity: 0.25; } to { opacity: 0.25; } }", sheet.cssRules.length);
getComputedStyle(host).animationName;
const invalidations = internals.getStyleInvalidationCounters().styleInvalidations;
if (invalidations <= 1)
println(`PASS: bare :host keyframes insertRule only invalidates the host (${invalidations} invalidations)`);
else
println(`FAIL: bare :host keyframes insertRule only invalidates the host (${invalidations} invalidations)`);
});
</script>

View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<div id="host"><span id="slotted" class="item">slotted</span></div>
<script>
asyncTest(async done => {
const host = document.getElementById("host");
const slotted = document.getElementById("slotted");
const shadowRoot = host.attachShadow({ mode: "open" });
const declarationsSheet = new CSSStyleSheet();
const keyframesSheet = new CSSStyleSheet();
shadowRoot.innerHTML = "<slot></slot>";
shadowRoot.adoptedStyleSheets = [declarationsSheet, keyframesSheet];
declarationsSheet.replaceSync(`
::slotted(.item) {
animation-name: fade;
animation-duration: 1s;
animation-fill-mode: both;
}
`);
println(`opacity before keyframes insertRule: ${getComputedStyle(slotted).opacity}`);
keyframesSheet.insertRule("@keyframes fade { from { opacity: 0.25; } to { opacity: 0.25; } }", keyframesSheet.cssRules.length);
await animationFrame();
println(`opacity after keyframes insertRule: ${getComputedStyle(slotted).opacity}`);
done();
});
</script>

View File

@@ -0,0 +1,31 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<style id="document-style"></style>
<div id="host"></div>
<script>
asyncTest(async done => {
const host = document.getElementById("host");
const shadowRoot = host.attachShadow({ mode: "open" });
shadowRoot.innerHTML = `
<style>
#target {
animation-name: fade;
animation-duration: 1s;
animation-fill-mode: both;
}
</style>
<div id="target">target</div>
`;
const target = shadowRoot.getElementById("target");
const documentStyleSheet = document.getElementById("document-style").sheet;
println(`opacity before keyframes: ${getComputedStyle(target).opacity}`);
documentStyleSheet.insertRule("@keyframes fade { from { opacity: 0.25; } to { opacity: 0.25; } }", documentStyleSheet.cssRules.length);
await animationFrame();
println(`opacity after keyframes: ${getComputedStyle(target).opacity}`);
done();
});
</script>

View File

@@ -0,0 +1,27 @@
<!DOCTYPE html>
<style id="dynamic-style">
#target {
animation-duration: 1s;
animation-name: inserted;
}
</style>
<div id="target"></div>
<script src="../include.js"></script>
<script>
asyncTest(async done => {
const target = document.getElementById("target");
const sheet = document.getElementById("dynamic-style").sheet;
println(`before: ${target.getAnimations().length}`);
sheet.insertRule("@keyframes inserted { from { opacity: 0.25; } to { opacity: 0.75; } }", 1);
await animationFrame();
const animations = target.getAnimations();
println(`after: ${animations.length}`);
if (animations.length > 0)
println(`keyframes: ${animations[0].effect.getKeyframes().length}`);
done();
});
</script>

View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<style id="style-sheet">
#target {
animation-name: fade;
animation-duration: 1s;
}
</style>
<div id="target">target</div>
<div>bystander 1</div>
<div>bystander 2</div>
<div>bystander 3</div>
<script>
function settleAndReset(triggerElement) {
// Force style resolution before we observe counters so the inserted
// @keyframes rule is the only thing contributing to the measurement.
getComputedStyle(triggerElement).animationName;
getComputedStyle(triggerElement).animationName;
internals.resetStyleInvalidationCounters();
}
function addBystanders(parent, count) {
for (let i = 0; i < count; ++i) {
const bystander = document.createElement("div");
bystander.textContent = `bystander ${i + 4}`;
parent.appendChild(bystander);
}
}
test(() => {
const target = document.getElementById("target");
addBystanders(document.body, 25);
const sheet = document.getElementById("style-sheet").sheet;
settleAndReset(target);
sheet.insertRule("@keyframes fade { from { opacity: 0; } to { opacity: 1; } }", sheet.cssRules.length);
getComputedStyle(target).animationName;
const invalidations = internals.getStyleInvalidationCounters().styleInvalidations;
if (invalidations <= 1)
println(`PASS: keyframes insertRule only invalidates animation users (${invalidations} invalidations)`);
else
println(`FAIL: keyframes insertRule only invalidates animation users (${invalidations} invalidations)`);
});
</script>

View File

@@ -0,0 +1,40 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<div id="host"><span id="slotted" class="item">slotted</span><span id="inherited">inherited</span></div>
<script>
asyncTest(async done => {
const host = document.getElementById("host");
const slotted = document.getElementById("slotted");
const inherited = document.getElementById("inherited");
const shadowRoot = host.attachShadow({ mode: "open" });
shadowRoot.innerHTML = `
<style id="shadow-style">
::slotted(.item) {
animation-name: inserted;
animation-duration: 1s;
animation-fill-mode: both;
}
</style>
<slot></slot>
`;
const styleSheet = shadowRoot.getElementById("shadow-style").sheet;
println(`slotted before rule: ${getComputedStyle(slotted).color}`);
shadowRoot.appendChild(document.createElement("b"));
styleSheet.insertRule("::slotted(.item) { color: rgb(0, 0, 255); }", styleSheet.cssRules.length);
println(`slotted after rule: ${getComputedStyle(slotted).color}`);
println(`inherited before slot rule: ${getComputedStyle(inherited).color}`);
styleSheet.insertRule("slot { border-color: rgb(0, 0, 0); color: rgb(255, 0, 0); }", styleSheet.cssRules.length);
println(`inherited after slot rule: ${getComputedStyle(inherited).color}`);
println(`opacity before keyframes: ${getComputedStyle(slotted).opacity}`);
shadowRoot.appendChild(document.createElement("i"));
styleSheet.insertRule("@keyframes inserted { from { opacity: 0.25; } to { opacity: 0.25; } }", styleSheet.cssRules.length);
await animationFrame();
println(`opacity after keyframes: ${getComputedStyle(slotted).opacity}`);
done();
});
</script>