import { IDocument , ITagNode } from 'svg-vdom' ; import { hasProp } from '../utils/has-prop' ; interface IXmlnsDefineUnit { target : ITagNode ; count : number ; } interface IXmlnsDefine { [ propName ] : IXmlnsDefineUnit ; } export const rmXMLNS = async ( dom ) : Promise < void > => new Promise ( resolve => { const traversalNode = ( node , nsStack ) => { const xmlnsObj = { } ; Object . assign ( xmlnsObj , nsStack [ nsStack . length - 1 ] ) ; if ( node . namespace ) { if ( hasProp ( xmlnsObj , node . namespace ) ) { xmlnsObj [ node . namespace ] . count ++ ; } else { node . remove ( ) ; return ; } } for ( let i = node . attributes . length ; i -- ; ) { const attr = node . attributes [ i ] ; if ( attr . namespace === 'xmlns' ) { xmlnsObj [ attr . name ] = { target : node , count : 0 , } ; } else if ( attr . namespace ) { if ( hasProp ( xmlnsObj , attr . namespace ) ) { xmlnsObj [ attr . namespace ] . count ++ ; } else if ( attr . fullname !== 'xml:space' ) { node . removeAttribute ( attr . fullname ) ; } } } nsStack . push ( xmlnsObj ) ; node . children . forEach ( childNode => { traversalNode ( childNode , nsStack ) ; } ) ; Object . keys ( xmlnsObj ) . forEach ( ns => { if ( xmlnsObj [ ns ] . count === 0 && xmlnsObj [ ns ] . target === node ) { node . removeAttribute ( `xmlns:${ ns }` ) ; } } ) ; nsStack . pop ( ) ; } ; dom . children . forEach ( node => { traversalNode ( node , [ { } ] ) ; } ) ; resolve ( ) ; } ) ;