-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathattributes.js
62 lines (50 loc) · 1.45 KB
/
attributes.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
var forOwn = require('lodash.forown')
var escape = require('lodash.escape')
var union = require('lodash.union')
var parseSelector = require('../parse-selector')
// data.attrs, data.props, data.class
module.exports = function attributes (vnode) {
var selector = parseSelector(vnode.sel)
var parsedClasses = selector.className.split(' ')
var attributes = []
var classes = []
var values = {}
if (selector.id) {
values.id = selector.id
}
setAttributes(vnode.data.props, values)
setAttributes(vnode.data.attrs, values) // `attrs` override `props`, not sure if this is good so
if (vnode.data.class) {
// Omit `className` attribute if `class` is set on vnode
values.class = undefined
}
forOwn(vnode.data.class, function (value, key) {
if (value === true) {
classes.push(key)
}
})
classes = union(classes, values.class, parsedClasses).filter(x => x !== '')
if (classes.length) {
values.class = classes.join(' ')
}
forOwn(values, function (value, key) {
attributes.push(value === true ? key : `${key}="${escape(value)}"`)
})
return attributes.length ? attributes.join(' ') : ''
}
function setAttributes (values, target) {
forOwn(values, function (value, key) {
if (key === 'htmlFor') {
target['for'] = value
return
}
if (key === 'className') {
target['class'] = value.split(' ')
return
}
if (key === 'innerHTML') {
return
}
target[key] = value
})
}