Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
feat(lint): warn on negative z-index, which renders invisibly behind the root (#4403)
WaterrrForever42ae3da5b6c5
diff --git a/packages/lint/src/rules/composition.test.ts b/packages/lint/src/rules/composition.test.tsindex f4c5c0150b..1be7f29efc 100644--- a/packages/lint/src/rules/composition.test.ts+++ b/packages/lint/src/rules/composition.test.ts@@ -2355,4 +2355,125 @@ describe("composition rules", () => {}});});++ describe("negative z-index", () => {+ const wrap = (+ head: string,+ body: string,+ ) => `<!doctype html><html><head><style>${head}</style></head><body>+ <div id="root" data-composition-id="main" data-no-timeline data-width="640" data-height="360" data-start="0" data-duration="1">+${body}+ </div>+</body></html>`;++ it("flags a negative z-index in a style block and names the selector", async () => {+ const result = await lintHyperframeHtml(+ wrap(+ "#under { position:absolute; z-index: -1; }",+ ` <div id="under" class="clip" data-start="0" data-duration="1"></div>`,+ ),+ );+ const finding = result.findings.find((f) => f.code === "negative_z_index");+ expect(finding).toBeDefined();+ expect(finding?.severity).toBe("warning");+ expect(finding?.selector).toBe("#under");+ expect(finding?.message).toContain("z-index: -1");+ });++ // Reviewed nit, confirmed on rendered pixels at 0.8.72: the element is only+ // dropped when its nearest ancestor stacking context is the composition root.+ // One frame carried the same `z-index: -1` band under isolation:isolate,+ // transform, opacity<1, filter, contain:paint and will-change -- all six+ // PRESENT at full coverage, the no-stacking-context control ABSENT at zero.+ // This rule matches CSS text and cannot resolve the cascade, so it fires on+ // both shapes; the message must therefore stay conditional.+ it("states the stacking-context CONDITION rather than asserting absence", async () => {+ const result = await lintHyperframeHtml(+ wrap(+ "#under { position:absolute; z-index: -1; }",+ ` <div id="under" class="clip" data-start="0" data-duration="1"></div>`,+ ),+ );+ const finding = result.findings.find((f) => f.code === "negative_z_index");+ expect(finding?.message).toContain("behind its nearest stacking context");+ // Reviewer's edge case: with a TRANSPARENT composition root the element is+ // VISIBLE (measured), so the hiding must be stated as CONDITIONAL on an opaque+ // background, never asserted outright.+ expect(finding?.message).toContain("an opaque background there hides it");+ expect(finding?.message).toContain("A transparent root leaves it visible");+ expect(finding?.message).not.toContain("renders invisibly unless");+ // The cause is painting order, not a tool failure. Both phrasings below imply+ // the renderer dropped the element, which the pixels refute.+ expect(finding?.message).not.toContain("is silently dropped");+ expect(finding?.message).not.toContain("zero exit code");+ });++ it("offers a stacking context as the measured remedy, not only DOM reordering", async () => {+ const result = await lintHyperframeHtml(+ wrap(+ "#under { position:absolute; z-index: -1; }",+ ` <div id="under" class="clip" data-start="0" data-duration="1"></div>`,+ ),+ );+ const finding = result.findings.find((f) => f.code === "negative_z_index");+ expect(finding?.fixHint).toContain("isolation: isolate");+ expect(finding?.fixHint).toContain("DOM order");+ });++ it("flags a negative z-index in an inline style attribute", async () => {+ const result = await lintHyperframeHtml(+ wrap(+ "",+ ` <div id="under" class="clip" style="position:absolute;z-index:-2" data-start="0" data-duration="1"></div>`,+ ),+ );+ const finding = result.findings.find((f) => f.code === "negative_z_index");+ expect(finding).toBeDefined();+ expect(finding?.selector).toBe("#under");+ });++ it("does not flag zero or positive z-index", async () => {+ const result = await lintHyperframeHtml(+ wrap(+ "#a { z-index: 0; } #b { z-index: 1; } #c { z-index: 999; }",+ ` <div id="a"></div>`,+ ),+ );+ expect(result.findings.some((f) => f.code === "negative_z_index")).toBe(false);+ });++ it("does not flag a custom property whose name ends in z-index", async () => {+ // Regression: /\bz-index\b/ treats the hyphen as a word break, so a plain+ // boundary matches `--panel-z-index: -1`, which declares a variable and+ // stacks nothing.+ const result = await lintHyperframeHtml(+ wrap("#a { --z-index: -1; --panel-z-index: -3; }", ` <div id="a"></div>`),+ );+ expect(result.findings.some((f) => f.code === "negative_z_index")).toBe(false);+ });++ it("does not flag a negative z-index inside a CSS comment", async () => {+ const result = await lintHyperframeHtml(+ wrap(+ "#a { /* z-index: -1; was dropped by the renderer */ z-index: 2; }",+ ` <div id="a"></div>`,+ ),+ );+ expect(result.findings.some((f) => f.code === "negative_z_index")).toBe(false);+ });++ it("does not flag -0, which is not a negative stacking level", async () => {+ const result = await lintHyperframeHtml(+ wrap("#a { z-index: -0; }", ` <div id="a"></div>`),+ );+ expect(result.findings.some((f) => f.code === "negative_z_index")).toBe(false);+ });++ it("flags every negative declaration, not just the first", async () => {+ const result = await lintHyperframeHtml(+ wrap("#a { z-index: -1; } #b { z-index: -2; }", ` <div id="a"></div>`),+ );+ expect(result.findings.filter((f) => f.code === "negative_z_index")).toHaveLength(2);+ });+ });});diff --git a/packages/lint/src/rules/composition.ts b/packages/lint/src/rules/composition.tsindex d02e3eabe7..28a819d5c0 100644--- a/packages/lint/src/rules/composition.ts+++ b/packages/lint/src/rules/composition.ts@@ -418,6 +418,54 @@ function isInsideInertTemplate(tag: OpenTag, tags: readonly OpenTag[]): boolean);}+// `(?<![\w-])` not `\b`: the hyphen in a custom property is a word break, so a+// plain boundary matches `--z-index: -1` and `--panel-z-index: -1`, which+// declare variables and stack nothing. `-0` is excluded because it is not a+// negative stacking level; `-01` and `-0.5` are.+const NEGATIVE_Z_INDEX = /(?<![\w-])z-index\s*:\s*-(?!0(?:\s*[;}]|\s*$))([\d.]+)/g;++// Name the selector that owns the declaration so the finding points at+// something the author can search for.+function cssOwnerSelector(content: string, matchIndex: number): string | undefined {+ const blockStart = content.lastIndexOf("{", matchIndex);+ if (blockStart === -1) return undefined;+ const previousBlockEnd = content.lastIndexOf("}", blockStart);+ const lines = content+ .slice(previousBlockEnd + 1, blockStart)+ .trim()+ .split("\n");+ return lines[lines.length - 1]?.trim() || undefined;+}++function elementSelector(tag: OpenTag): string | undefined {+ const elementId = readAttr(tag.raw, "id");+ return elementId ? `#${elementId}` : undefined;+}++function negativeZIndexFinding(+ match: RegExpExecArray,+ selector: string | undefined,+): HyperframeLintFinding {+ const level = match[1] ?? "";+ return {+ code: "negative_z_index",+ severity: "warning",+ ...(selector ? { selector } : {}),+ message:+ `\`z-index: -${level}\` paints the element behind its nearest stacking context's own content. ` ++ "With no stacking-context ancestor that context is the composition root itself, so an opaque " ++ "background there hides it entirely: in the DOM, laid out, and absent from the picture. " ++ "A transparent root leaves it visible. Siblings at `z-index: 0` or above are unaffected.",+ fixHint:+ "Give the element's parent a stacking context - `isolation: isolate` is the cheapest, and " ++ "`transform`, `filter`, `opacity` below 1, `contain: paint` and `will-change` all work too. " ++ "The negative-z child then paints above that parent's background and renders normally. " ++ "Or express paint order through DOM order - an earlier sibling paints behind a " ++ "later one - and raise the elements that should sit in front rather than lowering this one.",+ snippet: truncateSnippet(match[0]),+ };+}+export const compositionRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [// duplicate_composition_id catches meta-tag/root collisions that create duplicate composition entries.({ tags }) => {@@ -808,6 +856,60 @@ export const compositionRules: Array<(ctx: LintContext) => HyperframeLintFinding];},+ // negative_z_index+ // An element at a negative z-index is silently absent from both `snapshot`+ // and `render`, while siblings differing only in the sign of z-index render+ // exactly (heygen-com/hyperframes#4366). lint, validate and render all exit 0+ // and report nothing, so the first suspicion falls on the author's own CSS.+ // NOT A RENDERER DEFECT -- ORDINARY CSS PAINTING ORDER, measured at 0.8.72.+ // The element is PAINTED; it is simply painted beneath something opaque. Remove+ // every opaque background above it and the same `z-index: -1` band renders at+ // full coverage, identically to the same band with `z-index` deleted. A negative-z+ // descendant paints at step 2 of its nearest stacking context -- above that+ // context root's own background, but below the context's positioned in-flow+ // content -- so a `position: relative` composition root with an opaque background+ // paints over it. That is the shape #4366 reports; nothing is dropped and the+ // renderer has no say in it.+ //+ // Kept as a warning because authors hit it and nothing explains why: the element+ // is in the DOM, laid out, and invisible. The message therefore names the CAUSE+ // and the remedy rather than implying the tool failed.+ //+ // THE CONDITION IS LOAD-BEARING AND THE MESSAGE STATES IT. The element is only+ // dropped when its nearest ancestor stacking context is the composition root,+ // which is the shape #4366 reports. Give any ancestor a stacking context and it+ // renders correctly: measured at 0.8.72 on ONE frame carrying the same+ // `z-index: -1` band under six triggers -- isolation:isolate, transform,+ // opacity below 1, filter, contain:paint, will-change -- all six PRESENT at full+ // coverage, against the no-stacking-context control ABSENT at zero coverage.+ // That is ordinary CSS painting order: a negative-z child paints above its+ // stacking context root's own background, so only the root case is hidden.+ //+ // This rule matches CSS TEXT and does not resolve the cascade, so it cannot know+ // whether an ancestor forms a stacking context and fires on both shapes. That is+ // why the message is CONDITIONAL rather than an assertion of absence: an+ // unconditional "silently dropped" is false for every isolated case, and a lint+ // message that overclaims is how authors learn to disregard the rule.+ ({ tags, styles }) => {+ const findings: HyperframeLintFinding[] = [];+ for (const style of styles) {+ const content = stripCssComments(style.content);+ NEGATIVE_Z_INDEX.lastIndex = 0;+ let match: RegExpExecArray | null;+ while ((match = NEGATIVE_Z_INDEX.exec(content)) !== null) {+ findings.push(negativeZIndexFinding(match, cssOwnerSelector(content, match.index)));+ }+ }+ for (const tag of tags) {+ const inline = readAttr(tag.raw, "style");+ if (!inline) continue;+ NEGATIVE_Z_INDEX.lastIndex = 0;+ const match = NEGATIVE_Z_INDEX.exec(inline);+ if (match) findings.push(negativeZIndexFinding(match, elementSelector(tag)));+ }+ return findings;+ },+// requestanimationframe_in_composition({ scripts, rawSource, options }) => {if (isRegistrySourceFile(options.filePath) || isRegistryInstalledFile(rawSource)) return [];