diff --git a/packages/core/src/runtime/editingGestureStart.test.ts b/packages/core/src/runtime/editingGestureStart.test.ts index a8bba61525..eedacc60cd 100644 --- a/packages/core/src/runtime/editingGestureStart.test.ts +++ b/packages/core/src/runtime/editingGestureStart.test.ts @@ -308,6 +308,21 @@ describe("withoutGsapTakeover", () => { expect(title._gsap!.origin).toBe(origin); }); + it("puts back what a read changed when the read throws", () => { + document.body.innerHTML = `

Title

`; + const title = document.getElementById("title")! as HTMLElement & Cached; + title._gsap = { x: 0 }; + expect(() => + withoutGsapTakeover(title, () => { + title.style.translate = "none"; + title._gsap!.renderTransform = () => {}; + throw new Error("GSAP failed"); + }), + ).toThrow("GSAP failed"); + expect(title.getAttribute("style")).toBe("translate: 0 -200px"); + expect(title._gsap).toEqual({ x: 0 }); + }); + it("drops a GSAP cache the read created", () => { document.body.innerHTML = `

Title

`; const title = document.getElementById("title")! as HTMLElement & Cached; diff --git a/packages/core/src/runtime/editingGestureStart.ts b/packages/core/src/runtime/editingGestureStart.ts index b508805598..0cfb6d8900 100644 --- a/packages/core/src/runtime/editingGestureStart.ts +++ b/packages/core/src/runtime/editingGestureStart.ts @@ -198,10 +198,12 @@ export function withoutGsapTakeover(element: Element, read: () => T): T { const cache = host._gsap; const restoreCache = cache ? keptFields(cache) : () => Reflect.deleteProperty(host, "_gsap"); const restoreAttributes = keptAttributes(element); - const value = read(); - restoreAttributes(); - restoreCache(); - return value; + try { + return read(); + } finally { + restoreAttributes(); + restoreCache(); + } } export type EditingGestureBase = { diff --git a/packages/core/src/runtime/positionEdits.ts b/packages/core/src/runtime/positionEdits.ts index 1a832afa6e..386e08d680 100644 --- a/packages/core/src/runtime/positionEdits.ts +++ b/packages/core/src/runtime/positionEdits.ts @@ -78,8 +78,8 @@ export const composeTranslate = (original: string, x: string, y: string): string * the edit translate is written. GSAP folds a CSS `translate` it sees at an * element's first parse into its cached transform (losing it per-axis on * absolute tweens); once the cache exists, later tweens and positioned set()s - * reuse it and never read the translate again. gsap.getProperty parses - * without mutating the element. Best-effort — absent or failing GSAP is fine. + * reuse it and never read the translate again. The parse also writes the + * folded transform onto the element. Best-effort — absent or failing GSAP is fine. */ const primeGsapTransformCache = (el: StylableElement): void => { try {