mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-26 00:57:05 +00:00
fix(studio-server): a cut names its new half from the file's own id (#8481)
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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(
|
||||
"</div></div>",
|
||||
|
||||
@@ -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 = `<style>#a { color: blue; }</style>
|
||||
<div id="a" data-hf-id="h1" data-start="0" data-duration="4">A</div>
|
||||
<script data-hyperframes-gsap>
|
||||
const tl = gsap.timeline();
|
||||
tl.to("#a", { x: 100, duration: 1 }, 3);
|
||||
</script>`;
|
||||
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(/<style>[\s\S]*?<\/style>/)![0].replace(/\s+/g, "")).toBe(
|
||||
"<style>#a{color:blue;}#a-split{color:blue;}</style>",
|
||||
);
|
||||
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 =
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -12,7 +12,6 @@ type ProjectFileWriter = (path: string, content: string, expectedContent?: strin
|
||||
|
||||
interface CutTarget {
|
||||
target: NonNullable<ReturnType<typeof buildPatchTarget>>;
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user