forked from Olical/react-faux-dom
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathElement.js
More file actions
325 lines (278 loc) · 7.34 KB
/
Element.js
File metadata and controls
325 lines (278 loc) · 7.34 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var React = require('react')
var clone = require('lodash.clone')
var styleAttr = require('style-attr')
var camelCase = require('lodash.camelcase')
var assign = require('lodash.assign')
var mapValues = require('lodash.mapvalues')
var querySelectorAll = require('query-selector')
function styleCamelCase (name) {
var camel = camelCase(name)
if (name[0] === '-') {
return camel[0].toUpperCase() + camel.slice(1)
} else {
return camel
}
}
function Element (nodeName, parentNode) {
this.nodeName = nodeName
this.parentNode = parentNode
this.children = []
this.eventListeners = {}
this.text = ''
var props = this.props = {
style: {
setProperty: function (name, value) {
props.style[styleCamelCase(name)] = value
},
getProperty: function (name) {
return props.style[styleCamelCase(name)]
},
removeProperty: function (name) {
delete props.style[styleCamelCase(name)]
}
}
}
this.style = props.style
}
Element.prototype.nodeType = 1
// This was easy to do with Vim.
// Just saying.
Element.prototype.eventNameMappings = {
'blur': 'onBlur',
'change': 'onChange',
'click': 'onClick',
'contextmenu': 'onContextMenu',
'copy': 'onCopy',
'cut': 'onCut',
'doubleclick': 'onDoubleClick',
'drag': 'onDrag',
'dragend': 'onDragEnd',
'dragenter': 'onDragEnter',
'dragexit': 'onDragExit',
'dragleave': 'onDragLeave',
'dragover': 'onDragOver',
'dragstart': 'onDragStart',
'drop': 'onDrop',
'error': 'onError',
'focus': 'onFocus',
'input': 'onInput',
'keydown': 'onKeyDown',
'keypress': 'onKeyPress',
'keyup': 'onKeyUp',
'load': 'onLoad',
'mousedown': 'onMouseDown',
'mouseenter': 'onMouseEnter',
'mouseleave': 'onMouseLeave',
'mousemove': 'onMouseMove',
'mouseout': 'onMouseOut',
'mouseover': 'onMouseOver',
'mouseup': 'onMouseUp',
'paste': 'onPaste',
'scroll': 'onScroll',
'submit': 'onSubmit',
'touchcancel': 'onTouchCancel',
'touchend': 'onTouchEnd',
'touchmove': 'onTouchMove',
'touchstart': 'onTouchStart',
'wheel': 'onWheel'
}
Element.prototype.attributeNameMappings = {
'class': 'className'
}
Element.prototype.attributeToPropName = function (name) {
if (name.match(/^data-/)) {
return name
} else if (name.match(/^aria-/)) {
return name
} else {
return this.attributeNameMappings[name] || camelCase(name)
}
}
Element.prototype.setAttribute = function (name, value) {
if (name === 'style' && typeof value === 'string') {
var styles = styleAttr.parse(value)
for (var key in styles) {
this.style.setProperty(key, styles[key])
}
} else {
this.props[this.attributeToPropName(name)] = value
}
}
Element.prototype.getAttribute = function (name) {
return this.props[this.attributeToPropName(name)]
}
Element.prototype.getAttributeNode = function (name) {
var value = this.getAttribute(name)
if (typeof value !== 'undefined') {
return {
value: value,
specified: true
}
}
}
Element.prototype.removeAttribute = function (name) {
delete this.props[this.attributeToPropName(name)]
}
Element.prototype.eventToPropName = function (name) {
return this.eventNameMappings[name] || name
}
Element.prototype.addEventListener = function (name, fn) {
var prop = this.eventToPropName(name)
this.eventListeners[prop] = this.eventListeners[prop] || []
this.eventListeners[prop].push(fn)
}
Element.prototype.removeEventListener = function (name, fn) {
var listeners = this.eventListeners[this.eventToPropName(name)]
if (listeners) {
var match = listeners.indexOf(fn)
if (match !== -1) {
listeners.splice(match, 1)
}
}
}
Element.prototype.appendChild = function (el) {
el.parentNode = this
this.children.push(el)
return el
}
Element.prototype.insertBefore = function (el, before) {
var index = this.children.indexOf(before)
el.parentNode = this
if (index !== -1) {
this.children.splice(index, 0, el)
} else {
this.children.push(el)
}
return el
}
Element.prototype.removeChild = function (child) {
var target = this.children.indexOf(child)
this.children.splice(target, 1)
}
Element.prototype.querySelector = function () {
return this.querySelectorAll.apply(this, arguments)[0]
}
Element.prototype.querySelectorAll = function (selector) {
return querySelectorAll(selector, this)
}
Element.prototype.getElementsByTagName = function (nodeName) {
var children = this.children
if (children.length === 0) {
return []
} else {
var matches
if (nodeName !== '*') {
matches = children.filter(function (el) {
return el.nodeName === nodeName
})
} else {
matches = children
}
var childMatches = children.map(function (el) {
return el.getElementsByTagName(nodeName)
})
return matches.concat.apply(matches, childMatches)
}
}
Element.prototype.getElementById = function (id) {
var children = this.children
if (children.length === 0) {
return null
} else {
var match = children.filter(function (el) {
return el.getAttribute('id') === id
})[0]
if (match) {
return match
} else {
var childMatches = children.map(function (el) {
return el.getElementById(id)
})
return childMatches.filter(function (match) {
return match !== null
})[0] || null
}
}
}
Element.prototype.toReact = function (index) {
index = index || 0
var props = clone(this.props)
props.style = clone(props.style)
function uniqueKey () {
return 'faux-dom-' + index
}
if (typeof props.key === 'undefined') {
props.key = uniqueKey()
}
delete props.style.setProperty
delete props.style.getProperty
delete props.style.removeProperty
assign(props, mapValues(this.eventListeners, function (listeners) {
return function (syntheticEvent) {
var event
if (syntheticEvent) {
event = syntheticEvent.nativeEvent
event.syntheticEvent = syntheticEvent
}
mapValues(listeners, function (listener) {
listener(event)
})
}
}))
return React.createElement(this.nodeName, props, this.text || this.children.map(function (el, i) {
if (el instanceof Element) {
return el.toReact(i)
} else {
return el
}
}))
}
Object.defineProperties(Element.prototype, {
nextSibling: {
get: function () {
var siblings = this.parentNode.children
var me = siblings.indexOf(this)
return siblings[me + 1]
}
},
previousSibling: {
get: function () {
var siblings = this.parentNode.children
var me = siblings.indexOf(this)
return siblings[me - 1]
}
},
innerHTML: {
get: function () {
return this.text
},
set: function (text) {
this.text = text
}
},
textContent: {
get: function () {
return this.text
},
set: function (text) {
this.text = text
}
}
})
// These NS methods are called by things like D3 if it spots a namespace.
// Like xlink:href. I don't care about namespaces, so these functions have NS aliases created.
var namespaceMethods = [
'setAttribute',
'getAttribute',
'getAttributeNode',
'removeAttribute',
'getElementsByTagName',
'getElementById'
]
namespaceMethods.forEach(function (name) {
var fn = Element.prototype[name]
Element.prototype[name + 'NS'] = function () {
return fn.apply(this, Array.prototype.slice.call(arguments, 1))
}
})
module.exports = Element