fix(core): restore composition id on the wrapper for anonymous hosts

flattenInnerRoot strips data-composition-id from the flattened wrapper,
assuming the host already carries the composition's identity. That's true
for hosts authored with their own data-composition-id, but not for a host
mounted via data-composition-src with no id of its own (an "anonymous"
host) — a pattern the producer already had a dedicated regression test for
(missing-host-comp-id) and correctly supported before flattenInnerRoot was
wired in, via a different code path that preserved the whole composition
element as-is.

Once nothing in the render DOM carries the composition's own id, its root-
styling CSS (`[data-composition-id="X"] { ... }`) and any script that
self-references it (e.g. `document.querySelector('[data-composition-id="X"]')`)
both silently stop resolving. Restore the id onto the wrapper specifically
when the host has none of its own, matching what the pre-flatten producer
path did and what preview visually expects.
This commit is contained in:
Miguel Angel Simon Sierra
2026-07-02 13:56:56 -07:00
parent 4da032281e
commit 86d3693401
3 changed files with 62 additions and 5 deletions
@@ -175,6 +175,52 @@ describe("inlineSubCompositions – #ID selector scoping divergence", () => {
expect(scopedCss).toContain('[data-hf-authored-id="intro"]');
});
it("with flattenInnerRoot: restores data-composition-id on the wrapper for an anonymous host", () => {
// Regression test: a host mounted via data-composition-src with no
// data-composition-id of its own (an "anonymous" host). The composition
// styles its own root box via the bare composition-id selector and a
// script self-references it too — both need something in the render DOM
// to actually carry that id once flattenInnerRoot strips it from the
// wrapper by default.
const { document } = parseHTML(`<!DOCTYPE html>
<html><body>
<div data-composition-id="main">
<div data-composition-src="scoped-text.html" data-start="0" data-duration="3"></div>
</div>
</body></html>`);
const host = document.querySelector('[data-composition-src="scoped-text.html"]')!;
const scopedTextHtml = `<template id="scoped-text-template">
<div data-composition-id="scoped-text" data-width="1080" data-height="1920" data-duration="3">
<div class="label">Scoped Text Should Stay Styled</div>
<style>
[data-composition-id="scoped-text"] { display: flex; background: rgb(12, 12, 12); }
</style>
</div>
</template>`;
function flattenInnerRoot(innerRoot: Element): Element {
const clone = innerRoot.cloneNode(true) as Element;
clone.removeAttribute("data-composition-id");
clone.removeAttribute("data-start");
clone.removeAttribute("data-duration");
clone.setAttribute("data-hf-inner-root", "true");
return clone;
}
const result = inlineSubCompositions(document, [host], {
resolveHtml: () => scopedTextHtml,
parseHtml: (html) => parseHTML(html).document,
flattenInnerRoot,
});
const wrapper = host.querySelector("[data-hf-inner-root]");
expect(wrapper?.getAttribute("data-composition-id")).toBe("scoped-text");
const scopedCss = result.styles.join("\n");
expect(scopedCss).toContain("display: flex");
});
it("extracts <link> elements from sub-composition <head> with original rel and crossorigin", () => {
const subCompWithLinks = `<!doctype html>
<html><head>
@@ -372,6 +372,16 @@ export function inlineSubCompositions(
for (const child of [...innerRoot.querySelectorAll("style, script")]) child.remove();
if (flattenInnerRoot) {
const prepared = flattenInnerRoot(innerRoot);
if (!compId && inferredCompId) {
// Anonymous host: flattenInnerRoot strips data-composition-id,
// assuming the host already carries the composition's identity.
// When the host has none, nothing in the render DOM matches the
// composition's own root-styling CSS or self-referencing scripts
// (e.g. document.querySelector('[data-composition-id="X"]')).
// Restore it on the wrapper so both keep resolving, same as
// before flattening preserved it via outerHTML.
prepared.setAttribute("data-composition-id", inferredCompId);
}
hostEl.innerHTML = prepared.outerHTML || "";
} else {
hostEl.innerHTML = compId ? innerRoot.innerHTML || "" : innerRoot.outerHTML || "";
@@ -903,11 +903,12 @@ describe("template-wrapped sub-composition media offsets", () => {
const host = document.querySelector("#scene-host");
expect(host?.getAttribute("data-composition-id")).toBeNull();
// The flattened inner root strips data-composition-id (same as the bundler's
// prepareFlattenedInnerRoot), so an anonymous host's content is only reachable
// via the preserved wrapper, not a [data-composition-id="scene"] selector —
// this matches preview parity, not a producer-specific contract.
expect(host?.querySelector("[data-hf-inner-root] .title")?.textContent).toBe("Scene");
// The host has no data-composition-id of its own, but the composition's
// own id is restored onto the flattened wrapper, so root-scoped
// selectors and self-referencing scripts still resolve.
const wrapper = host?.querySelector("[data-hf-inner-root]");
expect(wrapper?.getAttribute("data-composition-id")).toBe("scene");
expect(wrapper?.querySelector(".title")?.textContent).toBe("Scene");
expect(compiled.html).toContain('var __hfCompId = "scene";');
});
});