/** * @vitest-environment jsdom */ import { describe, it, expect } from "vitest"; import { generateHyperframesHtml, generateGsapTimelineScript, generateHyperframesStyles, } from "./hyperframes.js"; import { parseHtml } from "@hyperframes/parsers"; import { MEDIA_ELEMENT_LEAD_SECONDS } from "../runtime/mediaClock"; import { GSAP_CDN } from "../templates/constants.js"; import type { TimelineTextElement, TimelineMediaElement, TimelineCompositionElement, } from "../core.types"; function makeTextElement(overrides: Partial = {}): TimelineTextElement { return { id: "text-1", type: "text", name: "Title", content: "Hello World", startTime: 0, duration: 5, zIndex: 1, ...overrides, }; } function makeVideoElement(overrides: Partial = {}): TimelineMediaElement { return { id: "vid-1", type: "video", name: "Background", src: "video.mp4", startTime: 0, duration: 10, zIndex: 0, ...overrides, }; } describe("generateHyperframesHtml", () => { it("contains mixed-case style closing tags in authored CSS", () => { const styles = '.label::after { content: ""; }'; const doc = new DOMParser().parseFromString( generateHyperframesHtml([], 1, { styles, includeStyles: true }), "text/html", ); expect(doc.querySelectorAll("style")).toHaveLength(2); expect(doc.querySelector("script")).toBeNull(); expect(doc.querySelector("style[data-hf-custom]")?.textContent).toContain("StYlE"); }); it("contains script closing tags while retaining JS string values and raw expressions", () => { const targetSelector = '#x"'; const position = 'label"; bad(); //'; const doc = new DOMParser().parseFromString( generateHyperframesHtml([], 1, { includeScripts: true, animations: [ { targetSelector, method: "to", position, properties: { x: "__raw:1 < 2 ? 3 : 4" } }, ], }), "text/html", ); expect(doc.querySelectorAll("script")).toHaveLength(2); const script = doc.querySelector("script:not([src])")?.textContent ?? ""; const calls: unknown[][] = []; const gsap = { timeline: () => ({ to: (...args: unknown[]) => calls.push(args) }) }; new Function("gsap", script)(gsap); expect(calls).toEqual([[targetSelector, { x: 3 }, position]]); }); it("round-trips element attribute values without creating event handlers", () => { const marker = `x" onmouseover="bad()"<`; const element = makeVideoElement({ id: marker, name: marker, src: marker }); const doc = new DOMParser().parseFromString(generateHyperframesHtml([element], 1), "text/html"); const video = doc.querySelector("video"); for (const name of ["id", "data-hf-id", "data-name", "src"]) expect(video?.getAttribute(name)).toBe(marker); expect(video?.hasAttribute("onmouseover")).toBe(false); expect(doc.querySelector("script")).toBeNull(); }); it("keeps special IDs targeted by generated visibility animations", () => { const element = makeTextElement({ id: '9 title"[x],#other', name: "Title" }); const doc = new DOMParser().parseFromString(generateHyperframesHtml([element], 1), "text/html"); const targets: string[] = []; const gsap = { timeline: () => ({ set: (selector: string) => targets.push(selector) }) }; new Function("gsap", generateGsapTimelineScript([element], 1))(gsap); expect(targets.length).toBeGreaterThan(0); for (const selector of targets) expect(doc.querySelector(selector)?.id).toBe(element.id); }); it("preserves supported rich text while removing executable markup", () => { const content = 'AB
CD'; const html = generateHyperframesHtml([makeTextElement({ content })], 1); const doc = new DOMParser().parseFromString(html, "text/html"); expect(doc.querySelector("strong span")?.getAttribute("style")).toContain("font-size: 32px"); expect(doc.querySelector("strong span")?.hasAttribute("onclick")).toBe(false); expect(doc.querySelector("br")).not.toBeNull(); expect(doc.querySelector("script,svg,sup,[onclick]")).toBeNull(); expect(doc.querySelector("#text-1")?.textContent).toBe("ABCD"); expect(html).not.toContain("url(evil)"); expect(parseHtml(html).elements[0]).toMatchObject({ content: "ABCD" }); }); it.each(["", "&quot; &#39; &lt;"])( "preserves empty and entity-looking caption content: %s", (content) => { const html = generateHyperframesHtml([makeTextElement({ content })], 1); const doc = new DOMParser().parseFromString(html, "text/html"); expect(doc.querySelector("#text-1")?.textContent).toBe(content ? "" ' <" : ""); expect(parseHtml(html).elements[0]).toMatchObject({ content: content ? "" ' <" : "", }); }, ); it.each([ "javascript:bad()", "java\nscript:bad()", "vbscript:bad()", "data:text/html,", ])("rejects executable source URLs: %s", (src) => { expect(() => generateHyperframesHtml([makeVideoElement({ src })], 1)).toThrow( "Unsafe media or composition source URL", ); }); it("rejects JavaScript supplied through a numeric element field", () => { const element = { ...makeTextElement(), startTime: "0); bad(); //" }; expect(() => Reflect.apply(generateGsapTimelineScript, undefined, [[element], 1])).toThrow( "finite generator numeric value", ); }); it("rejects declaration breakouts in generated color values", () => { expect(() => generateHyperframesStyles( [makeTextElement({ color: "red; } body { color: blue" })], "landscape", ), ).toThrow("Invalid generated CSS value"); }); it("keeps composition identifiers inside their attribute and round-trips entities", () => { const compositionId = `x" autofocus onfocus="alert(1)'>"&`; const doc = new DOMParser().parseFromString( generateHyperframesHtml([], 1, { compositionId }), "text/html", ); expect(doc.documentElement.getAttribute("data-composition-id")).toBe(compositionId); expect(doc.documentElement.hasAttribute("autofocus")).toBe(false); expect(doc.documentElement.hasAttribute("onfocus")).toBe(false); expect(doc.querySelector("script")).toBeNull(); }); it("contains resolution values supplied by JavaScript callers inside their attribute", () => { const resolution = `x" autofocus onfocus="alert(1)'>"&`; const html = Reflect.apply(generateHyperframesHtml, undefined, [ [], 1, { resolution, includeStyles: false, includeScripts: false }, ]); const doc = new DOMParser().parseFromString(html, "text/html"); expect(doc.documentElement.getAttribute("data-resolution")).toBe(resolution); expect(doc.documentElement.hasAttribute("autofocus")).toBe(false); expect(doc.documentElement.hasAttribute("onfocus")).toBe(false); expect(doc.querySelector("script")).toBeNull(); }); it("round-trips entity-bearing CSS through the JSON metadata attribute", () => { const styles = `.x::after { content: "" ' & < > '"; }`; const doc = new DOMParser().parseFromString( generateHyperframesHtml([], 1, { styles }), "text/html", ); expect(JSON.parse(doc.documentElement.getAttribute("data-custom-styles")!)).toBe(styles); expect(doc.querySelector("style")).toBeNull(); expect(parseHtml(generateHyperframesHtml([], 1, { styles })).styles).toBe(styles); }); it("round-trips composition variable metadata through the public parser", () => { const variableValues = { label: `" ' & < > '`, count: 2, enabled: true }; const element: TimelineCompositionElement = { id: "nested", type: "composition", name: "Nested", startTime: 0, duration: 1, zIndex: 0, src: "nested.html", compositionId: "nested-comp", variableValues, }; const html = generateHyperframesHtml([element], 1); const parsed = parseHtml(html).elements[0]; expect(parsed?.type).toBe("composition"); if (parsed?.type !== "composition") throw new Error("Expected composition"); expect(parsed.variableValues).toEqual(variableValues); const doc = new DOMParser().parseFromString(html, "text/html"); expect(JSON.parse(doc.getElementById("nested")!.getAttribute("data-variable-values")!)).toEqual( variableValues, ); }); it("generates valid HTML with proper data attributes", () => { const elements = [makeTextElement()]; const html = generateHyperframesHtml(elements, 5); expect(html).toContain(""); expect(html).toContain("data-composition-id="); expect(html).toContain("data-composition-duration="); expect(html).toContain('id="stage"'); expect(html).toContain('id="stage-zoom-container"'); }); it("includes element data attributes", () => { const elements = [makeTextElement({ id: "my-text", startTime: 2, duration: 3 })]; const html = generateHyperframesHtml(elements, 5); expect(html).toContain('id="my-text"'); expect(html).toContain('data-start="2"'); expect(html).toContain('data-duration="3"'); expect(html).toContain('data-track-index="1"'); expect(html).not.toContain('data-end="'); expect(html).not.toContain('data-layer="'); }); it("includes GSAP CDN script tag when includeScripts is true", () => { const elements = [makeTextElement()]; const html = generateHyperframesHtml(elements, 5, { includeScripts: true }); expect(html).toContain(``); }); it("does NOT include GSAP CDN by default", () => { const elements = [makeTextElement()]; const html = generateHyperframesHtml(elements, 5); expect(html).not.toContain(GSAP_CDN); }); it("includes timeline script when includeScripts is true", () => { const elements = [makeTextElement()]; const html = generateHyperframesHtml(elements, 5, { includeScripts: true }); expect(html).toContain("gsap.timeline({ paused: true })"); }); it("generates GSAP timeline with visibility animations when includeScripts is true", () => { const elements = [makeTextElement({ id: "el-1", startTime: 1, duration: 3 })]; const html = generateHyperframesHtml(elements, 5, { includeScripts: true }); // Default animations include visibility bookends expect(html).toContain('tl.set("#el-1"'); expect(html).toContain('visibility: "hidden"'); expect(html).toContain('visibility: "visible"'); }); it("sets resolution data attribute", () => { const elements = [makeTextElement()]; const landscapeHtml = generateHyperframesHtml(elements, 5, { resolution: "landscape" }); expect(landscapeHtml).toContain('data-resolution="landscape"'); const portraitHtml = generateHyperframesHtml(elements, 5, { resolution: "portrait" }); expect(portraitHtml).toContain('data-resolution="portrait"'); }); it("generates video elements with proper tags", () => { const elements = [makeVideoElement()]; const html = generateHyperframesHtml(elements, 10); expect(html).toContain(" { const elements = [makeTextElement({ content: "My Content" })]; const html = generateHyperframesHtml(elements, 5); expect(html).toContain("
My Content
"); }); it("includes custom compositionId", () => { const elements = [makeTextElement()]; const html = generateHyperframesHtml(elements, 5, { compositionId: "test-comp-123" }); expect(html).toContain('data-composition-id="test-comp-123"'); }); it("calculates total duration from elements if they exceed provided duration", () => { const elements = [makeTextElement({ startTime: 0, duration: 15 })]; const html = generateHyperframesHtml(elements, 5); expect(html).toContain('data-composition-duration="15"'); }); it("includes style tags when includeStyles is true", () => { const elements = [makeTextElement()]; const html = generateHyperframesHtml(elements, 5, { includeStyles: true }); expect(html).toContain('