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
This commit is contained in:
Miguel Ángel
2026-11-20 07:26:49 +00:00
committed by GitHub
parent fbbbfe0587
commit 95a8a91b5e
7 changed files with 166 additions and 14 deletions
+6
View File
@@ -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,
@@ -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 = '<div data-hf-id="box"></div>';
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 = '<div data-hf-id="box"></div><i data-hf-studio-rotation="true"></i>';
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 = '<div data-hf-id="box" data-hf-studio-motion="{}"></div>';
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 = '<div data-hf-id="box"></div>';
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('<iframe data-hf-id="box" sandbox=""></iframe>', [id]);
@@ -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:<data-hf-id>` or `id:<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<string>(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<string, unknown>)[STUDIO_SEEK_REAPPLY_STARTED] !== true;
const isRecord = (value: unknown): value is Record<string, unknown> =>
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 {
@@ -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<HTMLElement>).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", () => {
+23
View File
@@ -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");
@@ -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<string, unknown>)[STUDIO_SEEK_REAPPLY_STARTED];
return [hf.seek !== seek, said === true];
};
const plain = '<div data-hf-studio-original-inline-translate=""></div>';
expect([...startsOn(plain), htmlHasStudioPositionEdits(plain)]).toEqual([false, false, false]);
for (const { name, value } of STUDIO_REAPPLIED_MARKS) {
const marked = `<div ${name}="${value ?? "{}"}"></div>`;
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(`<div id="a" ${attr}></div>`);
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);
@@ -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<string, unknown>).__hfStudioSeekReapplyStarted = true;
const splitTopLevelWhitespace = (value: string): string[] => {
const parts: string[] = [];