import { any , both , equals , not , prop } from 'ramda' ; import { IDocument , ITagNode } from 'svg-vdom' ; import { IRuleOption } from '../../typings' ; import { regularTag } from '../const/regular-tag' ; import { isTag } from '../xml/is-tag' ; export const rmIrregularNesting = async ( dom , { option : { ignore } } ) : Promise < void > => new Promise ( resolve => { const notIgnore = ( node ) => not ( any ( equals ( prop ( 'nodeName' , node ) ) , ignore as string [ ] ) ) ; const tags = dom . querySelectorAll ( both ( isTag , notIgnore ) ) as ITagNode [ ] ; for ( let i = tags . length ; i -- ; ) { const node = tags [ i ] ; const parent = node . parentNode ; if ( ! parent ) { continue ; } let legalRule = regularTag [ parent . nodeName ] ; if ( legalRule . isUndef ) { continue ; } if ( legalRule . legalChildElements . noself && node . nodeName === parent . nodeName ) { node . remove ( ) ; continue ; } if ( legalRule . legalChildElements . transparent ) { const ancestor = ( parent . parentNode as ITagNode ) . closest ( n => n . nodeName !== 'switch' ) ; if ( ! ancestor || ! isTag ( ancestor ) ) { continue ; } legalRule = regularTag [ ancestor . nodeName ] ; if ( legalRule . isUndef ) { continue ; } } if ( legalRule . legalChildElements . any ) { continue ; } else if ( legalRule . legalChildElements . childElements && ! legalRule . legalChildElements . childElements . includes ( node . nodeName ) ) { node . remove ( ) ; } } resolve ( ) ; } ) ;