diff --git a/packages/cli/src/commands/snapshot.sceneSvgIds.browser.test.ts b/packages/cli/src/commands/snapshot.sceneSvgIds.browser.test.ts new file mode 100644 index 0000000000..9fb991d89d --- /dev/null +++ b/packages/cli/src/commands/snapshot.sceneSvgIds.browser.test.ts @@ -0,0 +1,79 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import sharp from "sharp"; +import { afterAll, expect, it, vi } from "vitest"; +import snapshotCommand from "./snapshot.js"; + +const dir = mkdtempSync(join(tmpdir(), "hf-snapshot-scene-svg-ids-")); + +afterAll(() => rmSync(dir, { recursive: true, force: true })); + +// Two scenes drawn with the same SVG ids; the corner piece fills from a gradient both share at page level. +const portrait = ( + color: string, +) => ` + + + + + + +`; +writeFileSync( + join(dir, "index.html"), + ` + +
+
${portrait("#f00")}
+
${portrait("#00f")}
+
+`, +); + +const colourAt = async (path: string, x: number, y: number) => { + const { data, info } = await sharp(path).raw().toBuffer({ resolveWithObject: true }); + const at = (y * info.width + x) * info.channels; + const [r, g, b] = [data[at]!, data[at + 1]!, data[at + 2]!]; + return r > 200 ? "red" : b > 200 ? "blue" : g > 200 ? "green" : "none"; +}; + +// fails without the fix: at 14 s the second scene's face painted nothing and its corner showed the first's red. +it("shows each scene's own SVG portrait when two scenes reuse the same ids", async () => { + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); + vi.spyOn(console, "error").mockImplementation(() => undefined); + vi.spyOn(console, "warn").mockImplementation(() => undefined); + try { + await snapshotCommand.run?.({ + args: { + dir, + at: "4,14", + end: false, + json: true, + output: join(dir, "out"), + describe: "false", + "browser-gpu": false, + }, + } as never); + const { frames } = JSON.parse(String(log.mock.calls.at(-1)?.[0])) as { + frames: { path: string; time: number }[]; + }; + const seen = await Promise.all( + frames.map(async ({ path, time }) => [ + time, + await colourAt(path, 80, 45), + await colourAt(path, 10, 5), + await colourAt(path, 150, 80), + ]), + ); + expect(seen).toEqual([ + [4, "red", "red", "green"], + [14, "blue", "blue", "green"], + ]); + } finally { + vi.restoreAllMocks(); + } +}, 120_000); diff --git a/packages/core/src/compiler/inlineSubCompositions.test.ts b/packages/core/src/compiler/inlineSubCompositions.test.ts index fe42eac821..56ea9ed157 100644 --- a/packages/core/src/compiler/inlineSubCompositions.test.ts +++ b/packages/core/src/compiler/inlineSubCompositions.test.ts @@ -949,6 +949,58 @@ describe("inlineSubCompositions – #3490 nested SVG id collisions", () => { }); }); +describe("inlineSubCompositions – SVG ids in top-level scene clips", () => { + const portrait = (color: string) => + `` + + `` + + ``; + const compile = (clips: string) => { + const { document } = parseHTML(` + +
${clips}
`); + inlineSubCompositions(document, [], { + resolveHtml: () => null, + parseHtml: (html) => parseHTML(html).document, + hostIdentityMap: assignTestRuntimeCompositionIds([]), + }); + return document; + }; + // The first element with an id is what the browser resolves `url(#id)` and `href="#id"` to. + const resolved = (document: Document, value: string | null) => + document.getElementById(value!.replace(/^url\(#|\)$|^#/g, "")); + + // fails without the fix: the second scene's gradient and resolved to the first scene's portrait. + it("gives each scene clip its own portrait when two scenes reuse the same SVG ids", () => { + const document = compile( + `
${portrait("red")}
` + + `
${portrait("blue")}
`, + ); + const [first, second] = [...document.querySelectorAll(".scene")]; + for (const scene of [first!, second!]) { + expect( + scene.contains(resolved(document, scene.querySelector(".face")!.getAttribute("fill"))), + ).toBe(true); + expect( + scene.contains(resolved(document, scene.querySelector(".piece")!.getAttribute("href"))), + ).toBe(true); + } + expect(first!.querySelector("linearGradient")!.id).toBe("skin"); + expect(first!.querySelector("symbol")!.id).toBe("portrait"); + }); + + it("leaves a page-level definition the scenes share where it is", () => { + const document = compile( + `
${portrait("red")}
` + + `
${portrait("blue")}
`, + ); + const shared = document.getElementById("shared")!; + expect(shared.closest(".scene")).toBeNull(); + for (const face of document.querySelectorAll(".face")) { + expect(face.getAttribute("clip-path")).toBe("url(#shared)"); + } + }); +}); + describe("inlineSubCompositions – renamed SVG ids stay reachable from author scripts", () => { // These tests execute the compiled, scoped scripts inside a real jsdom // window built from the assembled document — the same thing the browser diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index 74ab26555a..c4f591e156 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -35,6 +35,7 @@ import { } from "./compositionScoping"; import { namespaceCollidingSvgIds, + sceneClipRoots, rewriteSvgIdReferencesInCss, type SvgIdScope, } from "./svgIdNamespacing"; @@ -778,6 +779,8 @@ export function inlineSubCompositions( // `