tiny-dream
Version:
A tiny Incremental DOM implementation for modern browsers.
52 lines (42 loc) • 1.29 kB
JavaScript
import initNodeData from './init-node-data'
import getNodeData from './get-node-data'
const isElementNode = (node) => {
try {
// for browsers supporting W3 DOM2 like Firefox, Opera and Chrome
return (node instanceof HTMLElement)
} catch (e) {
// for IE
return (typeof node === 'object')
&& (node.nodeType === 1)
&& (typeof node.style === 'object')
&& (typeof node.ownerDocument === 'object')
}
}
const importNode = (node) => {
if (node['__TinyDreamDOMData']) {
return
}
const isElement = isElementNode(node)
const nodeName = (isElement) ? node.localName : node.nodeName
const key = isElement ? node.getAttribute('key') : null
const typeId = node['typeId']
const data = initNodeData(node, nodeName, key, typeId)
if (key) {
getNodeData(node.parentNode).keyMap[key] = node
}
if (isElement) {
const attributes = node.attributes
const attrsArr = data.attributes
for (let i = 0; i < attributes.length; i += 1) {
const attr = attributes[i]
const name = attr.name
const value = attr.value
attrsArr.push(name)
attrsArr.push(value)
}
}
for (let child = node.firstChild; child; child = child.nextSibling) {
importNode(child)
}
}
export default importNode