import { IDocument , ITagNode , ITextNode , NodeType } from 'svg-vdom' ; import { combineText } from '../xml/combine-text' ; export const combineScript = async ( dom ) : Promise < void > => new Promise ( resolve => { let firstScript ; const scripts = dom . querySelectorAll ( 'script' ) as ITagNode [ ] ; scripts . forEach ( node => { const type = node . getAttribute ( 'type' ) ; if ( type && ! /^(?:application|text)\/(?:javascript|ecmascript)$/ . test ( type ) ) { node . remove ( ) ; return ; } if ( firstScript ) { firstScript . appendChild ( node . childNodes ) ; node . remove ( ) ; } else { firstScript = node ; } } ) ; if ( firstScript ) { combineText ( firstScript , true ) ; const childNodes = firstScript . childNodes as ITextNode [ ] ; if ( childNodes . length !== 1 || ! childNodes [ 0 ] . textContent ) { firstScript . remove ( ) ; } else { const textContent = childNodes [ 0 ] . textContent ; if ( ! textContent . includes ( '<' ) ) { childNodes [ 0 ] . nodeType = NodeType . Text ; } if ( textContent . slice ( - 1 ) === ';' ) { childNodes [ 0 ] . textContent = childNodes [ 0 ] . textContent . slice ( 0 , - 1 ) ; } ( dom . querySelector ( 'svg' ) as ITagNode ) . appendChild ( firstScript ) ; } } resolve ( ) ; } ) ;