@@ -10,6 +10,7 @@ import {
1010 addAnimationToScript ,
1111 addKeyframeToScript ,
1212 convertToKeyframesFromScript ,
13+ copyAnimationsInScript ,
1314 removeAnimationFromScript ,
1415 removeKeyframeFromScript ,
1516 updateAnimationInScript ,
@@ -445,3 +446,92 @@ window.__timelines["t"] = tl;`;
445446 expect ( reparsed . global ) . toBeFalsy ( ) ;
446447 } ) ;
447448} ) ;
449+
450+ describe ( "copyAnimationsInScript" , ( ) => {
451+ const script = `\
452+ gsap.set("#goodbye", { rotation: 4 });
453+ var tl = gsap.timeline({ paused: true });
454+ tl.from("#goodbye", { opacity: 0, y: 40, ease: EASE }, 1);
455+ tl.to("#title", { opacity: 0.5, duration: 10 });
456+ function pop(sel) { tl.to(sel, { scale: 1.2 }, 2); }
457+ pop("#goodbye");
458+ window.__timelines["t"] = tl;` ;
459+
460+ it ( "adds each tween on the original for the copy, moved by the delta, with its own argument text" , ( ) => {
461+ const result = copyAnimationsInScript ( script , "#goodbye" , "#goodbye-2" , 3 ) ;
462+ expect ( result ) . toContain ( `tl.from("#goodbye-2", { opacity: 0, y: 40, ease: EASE }, 4);` ) ;
463+ expect ( result ) . toContain ( `gsap.set("#goodbye-2", { rotation: 4 });\nvar tl` ) ;
464+ const copies = parseGsapScriptAcorn ( result ) ?. animations . filter (
465+ ( a ) => a . targetSelector === "#goodbye-2" && a . method !== "set" ,
466+ ) ;
467+ expect ( copies ?. map ( ( a ) => a . position ) ) . toEqual ( [ 4 ] ) ;
468+ // The original lines are left as they were; the tween's copy ends the timeline's block.
469+ expect ( result . replace ( / \n ? .* g o o d b y e - 2 .* / g, "" ) ) . toBe ( script . replace ( / \n ? .* g o o d b y e - 2 .* / g, "" ) ) ;
470+ expect (
471+ result . endsWith ( `= tl;\ntl.from("#goodbye-2", { opacity: 0, y: 40, ease: EASE }, 4);` ) ,
472+ ) . toBe ( true ) ;
473+ } ) ;
474+
475+ it ( "copies no tween whose start is not a number, rather than guess it" , ( ) => {
476+ const unsure = `var tl = gsap.timeline();
477+ tl.to("#title", { x: 1, duration: 2 });
478+ tl.from("#goodbye", { opacity: 0 });
479+ tl.addLabel("end");
480+ tl.to("#goodbye", { x: 2 }, "end");` ;
481+ expect ( copyAnimationsInScript ( unsure , "#goodbye" , "#goodbye-2" , 3 ) ) . toBe ( unsure ) ;
482+ } ) ;
483+
484+ it ( "keeps the copy in the block that declares the timeline" , ( ) => {
485+ const guarded = `if (window.gsap) {
486+ const tl = gsap.timeline();
487+ tl.from("#goodbye", { opacity: 0 }, 1);
488+ window.__timelines["main"] = tl;
489+ }` ;
490+ expect ( copyAnimationsInScript ( guarded , "#goodbye" , "#goodbye-2" , 3 ) ) . toContain (
491+ ` window.__timelines["main"] = tl;\n tl.from("#goodbye-2", { opacity: 0 }, 4);\n}` ,
492+ ) ;
493+ } ) ;
494+
495+ it ( "copies no tween inside a callback or a guard, which may never run" , ( ) => {
496+ const deferred = `var tl = gsap.timeline();
497+ el.addEventListener("click", () => tl.to("#goodbye", { x: 1 }, 1));
498+ window.go && tl.from("#goodbye", { opacity: 0 }, 1);
499+ tl.call(() => tl.to("#goodbye", { y: 1 }, 1));
500+ tl.to("#title", { x: 1 }, 0).from("#goodbye", { opacity: 0 }, 2);` ;
501+ const result = copyAnimationsInScript ( deferred , "#goodbye" , "#goodbye-2" , 3 ) ;
502+ expect ( result . match ( / g o o d b y e - 2 / g) ) . toEqual ( [ "goodbye-2" ] ) ;
503+ expect ( result ) . toContain ( `tl.from("#goodbye-2", { opacity: 0 }, 5);` ) ;
504+ } ) ;
505+
506+ it ( "copies no set that is the bare body of an if or an else" , ( ) => {
507+ const guarded = `var tl = gsap.timeline();
508+ if (window.go) gsap.set("#goodbye", { x: 9 });
509+ else gsap.set("#goodbye", { x: 1 });` ;
510+ expect ( copyAnimationsInScript ( guarded , "#goodbye" , "#goodbye-2" , 3 ) ) . toBe ( guarded ) ;
511+ } ) ;
512+
513+ it ( "puts the copy before the block's return" , ( ) => {
514+ const built = `function build() {
515+ const tl = gsap.timeline();
516+ tl.from("#goodbye", { opacity: 0, stagger: 0.1 }, 1);
517+ return tl;
518+ }` ;
519+ expect ( copyAnimationsInScript ( built , "#goodbye" , "#goodbye-2" , 3 ) ) . toContain (
520+ ` tl.from("#goodbye-2", { opacity: 0, stagger: 0.1 }, 4);\n return tl;` ,
521+ ) ;
522+ } ) ;
523+
524+ it ( "puts a set's copy right after it, where what it reads is defined" , ( ) => {
525+ const late = `const tl = gsap.timeline();
526+ const X = 40;
527+ gsap.set("#goodbye", { x: X });
528+ tl.from("#goodbye", { opacity: 0 }, 1);` ;
529+ expect ( copyAnimationsInScript ( late , "#goodbye" , "#goodbye-2" , 3 ) ) . toContain (
530+ `gsap.set("#goodbye", { x: X });\ngsap.set("#goodbye-2", { x: X });` ,
531+ ) ;
532+ } ) ;
533+
534+ it ( "leaves the script as it was when nothing targets the original" , ( ) => {
535+ expect ( copyAnimationsInScript ( script , "#tag" , "#tag-2" , 3 ) ) . toBe ( script ) ;
536+ } ) ;
537+ } ) ;
0 commit comments