import { IDocument , IParentNode , ITagNode , ITextNode , NodeType } from 'svg-vdom' ; import { regularAttr } from '../const/regular-attr' ; import { parseStyle } from '../style/parse' ; import { styleToValue } from '../style/style-to-value' ; const mergeTspan = ( node , parent ) => { const nodeStyle = parseStyle ( node . getAttribute ( 'style' ) ?? '' ) ; for ( let i = parent . attributes . length ; i -- ; ) { const attr = parent . attributes [ i ] ; if ( attr . fullname === 'style' ) { const parentStyle = parseStyle ( attr . value ) ; for ( let pi = parentStyle . length ; pi -- ; ) { const styleItem = parentStyle [ pi ] ; const styleDefine = regularAttr [ styleItem . fullname ] ; if ( ! styleDefine . isUndef && node . hasAttribute ( styleItem . fullname ) ) { continue ; } if ( nodeStyle . some ( ( { fullname } ) => fullname === styleItem . fullname ) ) { continue ; } nodeStyle . unshift ( { ... styleItem } ) ; } node . setAttribute ( 'style' , styleToValue ( nodeStyle ) ) ; } else { const attrDefine = regularAttr [ attr . fullname ] ; if ( ! node . hasAttribute ( attr . fullname ) && ! attrDefine . isUndef ) { node . setAttribute ( attr . fullname , attr . value ) ; } } } const index = parent . childNodes . indexOf ( node ) ; node . appendChild ( parent . childNodes . slice ( index + 1 ) ) ; node . insertBefore ( parent . childNodes . slice ( 0 , index ) , node . childNodes [ 0 ] ) ; ( parent . parentNode as ITagNode ) . replaceChild ( node , parent ) ; } ; export const collapseTextwrap = async ( dom ) : Promise < void > => new Promise ( resolve => { const textWraps = dom . querySelectorAll ( 'tspan' ) as ITagNode [ ] ; textWraps . forEach ( node => { if ( node . attributes . every ( ( { value } ) => ! value ) ) { ( node . parentNode as IParentNode ) . replaceChild ( node . childNodes , node ) ; } else { if ( node . querySelectorAll ( '>tspan' ) . length === 1 && node . childNodes . filter ( childNode => ( childNode . nodeType === NodeType . Text || childNode . nodeType === NodeType . CDATA ) && ( childNode as ITextNode ) . textContent ) . length === 0 ) { mergeTspan ( node . querySelector ( '>tspan' ) as ITagNode , node ) ; } } } ) ; resolve ( ) ; } ) ;