Skip to content

Commit bfaf6eb

Browse files
fix(cli): prune unrendered content from the motion signature and measure asserted scopes
The shared motion classifier pruned only display:none subtrees, so text, pseudo content, media pixels, and counter decoys inside skipped content-visibility contents changed the frozen-sweep and liveness signatures although nothing painted. Pruning is now platform-owned where checkVisibility exists (display:none, skipped hidden/auto contents; a display:contents host keeps its painting pseudo-elements), with the computed-style fallback for happy-dom. A content-visibility:hidden host signs its own box, opacity, clip-path, font axes, and widget state, but not its text, pseudo content, control value, or media pixels; hosts where size containment does not apply (non-atomic inline, display:contents, table and internal table boxes, ruby) skip nothing. isVisibleElement regains the checkVisibility floor layout-audit uses on its opacity-floor path, so an asserted selector inside skipped contents reports hidden. The layout-audit opt-out no longer applies to the element passed as the signature root: a keepsMoving scope that is itself data-layout-ignore is measured (an assertion naming an element outranks the opt-out) instead of producing an empty signature that motionAudit reports as a missing selector. Opt-outs inside the scope still apply. motion-sample defers the missing-classifier error to the first call so the driver's evaluate surfaces it instead of an opaque "is not a function" page error; round() is shared. The real-Chromium suite is opt-in on Windows (HYPERFRAMES_BROWSER_TESTS=1): in the shared Windows package lane the runner's Chrome did not launch within the package hookTimeout, and the suite already runs on Linux CI where the system Chrome is found. The launch hook has its own 120s ceiling. Co-Authored-By: Miguel Ángel <[email protected]>
1 parent fbec4c6 commit bfaf6eb

6 files changed

Lines changed: 382 additions & 74 deletions

File tree

‎docs/packages/cli.mdx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,11 @@ geometry, opacity, text and generated content, form-control state, painted CSS
720720
counters, clip-path, variable-font axes, and the pixels of visible
721721
canvas/video/img elements all count as motion — so a playing same-origin (or
722722
CORS-readable) background video keeps a scope live on its own. Elements under
723-
`data-layout-ignore` never count.
723+
`data-layout-ignore` inside the scope never count; the opt-out does not apply to
724+
the scope element itself, since naming it in an assertion outranks it. Content
725+
the browser does not paint (`display: none` subtrees, `visibility: hidden`
726+
boxes, skipped `content-visibility` contents) never counts as motion either.
727+
724728
`duration`, `keepsMoving.withinSelector`, and `keepsMoving.maxStaticSec` are
725729
optional. Findings use the same shape and envelope as layout findings and are
726730
errors by default, so a failed assertion fails the run. A selector that matches

‎packages/cli/src/commands/motion-sample.browser.js‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,27 @@
77
// Visibility, opacity, and the liveness signature all come from the shared
88
// motion classifier so this sampler and the frozen-sweep guard can never
99
// disagree on what counts as motion (see motion-signature.browser.js).
10+
// Resolved at install, checked at call: addScriptTag resolves on load, so an
11+
// install-time throw would only surface as a page error and an opaque
12+
// "__hyperframesMotionSample is not a function" from the driver's evaluate.
1013
const shared = window.__hyperframesMotionSignature;
11-
if (!shared) {
12-
throw new Error("motion-signature.browser.js must be injected before motion-sample.browser.js");
13-
}
14-
15-
function round(value) {
16-
return Math.round(value * 100) / 100;
17-
}
1814

1915
function toRect(rect) {
2016
return {
21-
left: round(rect.left),
22-
top: round(rect.top),
23-
right: round(rect.right),
24-
bottom: round(rect.bottom),
25-
width: round(rect.width),
26-
height: round(rect.height),
17+
left: shared.round(rect.left),
18+
top: shared.round(rect.top),
19+
right: shared.round(rect.right),
20+
bottom: shared.round(rect.bottom),
21+
width: shared.round(rect.width),
22+
height: shared.round(rect.height),
2723
};
2824
}
2925

