UNPKG

vdom-virtualize

Version:

Virtualize any DOM node and turn it into a virtual-dom node.

243 lines (215 loc) 6.5 kB
/*! * vdom-virtualize * Copyright 2014 by Marcel Klehr <mklehr@gmx.net> * * (MIT LICENSE) * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ var VNode = require("virtual-dom/vnode/vnode") , VText = require("virtual-dom/vnode/vtext") , VComment = require("./vcomment") module.exports = createVNode function createVNode(domNode, key) { key = key || null // XXX: Leave out `key` for now... merely used for (re-)ordering if(domNode.nodeType == 1) return createFromElement(domNode, key) if(domNode.nodeType == 3) return createFromTextNode(domNode, key) if(domNode.nodeType == 8) return createFromCommentNode(domNode, key) return } function createFromTextNode(tNode) { return new VText(tNode.nodeValue) } function createFromCommentNode(cNode) { return new VComment(cNode.nodeValue) } function createFromElement(el) { var tagName = el.tagName , namespace = el.namespaceURI == 'http://www.w3.org/1999/xhtml'? null : el.namespaceURI , properties = getElementProperties(el) , children = [] for (var i = 0; i < el.childNodes.length; i++) { children.push(createVNode(el.childNodes[i]/*, i*/)) } return new VNode(tagName, properties, children, null, namespace) } function getElementProperties(el) { var obj = {} for(var i=0; i<props.length; i++) { var propName = props[i] if(!el[propName]) continue // Special case: style // .style is a DOMStyleDeclaration, thus we need to iterate over all // rules to create a hash of applied css properties. // // You can directly set a specific .style[prop] = value so patching with vdom // is possible. if("style" == propName) { var css = {} , styleProp if ('undefined' !== typeof el.style.length) { for(var j=0; j<el.style.length; j++) { styleProp = el.style[j] css[styleProp] = el.style.getPropertyValue(styleProp) // XXX: add support for "!important" via getPropertyPriority()! } } else { // IE8 for (var styleProp in el.style) { if (el.style[styleProp] && el.style.hasOwnProperty(styleProp)) { css[styleProp] = el.style[styleProp]; } } } if(Object.keys(css).length) obj[propName] = css continue } // https://msdn.microsoft.com/en-us/library/cc848861%28v=vs.85%29.aspx // The img element does not support the HREF content attribute. // In addition, the href property is read-only for the img Document Object Model (DOM) object if (el.tagName.toLowerCase() === 'img' && propName === 'href') { continue; } // Special case: dataset // we can iterate over .dataset with a simple for..in loop. // The all-time foo with data-* attribs is the dash-snake to camelCase // conversion. // // *This is compatible with h(), but not with every browser, thus this section was removed in favor // of attributes (specified below)!* // // .dataset properties are directly accessible as transparent getters/setters, so // patching with vdom is possible. /*if("dataset" == propName) { var data = {} for(var p in el.dataset) { data[p] = el.dataset[p] } obj[propName] = data return }*/ // Special case: attributes // these are a NamedNodeMap, but we can just convert them to a hash for vdom, // because of https://github.com/Matt-Esch/virtual-dom/blob/master/vdom/apply-properties.js#L57 if("attributes" == propName){ var atts = Array.prototype.slice.call(el[propName]); var hash = {} for(var k=0; k<atts.length; k++){ var name = atts[k].name; if(obj[name] || obj[attrBlacklist[name]]) continue; hash[name] = el.getAttribute(name); } obj[propName] = hash; continue } if("tabIndex" == propName && el.tabIndex === -1) continue // Special case: contentEditable // browser use 'inherit' by default on all nodes, but does not allow setting it to '' // diffing virtualize dom will trigger error // ref: https://github.com/Matt-Esch/virtual-dom/issues/176 if("contentEditable" == propName && el[propName] === 'inherit') continue if('object' === typeof el[propName]) continue // default: just copy the property obj[propName] = el[propName] } return obj } /** * DOMNode property white list * Taken from https://github.com/Raynos/react/blob/dom-property-config/src/browser/ui/dom/DefaultDOMPropertyConfig.js */ var props = module.exports.properties = [ "accept" ,"accessKey" ,"action" ,"alt" ,"async" ,"autoComplete" ,"autoPlay" ,"cellPadding" ,"cellSpacing" ,"checked" ,"className" ,"colSpan" ,"content" ,"contentEditable" ,"controls" ,"crossOrigin" ,"data" //,"dataset" removed since attributes handles data-attributes ,"defer" ,"dir" ,"download" ,"draggable" ,"encType" ,"formNoValidate" ,"href" ,"hrefLang" ,"htmlFor" ,"httpEquiv" ,"icon" ,"id" ,"label" ,"lang" ,"list" ,"loop" ,"max" ,"mediaGroup" ,"method" ,"min" ,"multiple" ,"muted" ,"name" ,"noValidate" ,"pattern" ,"placeholder" ,"poster" ,"preload" ,"radioGroup" ,"readOnly" ,"rel" ,"required" ,"rowSpan" ,"sandbox" ,"scope" ,"scrollLeft" ,"scrolling" ,"scrollTop" ,"selected" ,"span" ,"spellCheck" ,"src" ,"srcDoc" ,"srcSet" ,"start" ,"step" ,"style" ,"tabIndex" ,"target" ,"title" ,"type" ,"value" // Non-standard Properties ,"autoCapitalize" ,"autoCorrect" ,"property" , "attributes" ] var attrBlacklist = module.exports.attrBlacklist = { 'class': 'className' }