diff --git a/packages/studio/src/utils/sourcePatcher.test.ts b/packages/studio/src/utils/sourcePatcher.test.ts index 0417c673a8..15a996e3e9 100644 --- a/packages/studio/src/utils/sourcePatcher.test.ts +++ b/packages/studio/src/utils/sourcePatcher.test.ts @@ -698,6 +698,36 @@ describe("a file nothing has stamped yet", () => { it("leaves the file as it was when no element has the id, ids and all", () => { expect(applyPatchByTarget(ON_DISK, { hfId: "hf-gone" }, op)).toBe(ON_DISK); }); + + const hfIdOf = (html: string, id: string) => + /data-hf-id="([^"]+)"/.exec( + new RegExp(`<[^>]*\\bid="${id}"[^>]*>`).exec(html)?.[0] ?? "", + )?.[1] ?? ""; + + // fails without the fix: found by its id, the clip was patched into a file left without ids, and a later save + // minted ids from the edited file, so they no longer matched the preview's and the preview reloaded. + it("writes the ids the preview served, even when the element's own id finds it", () => { + const film = `
`; + const served = ensureHfIds(film); + const boxId = hfIdOf(served, "box"); + expect(boxId).toMatch(/^hf-/); + const moved = { type: "attribute", property: "start", value: "0.5" } as const; + const saved = applyPatchByTarget(film, { hfId: boxId, id: "box", selector: "#box" }, moved); + const ids = (html: string) => [...html.matchAll(/data-hf-id="([^"]+)"/g)].map((m) => m[1]); + expect(ids(saved)).toEqual(ids(served)); + expect(ensureHfIds(saved)).toBe(saved); + }); + + it("writes a film that already has ids unchanged apart from the edit", () => { + const film = ensureHfIds( + `
`, + ); + const boxId = hfIdOf(film, "box"); + const moved = { type: "attribute", property: "start", value: "0.5" } as const; + expect(applyPatchByTarget(film, { hfId: boxId, id: "box" }, moved)).toBe( + film.replace('data-start="0"', 'data-start="0.5"'), + ); + }); }); describe("file names with special characters", () => { diff --git a/packages/studio/src/utils/sourcePatcher.ts b/packages/studio/src/utils/sourcePatcher.ts index a15b0c16a4..c96e492613 100644 --- a/packages/studio/src/utils/sourcePatcher.ts +++ b/packages/studio/src/utils/sourcePatcher.ts @@ -279,7 +279,7 @@ function findTagByClass(html: string, target: PatchTarget): TagMatch | null { } export function servedSource(html: string, target: PatchTarget): string { - if (!target.hfId || findTagByTarget(html, target)) return html; + if (!target.hfId || findTagByAttribute(html, "data-hf-id", target.hfId)) return html; const stamped = stampHfIdsInPlace(html); return findTagByTarget(stamped, target) ? stamped : html; }