import { IDocument , ITagNode } from 'svg-vdom' ; import { IRuleOption } from '../../typings' ; import { checkAnimateMotion } from '../animate/check-animate-motion' ; import { animationAttrElements , animationAttributes , animationElements } from '../const/definitions' ; import { regularAttr } from '../const/regular-attr' ; import { regularTag } from '../const/regular-tag' ; import { legalValue } from '../validate/legal-value' ; export const shortenAnimate = async ( dom , { option : { remove } } ) : Promise < void > => new Promise ( resolve => { const animateNodes = dom . querySelectorAll ( animationElements . join ( ',' ) ) as ITagNode [ ] ; animateNodes . forEach ( node => { if ( remove ) { node . remove ( ) ; return ; } const href = node . hasAttribute ( 'href' ) ? node . getAttribute ( 'href' ) : node . getAttribute ( 'xlink:href' ) ; if ( href ) { const target = dom . querySelector ( href ) as ITagNode ; if ( target ) { target . appendChild ( node ) ; } } node . removeAttribute ( 'href' ) ; node . removeAttribute ( 'xlink:href' ) ; if ( animationAttrElements . includes ( node . nodeName ) ) { const attributeName = node . getAttribute ( 'attributeName' ) || '' ; const animateDefine = regularAttr [ attributeName ] ; if ( ! attributeName || ! animateDefine . animatable ) { node . remove ( ) ; return ; } const parentName = ( node . parentNode as ITagNode ) . nodeName ; if ( ! animateDefine . applyTo . includes ( parentName ) && ! regularTag [ parentName ] . ownAttributes . includes ( attributeName ) ) { node . remove ( ) ; return ; } if ( node . nodeName === 'animateTransform' && attributeName !== 'transform' && attributeName !== 'patternTransform' ) { node . remove ( ) ; return ; } for ( const attr of node . attributes ) { if ( animationAttributes . includes ( attr . fullname ) ) { if ( ( attr . fullname !== 'values' && ! legalValue ( animateDefine , attr ) ) ) { node . removeAttribute ( attr . fullname ) ; continue ; } const values = attr . value . split ( ';' ) ; if ( values . every ( val => ! legalValue ( animateDefine , { name : 'values' , fullname : 'values' , namespace : '' , value : val . trim ( ) , } ) ) ) { node . removeAttribute ( attr . fullname ) ; } } } if ( node . nodeName === 'set' && ! node . hasAttribute ( 'to' ) ) { node . remove ( ) ; return ; } if ( ! animationAttributes . some ( key => node . hasAttribute ( key ) ) ) { node . remove ( ) ; return ; } } if ( node . nodeName === 'animateMotion' ) { if ( ! checkAnimateMotion ( node , dom ) ) { node . remove ( ) ; return ; } } } ) ; resolve ( ) ; } ) ;