import { describe, expect, it } from "vitest"; import { parseHTML } from "linkedom"; import { injectPreviewVariables } from "./previewVariables"; const SCRIPT = ''; describe("injectPreviewVariables", () => { it.each([ [ "x", "", "x", ], [ "x", "", "x", ], ["x", "", "x"], [" \nx", " \n", "x"], ["fragment", "", "fragment"], ["xy", "", "xy"], ["", "x", "", "x"], ['x', '', "x"], ["x", "", "x"], ["x", "", "x"], [ "x", "", "x", ], ["x", "", "x"], ["x", "", "x"], ["x", "", "x"], ["x", "", "x"], ["x", "", "x"], ["x", "", "x"], ["t`, ], ])("puts one script in and leaves as written when %s", (_name, html) => { const before = parseHTML(html).document; const { document } = parseHTML( injectPreviewVariables(html, { x: '" onpointermove="1', y: "' onclick='1", z: "-->" }), ); const scripts = document.querySelectorAll("script[data-hf-preview-variables]"); expect(scripts).toHaveLength(1); expect(scripts[0]?.parentElement).toBe(document.head); expect(document.head.firstElementChild).toBe(scripts[0]); expect(document.documentElement.getAttributeNames()).toEqual( before.documentElement.getAttributeNames(), ); expect(document.documentElement.getAttribute("data-composition-variables")).toBe( before.documentElement.getAttribute("data-composition-variables"), ); }); it("escapes script-breaking input before inserting it", () => { expect(injectPreviewVariables("", { a: "" })).toBe( '', ); }); });