Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/core/src/compiler/inlineSubCompositions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ function makeHostDocument(compId: string) {
return document;
}

function makeAnonymousHostDocument() {
const { document } = parseHTML(`<!DOCTYPE html>
<html><body>
<div data-composition-id="main">
<div data-composition-src="intro.html" data-start="0" data-duration="4"></div>
</div>
</body></html>`);
return document;
}

describe("inlineSubCompositions – #ID selector scoping divergence", () => {
it("throws an actionable error when a resolved sub-composition file is empty", () => {
const document = makeHostDocument("intro");
Expand Down Expand Up @@ -145,6 +155,31 @@ describe("inlineSubCompositions – #ID selector scoping divergence", () => {
expect(scopedCss).toContain('[data-hf-authored-id="intro"]');
});

it("preserves the inferred composition boundary for anonymous hosts", () => {
const document = makeAnonymousHostDocument();
const host = document.querySelector('[data-composition-src="intro.html"]')!;

function flattenInnerRoot(innerRoot: Element): Element {
const clone = innerRoot.cloneNode(true) as Element;
clone.removeAttribute("id");
clone.removeAttribute("data-composition-id");
clone.setAttribute("data-hf-inner-root", "true");
return clone;
}

inlineSubCompositions(document, [host], {
resolveHtml: () => SUB_COMP_HTML,
parseHtml: (html) => parseHTML(html).document,
flattenInnerRoot,
});

expect(host.getAttribute("data-composition-id")).toBeNull();
const inferredRoot = host.querySelector('[data-composition-id="intro"]');
expect(inferredRoot?.getAttribute("data-hf-inner-root")).toBe("true");
expect(inferredRoot?.getAttribute("id")).toBeNull();
expect(inferredRoot?.querySelector(".title")?.textContent).toBe("HELLO WORLD");
});

it("extracts <link> elements from sub-composition <head> with original rel and crossorigin", () => {
const subCompWithLinks = `<!doctype html>
<html><head>
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/compiler/inlineSubCompositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ export function inlineSubCompositions(
for (const child of [...innerRoot.querySelectorAll("style, script")]) child.remove();
if (flattenInnerRoot) {
const prepared = flattenInnerRoot(innerRoot);
if (!compId && inferredCompId) {
// Anonymous hosts have no outer composition id, so keep the inferred
// boundary on the preserved inner root after flattenInnerRoot strips it.
prepared.setAttribute("data-composition-id", inferredCompId);
}
hostEl.innerHTML = prepared.outerHTML || "";
} else {
hostEl.innerHTML = compId ? innerRoot.innerHTML || "" : innerRoot.outerHTML || "";
Expand Down
28 changes: 18 additions & 10 deletions packages/producer/src/services/htmlCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,15 @@ describe("template-wrapped sub-composition media offsets", () => {
join(compositionsDir, "scene.html"),
`<template id="scene-template">
<div
id="scene-root"
class="scene-wrapper"
data-composition-id="scene"
data-start="0"
data-width="640"
data-height="360"
data-duration="4"
>
<style>.title { opacity: 0; }</style>
<style>.scene-wrapper .title { opacity: 0; }</style>
<h1 class="title">Scene</h1>
<video
id="scene-video"
Expand Down Expand Up @@ -718,7 +720,7 @@ describe("template-wrapped sub-composition media offsets", () => {
);
});

it("flattens the sub-composition root onto the host in compiled render HTML", async () => {
it("preserves the sub-composition root wrapper in compiled render HTML", async () => {
const { projectDir, indexPath } = writeTemplateWrappedProject(
'data-start="20" data-duration="6" data-width="640" data-height="360"',
'data-start="1.5" data-duration="4"',
Expand All @@ -728,17 +730,19 @@ describe("template-wrapped sub-composition media offsets", () => {

const { document } = parseHTML(compiled.html);
const host = document.querySelector("#scene-host");
const innerRoot = host?.querySelector('[data-hf-inner-root="true"]');

expect(host?.getAttribute("data-composition-id")).toBe("scene");
expect(host?.getAttribute("data-start")).toBe("20");
expect(host?.getAttribute("data-width")).toBe("640");
expect(host?.querySelector(".title")?.textContent).toBe("Scene");
expect(
Array.from(host?.children ?? []).some(
(child) => child.getAttribute("data-composition-id") === "scene",
),
).toBe(false);
expect(compiled.html).toContain('[data-composition-id="scene"] .title');
expect(innerRoot?.classList.contains("scene-wrapper")).toBe(true);
expect(innerRoot?.getAttribute("data-hf-authored-id")).toBe("scene-root");
expect(innerRoot?.getAttribute("id")).toBeNull();
expect(innerRoot?.getAttribute("data-composition-id")).toBeNull();
expect(innerRoot?.getAttribute("data-start")).toBeNull();
expect(innerRoot?.getAttribute("data-duration")).toBeNull();
expect(compiled.html).toContain('[data-composition-id="scene"] .scene-wrapper .title');
expect(compiled.html).toContain("new Proxy(window.document");
expect(compiled.html).toContain("__hfNormalizeSelector");
});
Expand All @@ -761,7 +765,7 @@ describe("template-wrapped sub-composition media offsets", () => {
writeFileSync(
join(compositionsDir, "scene.html"),
`<template id="scene-template">
<div data-composition-id="scene" data-width="640" data-height="360" data-duration="4">
<div id="scene-root" data-composition-id="scene" data-width="640" data-height="360" data-duration="4">
<style>.title { opacity: 0; }</style>
<h1 class="title">Scene</h1>
<script>
Expand All @@ -777,7 +781,11 @@ describe("template-wrapped sub-composition media offsets", () => {
const host = document.querySelector("#scene-host");

expect(host?.getAttribute("data-composition-id")).toBeNull();
expect(host?.querySelector('[data-composition-id="scene"] .title')?.textContent).toBe("Scene");
const innerRoot = host?.querySelector('[data-composition-id="scene"]');
expect(innerRoot?.getAttribute("data-hf-inner-root")).toBe("true");
expect(innerRoot?.getAttribute("id")).toBeNull();
expect(innerRoot?.getAttribute("data-duration")).toBeNull();
expect(innerRoot?.querySelector(".title")?.textContent).toBe("Scene");
expect(compiled.html).toContain('var __hfCompId = "scene";');
});
});
Expand Down
19 changes: 5 additions & 14 deletions packages/producer/src/services/htmlCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
type ResolvedDuration,
type UnresolvedElement,
} from "@hyperframes/core";
import { inlineSubCompositions as inlineSubCompositionsShared } from "@hyperframes/core/compiler";
import {
inlineSubCompositions as inlineSubCompositionsShared,
prepareFlattenedInnerRoot,
} from "@hyperframes/core/compiler";
import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js";
import { isPathInside, toExternalAssetKey } from "../utils/paths.js";
import {
Expand Down Expand Up @@ -603,22 +606,10 @@ function inlineSubCompositions(
},
parseHtml: (htmlStr: string) => parseHTML(htmlStr).document as unknown as Document,
scriptErrorLabel: "[Compiler] Composition script failed",
compoundAuthoredRoot: true,
flattenInnerRoot: prepareFlattenedInnerRoot,
},
);

// Set data-hf-authored-id on host elements so the scoped script proxy
// can rewrite #id selectors (e.g. #us-map → [data-hf-authored-id="us-map"]).
// Unlike flattenInnerRoot (which changes DOM structure and breaks baselines),
// this preserves the existing innerHTML-based inlining while enabling the
// authored-id selector contract.
for (const hostEl of hosts) {
const compId = hostEl.getAttribute("data-composition-id");
if (compId && !hostEl.getAttribute("data-hf-authored-id")) {
hostEl.setAttribute("data-hf-authored-id", compId);
}
}

// Producer-specific: set explicit pixel dimensions on host elements so
// children using width/height: 100% resolve correctly. The runtime does
// this automatically but compiled HTML needs it inline.
Expand Down
Loading