-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (34 loc) · 1.03 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
const classIdSplit = /([\.#]?[a-zA-Z0-9\u007F-\uFFFF_:-]+)/
const notClassId = /^\.|#/
module.exports = (h) => (element, data, children) => {
// null means div
if (element == null)
return h('div', data, children)
// support h(Component) syntax
if (typeof(element) != 'string')
return h(element, data, children)
// support h('div', [ ]) syntax (optional params)
if (data == null || typeof(data) != 'object' || Array.isArray(data)) {
children = data
data = {}
}
// parse parts and assign to classes and id
const parts = element.split(classIdSplit)
for (let part of parts) {
const type = part.charAt(0)
const value = part.substring(1, part.length)
if (type === '.') {
if (!data.class) data.class = {}
data.class[value] = true
}
else if (type === '#' && !data.hasOwnProperty('id')) {
if (!data.attrs) data.attrs = {}
data.attrs.id = value
}
}
// default to div
const tag = notClassId.test(parts[1])
? 'div'
: parts[1]
return h(tag, data, children)
}