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 = ` +