Skip to content

Commit 6c2d668

Browse files
vanceingallsclaude
andcommitted
fix(sdk): cascadeRemoveAnimations re-parses per removal (R4 — SDK twin of #3)
cascadeRemoveAnimations captured every matching animation id from a single up-front parse, then removed against the mutating script — the SDK-side twin of the stripGsapForId bug (R3 #3). Animation ids are positional, so removing the first tween for an element renumbered the survivors and the stale later ids no-op'd, orphaning those tweens on the just-removed element. Now re-parse after each removal and strip the first still-matching animation until none remain. Also adds the reviewer's defense-in-depth test: an aliased multi-target setStyle (same id twice) undoes to the original, not the intermediate (exercises the single-dispatch inverse reversal from R3 #5/#11). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 4f1eb96 commit 6c2d668

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

‎packages/sdk/src/engine/mutate.gsap.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,21 @@ window.__timelines["t"] = tl;`;
810810
expect(newScript).not.toContain("hf-box");
811811
expect(newScript).toContain("hf-stage");
812812
});
813+
814+
it("strips ALL tweens for the element, not just the first (positional-id renumber)", () => {
815+
// Two tweens on the same element: removing the first renumbers the survivor's
816+
// count-based id, so a single up-front parse left the second tween orphaned.
817+
const twoOwnTweens = `var tl = gsap.timeline({ paused: true });
818+
tl.to("[data-hf-id=\\"hf-box\\"]", { x: 100, duration: 1 }, 0);
819+
tl.to("[data-hf-id=\\"hf-box\\"]", { x: 200, duration: 1 }, 1);
820+
window.__timelines["t"] = tl;`;
821+
const parsed = fresh(twoOwnTweens);
822+
const result = applyOp(parsed, { type: "removeElement", target: "hf-box" });
823+
const newScript = String(result.forward[1]?.value ?? "");
824+
expect(newScript).not.toContain("hf-box");
825+
expect(newScript).not.toContain("x: 100");
826+
expect(newScript).not.toContain("x: 200");
827+
});
813828
});
814829

815830
// ─── GSAP ops on composition with no script block ────────────────────────────

‎packages/sdk/src/engine/mutate.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -684,15 +684,20 @@ function collectSubtreeHfIds(el: Element): string[] {
684684
}
685685

686686
function cascadeRemoveAnimations(script: string, id: HfId): string {
687-
const parsedGsap = parseGsapScriptAcornForWrite(script);
688-
if (!parsedGsap) return script;
687+
// Re-parse after each removal: animation ids are positional, so removing one
688+
// tween renumbers the survivors — ids from a single up-front parse go stale and
689+
// no-op, orphaning later tweens on the removed element. Same fix as
690+
// stripGsapForId in htmlParser.ts (R3 #3); this is its SDK-side twin.
689691
let current = script;
690-
for (const { id: animId, animation } of parsedGsap.located) {
691-
if (selectorMatchesId(animation.targetSelector, id)) {
692-
current = removeAnimationFromScript(current, animId);
693-
}
692+
for (;;) {
693+
const parsedGsap = parseGsapScriptAcornForWrite(current);
694+
if (!parsedGsap) return current;
695+
const match = parsedGsap.located.find((l) => selectorMatchesId(l.animation.targetSelector, id));
696+
if (!match) return current;
697+
const next = removeAnimationFromScript(current, match.id);
698+
if (next === current) return current; // guard against a non-removing match
699+
current = next;
694700
}
695-
return current;
696701
}
697702

698703
// ─── setClassStyle handler ────────────────────────────────────────────────────

‎packages/sdk/src/session.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ describe("single-dispatch undo reverses the inverse patch list", () => {
317317
expect(comp.getElement("hf-parent")).not.toBeNull();
318318
expect(comp.getElement("hf-child")).not.toBeNull();
319319
});
320+
321+
// Defense-in-depth: an aliased multi-target (the same element twice) makes the
322+
// 2nd id capture the value the 1st already wrote; undo must replay the inverse
323+
// in reverse to land on the ORIGINAL, not the intermediate.
324+
it("setStyle with a duplicate target undoes to the original, not the intermediate", async () => {
325+
const comp = await openComposition(BASE_HTML);
326+
comp.dispatch({
327+
type: "setStyle",
328+
target: ["hf-title", "hf-title"],
329+
styles: { fontSize: "96px" },
330+
});
331+
expect(comp.getElement("hf-title")?.inlineStyles.fontSize).toBe("96px");
332+
comp.undo();
333+
expect(comp.getElement("hf-title")?.inlineStyles.fontSize).toBe("64px");
334+
});
320335
});
321336

322337
// ─── setSelection / getSelection / selectionchange ───────────────────────────

0 commit comments

Comments
 (0)