-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate.js
78 lines (60 loc) · 2.08 KB
/
create.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
import { init, thunk, h } from 'snabbdom'
import hyperx from 'hyperx'
import isBoolAttribute from 'is-boolean-attribute'
export default function create (modules, options={}) {
const directive = options.directive || '@'
function createElement (sel, input, content) {
if (sel === '!--')
return h('!', input.comment)
if (content && content.length) {
if (content.length === 1)
content = content[0]
else
content = [].concat.apply([], content) // flatten nested arrays
}
// attribute names, and handling none faster:
const names = Object.keys(input)
if (!names || !names.length)
return h(sel, content)
// parse Snabbdom's `data` from attributes:
const data = { }
for (let i = 0, max = names.length; max > i; i++) {
const name = names[i]
const isDirective = name.indexOf(directive) === 0
if (isDirective) {
const parts = name.slice(1).split(':')
if ((parts[0] !== 'attrs' || isBoolAttribute(parts[1])) && input[name] === 'false')
input[name] = false
let previous = data
for (let p = 0, pmax = parts.length, last = pmax - 1; p < pmax; p++) {
const part = parts[p]
if (p === last)
previous[part] = input[name]
else if (!previous[part])
previous = previous[part] = { }
else
previous = previous[part]
}
} else {
// put all other attributes into `data.attrs`
if (isBoolAttribute(name) && input[name] === 'false')
input[name] = false
if (!data.attrs)
data.attrs = { }
data.attrs[name] = input[name]
}
}
// return vnode
return h(sel, data, content)
}
// create the snabbdom + hyperx functions
const patch = init(modules || [ ])
// create snabby function
const snabby = hyperx(createElement, { comments: true, attrToProp: false })
// create yo-yo-like update function
snabby.update = function update (dest, src) {
return patch(dest, src)
}
snabby.thunk = thunk
return snabby
}