Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

fix(studio): diagnose elements inside composition templates

罗高b289ca08a087
diff --git a/studio/src/components/editor/probeSourceElement.test.ts b/studio/src/components/editor/probeSourceElement.test.ts
index 6857e98b..54208201 100644
--- a/studio/src/components/editor/probeSourceElement.test.ts
+++ b/studio/src/components/editor/probeSourceElement.test.ts
@@ -31,6 +31,19 @@ describe("probeSourceElement", () => {
expect(fetchMock.mock.calls.every(call => call[1]?.method === "POST")).toBe(true);
});
+ it("finds rejected SVG targets inside nested native composition templates", async () => {
+ vi.stubGlobal("fetch", vi.fn(async (_url: unknown, init?: RequestInit) => init?.method === "POST"
+ ? answer({ exists: [false, false] })
+ : answer({ content: '<template id="scene"><div><template><svg id="chart" data-hf-id="hf-chart"><rect class="bar"/></svg></template></div></template>' })));
+ expect(await Promise.all([
+ probeSourceElement("p", "compositions/chart.html", { id: "chart", hfId: "hf-chart" }),
+ probeSourceElement("p", "compositions/chart.html", { selector: ".bar", selectorIndex: 0 }),
+ ])).toEqual([
+ { exists: false, failure: "unsupported-element" },
+ { exists: false, failure: "unsupported-element" },
+ ]);
+ });
+
it.each([404, 403, 503])("reports source read status %s without attributing it to generated content", async status => {
vi.stubGlobal("fetch", vi.fn(async (_url: unknown, init?: RequestInit) => init?.method === "POST"
? answer({ exists: [false] }) : answer({}, status)));
diff --git a/studio/src/components/editor/probeSourceElement.ts b/studio/src/components/editor/probeSourceElement.ts
index 871a2fe4..0be96528 100644
--- a/studio/src/components/editor/probeSourceElement.ts
+++ b/studio/src/components/editor/probeSourceElement.ts
@@ -52,12 +52,22 @@ function requestFailure(error: unknown): SourceProbeFailure {
return error instanceof TypeError || (error as { name?: string })?.name === "TimeoutError" ? "connection-failed" : "check-failed";
}
+function sourceMatches(root: ParentNode, selector: string): Element[] {
+ const matches = Array.from(root.querySelectorAll(selector));
+ if (matches.length) return matches;
+ for (const template of root.querySelectorAll<HTMLTemplateElement>("template")) {
+ const nested = sourceMatches(template.content, selector);
+ if (nested.length) return nested;
+ }
+ return [];
+}
+
function findSourceElement(doc: Document, target: ProbeTarget): Element | null {
const attrValue = (value: string) => value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
- const byHfId = target.hfId && doc.querySelector(`[data-hf-id="${attrValue(target.hfId)}"]`);
- const byId = target.id && doc.getElementById(target.id);
+ const byHfId = target.hfId && sourceMatches(doc, `[data-hf-id="${attrValue(target.hfId)}"]`)[0];
+ const byId = target.id && sourceMatches(doc, `[id="${attrValue(target.id)}"]`)[0];
if (byHfId || byId) return byHfId || byId || null;
- return target.selector ? doc.querySelectorAll(target.selector)[target.selectorIndex ?? 0] ?? null : null;
+ return target.selector ? sourceMatches(doc, target.selector)[target.selectorIndex ?? 0] ?? null : null;
}
async function diagnoseMissingTargets(projectId: string, sourceFile: string, targets: ProbeTarget[]): Promise<SourceProbeFailure[]> {