import { IDocument , INode } from 'svg-vdom' ; import { IRegularAttr } from '../../typings' ; import { ITag } from '../../typings/node' ; import { parseStyle } from './parse' ; const getXlink = ( dom , idStr ) => dom . querySelector ( idStr ) as ITag ; const traversal = ( condition , node ) : void => { for ( const childNode of node . children ) { if ( condition ( childNode ) ) { traversal ( condition , childNode ) ; } } } ; const check = ( styleDefine , node , dom , unique , fromStyleTag , isImportant ) : boolean => { if ( ! node ) return false ; if ( fromStyleTag ) { const childStyleList = parseStyle ( node . getAttribute ( 'style' ) ?? '' ) ; if ( childStyleList . some ( style => { return style . fullname === styleDefine . name && ( ! isImportant || style . important ) ; } ) ) { return false ; } } if ( styleDefine . applyTo . includes ( node . nodeName ) ) return true ; if ( unique . has ( node ) ) { return false ; } unique . add ( node ) ; let result = false ; if ( node . hasAttribute ( 'href' ) ) { result = check ( styleDefine , getXlink ( dom , node . getAttribute ( 'href' ) as string ) , dom , unique , fromStyleTag , false ) ; } else if ( node . hasAttribute ( 'xlink:href' ) ) { result = check ( styleDefine , getXlink ( dom , node . getAttribute ( 'xlink:href' ) as string ) , dom , unique , fromStyleTag , false ) ; } if ( result ) return true ; traversal ( ( childNode ) => { if ( result ) return false ; if ( unique . has ( childNode ) ) return false ; unique . add ( childNode ) ; for ( let i = childNode . attributes . length ; i -- ; ) { const attr = childNode . attributes [ i ] ; if ( attr . fullname === 'style' ) { const childStyleList = parseStyle ( attr . value ) ; if ( childStyleList . some ( style => style . fullname === styleDefine . name ) ) { return false ; } } else if ( attr . fullname === styleDefine . name ) { return false ; } } if ( styleDefine . applyTo . includes ( childNode . nodeName ) ) { result = true ; return false ; } else { if ( childNode . hasAttribute ( 'href' ) ) { if ( check ( styleDefine , getXlink ( dom , childNode . getAttribute ( 'href' ) as string ) , dom , unique , fromStyleTag , false ) ) { result = true ; return false ; } } else if ( childNode . hasAttribute ( 'xlink:href' ) ) { if ( check ( styleDefine , getXlink ( dom , childNode . getAttribute ( 'xlink:href' ) as string ) , dom , unique , fromStyleTag , false ) ) { result = true ; return false ; } } return true ; } } , node ) ; return result ; } ; export const checkApply = ( styleDefine , node , dom , fromStyleTag = false , isImportant = false ) : boolean => check ( styleDefine , node , dom , new Set < INode > ( ) , fromStyleTag , isImportant ) ;