@@ -12,6 +12,8 @@ import type {
1212} from "../core.types" ;
1313import { validateCompositionGsap } from "./gsapSerialize" ;
1414import { ensureHfIds } from "./hfIds.js" ;
15+ import { parseGsapScriptAcornForWrite } from "./gsapParserAcorn.js" ;
16+ import { removeAnimationFromScript } from "./gsapWriterAcorn.js" ;
1517import type { ValidationResult } from "../core.types" ;
1618
1719const MEDIA_TYPES = new Set < string > ( [ "video" , "image" , "audio" ] ) ;
@@ -672,15 +674,40 @@ export function addElementToHtml(
672674 } ;
673675}
674676
675- export function removeElementFromHtml ( html : string , elementId : string ) : string {
676- const parser = new DOMParser ( ) ;
677- const doc = parser . parseFromString ( html , "text/html" ) ;
677+ function selectorTargetsId ( selector : string , id : string ) : boolean {
678+ return (
679+ selector === `#${ id } ` ||
680+ selector === `[data-hf-id="${ id } "]` ||
681+ selector === `[data-hf-id='${ id } ']`
682+ ) ;
683+ }
678684
679- const el = doc . getElementById ( elementId ) ;
680- if ( el ) {
681- el . remove ( ) ;
685+ function stripGsapForId ( script : string , elementId : string ) : string {
686+ const parsed = parseGsapScriptAcornForWrite ( script ) ;
687+ if ( ! parsed ) return script ;
688+ let current = script ;
689+ for ( const { id : animId , animation } of parsed . located ) {
690+ if ( selectorTargetsId ( animation . targetSelector , elementId ) ) {
691+ current = removeAnimationFromScript ( current , animId ) ;
692+ }
682693 }
694+ return current ;
695+ }
683696
697+ function cascadeRemoveGsapById ( doc : Document , elementId : string ) : void {
698+ for ( const script of Array . from ( doc . querySelectorAll ( "script" ) ) ) {
699+ const text = script . textContent ?? "" ;
700+ if ( ! text . includes ( "gsap" ) && ! text . includes ( "ScrollTrigger" ) ) continue ;
701+ const updated = stripGsapForId ( text , elementId ) ;
702+ if ( updated !== text ) script . textContent = updated ;
703+ }
704+ }
705+
706+ export function removeElementFromHtml ( html : string , elementId : string ) : string {
707+ const parser = new DOMParser ( ) ;
708+ const doc = parser . parseFromString ( html , "text/html" ) ;
709+ doc . getElementById ( elementId ) ?. remove ( ) ;
710+ cascadeRemoveGsapById ( doc , elementId ) ;
684711 return "<!DOCTYPE html>\n" + doc . documentElement . outerHTML ;
685712}
686713
0 commit comments