From fd4b1cf52bb14fa1d1b86adea2315f48f3251737 Mon Sep 17 00:00:00 2001 From: Val Date: Mon, 9 Nov 2026 17:51:08 -0700 Subject: [PATCH] fix(render): probe data-var-src media whose value differs from its src --- .../src/services/htmlCompiler.test.ts | 51 +++++++++++++++++++ .../producer/src/services/htmlCompiler.ts | 44 +++++++++++++++- .../services/render/stages/probeStage.test.ts | 8 +++ .../src/services/render/stages/probeStage.ts | 8 ++- 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index 29d2334bdc0..77439f354a4 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -3025,6 +3025,57 @@ describe("sub-composition variable injection (render path, #2064)", () => { }); }); +describe("variable-bound media src (render path)", () => { + function compileBoundClip(opts: { defaultSrc: string; hostVars?: string }) { + const projectDir = mkdtempSync(join(tmpdir(), "hf-var-src-")); + mkdirSync(join(projectDir, "compositions"), { recursive: true }); + writeFileSync( + join(projectDir, "compositions", "clip.html"), + ` + + +
+ +
+ +`, + ); + writeFileSync( + join(projectDir, "index.html"), + ` + + +
+
+
+ +`, + ); + return compileForRender(projectDir, join(projectDir, "index.html"), projectDir); + } + + it("marks a clip whose host value differs from its static src", async () => { + const compiled = await compileBoundClip({ + defaultSrc: "a.mp4", + hostVars: `data-variable-values='{"clip":"b.mp4"}'`, + }); + expect(compiled.html).toContain("data-hf-var-src-changed"); + }); + + it("marks a clip whose declared default differs from its static src", async () => { + const compiled = await compileBoundClip({ defaultSrc: "b.mp4" }); + expect(compiled.html).toContain("data-hf-var-src-changed"); + }); + + it("leaves a clip whose resolved value is its static src", async () => { + const compiled = await compileBoundClip({ + defaultSrc: "b.mp4", + hostVars: `data-variable-values='{"clip":"a.mp4"}'`, + }); + expect(compiled.html).not.toContain("data-hf-var-src-changed"); + }); +}); + // ── Markup payload sniff (STUDIO-5433) ───────────────────────────────────── // // Producer's `resolveMediaDuration` is a two-step pipeline (download → probe) diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 1dbc2d211fa..0e2ed916b3f 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -995,6 +995,46 @@ function coalesceHeadStylesAndBodyScripts(html: string): string { return document.toString(); } +/** Stamped on bound media whose variable gives a src other than its static one. */ +export const VARIABLE_SRC_CHANGED_ATTR = "data-hf-var-src-changed"; + +/** + * Stamp bound audio and video that any declared default or host + * data-variable-values in their scope chain would give a src other than the + * static one media extraction reads. Runs before remote sources are localized, + * while both still read as authored. + */ +function markChangedVariableMediaSrc( + document: Document, + variablesByComp: Record>, +): boolean { + const topLevel: Record = {}; + for (const declarer of document.querySelectorAll("[data-composition-variables]")) { + Object.assign(topLevel, readDeclaredDefaults(declarer)); + } + let marked = false; + for (const el of document.querySelectorAll( + "audio[data-var-src], video[data-var-src], source[data-var-src]", + )) { + const id = el.getAttribute("data-var-src")!.trim(); + const values = [topLevel[id]]; + for (let scope = el.closest("[data-composition-id]"); scope; ) { + values.push(variablesByComp[scope.getAttribute("data-composition-id")!.trim()]?.[id]); + scope = scope.parentElement?.closest("[data-composition-id]") ?? null; + } + const changed = values.some((value) => { + const url = + value !== null && typeof value === "object" ? (value as { url?: unknown }).url : value; + return typeof url === "string" && url !== "" && url !== el.getAttribute("src"); + }); + if (changed) { + el.setAttribute(VARIABLE_SRC_CHANGED_ATTR, ""); + marked = true; + } + } + return marked; +} + /** * Inline sub-composition HTML into the main document using the shared * inlining logic from @hyperframes/core. This wrapper handles the @@ -1023,7 +1063,8 @@ function inlineSubCompositions( {}, variableOverrides, ); - return emitted ? document.toString() : html; + const marked = markChangedVariableMediaSrc(document as unknown as Document, {}); + return emitted || marked ? document.toString() : html; } // Assign per-instance runtime composition ids before each host is inlined, @@ -1181,6 +1222,7 @@ function inlineSubCompositions( // document-unique key here, while the merged document is in hand and before // anything downstream keys media on an id. See core's mediaRenderIds.ts. assignMediaRenderIds(document as unknown as Document); + markChangedVariableMediaSrc(document as unknown as Document, result.variablesByComp); return document.toString(); } diff --git a/packages/producer/src/services/render/stages/probeStage.test.ts b/packages/producer/src/services/render/stages/probeStage.test.ts index cbd1a105d0e..3bf9b35c758 100644 --- a/packages/producer/src/services/render/stages/probeStage.test.ts +++ b/packages/producer/src/services/render/stages/probeStage.test.ts @@ -377,6 +377,14 @@ describe("hasVariableBoundMedia", () => { expect(hasVariableBoundMedia(audio, { title: "Row 02" })).toBe(false); expect(hasVariableBoundMedia(image, { hero_src: "row-02.png" })).toBe(true); }); + + it("probes media the compiler marked as bound to a different src", () => { + const marked = ``; + const unmarked = ``; + + expect(hasVariableBoundMedia(marked, undefined)).toBe(true); + expect(hasVariableBoundMedia(unmarked, undefined)).toBe(false); + }); }); describe("runProbeStage — forceScreenshot threading", () => { diff --git a/packages/producer/src/services/render/stages/probeStage.ts b/packages/producer/src/services/render/stages/probeStage.ts index 9ddf7843246..a525932d933 100644 --- a/packages/producer/src/services/render/stages/probeStage.ts +++ b/packages/producer/src/services/render/stages/probeStage.ts @@ -62,6 +62,7 @@ import { discoverVideoVisibilityFromTimeline, recompileWithResolutions, resolveCompositionDurations, + VARIABLE_SRC_CHANGED_ATTR, } from "../../htmlCompiler.js"; import { closeFileServerSafely, @@ -172,14 +173,17 @@ export function hasAutoStartVideos(html: string): boolean { /** * Variable-bound image/audio/video sources are resolved by the browser runtime, not * the static compiler. Probe them whenever the current render overrides the - * referenced variable so media extraction follows the resolved row value. + * referenced variable so media extraction follows the resolved row value, or + * when the compiler marked bound audio/video whose declared default or host + * data-variable-values differ from the static src. */ export function hasVariableBoundMedia( html: string, variables: Record | undefined, ): boolean { - if (!variables || Object.keys(variables).length === 0) return false; const { document } = parseHTML(html); + if (document.querySelector(`[${VARIABLE_SRC_CHANGED_ATTR}]`)) return true; + if (!variables || Object.keys(variables).length === 0) return false; return Array.from( document.querySelectorAll( "img[data-var-src], audio[data-var-src], video[data-var-src], source[data-var-src]",