From 95e4e7f880f5005211412e926b307f4a53f47246 Mon Sep 17 00:00:00 2001 From: Ice cola <90940182+IceCoooola@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:58:41 +0000 Subject: [PATCH] fix(producer): probe sub-composition media defaults (#7550) * fix(producer): probe sub-composition media defaults Sub-composition defaults declared on disappear from the DOM during inlining. The browser-probe gate then misses data-var-src bindings, causing renders to use the authored placeholder source. Pass the inliner's variablesByComp table through CompiledComposition and consult the nearest composition scope when checking media bindings. Give anonymous mounts distinct runtime scopes and reserve IDs as nested mounts are processed, preserving each instance's variable values. Add regression coverage for declared defaults, repeated mounts, nested scopes, and named/anonymous mount collisions. Fixes #7505 * fix(core): keep anonymous hosts unnamed, key their variables by the authored id The previous commit made an anonymous mount claim its sub-composition's id on the host element, which changed the DOM contract the runtime mount path and the `missing-host-comp-id` regression golden pin: the compile paths put the host's start/duration on the composition while the mount path kept them on an unnamed timed element, so the producer parity test and the regression shard failed on the first CI run. Restore the contract: an anonymous host carries no composition id and its flattened wrapper keeps the authored one. For #7505 that wrapper is already the scope the probe gate (and `variableScope.ts`) resolves through, so the inliner now keys an anonymous mount's merged defaults by the authored id instead of skipping them. A named mount of the same file shares that scope; its explicit instance values win regardless of inlining order, which the replacement unit test pins in both orders. The id-reservation machinery for claimed ids goes with the claim. Co-Authored-By: Claude Fable 5.1 --------- Co-authored-by: Claude Fable 5.1 --- .../compiler/inlineSubCompositions.test.ts | 45 +++++++++++++++++++ .../src/compiler/inlineSubCompositions.ts | 23 +++++++--- .../core/src/runtime/compositionLoader.ts | 4 +- .../producer/src/services/htmlCompiler.ts | 10 +++-- .../htmlCompiler.variableMedia.test.ts | 38 +++++++++++++++- .../services/render/stages/probeStage.test.ts | 41 +++++++++++++++++ .../src/services/render/stages/probeStage.ts | 22 +++++++-- 7 files changed, 169 insertions(+), 14 deletions(-) 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); } // `` /