fix(cli): a scoped keepsMoving capture ignores the page background behind it (#5966)

The root (and propagated body) background paints the page canvas whatever its
visibility, so an animated page background could make a still scope read as
moving. Scoped captures now clear those backgrounds and keep the resulting
transparency, and the frame comparison reads alpha, so dark paint revealed on
the cleared page still counts.
This commit is contained in:
Miguel Ángel
2026-10-20 17:31:45 +00:00
committed by GitHub
parent 234ebc4ba1
commit ba1fa8b12a
3 changed files with 31 additions and 3 deletions
+8 -2
View File
@@ -1013,14 +1013,19 @@ async function captureFrame(page: Page, scope?: string): Promise<string> {
try {
const box = await page.evaluate(isolateScopePaint, scope);
if (!box || box.width < 1 || box.height < 1) return "";
return await page.screenshot({ encoding: "base64", type: "png", clip: box });
return await page.screenshot({
encoding: "base64",
type: "png",
clip: box,
omitBackground: true,
});
} finally {
await page.evaluate(restoreScopePaint);
}
}
// Liveness signs only the scope's subtree, so its frame must show only that paint: anything else behind or
// over the box (a moving background, grain) would read as the scope moving. Hidden like the contrast pass's glyphs.
// over the box (a moving background, grain, the page canvas) would read as the scope moving; the canvas stays clear.
function isolateScopePaint(
scope: string,
): { x: number; y: number; width: number; height: number } | null {
@@ -1036,6 +1041,7 @@ function isolateScopePaint(
const style = document.createElement("style");
style.id = "__hf_capture_scope_style";
style.textContent = `:not([data-hf-capture-scope], [data-hf-capture-scope] *) { visibility: hidden !important; }
:root:not([data-hf-capture-scope]), body:not([data-hf-capture-scope]) { background: none !important; }
[data-hf-capture-scope] { visibility: ${visibility} !important; }`;
document.head.append(style);
const rect = target.getBoundingClientRect();
@@ -150,6 +150,27 @@ describe.skipIf(!RUNS_CHROMIUM)("frozen-sweep guard in Chromium", () => {
expect(result.frozen).toEqual([expect.stringContaining("nothing moves within #title")]);
});
it("keeps a still title frozen under keepsMoving while the page's root background changes", async () => {
const html = film(
`<h1 id="title" style="position: absolute; left: 120px; top: 120px; margin: 0; padding: 40px;
color: #fff; font: 600 48px sans-serif">Still</h1>`,
`document.documentElement.style.background = "rgb(" + Math.round(t * 40) + ", 0, 80)";`,
);
const result = await findings(html, [{ kind: "keepsMoving", withinSelector: "#title" }]);
expect(result.frozen).toEqual([expect.stringContaining("nothing moves within #title")]);
});
it("sees dark paint revealed inside a scope, where only its alpha tells it from the cleared page", async () => {
const html = film(
`<svg id="scene" width="640" height="360"><defs><mask id="dark"><rect width="640" height="360" fill="black"/>
<circle id="hole" cx="320" cy="180" r="0" fill="white"/></mask></defs>
<rect width="640" height="360" fill="#000" mask="url(#dark)"/></svg>`,
`document.getElementById("hole").setAttribute("r", String(t * 60));`,
);
const result = await findings(html, [{ kind: "keepsMoving", withinSelector: "#scene" }]);
expect(result.frozen).toEqual([]);
});
it("still warns on a canvas that animates by the wall clock, not by the seek", async () => {
const html = film(
`<canvas id="c" width="640" height="360"></canvas>