import { DOMRenderOptions , Style , RendererNode } from '../shared/svu' ; import { isString , isOn , } from '../shared' ; function patchClass ( el , value ) { if ( value == null ) { value = '' ; } el . className = value ; } function patchStyle ( el , prev , next ) { const style = ( el as HTMLElement ) . style ; if ( ! next ) { el . removeAttribute ( 'style' ) ; } else if ( isString ( next ) ) { if ( prev !== next ) { style . cssText = next ; } } else { for ( const key in next ) { style [ key as any ] = next [ key ] ; } } } function patchAttr ( el , key , value ) { if ( value == null ) { el . removeAttribute ( key ) ; } else { el . setAttribute ( key , value ) ; } } function patchEvent ( el , rawName , nextValue ) { const invokers = el . _vei || ( el . _vei = { } ) ; const existingInvoker = invokers [ rawName ] ; if ( nextValue && existingInvoker ) { existingInvoker . value = nextValue ; } else { const name = rawName . replace ( 'on' , '' ) . toLowerCase ( ) ; if ( nextValue ) { el . addEventListener ( name , nextValue ) ; } else if ( existingInvoker ) { el . removeEventListener ( name , existingInvoker ) ; invokers [ rawName ] = undefined ; } } } export const patchProp = ( el , key , prevValue , nextValue ) => { switch ( key ) { case 'class' : patchClass ( el , nextValue ) ; break ; case 'style' : patchStyle ( el , prevValue , nextValue ) ; break ; default : if ( isOn ( key ) ) { patchEvent ( el , key , nextValue ) ; } else { patchAttr ( el , key , nextValue ) ; } } } ;