-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprepareAttributes.js
101 lines (82 loc) · 2.27 KB
/
prepareAttributes.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
var render = require('./render')
var bindModel = require('./bindModel')
module.exports = function (tag, attributes, childElements) {
var dataset
var currentRender = render.currentRender()
if (attributes.binding) {
bindModel(tag, attributes, childElements)
delete attributes.binding
}
var keys = Object.keys(attributes)
for (var k = 0; k < keys.length; k++) {
var key = keys[k]
var attribute = attributes[key]
if (typeof (attribute) === 'function' && currentRender) {
attributes[key] = currentRender.transformFunctionAttribute(key, attribute)
}
var rename = renames[key]
if (rename) {
attributes[rename] = attribute
delete attributes[key]
continue
}
if (dataAttributeRegex.test(key)) {
if (!dataset) {
dataset = attributes.dataset
if (!dataset) {
dataset = attributes.dataset = {}
}
}
var datakey = key
.replace(dataAttributeRegex, '')
.replace(/-([a-z])/ig, function (_, x) { return x.toUpperCase() })
dataset[datakey] = attribute
delete attributes[key]
continue
}
}
if (process.env.NODE_ENV !== 'production' && attributes.__source) {
if (!dataset) {
dataset = attributes.dataset
if (!dataset) {
dataset = attributes.dataset = {}
}
}
dataset.fileName = attributes.__source.fileName
dataset.lineNumber = attributes.__source.lineNumber
}
if (attributes.className) {
attributes.className = generateClassName(attributes.className)
}
if (attributes.innerHTML === false) {
delete attributes.innerHTML
}
return attributes
}
var renames = {
for: 'htmlFor',
class: 'className',
contenteditable: 'contentEditable',
tabindex: 'tabIndex',
colspan: 'colSpan'
}
var dataAttributeRegex = /^data-/
function generateClassName (obj) {
if (typeof (obj) === 'object') {
if (obj instanceof Array) {
var names = obj.map(function (item) {
return generateClassName(item)
})
return names.join(' ') || undefined
} else {
return generateConditionalClassNames(obj)
}
} else {
return obj
}
}
function generateConditionalClassNames (obj) {
return Object.keys(obj).filter(function (key) {
return obj[key]
}).join(' ') || undefined
}