From 95a8a91b5ea28ec21ac638aa69cd43b3a8c46cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 20 Nov 2026 07:26:49 +0000 Subject: [PATCH] fix(core): a redo in a sealed preview puts Studio's edit marks back in place (#8264) * fix(core): a redo in a sealed preview puts Studio's edit marks back in place instead of reloading * fix(core): Studio's motion marks keep the reload; name the edit marks instead of commenting * fix(core): a restore that needs the reapply script, or drops a motion mark, reloads * fix(core): a restore waits for the reapply script to start, and any quoting finds its marks * refactor(core): name the reapplied-mark check instead of commenting it * fix(core): an in-place undo re-run refuses a reapplied mark until the reapply script started * test(core): keep the soft-reload refusal test's comment under the line limit --- packages/core/src/index.ts | 6 +++ .../runtime/editing/restoreAttributes.test.ts | 53 ++++++++++++++++++- .../src/runtime/editing/restoreAttributes.ts | 18 +++++-- .../src/runtime/editing/softReload.test.ts | 24 ++++++++- packages/core/src/studioReappliedMarks.ts | 23 ++++++++ .../helpers/manualEditsRenderScript.test.ts | 45 ++++++++++++++++ .../src/helpers/manualEditsRenderScript.ts | 11 ++-- 7 files changed, 166 insertions(+), 14 deletions(-) create mode 100644 packages/core/src/studioReappliedMarks.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 3b9abec906..7aa4d5c799 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -288,6 +288,12 @@ export { type ToneMapFrameColour, type ToneMapSourceColour, } from "./hdrToneMap"; +export { + htmlHasStudioReappliedMark, + isStudioMotionMark, + STUDIO_REAPPLIED_MARKS, + STUDIO_SEEK_REAPPLY_STARTED, +} from "./studioReappliedMarks"; export { HEAVY_BITRATE, HEAVY_PIXEL_SHARE, diff --git a/packages/core/src/runtime/editing/restoreAttributes.test.ts b/packages/core/src/runtime/editing/restoreAttributes.test.ts index 66cfa18af7..80ab7df598 100644 --- a/packages/core/src/runtime/editing/restoreAttributes.test.ts +++ b/packages/core/src/runtime/editing/restoreAttributes.test.ts @@ -14,7 +14,12 @@ function player() { afterEach(() => { document.body.innerHTML = ""; - for (const name of ["__player", "__hfForceTimelineRebind", "__hfStudioManualEditsApply"]) + for (const name of [ + "__player", + "__hfForceTimelineRebind", + "__hfStudioManualEditsApply", + "__hfStudioSeekReapplyStarted", + ]) delete win[name]; }); @@ -73,6 +78,46 @@ describe("restore-attributes", () => { expect(rebind).not.toHaveBeenCalled(); }); + // fails without the fix: a redo of a sealed move was refused for setting Studio's mark and reloaded the preview. + it("puts back Studio's own edit marks in place, as a redo of a move needs", () => { + document.body.innerHTML = '
'; + player(); + const params = restore("hf:box", [ + ["data-hf-id", "box"], + ["data-hf-studio-original-inline-translate", ""], + ["style", "translate: 130px 90px"], + ]); + expect(restoreAttributes(params)).toEqual({ applied: true }); + const box = document.querySelector('[data-hf-id="box"]')!; + expect(box.getAttribute("data-hf-studio-original-inline-translate")).toBe(""); + expect(box.getAttribute("style")).toBe("translate: 130px 90px"); + }); + + // fails without the fix: a page whose seek-reapply script never started reverted the size on the next seek. + it("reloads for a mark a seek has to reapply until the page's reapply script has started", () => { + document.body.innerHTML = '
'; + player(); + const sized = () => + restoreAttributes( + restore("hf:box", [ + ["data-hf-id", "box"], + ["data-hf-studio-box-size", "true"], + ]), + ); + expect(sized()).toEqual({ applied: false }); + win.__hfStudioSeekReapplyStarted = true; + expect(sized()).toEqual({ applied: true }); + }); + + // fails without the fix: dropping a motion mark in place left its timeline running. + it("reloads to take a motion mark away", () => { + document.body.innerHTML = '
'; + player(); + expect(restoreAttributes(restore("hf:box", [["data-hf-id", "box"]]))).toEqual({ + applied: false, + }); + }); + it("changes no handler, URL or sandbox-style attribute, and leaves an unchanged one alone", () => { const refused = (html: string, attributes: [string, string][]) => { document.body.innerHTML = html; @@ -83,8 +128,12 @@ describe("restore-attributes", () => { }; const box = '
'; const id: [string, string] = ["data-hf-id", "box"]; - for (const name of ["onclick", "ONCLICK", "href", "src", "srcdoc", "xlink:href", "sandbox"]) + for (const name of ["onclick", "ONCLICK", "onerror", "href", "src", "srcdoc", "xlink:href"]) refused(box, [id, [name, "alert(1)"]]); + for (const name of ["sandbox", "data-hf-handle", "data-hf-studio", "data-studio-x"]) + refused(box, [id, [name, "alert(1)"]]); + for (const name of ["data-hf-studio-motion", "data-hf-studio-motion-original-opacity"]) + refused(box, [id, [name, "{}"]]); refused(box, [id, ["data-composition-src", "https://example.com/a.html"]]); refused('', [id]); diff --git a/packages/core/src/runtime/editing/restoreAttributes.ts b/packages/core/src/runtime/editing/restoreAttributes.ts index 34c6b75e6f..858cff3c05 100644 --- a/packages/core/src/runtime/editing/restoreAttributes.ts +++ b/packages/core/src/runtime/editing/restoreAttributes.ts @@ -1,5 +1,10 @@ // `restore-attributes`: an undo or redo that only changes identified elements' attributes, applied in place. // The parent diffs the saved files and sends each changed element's full restored attribute set. +import { + isStudioMotionMark, + STUDIO_REAPPLIED_MARKS, + STUDIO_SEEK_REAPPLY_STARTED, +} from "../../studioReappliedMarks"; /** `hf:` or `id:`, the key the parent's restore diff uses. */ export type RestoreElementKey = string; @@ -40,8 +45,15 @@ const SAFE_TO_SET = new Set([ "data-y", "data-hf-group", ]); +const REAPPLIED = new Set(STUDIO_REAPPLIED_MARKS.map(({ name }) => name)); +const isStudioEditMark = (name: string) => + name.startsWith("data-hf-studio-") && !isStudioMotionMark(name); +const safeToSet = (name: string) => SAFE_TO_SET.has(name) || isStudioEditMark(name); const safeToDrop = (name: string) => - SAFE_TO_SET.has(name) || name === "loading" || name.startsWith("data-"); + !isStudioMotionMark(name) && (safeToSet(name) || name === "loading" || name.startsWith("data-")); +const reapplyScriptMissing = (plan: RestorePlan) => + plan.some(([, , set]) => set.some(([name]) => REAPPLIED.has(name))) && + (window as unknown as Record)[STUDIO_SEEK_REAPPLY_STARTED] !== true; const isRecord = (value: unknown): value is Record => Object.prototype.toString.call(value) === "[object Object]"; @@ -141,7 +153,7 @@ function planRoot( const keep = new Set(attributes.map(([name]) => name)); const drop = el.getAttributeNames().filter((name) => !keep.has(name)); const set = attributes.filter(([name, value]) => el.getAttribute(name) !== value); - if (!drop.every(safeToDrop) || !set.every(([name]) => SAFE_TO_SET.has(name))) return false; + if (!drop.every(safeToDrop) || !set.every(([name]) => safeToSet(name))) return false; plan.push([el, drop, set]); } return true; @@ -154,7 +166,7 @@ export function planRestore(scopes: RestoreScope[]): RestorePlan | null { const roots = scopeRoots(file); if (!roots.length || !roots.every((root) => planRoot(root, elements, plan))) return null; } - return plan; + return reapplyScriptMissing(plan) ? null : plan; } export function applyRestorePlan(plan: RestorePlan): void { diff --git a/packages/core/src/runtime/editing/softReload.test.ts b/packages/core/src/runtime/editing/softReload.test.ts index 284e81c4a9..64d5f592e0 100644 --- a/packages/core/src/runtime/editing/softReload.test.ts +++ b/packages/core/src/runtime/editing/softReload.test.ts @@ -38,7 +38,14 @@ function preview(markup: string) { afterEach(() => { document.body.innerHTML = ""; - for (const key of ["gsap", "__timelines", "__hfForceTimelineRebind", "__player"]) delete win[key]; + for (const key of [ + "gsap", + "__timelines", + "__hfForceTimelineRebind", + "__player", + "__hfStudioSeekReapplyStarted", + ]) + delete win[key]; delete (document.body as Partial).appendChild; killed.mockReset(); }); @@ -140,6 +147,21 @@ describe("soft-reload of an undo", () => { expect(killed).not.toHaveBeenCalled(); expect(document.querySelector("script")?.textContent).toBe(SCRIPT); }); + + // fails without the fix: the in-place re-run set a mark the next seek reverts, its reapply script never started. + it("refuses a restored mark a seek has to reapply until the page's reapply script has started", async () => { + preview(MARKUP); + const el = document.getElementById("a")!; + const restore = [restoreOf("hf:h1", [...KEPT, ["data-hf-studio-box-size", "true"]])]; + + expect(await softReload(params({ restore }))).toEqual({ result: "cannot-soft-reload" }); + expect(el.hasAttribute("data-hf-studio-box-size")).toBe(false); + expect(killed).not.toHaveBeenCalled(); + + win.__hfStudioSeekReapplyStarted = true; + expect(await softReload(params({ restore }))).toEqual({ result: "applied" }); + expect(el.getAttribute("data-hf-studio-box-size")).toBe("true"); + }); }); describe("soft-reload of a timing edit", () => { diff --git a/packages/core/src/studioReappliedMarks.ts b/packages/core/src/studioReappliedMarks.ts new file mode 100644 index 0000000000..597128826e --- /dev/null +++ b/packages/core/src/studioReappliedMarks.ts @@ -0,0 +1,23 @@ +/** Studio edit marks only the preview's seek-reapply script keeps applied; a page loaded with none has no script. */ +export const STUDIO_REAPPLIED_MARKS = [ + { name: "data-hf-studio-path-offset", value: "true" }, + { name: "data-hf-studio-box-size", value: "true" }, + { name: "data-hf-studio-rotation", value: "true" }, + { name: "data-hf-studio-motion", value: null }, +] as const; + +/** Set on the page's window by that script once it has started keeping the marks applied. */ +export const STUDIO_SEEK_REAPPLY_STARTED = "__hfStudioSeekReapplyStarted"; + +const MARK_FOUND_IN_ANY_QUOTING_WITH_EXACT_VALUE = STUDIO_REAPPLIED_MARKS.map(({ name, value }) => + value === null + ? (html: string) => new RegExp(`\\s${name}\\s*=`, "i").test(html) + : (html: string) => + [...html.matchAll(new RegExp(`\\s${name}\\s*=\\s*(["']?)([^"'\\s/>]*)\\1`, "gi"))].some( + (match) => match[2] === value, + ), +); +export const htmlHasStudioReappliedMark = (html: string) => + MARK_FOUND_IN_ANY_QUOTING_WITH_EXACT_VALUE.some((has) => has(html)); + +export const isStudioMotionMark = (name: string) => name.startsWith("data-hf-studio-motion"); diff --git a/packages/studio-server/src/helpers/manualEditsRenderScript.test.ts b/packages/studio-server/src/helpers/manualEditsRenderScript.test.ts index b338bba75e..b5aced5e10 100644 --- a/packages/studio-server/src/helpers/manualEditsRenderScript.test.ts +++ b/packages/studio-server/src/helpers/manualEditsRenderScript.test.ts @@ -1,9 +1,11 @@ import { describe, expect, it } from "vitest"; import { Window } from "happy-dom"; +import { STUDIO_REAPPLIED_MARKS, STUDIO_SEEK_REAPPLY_STARTED } from "@hyperframes/core"; import { createSealedPreviewSeekReapplyScript, createStudioManualEditsRenderBodyScript, createStudioPositionSeekReapplyScript, + htmlHasStudioPositionEdits, } from "./manualEditsRenderScript"; function runScript( @@ -431,6 +433,49 @@ describe("createStudioPositionSeekReapplyScript", () => { return { card, clear: () => card.style.removeProperty("width") }; } + it("starts, says so, and is served on a page with any one of core's reapplied marks only", () => { + const startsOn = (markup: string) => { + const window = new Window(); + window.document.body.innerHTML = markup; + const seek = () => {}; + const hf = { seek }; + (window as unknown as { __hf: typeof hf }).__hf = hf; + runPositionScript(window); + const said = (window as unknown as Record)[STUDIO_SEEK_REAPPLY_STARTED]; + return [hf.seek !== seek, said === true]; + }; + const plain = '
'; + expect([...startsOn(plain), htmlHasStudioPositionEdits(plain)]).toEqual([false, false, false]); + for (const { name, value } of STUDIO_REAPPLIED_MARKS) { + const marked = `
`; + expect([...startsOn(marked), htmlHasStudioPositionEdits(marked)], name).toEqual([ + true, + true, + true, + ]); + } + }); + + // fails without the fix: a single-quoted mark got no script, so a seek undid the edit. + it("finds a reapplied mark in any attribute quoting, and no near miss", () => { + const found = (attr: string) => htmlHasStudioPositionEdits(`
`); + for (const attr of [ + "data-hf-studio-rotation='true'", + "data-hf-studio-box-size=true", + 'DATA-HF-STUDIO-PATH-OFFSET = "true"', + "data-hf-studio-motion='{}'", + ]) + expect(found(attr), attr).toBe(true); + for (const attr of [ + 'data-hf-studio-rotation="false"', + 'data-hf-studio-box-size="truey"', + 'data-hf-studio-motion-original-opacity="1"', + 'xdata-hf-studio-rotation="true"', + 'data-hf-studio-rotation="TRUE"', + ]) + expect(found(attr), attr).toBe(false); + }); + it("hands every argument to the seek it wraps", () => { const window = new Window(); sizedCard(window); diff --git a/packages/studio-server/src/helpers/manualEditsRenderScript.ts b/packages/studio-server/src/helpers/manualEditsRenderScript.ts index 0dd4d00df4..15d4ca8f33 100644 --- a/packages/studio-server/src/helpers/manualEditsRenderScript.ts +++ b/packages/studio-server/src/helpers/manualEditsRenderScript.ts @@ -1,4 +1,5 @@ // fallow-ignore-file code-duplication +import { htmlHasStudioReappliedMark } from "@hyperframes/core"; export interface StudioManualEditsRenderScriptOptions { activeCompositionPath?: string | null; } @@ -21,14 +22,7 @@ export function createStudioPositionSeekReapplyScript(): string { return positionSeekReapplyCall("false"); } -const POSITION_EDIT_MARKS = [ - 'data-hf-studio-path-offset="true"', - 'data-hf-studio-box-size="true"', - 'data-hf-studio-rotation="true"', - 'data-hf-studio-motion="', -]; -export const htmlHasStudioPositionEdits = (html: string) => - POSITION_EDIT_MARKS.some((mark) => html.includes(mark)); +export const htmlHasStudioPositionEdits = htmlHasStudioReappliedMark; /** The reapply for a preview frame Studio cannot script; Studio wraps one it can script itself. */ export function createSealedPreviewSeekReapplyScript(): string { @@ -57,6 +51,7 @@ function studioPositionSeekReapplyRuntime(followFrames: boolean): void { !document.querySelector("[" + MOTION_ATTR + "]") ) return; + (window as unknown as Record).__hfStudioSeekReapplyStarted = true; const splitTopLevelWhitespace = (value: string): string[] => { const parts: string[] = [];