Skip to content

Write HTML. Render video. Built for agents.

fix(sdk): preserve fragments with inert document markers (#5219)

Abdullah M. Nazifea033bcd5a58
diff --git a/packages/parsers/src/hfIds.test.ts b/packages/parsers/src/hfIds.test.ts
index aaa1b47601..d5a4463bb8 100644
--- a/packages/parsers/src/hfIds.test.ts
+++ b/packages/parsers/src/hfIds.test.ts
@@ -18,6 +18,31 @@ function idOf(html: string, selector: string): string | null {
const doc = (body: string) => `<!doctype html><html><body>${body}</body></html>`;
+describe("ensureHfIds fragment detection", () => {
+ it.each([
+ `<!-- Example: <html lang="en"> --><div id="stage"><p id="title">Hello</p></div>`,
+ `<!-- Example: <!doctype html> --><div id="stage"><p id="title">Hello</p></div>`,
+ `<div id="stage" title="<html>"><p id="title">Hello</p></div>`,
+ `<script>const example = "<!doctype html>";</script><div id="stage"><p id="title">Hello</p></div>`,
+ ])("stamps fragments containing document markers in inert text: %s", (html) => {
+ const out = ensureHfIds(html);
+ expect(ids(out)).toHaveLength(2);
+ expect(idOf(out, "#title")).toBe(
+ idOf(ensureHfIds(`<div id="stage"><p id="title">Hello</p></div>`), "#title"),
+ );
+ expect(out).not.toContain("<head>");
+ expect(out).not.toContain("<body>");
+ expect(ensureHfIds(out)).toBe(out);
+ });
+
+ it("preserves a full document following leading comments", () => {
+ const out = ensureHfIds(`<!-- note --><html><body><p id="title">Hello</p></body></html>`);
+ expect(out).toContain("<html>");
+ expect(idOf(out, "#title")).toMatch(/^hf-[a-z0-9]{4}$/);
+ expect(ensureHfIds(out)).toBe(out);
+ });
+});
+
describe("ensureHfIds", () => {
it("ignores HTML attribute case and editor state while retaining pinned IDs", () => {
const lower = doc(`<div id="x" data-start="2" data-hf-state="a">hello</div>`);
diff --git a/packages/parsers/src/hfIds.ts b/packages/parsers/src/hfIds.ts
index 03e88b67c3..0c81789c24 100644
--- a/packages/parsers/src/hfIds.ts
+++ b/packages/parsers/src/hfIds.ts
@@ -19,7 +19,9 @@ export {
export function ensureHfIds(html: string): string {
// Wrap fragments so every body element participates in stable ID assignment.
- const wrapped = !/<!doctype|<html[\s>]/i.test(html);
+ const wrapped = !/^(?:<!doctype|<html[\s>/])/i.test(
+ html.replace(/^(?:\s|<!--(?:>|->|[\s\S]*?-->))*/, ""),
+ );
const { document } = wrapped
? parseHTML(`<!DOCTYPE html><html><head></head><body>${html}</body></html>`)
: parseHTML(html);
diff --git a/packages/sdk/src/document.ts b/packages/sdk/src/document.ts
index 01cb4e6b40..21a924300f 100644
--- a/packages/sdk/src/document.ts
+++ b/packages/sdk/src/document.ts
@@ -8,7 +8,7 @@
* HyperFramesElement is the editing view (ALL editable elements, with raw attrs).
*/
-import { parseHTML } from "linkedom";
+import { parseHTMLContent } from "@hyperframes/core/compiler/html-document";
import { ensureHfIds, isCompositionTemplate } from "@hyperframes/parsers/hf-ids";
import { parseGsapScriptAcornForWrite } from "@hyperframes/core/gsap-parser-acorn";
import {
@@ -261,11 +261,7 @@ export function buildRoots(document: Document): HyperFramesElement[] {
export function buildDocument(html: string): SdkDocument {
const stamped = ensureHfIds(html);
- const hasShell = /<!doctype|<html[\s>]/i.test(stamped);
- const wrapped = !hasShell;
- const { document } = wrapped
- ? parseHTML(`<!DOCTYPE html><html><head></head><body>${stamped}</body></html>`)
- : parseHTML(stamped);
+ const document = parseHTMLContent(stamped);
const dims = extractDimensions(document);
diff --git a/packages/sdk/src/engine/model.ts b/packages/sdk/src/engine/model.ts
index 16515de1db..2910dd9fdb 100644
--- a/packages/sdk/src/engine/model.ts
+++ b/packages/sdk/src/engine/model.ts
@@ -5,7 +5,7 @@
* go here. serialize() walks the live DOM; no separate mutable tree to sync.
*/
-import { parseHTML } from "linkedom";
+import { isFullHtmlDocument, parseHTMLContent } from "@hyperframes/core/compiler/html-document";
import { findTimelineScript } from "@hyperframes/core/gsap-parser-acorn";
import { findVariableDeclaration } from "./variableModel.js";
import {
@@ -24,12 +24,9 @@ export interface ParsedDocument {
export function parseMutable(html: string): ParsedDocument {
const stamped = ensureHfIds(html);
- const hasShell = /<!doctype|<html[\s>]/i.test(stamped);
- const wrapped = !hasShell;
- const { document } = wrapped
- ? parseHTML(`<!DOCTYPE html><html><head></head><body>${stamped}</body></html>`)
- : parseHTML(stamped);
- return { document: document as unknown as Document, wrapped, stamped };
+ const wrapped = !isFullHtmlDocument(stamped);
+ const document = parseHTMLContent(stamped);
+ return { document, wrapped, stamped };
}
// ─── Element lookup ───────────────────────────────────────────────────────────
diff --git a/packages/sdk/src/smoke.test.ts b/packages/sdk/src/smoke.test.ts
index 6b2a0e6837..0586fc9b62 100644
--- a/packages/sdk/src/smoke.test.ts
+++ b/packages/sdk/src/smoke.test.ts
@@ -17,6 +17,7 @@ import {
findById,
escapeHfId,
readVariableDefault,
+ buildDocument,
} from "./index.js";
import { createMemoryAdapter } from "./adapters/memory.js";
@@ -41,6 +42,28 @@ const BASE_HTML = `
// ─── init → mutate → serialize ────────────────────────────────────────────────
+describe("fragment document detection", () => {
+ it.each([
+ `<!-- Example: <html> --><div data-hf-root><p data-hf-id="hf-title">Hello</p></div>`,
+ `<!-- Example: <!doctype html> --><div data-hf-root><p data-hf-id="hf-title">Hello</p></div>`,
+ `<div data-hf-root title="<html>"><p data-hf-id="hf-title">Hello</p></div>`,
+ ])("models and edits a fragment containing inert document markers: %s", async (html) => {
+ const snapshot = buildDocument(html);
+ expect(snapshot.roots).toHaveLength(1);
+ expect(snapshot.roots[0]?.children[0]?.id).toBe("hf-title");
+ const comp = await openComposition(html);
+ expect(comp.getElement("hf-title")?.text).toBe("Hello");
+ comp.setText("hf-title", "Edited");
+ const serialized = comp.serialize();
+ expect(serialized).toContain("Edited");
+ expect(serialized).not.toContain("<body>");
+ const reopened = await openComposition(serialized);
+ expect(reopened.getElement("hf-title")?.text).toBe("Edited");
+ comp.dispose();
+ reopened.dispose();
+ });
+});
+
describe("openComposition + basic mutations", () => {
it("opens without error and exposes element snapshots", async () => {
const comp = await openComposition(BASE_HTML);