import { isDef , isString , isArray , isObject } from './types' ; import { setAttr , addClass } from './dom' ; function dataProps ( vnode , node , name ) { for ( const prop of Object . keys ( vnode . attributes [ name ] ) ) { setAttr ( node , `data-${ prop }` , vnode . attributes [ name ] [ prop ] ) ; } } function classProps ( vnode , node , name ) { if ( isString ( vnode . attributes [ name ] ) ) { node . className = vnode . attributes [ name ] ; } else if ( isArray ( vnode . attributes [ name ] ) ) { vnode . attributes [ name ] . forEach ( ( value ) => { addClass ( node , value ) ; } ) ; } } function styleProps ( vnode , node , name ) { if ( isString ( vnode . attributes [ name ] ) ) { node . style = vnode . attributes [ name ] ; } else if ( isObject ( vnode . attributes [ name ] ) ) { for ( const prop of Object . keys ( vnode . attributes [ name ] ) ) { node . style [ prop ] = vnode . attributes [ name ] [ prop ] ; } } } function diffProps ( vnode , node ) { for ( const name of Object . keys ( vnode . attributes ) ) { if ( name === 'class' ) { classProps ( vnode , node , name ) ; } else if ( name === 'style' ) { styleProps ( vnode , node , name ) ; } else if ( name === 'data' ) { dataProps ( vnode , node , name ) ; } else { setAttr ( node , name , vnode . attributes [ name ] ) ; } } } function h ( nodeName , attributes , ... args ) { const vnode = { nodeName } ; if ( attributes ) vnode . attributes = attributes ; if ( args . length ) vnode . children = [ ] . concat ( ... args ) ; return vnode ; } export function render ( vnode , parentDom ? ) { if ( vnode . split ) return document . createTextNode ( vnode ) ; const node = document . createElement ( vnode . nodeName ) ; if ( isDef ( vnode . attributes ) ) { diffProps ( vnode , node ) ; } ( vnode . children || [ ] ) . forEach ( ( c ) => node . appendChild ( render ( c ) ) ) ; return parentDom ? parentDom . appendChild ( node ) : node ; } export { h as el } ; export { h as createElement } ;