diff --git a/packages/core/src/compiler/inlineSubCompositions.test.ts b/packages/core/src/compiler/inlineSubCompositions.test.ts index 56ea9ed157..bd55d255e6 100644 --- a/packages/core/src/compiler/inlineSubCompositions.test.ts +++ b/packages/core/src/compiler/inlineSubCompositions.test.ts @@ -264,6 +264,7 @@ describe("inlineSubCompositions – #ID selector scoping divergence", () => { flattenInnerRoot, }); + expect(host.hasAttribute("data-composition-id")).toBe(false); const wrapper = host.querySelector("[data-hf-inner-root]"); expect(wrapper?.getAttribute("data-composition-id")).toBe("scoped-text"); @@ -574,6 +575,50 @@ describe("inlineSubCompositions – variable defaults on a template sub-comp roo }); expect(result.variablesByComp["card"]).toMatchObject({ headline: "Overridden" }); }); + + it("keys an anonymous host's values by the sub-composition's own id, which its wrapper and scripts use", () => { + const { document } = parseHTML(` +
+
+
`); + const host = document.querySelector('[data-composition-src="card.html"]')!; + const result = inlineSubCompositions(document, [host], { + resolveHtml: () => SUB_COMP_WITH_VAR, + parseHtml: (h) => parseHTML(h).document, + readVariableDefaults: readDeclaredDefaults, + parseHostVariables: parseHostVariableValues, + }); + expect(result.variablesByComp["card"]).toMatchObject({ headline: "Hi there" }); + // The host stays anonymous; the preserved wrapper is what carries the id. + expect(host.hasAttribute("data-composition-id")).toBe(false); + expect(host.querySelector('[data-composition-id="card"]')).not.toBeNull(); + }); + + it.each([ + ["named first", ["named", "anonymous"]], + ["anonymous first", ["anonymous", "named"]], + ] as const)( + "keeps a named mount's values when an anonymous mount of the same file shares its scope (%s)", + (_order, order) => { + // Both mounts resolve through `[data-composition-id="card"]`: the named + // host carries it, the anonymous wrapper keeps it. The explicit instance + // values on the named host must win whichever mount is inlined first. + const { document } = parseHTML(` +
+
+
+
`); + const hosts = order.map((id) => document.querySelector(`#${id}`)!); + const result = inlineSubCompositions(document, hosts, { + resolveHtml: () => SUB_COMP_WITH_VAR, + parseHtml: (h) => parseHTML(h).document, + readVariableDefaults: readDeclaredDefaults, + parseHostVariables: parseHostVariableValues, + }); + expect(result.variablesByComp).toEqual({ card: { headline: "Custom" } }); + expect(document.querySelector("#anonymous")!.hasAttribute("data-composition-id")).toBe(false); + }, + ); }); describe("inlineSubCompositions – recursive host discovery", () => { diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index e9f86ca74c..8b79144458 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -434,6 +434,7 @@ export function inlineSubCompositions( const scopeCssToComposition = createCompositionCssScoper(); const sceneHosts = new Map(); + const queue = hosts.map((element) => ({ element, ancestry: [] as string[], @@ -526,14 +527,26 @@ export function inlineSubCompositions( // document element (full-document sub-comps) AND the inner composition root // (template/fragment sub-comps store their schema on the root div, not a // synthetic ), then let per-instance host values override. - if (readVariableDefaults && parseHostVariables && runtimeCompId) { + // + // An anonymous host has no runtime id of its own. Its wrapper keeps the + // authored composition id (restored below), so that id is the scope its + // bound media and scripts resolve variables through (runtime/variableScope.ts) + // and the key the producer's probe gate reads — a full-document sub-comp's + // `` is dropped by inlining, so the table + // is the only place its declared defaults survive to compile time (#7505). + // A named mount of the same file shares that scope and its explicit + // instance values win, so an anonymous mount only fills the entry while + // nothing holds it; a named mount processed later still overwrites it. + const variablesCompId = runtimeCompId || scopeCompId; + if (readVariableDefaults && parseHostVariables && variablesCompId) { const mergedVariables: Record = {}; for (const carrier of plan.variableDefaultCarriers) { Object.assign(mergedVariables, readVariableDefaults(carrier)); } Object.assign(mergedVariables, parseHostVariables(hostEl)); - if (Object.keys(mergedVariables).length > 0) { - variablesByComp[runtimeCompId] = mergedVariables; + const holdsEntry = !runtimeCompId && Object.hasOwn(variablesByComp, variablesCompId); + if (Object.keys(mergedVariables).length > 0 && !holdsEntry) { + variablesByComp[variablesCompId] = mergedVariables; } // Compile time is the only place this defect is visible on the sub-comp // path: the instance value is baked into `__hfVariablesByComp` right @@ -541,8 +554,8 @@ export function inlineSubCompositions( // runtime's identical guard never runs. Same helper, so the message and // the per-process dedupe set are shared with the runtime path and the // author sees one warning either way. - warnUnknownEnumValues(compDoc.documentElement, mergedVariables, runtimeCompId); - warnUnknownEnumValues(innerRoot, mergedVariables, runtimeCompId); + warnUnknownEnumValues(compDoc.documentElement, mergedVariables, variablesCompId); + warnUnknownEnumValues(innerRoot, mergedVariables, variablesCompId); } // `` /