3026
function sampleElement(element) {
3127
const rect = element.getBoundingClientRect();
3228
return {
3329
rect: toRect(rect),
34-
opacity: round(shared.opacityChain(element)),
30+
opacity: shared.round(shared.opacityChain(element)),
3531
visible: shared.isVisibleElement(element),
3632
};
3733
}
@@ -69,6 +65,11 @@
6965
}
7066

7167
window.__hyperframesMotionSample = function motionSample(options) {
68+
if (!shared) {
69+
throw new Error(
70+
"motion-signature.browser.js must be injected before motion-sample.browser.js",
71+
);
72+
}
7273
const { selectors = [], livenessScopes = [] } = options || {};
7374
return { data: sampleSelectors(selectors), liveness: sampleLiveness(livenessScopes) };
7475
};

‎packages/cli/src/commands/motion-sample.browser.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ describe("motion-sample.browser", () => {
7777
Reflect.deleteProperty(window, "__hyperframesLayoutGeometry");
7878
});
7979

80-
it("throws at install when the shared motion classifier is missing", () => {
80+
it("installs without the shared motion classifier and names it on the first call", () => {
81+
// Installing must not throw: addScriptTag resolves on load regardless, so
82+
// the driver only ever sees the error from its evaluate call.
8183
// eslint-disable-next-line no-new-func
82-
expect(() => new Function(script)()).toThrow(/motion-signature\.browser\.js/);
84+
expect(() => new Function(script)()).not.toThrow();
85+
expect(() => sample({ livenessScopes: ["*"] })).toThrow(/motion-signature\.browser\.js/);
8386
});
8487

8588
it("samples a present, visible selector and returns null for an absent one", () => {

‎packages/cli/src/commands/motion-signature.browser.chromium.test.ts‎

Lines changed: 187 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
// (motion-signature.browser.test.ts) mocks getComputedStyle, so it asserts the
44
// classifier's control flow against a fake; this suite asserts the same
55
// branches against the platform — Blink's attr() substitution in computed
6-
// pseudo content, unsubstituted counter(), display:none subtrees, clip-path,
6+
// pseudo content, unsubstituted counter(), display:none and
7+
// content-visibility:hidden subtrees, opt-outs on a measured scope, clip-path,
78
// and form control state — using the exact scripts `hyperframes check` injects.
8-
// Skipped when no Chrome/Chromium binary is available without downloading.
9+
// Skipped when no Chrome/Chromium binary is available without downloading. On
10+
// Windows the suite is opt-in (HYPERFRAMES_BROWSER_TESTS=1): in the shared
11+
// Windows package test lane the launch of the runner's Chrome did not complete
12+
// within the package hookTimeout (cause not established; no other suite in this
13+
// package launches a browser), and the suite already runs on Linux CI, where
14+
// the runner's Chrome is found as a system browser.
915
import { existsSync, readFileSync } from "node:fs";
1016
import { dirname, join } from "node:path";
1117
import { fileURLToPath } from "node:url";
@@ -26,6 +32,12 @@ function resolveExecutable(): string | undefined {
2632
}
2733

2834
const executablePath = resolveExecutable();
35+
const RUNS_CHROMIUM =
36+
!!executablePath &&
37+
(process.platform !== "win32" || process.env.HYPERFRAMES_BROWSER_TESTS === "1");
38+
// The package hookTimeout is sized for cold module imports, not for launching
39+
// a browser; give the launch its own ceiling.
40+
const BROWSER_LAUNCH_TIMEOUT_MS = 120_000;
2941

3042
interface Samples {
3143
sweep: string;
@@ -52,7 +64,7 @@ function composition(css: string, body: string): string {
5264
</body></html>`;
5365
}
5466

55-
describe.skipIf(!executablePath)("motion-signature.browser in Chromium", () => {
67+
describe.skipIf(!RUNS_CHROMIUM)("motion-signature.browser in Chromium", () => {
5668
let browser: Browser;
5769
let page: Page;
5870

@@ -65,7 +77,7 @@ describe.skipIf(!executablePath)("motion-signature.browser in Chromium", () => {
6577
});
6678
page = await browser.newPage();
6779
await page.setViewport({ width: 640, height: 360 });
68-
});
80+
}, BROWSER_LAUNCH_TIMEOUT_MS);
6981

7082
afterAll(async () => {
7183
await browser?.close();
@@ -77,11 +89,17 @@ describe.skipIf(!executablePath)("motion-signature.browser in Chromium", () => {
7789
await page.addScriptTag({ content: motionSampleScript });
7890
}
7991

80-
async function sample(): Promise<Samples> {
81-
return page.evaluate(() => ({
82-
sweep: window.__hyperframesLayoutGeometry(),
83-
liveness: window.__hyperframesMotionSample({ livenessScopes: ["*"] }).liveness["*"] ?? "",
84-
}));
92+
async function sample(scope = "*"): Promise<Samples> {
93+
return page.evaluate(
94+
(livenessScope) => ({
95+
sweep: window.__hyperframesLayoutGeometry(),
96+
liveness:
97+
window.__hyperframesMotionSample({ livenessScopes: [livenessScope] }).liveness[
98+
livenessScope
99+
] ?? "",
100+
}),
101+
scope,
102+
);
85103
}
86104

87105
async function mutate(script: string): Promise<void> {
@@ -381,4 +399,164 @@ describe.skipIf(!executablePath)("motion-signature.browser in Chromium", () => {
381399

382400
expect(after).toEqual(before);
383401
});
402+
403+
// A keepsMoving scope names the element explicitly, so the layout-audit
404+
// opt-out on the scope itself does not empty its signature (which the Node
405+
// side would otherwise report as motion_selector_missing for an element that
406+
// exists). Opt-outs INSIDE the scope still apply.
407+
it("measures a keepsMoving scope that is itself data-layout-ignore, but not opted-out layers inside it", async () => {
408+
await load(
409+
composition(
410+
"#scene { position: absolute; width: 300px; height: 200px; } #logo, #glow { position: absolute; width: 50px; height: 50px; background: #00f; }",
411+
'<div id="scene" data-layout-ignore><div id="logo"></div><div id="glow" data-layout-ignore></div></div>',
412+
),
413+
);
414+
const before = await sample("#scene");
415+
expect(before.liveness).not.toBe("");
416+
await mutate('document.getElementById("glow").style.left = "100px"');
417+
const afterIgnoredMove = await sample("#scene");
418+
await mutate('document.getElementById("logo").style.left = "100px"');
419+
const afterMove = await sample("#scene");
420+
421+
expect(afterIgnoredMove.liveness).toBe(before.liveness);
422+
expect(afterMove.liveness).not.toBe(before.liveness);
423+
});
424+
425+
it("ignores text mutations inside content-visibility:hidden contents", async () => {
426+
await load(
427+
composition(
428+
"#wrap { content-visibility: hidden; }",
429+
'<div id="wrap"><span id="skipped" class="fixed">10</span></div><span class="fixed">10</span>',
430+
),
431+
);
432+
const before = await sample();
433+
await mutate('document.getElementById("skipped").textContent = "09"');
434+
const after = await sample();
435+
436+
expect(after).toEqual(before);
437+
});
438+
439+
it("ignores a decoy counter owner inside content-visibility:hidden contents", async () => {
440+
await load(
441+
composition(
442+
"#wrap { content-visibility: hidden; } #decoy { counter-reset: countdown 10; } #countdown::after { content: counter(countdown); }",
443+
'<div id="wrap"><div id="decoy"></div></div><span id="countdown" class="fixed"></span>',
444+
),
445+
);
446+
const before = await sample();
447+
await mutate('document.getElementById("decoy").style.counterReset = "countdown 9"');
448+
const after = await sample();
449+
450+
expect(after).toEqual(before);
451+
});
452+
453+
it("ignores a decoy counter owner inside off-screen content-visibility:auto contents", async () => {
454+
await load(
455+
composition(
456+
"#far { content-visibility: auto; position: absolute; top: 5000px; width: 10px; height: 10px; } #decoy { counter-reset: countdown 10; } #countdown::after { content: counter(countdown); }",
457+
'<div id="far"><div id="decoy"></div></div><span id="countdown" class="fixed"></span>',
458+
),
459+
);
460+
const before = await sample();
461+
await mutate('document.getElementById("decoy").style.counterReset = "countdown 9"');
462+
const after = await sample();
463+
464+
expect(after).toEqual(before);
465+
});
466+
467+
it("ignores a repaint of a canvas whose contents are skipped by content-visibility:hidden", async () => {
468+
await load(
469+
composition(
470+
"#c { content-visibility: hidden; width: 100px; height: 100px; }",
471+
'<canvas id="c" width="100" height="100"></canvas>',
472+
),
473+
);
474+
const before = await sample();
475+
await mutate(
476+
'const ctx = document.getElementById("c").getContext("2d"); ctx.fillStyle = "#f00"; ctx.fillRect(0, 0, 100, 100);',
477+
);
478+
const after = await sample();
479+
480+
expect(after).toEqual(before);
481+
});
482+
483+
// content-visibility only skips contents where size containment applies; on
484+
// a non-atomic inline host it is a no-op and everything inside still paints.
485+
it("keeps signing text and pseudo content inside an inline content-visibility:hidden host", async () => {
486+
await load(
487+
composition(
488+
"#host { content-visibility: hidden; } #host::after { content: attr(data-txt); }",
489+
'<span id="host" data-txt="10"><span id="kid" class="fixed">10</span></span>',
490+
),
491+
);
492+
const start = await sample();
493+
await mutate('document.getElementById("kid").textContent = "09"');
494+
const afterText = await sample();
495+
await mutate('document.getElementById("host").setAttribute("data-txt", "09")');
496+
const afterPseudo = await sample();
497+
498+
expect(afterText.sweep).not.toBe(start.sweep);
499+
expect(afterText.liveness).not.toBe(start.liveness);
500+
expect(afterPseudo.sweep).not.toBe(afterText.sweep);
501+
});
502+
503+
it("sees a checkbox toggle on a content-visibility:hidden control", async () => {
504+
await load(
505+
composition(
506+
"#toggle { content-visibility: hidden; width: 24px; height: 24px; }",
507+
'<input id="toggle" type="checkbox" />',
508+
),
509+
);
510+
const before = await sample();
511+
await mutate('document.getElementById("toggle").checked = true');
512+
const after = await sample();
513+
514+
expect(after.sweep).not.toBe(before.sweep);
515+
});
516+
517+
it("ignores a value change on a text input whose contents are skipped by content-visibility:hidden", async () => {
518+
await load(
519+
composition(
520+
"#note { content-visibility: hidden; width: 120px; height: 40px; font: 24px monospace; }",
521+
'<input id="note" value="10" />',
522+
),
523+
);
524+
const before = await sample();
525+
await mutate('document.getElementById("note").value = "09"');
526+
const after = await sample();
527+
528+
expect(after).toEqual(before);
529+
});
530+
531+
it("sees a counter painted only by a display:contents host's pseudo-element", async () => {
532+
await load(
533+
composition(
534+
"body { counter-reset: countdown 10; } #host { display: contents; } #host::after { content: counter(countdown); font: 32px/48px monospace; }",
535+
'<div id="host"></div>',
536+
),
537+
);
538+
const before = await sample();
539+
await mutate('document.body.style.counterReset = "countdown 9"');
540+
const after = await sample();
541+
542+
expect(after.sweep).not.toBe(before.sweep);
543+
});
544+
545+
it("ignores a content-visibility:hidden host's own text and pseudo content but still sees its box move", async () => {
546+
await load(
547+
composition(
548+
"#host { content-visibility: hidden; position: absolute; width: 80px; height: 48px; background: #f00; } #host::after { content: attr(data-txt); }",
549+
'<div id="host" data-txt="10">10</div>',
550+
),
551+
);
552+
const start = await sample();
553+
await mutate('document.getElementById("host").firstChild.textContent = "09"');
554+
await mutate('document.getElementById("host").setAttribute("data-txt", "09")');
555+
const afterContents = await sample();
556+
await mutate('document.getElementById("host").style.left = "100px"');
557+
const afterMove = await sample();
558+
559+
expect(afterContents).toEqual(start);
560+
expect(afterMove.sweep).not.toBe(start.sweep);
561+
});
384562
});

0 commit comments

Comments
 (0)