import { equals } from 'ramda' ; import { IDocument , ITagNode } from 'svg-vdom' ; import { parseNumberList } from '../utils/parse-numberlist' ; export const rmViewBox = async ( dom ) : Promise < void > => new Promise ( resolve => { const viewboxNodes = dom . querySelectorAll ( '[viewBox]' ) as ITagNode [ ] ; viewboxNodes . forEach ( node => { const size = [ '0' , '0' , '0' , '0' ] ; const viewBox = parseNumberList ( node . getAttribute ( 'viewBox' ) as string ) ; if ( viewBox . length !== 4 || viewBox [ 2 ] < 0 || viewBox [ 3 ] < 0 ) { node . removeAttribute ( 'viewBox' ) ; return ; } node . attributes . forEach ( attr => { if ( node . nodeName === 'marker' ) { if ( attr . fullname === 'markerWidth' ) { size [ 2 ] = attr . value . replace ( /px$/ , '' ) ; } else if ( attr . fullname === 'markerHeight' ) { size [ 3 ] = attr . value . replace ( /px$/ , '' ) ; } } else { switch ( attr . fullname ) { case 'x' : size [ 0 ] = attr . value . replace ( /px$/ , '' ) ; break ; case 'y' : size [ 1 ] = attr . value . replace ( /px$/ , '' ) ; break ; case 'width' : size [ 2 ] = attr . value . replace ( /px$/ , '' ) ; break ; case 'height' : size [ 3 ] = attr . value . replace ( /px$/ , '' ) ; break ; default : break ; } } } ) ; if ( equals ( size , viewBox . map ( s => `${ s }` ) ) ) { node . removeAttribute ( 'viewBox' ) ; } } ) ; resolve ( ) ; } ) ;