fix(core): keep a later scene's id-selected !important layer hidden before it starts (#8331)

* fix(core): keep a later scene's id-selected !important layer hidden before it starts

The hidden-clip contents rule (`[data-hf-clip-hidden][data-hf-clip-hidden] *`)
weighs two attribute selectors. A sub-composition's own
`#id { visibility: visible !important }` is rescoped under its composition
root, so it weighs an id plus an attribute and won the !important tie. The
element then painted from frame 0, over every earlier scene, in a render.

The rule now also ships inside a cascade layer. A layered !important outranks
every unlayered !important whatever its specificity. The unlayered copy stays
for engines that order layered !important the other way (jsdom).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(producer): move the hidden-clip cascade check into its own browser test file

The flake gate re-runs every changed test file under CPU contention with a
120 s cap per run, and the whole browser contract file does not fit in it.
The two cases now live in a small integration file the gate can re-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terencecho
2026-11-21 00:56:35 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 1ce69fd818
commit 861046f508
4 changed files with 103 additions and 2 deletions
@@ -9,9 +9,15 @@ import {
showHiddenClips,
} from "./timedClipHide";
const selectorsIn = (rules: CSSRuleList): string[] =>
Array.from(rules).flatMap((rule) =>
"selectorText" in rule
? [(rule as CSSStyleRule).selectorText]
: selectorsIn((rule as CSSGroupingRule).cssRules),
);
const injectedSelectors = () =>
Array.from(document.head.querySelectorAll("style")).flatMap((style) =>
Array.from(style.sheet!.cssRules).map((rule) => (rule as CSSStyleRule).selectorText),
selectorsIn(style.sheet!.cssRules),
);
type ShaderWindow = Window & {
@@ -52,6 +58,20 @@ describe("hideTimedClipsUntilFirstPass", () => {
hideTimedClipsUntilFirstPass();
expect(injectedSelectors()).toContain(`[${CLIP_HIDDEN_ATTR}][${CLIP_HIDDEN_ATTR}] *`);
});
// A scene's rescoped `#id { visibility: visible !important }` outweighs any attribute-only selector;
// only a layered !important outranks it. jsdom cannot cascade layers, so the browser contract
// asserts the rendered result and this asserts the layered rule is there.
it("also places the contents rule in a cascade layer", () => {
hideTimedClipsUntilFirstPass();
const layers = Array.from(document.head.querySelectorAll("style")).flatMap((style) =>
Array.from(style.sheet!.cssRules).filter((rule) => rule.cssText.startsWith("@layer ")),
);
expect(layers).toHaveLength(1);
expect(selectorsIn((layers[0] as CSSGroupingRule).cssRules)).toContain(
`[${CLIP_HIDDEN_ATTR}][${CLIP_HIDDEN_ATTR}] *`,
);
});
});
describe("keepInlineHideRulesForOlderShaders", () => {
+3 -1
View File
@@ -24,11 +24,13 @@ const HIDDEN_CLIP_DOUBLED_FOR_WEIGHT = `[${CLIP_HIDDEN_ATTR}][${CLIP_HIDDEN_ATTR
const HIDDEN_CLIP_BY_INLINE_STYLE = `[${CLIP_HIDDEN_ATTR}][style*="visibility: hidden"]`;
const skipHiddenImages = (clip: string) =>
`${SKIPPABLE_CLIP}${clip} img { display: none !important; }`;
const LAYER_OUTRANKING_UNLAYERED_IMPORTANT = "hf-hidden-clip";
const hideHiddenClipContents = (clip: string) => {
const filterOutputLayers = [clip, `${clip} *`]
.flatMap((layer) => [layer, `${layer}::before`, `${layer}::after`])
.join(", ");
return `${clip} * { visibility: hidden !important; } ${filterOutputLayers} { filter: none !important; }`;
const rules = `${clip} * { visibility: hidden !important; } ${filterOutputLayers} { filter: none !important; }`;
return `${rules} @layer ${LAYER_OUTRANKING_UNLAYERED_IMPORTANT} { ${rules} }`;
};
export function showHiddenClips(only: (el: HTMLElement) => boolean = () => true): () => void {
@@ -5,6 +5,7 @@
"src/regression-harness-audio.test.ts",
"src/services/bundleScriptPolicy.test.ts",
"src/services/coreRuntimeBrowser.test.ts",
"src/services/hiddenClipCascade.test.ts",
"src/services/heifImagePrep.test.ts",
"src/services/htmlCompiler.heif.test.ts",
"src/services/vfxDeterminism.test.ts",
@@ -0,0 +1,78 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import puppeteer, { type Browser, type Page } from "puppeteer";
import type {} from "../../../core/src/runtime/window";
const RUNTIME_PATH = resolve(import.meta.dirname, "../../../core/dist/hyperframe.runtime.iife.js");
const PREVIEW_HEAD = `<meta name="hyperframes-studio-preview">`;
const WIDTH = 320;
const HEIGHT = 180;
// A sub-composition's `#ground { visibility: visible !important }`, as the compiler rescopes it under its
// root, outweighs any attribute-only selector and once painted over every earlier scene.
const LATE_SCENE_WITH_ID_IMPORTANT = `
<style>[data-composition-id="late"] #ground { background: rgb(255, 0, 0); visibility: visible !important; }</style>
<div data-composition-id="late" class="clip" data-start="2" data-duration="1" style="position:absolute;inset:0">
<div id="ground" style="position:absolute;inset:0"></div>
</div>`;
async function openFilm(browser: Browser, head: string): Promise<Page> {
const page = await browser.newPage();
await page.setViewport({ width: WIDTH, height: HEIGHT });
await page.setContent(`<!doctype html>${head}
<style>html, body { margin: 0; background: rgb(128, 128, 128); }</style>
<div data-composition-id="root" data-start="0" data-duration="3" data-width="${WIDTH}" data-height="${HEIGHT}"
style="position:relative;width:${WIDTH}px;height:${HEIGHT}px">${LATE_SCENE_WITH_ID_IMPORTANT}</div>`);
await page.addScriptTag({ content: readFileSync(RUNTIME_PATH, "utf8") });
await page.waitForFunction(() => window.__playerReady && window.__renderReady);
return page;
}
async function redPixelsAt(page: Page, time: number): Promise<number> {
await page.evaluate((t) => window.__player!.renderSeek!(t), time);
const shot = await page.screenshot({ encoding: "base64" });
return page.evaluate(async (png) => {
const image = new Image();
image.src = `data:image/png;base64,${png}`;
await image.decode();
const context = new OffscreenCanvas(image.width, image.height).getContext("2d")!;
context.drawImage(image, 0, 0);
const { data } = context.getImageData(0, 0, image.width, image.height);
let red = 0;
for (let i = 0; i < data.length; i += 4) if (data[i]! > 200) red++;
return red;
}, shot);
}
describe("hidden clip contents cascade", () => {
let browser: Browser;
beforeAll(async () => {
browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});
}, 30_000);
afterAll(async () => {
await browser?.close();
});
it.each([
["a render", ""],
["a preview", PREVIEW_HEAD],
])(
"keeps a later scene's id-selected visibility:visible !important layer hidden until it starts in %s",
async (_, head) => {
const page = await openFilm(browser, head);
try {
expect(await redPixelsAt(page, 1)).toBe(0);
expect(await redPixelsAt(page, 2.5)).toBe(WIDTH * HEIGHT);
} finally {
await page.close();
}
},
30_000,
);
});