diff --git a/packages/core/src/compiler/inlineSubCompositions.test.ts b/packages/core/src/compiler/inlineSubCompositions.test.ts index bf52cffc4e..c20a3c750e 100644 --- a/packages/core/src/compiler/inlineSubCompositions.test.ts +++ b/packages/core/src/compiler/inlineSubCompositions.test.ts @@ -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(` + +
+
+
+`); + const host = document.querySelector('[data-composition-src="scoped-text.html"]')!; + + const scopedTextHtml = ``; + + 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 elements from sub-composition with original rel and crossorigin", () => { const subCompWithLinks = ` diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index bf74cc6a2d..2d7f79da58 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -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 || ""; diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index ca2881ebe1..f4d4aa4ea5 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -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";'); }); });