Skip to content

Write HTML. Render video. Built for agents.

fix(studio): splitting a video in an untimed wrapper cuts the video, not the wrapper (#5486)

Miguel Ángelaa9413b05ebb
diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts
index 0c34562867..588105959c 100644
--- a/packages/studio-server/src/helpers/sourceMutation.ts
+++ b/packages/studio-server/src/helpers/sourceMutation.ts
@@ -383,6 +383,16 @@ function setElementDuration(
});
}
+/** True when the media has an authored window and the row's range (if given) is that window. */
+function isWindowOf(
+ media: { start: number; duration: number },
+ row?: { start: number; duration: number },
+): boolean {
+ if (media.duration <= 0) return false;
+ const near = (a: number, b: number) => Math.abs(a - b) < 0.001;
+ return !row || (near(media.start, row.start) && near(media.duration, row.duration));
+}
+
// fallow-ignore-next-line complexity
export function splitElementInHtml(
source: string,
@@ -405,7 +415,20 @@ export function splitElementInHtml(
const el = findTargetElement(document, target);
if (!el || !isHTMLElement(el)) return { html: source, matched: false, newId: null };
- const timing = resolveElementTiming(el);
+ // 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.
+ const timedMediaSelector = "video[data-start], audio[data-start]";
+ const innerMedia =
+ el.hasAttribute("data-start") ||
+ resolveElementTiming(el).duration > 0 ||
+ el.matches("[data-composition-src], [data-composition-id]")
+ ? null
+ : el.querySelector(timedMediaSelector);
+ const timedMedia =
+ innerMedia && isWindowOf(resolveElementTiming(innerMedia), fallbackTiming) ? innerMedia : null;
+ const timed = timedMedia ?? el;
+
+ const timing = resolveElementTiming(timed);
let { start, duration } = timing;
// GSAP-animated elements carry their timing in the script, not in data-* attrs,
// so the source has no authored duration. Fall back to the store's (GSAP-derived)
@@ -432,7 +455,13 @@ export function splitElementInHtml(
// Descendants carry their own data-hf-id; leaving them duplicates the id of
// every nested node (e.g. an inner <span>), so strip them on the clone too.
for (const node of clone.querySelectorAll("[data-hf-id]")) node.removeAttribute("data-hf-id");
- setElementDuration(clone, splitTime, secondDuration, fallbackTiming?.track);
+ const clonedTimed = timedMedia ? clone.querySelector(timedMediaSelector)! : clone;
+ if (timedMedia?.id) {
+ const mediaId = nextUniqueId(document, `${timedMedia.id}-split`, new Set([newId]));
+ duplicateCssRulesForId(document, timedMedia.id, mediaId);
+ clonedTimed.setAttribute("id", mediaId);
+ }
+ setElementDuration(clonedTimed, splitTime, secondDuration, fallbackTiming?.track);
// Keep the "clip" class — the runtime uses it to control visibility
// based on data-start/data-duration timing.
@@ -440,26 +469,26 @@ export function splitElementInHtml(
// A split creates two views over the same media source or sub-composition.
// Even an untrimmed one needs an explicit zero in-point stamped on the first
// half so the second half can advance from it instead of restarting at zero.
- const playbackStartAttr = el.hasAttribute("data-playback-start")
+ const playbackStartAttr = timed.hasAttribute("data-playback-start")
? "data-playback-start"
- : el.hasAttribute("data-media-start")
+ : timed.hasAttribute("data-media-start")
? "data-media-start"
- : el.matches("[data-composition-src], [data-composition-id]")
+ : timed.matches("[data-composition-src], [data-composition-id]")
? "data-playback-start"
- : el.matches("audio, video")
+ : timed.matches("audio, video")
? "data-media-start"
: null;
if (playbackStartAttr) {
- const readAttr = (name: string) => el.getAttribute(name);
- const authoredTrim = el.getAttribute(playbackStartAttr);
+ const readAttr = (name: string) => timed.getAttribute(name);
+ const authoredTrim = timed.getAttribute(playbackStartAttr);
const currentTrim =
authoredTrim !== null
? readMediaOffsetSeconds(readAttr)
: (fallbackTiming?.playbackStart ?? 0);
const rate = readPlaybackRate(readAttr, fallbackTiming?.playbackRate);
if (authoredTrim === null || Number(authoredTrim) !== currentTrim)
- el.setAttribute(playbackStartAttr, String(Math.round(currentTrim * 1000) / 1000));
- clone.setAttribute(
+ timed.setAttribute(playbackStartAttr, String(Math.round(currentTrim * 1000) / 1000));
+ clonedTimed.setAttribute(
playbackStartAttr,
String(Math.round((currentTrim + firstDuration * rate) * 1000) / 1000),
);
@@ -473,7 +502,7 @@ export function splitElementInHtml(
// Trim the original element's duration. A GSAP element had no data-start; stamp
// it so the runtime windows the first half (visibility selects on [data-start]).
- setElementDuration(el, start, firstDuration, fallbackTiming?.track);
+ setElementDuration(timed, start, firstDuration, fallbackTiming?.track);
// Insert clone after original
if (el.nextSibling) {
diff --git a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts
index 966e8dba14..896bd488b3 100644
--- a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts
+++ b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts
@@ -222,6 +222,62 @@ describe("splitElementInHtml", () => {
expect(document.getElementById("box")?.getAttribute("data-playback-start")).toBe("1.5");
expect(document.getElementById("box-split")?.getAttribute("data-playback-start")).toBe("5.5");
});
+
+ it("splits the timed video inside an untimed wrapper and leaves both wrappers untimed", () => {
+ const wrapped = `<!DOCTYPE html><html><head><style>#v { object-fit: cover; }</style></head><body><div data-composition-id="root"><div id="wrap" class="frame"><video id="v" class="clip" src="a.mp4" data-start="1" data-duration="6"></video></div></div></body></html>`;
+ // The requested wrapper id is the media's natural split id: both must still come out unique.
+ const result = splitElementInHtml(wrapped, { id: "wrap" }, 3, "v-split", {
+ start: 1,
+ duration: 6,
+ });
+ const { document } = parseHTML(result.html);
+ const wrappers = [...document.querySelectorAll("div.frame")];
+ const [first, second] = [...document.querySelectorAll("video")];
+
+ expect(result.matched).toBe(true);
+ expect(wrappers).toHaveLength(2);
+ for (const wrapper of wrappers) {
+ expect(wrapper.hasAttribute("data-start")).toBe(false);
+ expect(wrapper.hasAttribute("data-duration")).toBe(false);
+ }
+ expect(first?.id).toBe("v");
+ expect(second?.id).toBeTruthy();
+ const ids = [...document.querySelectorAll("[id]")].map((node) => node.id);
+ expect(new Set(ids).size).toBe(ids.length);
+ expect(result.html).toContain(`#${second?.id} {`);
+ expect(first?.getAttribute("data-duration")).toBe("2");
+ expect(second?.getAttribute("data-start")).toBe("3");
+ expect(second?.getAttribute("data-duration")).toBe("4");
+ expect(second?.getAttribute("data-media-start")).toBe("2");
+ });
+
+ it.each(['data-start="1"', 'data-duration="6"', 'data-composition-id="scene"'])(
+ "cuts a wrapper with %s itself, not the video inside it",
+ (wrapperAttrs) => {
+ const wrapped = `<!DOCTYPE html><html><body><div data-composition-id="root"><div id="wrap" ${wrapperAttrs}><video id="v" src="a.mp4" data-start="1" data-duration="6"></video></div></div></body></html>`;
+ const result = splitElementInHtml(wrapped, { id: "wrap" }, 3, "wrap-split", {
+ start: 1,
+ duration: 6,
+ });
+ const { document } = parseHTML(result.html);
+
+ expect(document.getElementById("wrap-split")?.hasAttribute("data-start")).toBe(true);
+ expect(document.getElementById("v")?.getAttribute("data-duration")).toBe("6");
+ },
+ );
+
+ it("keeps cutting a GSAP scene on its own window when it holds a shorter timed video", () => {
+ const scene = `<!DOCTYPE html><html><body><div data-composition-id="root"><div id="card"><video id="v" src="a.mp4" data-start="2" data-duration="3"></video></div></div></body></html>`;
+ const result = splitElementInHtml(scene, { id: "card" }, 7, "card-split", {
+ start: 0,
+ duration: 10,
+ });
+ const { document } = parseHTML(result.html);
+
+ expect(result.matched).toBe(true);
+ expect(document.getElementById("card")?.getAttribute("data-duration")).toBe("7");
+ expect(document.getElementById("card-split")?.getAttribute("data-start")).toBe("7");
+ });
});
describe("wrapElementsInHtml / unwrapElementsFromHtml", () => {