Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
feat(lint): flag a canvas-level CSS zoom that rescales a fixed frame (#4412)
WaterrrForever201c829c454d
diff --git a/packages/lint/src/rules/composition.test.ts b/packages/lint/src/rules/composition.test.tsindex 719d8061e1..e41c35268c 100644--- a/packages/lint/src/rules/composition.test.ts+++ b/packages/lint/src/rules/composition.test.ts@@ -2180,4 +2180,121 @@ describe("composition rules", () => {expect(finding?.message).toMatch(/25 elements/);});});++ // root_zoom_rescales_a_fixed_canvas — measured at 0.8.71 on an 800x400+ // composition: root zoom:2 renders a left:600 box at ZERO pixels (scaled to+ // x=1200, outside a frame still 800 wide); root zoom:0.5 paints the whole+ // composition into the top-left quarter. A zoom on a DESCENDANT is honoured+ // exactly as CSS specifies and must stay silent.+ describe("root_zoom_rescales_a_fixed_canvas", () => {+ const codes = (r: { findings: Array<{ code: string }> }) => r.findings.map((f) => f.code);++ it("flags a rescaling zoom on the composition root", async () => {+ const result = await lintHyperframeHtml(`<!doctype html><html><head><style>+ #root { width: 800px; height: 400px; zoom: 2; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);++ expect(codes(result)).toContain("root_zoom_rescales_a_fixed_canvas");+ const finding = result.findings.find((f) => f.code === "root_zoom_rescales_a_fixed_canvas");+ expect(finding?.severity).toBe("error");+ expect(finding?.message).toContain("zero pixels");+ });++ it("describes the shrinking case as dead space, not clipping", async () => {+ const result = await lintHyperframeHtml(`<!doctype html><html><head><style>+ #root { zoom: 0.5; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);++ const finding = result.findings.find((f) => f.code === "root_zoom_rescales_a_fixed_canvas");+ expect(finding?.message).toContain("dead space");+ expect(finding?.message).not.toContain("zero pixels");+ });++ it("flags zoom on html and on body, which scale the canvas from above", async () => {+ for (const selector of ["html", "body"]) {+ const result = await lintHyperframeHtml(`<!doctype html><html><head><style>+ ${selector} { zoom: 1.5; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);+ expect(codes(result)).toContain("root_zoom_rescales_a_fixed_canvas");+ }+ });++ it("flags an inline zoom on the root", async () => {+ const result = await lintHyperframeHtml(`<!doctype html><html><body>+ <div id="root" style="zoom: 2" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);++ expect(codes(result)).toContain("root_zoom_rescales_a_fixed_canvas");+ });++ it("stays silent on a zoom applied to a descendant", async () => {+ const result = await lintHyperframeHtml(`<!doctype html><html><head><style>+ .badge { zoom: 2; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5">+ <div class="badge"></div>+ </div>+ </body></html>`);++ expect(codes(result)).not.toContain("root_zoom_rescales_a_fixed_canvas");+ });++ it("stays silent on identity zoom values", async () => {+ for (const value of ["1", "1.0", "100%", "normal", "unset", "initial"]) {+ const result = await lintHyperframeHtml(`<!doctype html><html><head><style>+ #root { zoom: ${value}; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);+ expect(codes(result)).not.toContain("root_zoom_rescales_a_fixed_canvas");+ }+ });++ // A custom property is not the CSS `zoom` property. A plain \b boundary+ // flags both, which is the same trap the negative-z-index rule hit.+ it("stays silent on custom properties whose name ends in zoom", async () => {+ const result = await lintHyperframeHtml(`<!doctype html><html><head><style>+ #root { --zoom: 2; --panel-zoom: 0.5; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);++ expect(codes(result)).not.toContain("root_zoom_rescales_a_fixed_canvas");+ });++ it("reads through !important", async () => {+ const identity = await lintHyperframeHtml(`<!doctype html><html><head><style>+ #root { zoom: 1 !important; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);+ expect(codes(identity)).not.toContain("root_zoom_rescales_a_fixed_canvas");++ const rescaling = await lintHyperframeHtml(`<!doctype html><html><head><style>+ #root { zoom: 2 !important; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);+ const finding = rescaling.findings.find(+ (f) => f.code === "root_zoom_rescales_a_fixed_canvas",+ );+ expect(finding?.message).toContain("zoom: 2`");+ });++ it("ignores a zoom inside a CSS comment", async () => {+ const result = await lintHyperframeHtml(`<!doctype html><html><head><style>+ #root { /* zoom: 2; */ width: 800px; }+ </style></head><body>+ <div id="root" data-composition-id="main" data-width="800" data-height="400" data-duration="5"></div>+ </body></html>`);++ expect(codes(result)).not.toContain("root_zoom_rescales_a_fixed_canvas");+ });+ });});diff --git a/packages/lint/src/rules/composition.ts b/packages/lint/src/rules/composition.tsindex 4fedabd3c9..8b80b0858a 100644--- a/packages/lint/src/rules/composition.ts+++ b/packages/lint/src/rules/composition.ts@@ -205,6 +205,155 @@ function rootClassStyledSelectors(styles: ExtractedBlock[], rootClasses: string[return offenders;}+// A `zoom` declaration and its value. The lookbehind keeps custom properties+// out: `--panel-zoom: 2` and `--zoom: 0.5` are author variables, not the CSS+// `zoom` property, and a plain \b would flag both.+const ZOOM_DECLARATION = /(?<![\w-])zoom\s*:\s*([^;}]+)/gi;++/** zoom values that leave the canvas alone; anything else rescales it. */+function isIdentityZoom(rawValue: string): boolean {+ const value = rawValue+ .trim()+ .replace(/!\s*important\s*$/i, "")+ .trim()+ .toLowerCase();+ return (+ value === "" ||+ value === "normal" ||+ value === "unset" ||+ value === "initial" ||+ value === "revert" ||+ value === "1" ||+ value === "1.0" ||+ value === "100%"+ );+}++/** First rescaling `zoom` value in a declaration block, or null. */+function firstRescalingZoom(css: string): string | null {+ ZOOM_DECLARATION.lastIndex = 0;+ let match: RegExpExecArray | null;+ while ((match = ZOOM_DECLARATION.exec(css)) !== null) {+ const value = (match[1] ?? "").trim();+ if (!isIdentityZoom(value)) return value.replace(/!\s*important\s*$/i, "").trim();+ }+ return null;+}++/**+ * Does this selector's leftmost compound target the canvas itself — the+ * composition root, or an ancestor of it? A zoom on a DESCENDANT is ordinary+ * authoring and renders exactly as authored; only the canvas-level one+ * desynchronises painted content from the declared frame.+ */+function targetsCanvasRoot(+ selector: string,+ rootId: string | null,+ rootClasses: string[],+): boolean {+ const leftmost = selector.trim().split(/[\s>+~]+/)[0] ?? "";+ const bare = leftmost.toLowerCase();+ if (bare === "html" || bare === "body" || bare === ":root" || bare === "*") return true;+ if (rootId && leftmostCompoundId(selector) === rootId) return true;+ return leftmostCompoundClasses(selector).some((cls) => rootClasses.includes(cls));+}++/**+ * A rescaling canvas-level `zoom`, with where it was declared. Collected in+ * priority order — inline on the root, then <html>, then <body>, then+ * stylesheet rules — because the rule reports the FIRST one it finds.+ *+ * `truncateSnippet` returns undefined for an empty normalised input, and+ * `Finding.snippet` is optional for exactly that reason: an absent snippet is+ * absent, not "". This mirrors that contract rather than coercing it away.+ */+type CanvasZoomHit = { where: string; value: string; snippet: string | undefined };++function inlineCanvasZoomHits(rootTag: OpenTag, tags: OpenTag[]): CanvasZoomHit[] {+ const hits: CanvasZoomHit[] = [];+ const htmlTag = findHtmlTag(tags);+ const bodyTag = tags.find((tag) => tag.name.toLowerCase() === "body");+ for (const [label, tag] of [+ ["the root element's inline style", rootTag],+ ["<html>'s inline style", htmlTag],+ ["<body>'s inline style", bodyTag],+ ] as const) {+ if (!tag) continue;+ const inline = readAttr(tag.raw, "style");+ const value = inline ? firstRescalingZoom(inline) : null;+ if (value) hits.push({ where: label, value, snippet: truncateSnippet(tag.raw) });+ }+ return hits;+}++/**+ * The first selector in a comma list that targets the canvas, or null. Only the+ * first matters: the rest of the list describes the same declaration block, so+ * one hit per RULE is what the caller wants.+ */+function firstCanvasSelector(+ header: string,+ rootId: string | null,+ rootClasses: string[],+): string | null {+ for (const selector of header.split(",")) {+ const trimmed = selector.trim();+ if (trimmed && targetsCanvasRoot(trimmed, rootId, rootClasses)) return trimmed;+ }+ return null;+}++/** A canvas-level rescaling `zoom` declared by one CSS rule, or null. */+function canvasZoomInRule(+ header: string,+ body: string,+ rootId: string | null,+ rootClasses: string[],+): CanvasZoomHit | null {+ const trimmedHeader = header.trim();+ // An at-rule's "header" is `@media ...`, not a selector list.+ if (!trimmedHeader || trimmedHeader.startsWith("@")) return null;+ const value = firstRescalingZoom(body);+ if (!value) return null;+ const selector = firstCanvasSelector(trimmedHeader, rootId, rootClasses);+ if (!selector) return null;+ return {+ where: `\`${selector}\``,+ value,+ snippet: truncateSnippet(`${selector} { zoom: ${value} }`),+ };+}++function stylesheetCanvasZoomHits(+ styles: ExtractedBlock[],+ rootId: string | null,+ rootClasses: string[],+): CanvasZoomHit[] {+ const hits: CanvasZoomHit[] = [];+ for (const style of styles) {+ const ruleWithBody = /([^{}]+)\{([^{}]*)\}/g;+ let match: RegExpExecArray | null;+ while ((match = ruleWithBody.exec(stripCssComments(style.content))) !== null) {+ const hit = canvasZoomInRule(match[1] ?? "", match[2] ?? "", rootId, rootClasses);+ if (hit) hits.push(hit);+ }+ }+ return hits;+}++function canvasZoomHits(+ rootTag: OpenTag,+ tags: OpenTag[],+ styles: ExtractedBlock[],+ rootId: string | null,+ rootClasses: string[],+): CanvasZoomHit[] {+ return [+ ...inlineCanvasZoomHits(rootTag, tags),+ ...stylesheetCanvasZoomHits(styles, rootId, rootClasses),+ ];+}+/** Declared variable ids from an <html> tag's raw text; null when the JSON is unparseable. */function collectDeclaredVariableIds(htmlTagRaw: string): Set<string> | null {const declared = new Set<string>();@@ -1229,6 +1378,64 @@ export const compositionRules: Array<(ctx: LintContext) => HyperframeLintFinding},];},++ // root_zoom_rescales_a_fixed_canvas+ //+ // The capture frame is sized from the root's data-width/data-height, which are+ // LAYOUT pixels. CSS `zoom` on the canvas itself (the root, or html/body above+ // it) rescales the painted content inside that frame without changing the frame,+ // so the two disagree and the author gets neither of the two things they might+ // have meant.+ //+ // Measured at 0.8.71 on an 800x400 composition holding two 100x100 boxes at+ // left:100 and left:600, rendered to a PNG sequence and the boxes located by+ // colour:+ //+ // root zoom:0.5 canvas stays 800x400; boxes paint at (50,25) and (300,25),+ // each 50x50 -- the composition occupies the top-left quarter+ // and the rest of every frame is dead space.+ // root zoom:2 canvas stays 800x400; the left:100 box paints at (200,100)+ // at 200x200, and the left:600 box renders ZERO PIXELS --+ // scaled to x=1200, outside a frame that is still 800 wide.+ //+ // The second case is the one worth an error: content that is inside the declared+ // composition silently does not exist in the output, with nothing else in lint,+ // check or the render log naming it.+ //+ // Deliberately NOT flagged: `zoom` on a descendant. Measured in the same run --+ // a child at left:100 with zoom:2 paints at (200,100) at 200x200, which is+ // exactly what CSS `zoom` specifies. It is honoured, not ignored, so a rule that+ // fired on every `zoom` would be flagging correct authoring.+ ({ rootTag, tags, styles }) => {+ if (!rootTag) return [];+ const rootId = readAttr(rootTag.raw, "id");+ const rootClasses = (readAttr(rootTag.raw, "class") || "").split(/\s+/).filter(Boolean);+ const hits = canvasZoomHits(rootTag, tags, styles, rootId, rootClasses);++ // `noUncheckedIndexedAccess` makes hits[0] `T | undefined`, and a length+ // check does not narrow it — guard on the element itself.+ const hit = hits[0];+ if (!hit) return [];+ const enlarging =+ !hit.value.startsWith("-") && parseFloat(hit.value) > (hit.value.includes("%") ? 100 : 1);+ return [+ {+ code: "root_zoom_rescales_a_fixed_canvas",+ severity: "error",+ message:+ `\`zoom: ${hit.value}\` on ${hit.where} rescales the painted composition inside a frame that does not rescale with it. ` ++ `The capture frame is sized from the root's data-width/data-height in LAYOUT pixels, and \`zoom\` changes only what is painted inside it, so ` ++ (enlarging+ ? `content past the frame's edge renders zero pixels — it is inside the declared composition and absent from the output, with nothing else reporting it.`+ : `the composition paints into part of the frame and the remainder of every output frame is dead space.`),+ fixHint:+ `Author the composition at its real size — set data-width/data-height (and the root's width/height) to the dimensions you want — and drop the canvas-level \`zoom\`. ` ++ `To scale the OUTPUT without changing layout, pass \`--output-resolution\` to render, which supersamples. ` ++ `\`zoom\` on descendants is fine and is not flagged: it is honoured exactly as specified.`,+ snippet: hit.snippet,+ },+ ];+ },];const CLIP_MEDIA_TAGS = new Set<string>(["img", "video", "audio"]);