-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
242 lines (215 loc) · 6.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*!
* vdom-virtualize
* Copyright 2014 by Marcel Klehr <[email protected]>
*
* (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'
}