diff --git a/packages/core/src/compiler/htmlDocument.test.ts b/packages/core/src/compiler/htmlDocument.test.ts index bbaacc22af..ca2f62e01a 100644 --- a/packages/core/src/compiler/htmlDocument.test.ts +++ b/packages/core/src/compiler/htmlDocument.test.ts @@ -6,6 +6,7 @@ import { findStartTags, injectScriptsAtHeadStart, injectScriptsIntoHtml, + headContentStart, injectTagsAtHeadStart, insertBeforeCloseTag, insertRuntimeTag, @@ -448,6 +449,45 @@ describe("injectTagsAtHeadStart on long adversarial input", () => { }); }); +describe("headContentStart", () => { + it.each([ + [ + "x", + "", + ], + ["x", ""], + [" \n
x", " \n"], + ["
x", ""], + ["textx", ""], + [ + "\" b='' c=x", + "\" b='' c=", + ], + [ + "x", + "", + ], + ["x", ""], + ["".repeat(20_000) + ""; + expect(headContentStart(prolog + "x")).toBe(prolog.length + 6); + expect(headContentStart("".repeat(20_000) + "")).toBe(11 * 20_000 + 6); + expect(headContentStart("")).toBe(0); + }); +}); + describe("hasCompositionOutsideTemplates", () => { it.each([ ["a root in ", '
', true], diff --git a/packages/core/src/compiler/htmlDocument.ts b/packages/core/src/compiler/htmlDocument.ts index da42a2a56a..0e7114cf02 100644 --- a/packages/core/src/compiler/htmlDocument.ts +++ b/packages/core/src/compiler/htmlDocument.ts @@ -431,6 +431,44 @@ export function injectTagsAtHeadStart(html: string, tags: string): string { return insertBeforeDocumentTag(html, "", at); + return close === -1 ? -1 : close + 1; +} + export function injectScriptsAtHeadStart(html: string, scripts: readonly string[]): string { if (scripts.length === 0) return html; return injectTagsAtHeadStart(html, inlineScriptTags(scripts)); diff --git a/packages/studio-server/src/helpers/previewVariables.test.ts b/packages/studio-server/src/helpers/previewVariables.test.ts index 95595a1154..7c2a0e5946 100644 --- a/packages/studio-server/src/helpers/previewVariables.test.ts +++ b/packages/studio-server/src/helpers/previewVariables.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { parseHTML } from "linkedom"; import { injectPreviewVariables } from "./previewVariables"; const SCRIPT = ''; @@ -19,10 +20,32 @@ describe("injectPreviewVariables", () => { [" \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"], + ["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"), ); }); diff --git a/packages/studio-server/src/helpers/previewVariables.ts b/packages/studio-server/src/helpers/previewVariables.ts index 79574e09b0..be7ab28edf 100644 --- a/packages/studio-server/src/helpers/previewVariables.ts +++ b/packages/studio-server/src/helpers/previewVariables.ts @@ -1,3 +1,5 @@ +import { headContentStart } from "@hyperframes/core/compiler/html-document"; + /** * Inject preview variable overrides: `?variables=` becomes * `window.__hfVariables` set before any composition script runs — the exact @@ -11,18 +13,9 @@ export function injectPreviewVariables(html: string, values: Recordwindow.__hfVariables=${json};`; - // Insert as early as possible without ever landing before the doctype — - // content before flips the document into quirks mode, so the - // fallback chain is → → after the doctype → prepend. - for (const pattern of [/, no later prefix can close either. - const end = html.indexOf(">", match.index + match[0].length); - if (end < 0) continue; - const at = end + 1; - return html.slice(0, at) + tag + html.slice(at); - } - } - return tag + html; + // Placed where a browser's tokenizer starts the head, never at the text "` previewed with + // `?variables={"a":"\" b=\""}` must give one script in and leave 's attributes alone. + const at = headContentStart(html); + return html.slice(0, at) + tag + html.slice(at); } diff --git a/packages/studio-server/src/routes/preview.test.ts b/packages/studio-server/src/routes/preview.test.ts index 76369530a6..65a3bf69c3 100644 --- a/packages/studio-server/src/routes/preview.test.ts +++ b/packages/studio-server/src/routes/preview.test.ts @@ -1627,6 +1627,34 @@ describe("preview ?variables= injection", () => { expect(injected?.[1]).not.toContain(""); }); + it("serves every head script inside when an attribute of holds text", async () => { + const projectDir = createProjectDir(); + const app = new Hono(); + registerPreviewRoutes(app, createAdapter(projectDir)); + const label = '[{"label":"Page and
text"}]'; + writeFileSync( + join(projectDir, "index.html"), + `t
`, + ); + + const values = { title: '" b="1', other: "' c='1" }; + const res = await app.request( + `http://localhost/projects/demo/preview?variables=${encodeURIComponent(JSON.stringify(values))}`, + ); + const { document } = parseHTML(await res.text()); + const root = document.documentElement; + expect(root.getAttributeNames().filter((name) => name !== "style")).toEqual([ + "lang", + "data-composition-variables", + ]); + expect(root.getAttribute("data-composition-variables")).toBe(label); + for (const attr of ["data-hf-preview-variables", "data-hf-gsap-fallback"]) { + const scripts = document.querySelectorAll(`script[${attr}]`); + expect(scripts, attr).toHaveLength(1); + expect(scripts[0]?.parentElement, attr).toBe(document.head); + } + }); + it("returns 400 for invalid JSON and non-object payloads", async () => { const projectDir = createProjectDir(); const app = new Hono(); diff --git a/packages/studio-server/src/routes/preview.ts b/packages/studio-server/src/routes/preview.ts index e0efabc443..b4d030aad8 100644 --- a/packages/studio-server/src/routes/preview.ts +++ b/packages/studio-server/src/routes/preview.ts @@ -42,7 +42,11 @@ import { normalizeGsapBaseUrl, normalizeGsapLocalRoot, } from "@hyperframes/core/gsap-cdn"; -import { findStartTags, injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document"; +import { + findStartTags, + headContentStart, + injectTagsAtHeadStart, +} from "@hyperframes/core/compiler/html-document"; import { hasAuthoredCode } from "../helpers/authoredCode.js"; import { THUMBNAIL_NO_AUTHORED_CODE_META } from "../helpers/screenshotClip.js"; import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution"; @@ -445,9 +449,8 @@ function gsapCdnFallbackScript(baseUrl: string): string { function injectGsapCdnFallback(html: string, baseUrl: string): string { if (html.includes(GSAP_FALLBACK_ATTR)) return html; - if (html.includes("")) - return html.replace("", "" + gsapCdnFallbackScript(baseUrl)); - return gsapCdnFallbackScript(baseUrl) + html; + const at = headContentStart(html); + return html.slice(0, at) + gsapCdnFallbackScript(baseUrl) + html.slice(at); } /**