diff --git a/packages/core/package-subpaths.json b/packages/core/package-subpaths.json index 36a0f34904..8697fafc43 100644 --- a/packages/core/package-subpaths.json +++ b/packages/core/package-subpaths.json @@ -548,6 +548,12 @@ "types": "./dist/compiler/htmlDocument.d.ts", "environments": ["browser", "bun", "node"] }, + "./compiler/selector-id-tokens": { + "source": "./src/compiler/selectorIdTokens.ts", + "runtime": "./dist/compiler/selectorIdTokens.js", + "types": "./dist/compiler/selectorIdTokens.d.ts", + "environments": ["browser", "bun", "node"] + }, "./compiler/png-colour-tags": { "source": "./src/compiler/pngColourTags.ts", "runtime": "./dist/compiler/pngColourTags.js", diff --git a/packages/core/package.json b/packages/core/package.json index cf4f62c724..6aa4a1c3e3 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -555,6 +555,12 @@ "import": "./src/compiler/htmlDocument.ts", "types": "./src/compiler/htmlDocument.ts" }, + "./compiler/selector-id-tokens": { + "bun": "./src/compiler/selectorIdTokens.ts", + "node": "./dist/compiler/selectorIdTokens.js", + "import": "./src/compiler/selectorIdTokens.ts", + "types": "./src/compiler/selectorIdTokens.ts" + }, "./compiler/png-colour-tags": { "bun": "./src/compiler/pngColourTags.ts", "node": "./dist/compiler/pngColourTags.js", @@ -1075,6 +1081,10 @@ "import": "./dist/compiler/htmlDocument.js", "types": "./dist/compiler/htmlDocument.d.ts" }, + "./compiler/selector-id-tokens": { + "import": "./dist/compiler/selectorIdTokens.js", + "types": "./dist/compiler/selectorIdTokens.d.ts" + }, "./compiler/png-colour-tags": { "import": "./dist/compiler/pngColourTags.js", "types": "./dist/compiler/pngColourTags.d.ts" diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts index ce8c12fd4d..4c5321522d 100644 --- a/packages/studio-server/src/helpers/sourceMutation.ts +++ b/packages/studio-server/src/helpers/sourceMutation.ts @@ -4,6 +4,7 @@ import { removeElementWithGsapCascade } from "@hyperframes/parsers"; import { readMediaOffsetSeconds, readPlaybackRate } from "@hyperframes/parsers/media-duration"; import postcss from "postcss"; import selectorParser from "postcss-selector-parser"; +import { escapeCssIdentifier } from "@hyperframes/core/compiler/selector-id-tokens"; import { isAllowedHtmlAttribute, isSafeAttributeValue } from "@hyperframes/core/html-attr-safety"; import { sanitizeRichTextChildren } from "@hyperframes/core/rich-text-sanitize"; import { @@ -88,7 +89,7 @@ function duplicateCssRulesForId(document: Document, originalId: string, newId: s const idToken = `#${originalId}`; const transform = selectorParser((selectors) => { selectors.walkIds((node) => { - if (node.value === originalId) node.value = newId; + if (node.value === originalId) node.value = escapeCssIdentifier(newId); }); }); for (const styleEl of document.querySelectorAll("style")) { @@ -429,6 +430,7 @@ export interface SplitElementResult { html: string; matched: boolean; newId: string | null; + originalId?: string | null; refusal?: SplitRefusal; } @@ -477,7 +479,7 @@ export function splitElementInHtml( source: string, target: SourceMutationTarget, splitTime: number, - newId: string, + newId: string | null, fallbackTiming?: { start: number; duration: number; @@ -533,7 +535,7 @@ export function splitElementInHtml( return refused(source, "outside-clip"); } - newId = nextUniqueId(document, newId); + newId = nextUniqueId(document, newId ?? `${el.getAttribute("id") || "clip"}-split`); const firstDuration = splitTime - start; const secondDuration = duration - firstDuration; @@ -616,6 +618,7 @@ export function splitElementInHtml( html: ensureHfIds(html), matched: true, newId, + originalId, }; } diff --git a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts index e029c59102..41b703aa64 100644 --- a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts +++ b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts @@ -1,5 +1,6 @@ // fallow-ignore-file code-duplication import { parseHTML } from "linkedom"; +import postcss from "postcss"; import { describe, expect, it } from "vitest"; import { readMediaOffsetSeconds } from "@hyperframes/parsers/media-duration"; import { @@ -99,6 +100,13 @@ describe("splitElementInHtml", () => { expect(cssMatches?.length).toBeGreaterThanOrEqual(1); }); + it("writes a new id into the stylesheet only as one escaped id selector", () => { + const result = splitElementInHtml(source, { id: "box" }, 3, "x{} body { display: none } #q"); + const css = parseHTML(result.html).document.querySelector("style")!.textContent!; + const selectors = postcss.parse(css).nodes.map((rule) => (rule as postcss.Rule).selector); + expect(selectors).toEqual(["#box", String.raw`#x\{\}\ body\ \{\ display\:\ none\ \}\ \#q`]); + }); + it("deduplicates IDs when the requested newId already exists", () => { const withExisting = source.replace( "", diff --git a/packages/studio-server/src/routes/files.test.ts b/packages/studio-server/src/routes/files.test.ts index bef56783a5..13b964091a 100644 --- a/packages/studio-server/src/routes/files.test.ts +++ b/packages/studio-server/src/routes/files.test.ts @@ -1197,6 +1197,44 @@ describe("registerFileRoutes", () => { expect(response.headers.get("etag")).toBe(payload.version); }); + it("names a cut's right half from the file's own id, whatever id the request carries", async () => { + const projectDir = createProjectDir(); + const before = ` +
A
+`; + writeFileSync(join(projectDir, "index.html"), before); + const app = new Hono(); + registerFileRoutes(app, createAdapter(projectDir)); + + const response = await postCutBatch(app, [ + { + path: "index.html", + expectedVersion: fileContentVersion(before), + targets: [ + { + target: { hfId: "h1", id: "t{} main { display: none } #t" }, + originalId: "x{} body { display: none } #q", + splitTime: 2, + elementStart: 0, + elementDuration: 4, + }, + ], + }, + ]); + const after = readFileSync(join(projectDir, "index.html"), "utf-8"); + + expect(response.status).toBe(200); + expect(after).not.toContain("display"); + expect(after.match(/", + ); + expect(after).toContain('id="a-split"'); + expect(after).toContain('"#a-split"'); + }); + it("folds multiple same-file cuts and writes one canonical file result", async () => { const projectDir = createProjectDir(); const before = diff --git a/packages/studio-server/src/routes/files.ts b/packages/studio-server/src/routes/files.ts index 6b49d2413b..7877ce4aea 100644 --- a/packages/studio-server/src/routes/files.ts +++ b/packages/studio-server/src/routes/files.ts @@ -287,7 +287,6 @@ interface ElementPatchBatchFileResult { interface AtomicCutTarget { target: MutationTarget; - originalId?: string; splitTime: number; elementStart: number; elementDuration: number; @@ -2305,8 +2304,7 @@ async function foldAtomicCutFile( }) .map(({ cut }) => cut); for (const cut of orderedTargets) { - const baseId = cut.originalId || cut.target.id || "clip"; - const split = splitElementInHtml(after, cut.target, cut.splitTime, `${baseId}-split`, { + const split = splitElementInHtml(after, cut.target, cut.splitTime, null, { start: cut.elementStart, duration: cut.elementDuration, playbackStart: cut.playbackStart, @@ -2323,13 +2321,13 @@ async function foldAtomicCutFile( splitCount++; rightHalfIds.push(split.newId); - if (!cut.originalId) continue; + if (!split.originalId) continue; const block = extractGsapScriptBlock(after); if (!block) continue; const result = await executeGsapMutation( { type: "split-animations", - originalId: cut.originalId, + originalId: split.originalId, newId: split.newId, splitTime: cut.splitTime, elementStart: cut.elementStart, diff --git a/packages/studio/src/hooks/useRazorSplit.test.tsx b/packages/studio/src/hooks/useRazorSplit.test.tsx index d5475ec9f3..eeae1d8ea7 100644 --- a/packages/studio/src/hooks/useRazorSplit.test.tsx +++ b/packages/studio/src/hooks/useRazorSplit.test.tsx @@ -137,7 +137,7 @@ describe("useRazorSplit linked clips", () => { await split?.(video, 2); }); const targets = JSON.parse(bodies[0] ?? "{}").files[0].targets.map( - (cut: { originalId: string }) => cut.originalId, + (cut: { target: { id: string } }) => cut.target.id, ); expect(targets).toEqual(["talk", "talk-audio"]); act(() => root.unmount()); diff --git a/packages/studio/src/utils/razorSplitTransaction.test.ts b/packages/studio/src/utils/razorSplitTransaction.test.ts index 464b74a4f8..024bdb7747 100644 --- a/packages/studio/src/utils/razorSplitTransaction.test.ts +++ b/packages/studio/src/utils/razorSplitTransaction.test.ts @@ -33,7 +33,8 @@ describe("buildAtomicCutIntents", () => { expect(intents).toHaveLength(1); expect(intents[0].targets).toHaveLength(2); - expect(intents[0].targets.map((target) => target.originalId)).toEqual(["host-a", "host-b"]); + expect(intents[0].targets.map((cut) => cut.target.id)).toEqual(["host-a", "host-b"]); + expect(intents[0].targets.some((cut) => "originalId" in cut)).toBe(false); }); it("carries the element's authored track onto the cut target, so both split halves stay pinned to it server-side", () => { diff --git a/packages/studio/src/utils/razorSplitTransaction.ts b/packages/studio/src/utils/razorSplitTransaction.ts index 7f17d2f849..c7f5d0abf2 100644 --- a/packages/studio/src/utils/razorSplitTransaction.ts +++ b/packages/studio/src/utils/razorSplitTransaction.ts @@ -12,7 +12,6 @@ type ProjectFileWriter = (path: string, content: string, expectedContent?: strin interface CutTarget { target: NonNullable>; - originalId?: string; splitTime: number; elementStart: number; elementDuration: number; @@ -64,7 +63,6 @@ function buildCutTarget( ): CutTarget { return { target, - ...(element.domId ? { originalId: element.domId } : {}), splitTime: Math.max(0, toAuthoredStart(element, splitTime)), elementStart: toAuthoredStart(element, element.start), elementDuration: element.duration,