diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts index 9d5413d689..75dce106af 100644 --- a/packages/studio-server/src/helpers/sourceMutation.ts +++ b/packages/studio-server/src/helpers/sourceMutation.ts @@ -417,12 +417,27 @@ export function probeElementInSource(source: string, target: SourceMutationTarge return probeElementsInSource(source, [target])[0] ?? false; } +/** Why a split changed nothing: the cut route says it, so a report names the cause and not just "rejected". */ +export type SplitRefusal = + | "target-not-found" + | "reference-without-range" + | "no-duration" + | "outside-clip"; + export interface SplitElementResult { html: string; matched: boolean; newId: string | null; + refusal?: SplitRefusal; } +const refused = (html: string, refusal: SplitRefusal): SplitElementResult => ({ + html, + matched: false, + newId: null, + refusal, +}); + function resolveElementTiming(el: Element): { start: number; duration: number; @@ -476,7 +491,7 @@ export function splitElementInHtml( ): SplitElementResult { const { document, wrappedFragment } = parseSourceDocument(source); const el = findTargetElement(document, target); - if (!el || !isHTMLElement(el)) return { html: source, matched: false, newId: null }; + if (!el || !isHTMLElement(el)) return refused(source, "target-not-found"); // An untimed wrapper whose row is its timed media's own window is a view of that // media: cut the media and leave the wrappers untimed. A GSAP scene keeps its own window. @@ -498,7 +513,7 @@ export function splitElementInHtml( const reference = readClipTiming(timed).startExpression; const startsAfter = reference?.kind === "reference" ? reference : null; if (startsAfter) { - if (!fallbackTiming) return { html: source, matched: false, newId: null }; + if (!fallbackTiming) return refused(source, "reference-without-range"); start = fallbackTiming.start; } // GSAP-animated elements carry their timing in the script, not in data-* attrs, @@ -509,12 +524,12 @@ export function splitElementInHtml( start = fallbackTiming.start; duration = fallbackTiming.duration; } + if (duration <= 0) return refused(source, "no-duration"); if ( - duration <= 0 || splitTime < start + SPLIT_BOUNDARY_EPSILON_S || splitTime > start + duration - SPLIT_BOUNDARY_EPSILON_S ) { - return { html: source, matched: false, newId: null }; + return refused(source, "outside-clip"); } newId = nextUniqueId(document, newId); @@ -523,7 +538,7 @@ export function splitElementInHtml( const secondDuration = duration - firstDuration; const clone = el.cloneNode(true); - if (!isHTMLElement(clone)) return { html: source, matched: false, newId: null }; + if (!isHTMLElement(clone)) return refused(source, "target-not-found"); clone.setAttribute("id", newId); dedupeClonedCompositionId(document, clone); clone.removeAttribute("data-hf-id"); diff --git a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts index 282f6a2371..e029c59102 100644 --- a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts +++ b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts @@ -600,7 +600,12 @@ describe("splitElementInHtml — a start that names another clip", () => { it("leaves the file as it was for a cut before the clip's real start, instead of rewriting it to start at 0", () => { const source = after("b"); const result = splitElementInHtml(source, { id: "a" }, 2, "a-split", { start: 3, duration: 4 }); - expect(result).toEqual({ html: source, matched: false, newId: null }); + expect(result).toEqual({ + html: source, + matched: false, + newId: null, + refusal: "outside-clip", + }); }); it("refuses when it cannot know where the clip plays", () => { @@ -609,13 +614,14 @@ describe("splitElementInHtml — a start that names another clip", () => { html: source, matched: false, newId: null, + refusal: "reference-without-range", }); }); }); describe("splitElementInHtml — a cut the file's own timing does not allow", () => { const clip = `