forked from juliangruber/hyperscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (87 loc) · 2.3 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
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
;(function () {
var ClassList = require("class-list")
function h() {
var args = [].slice.call(arguments), e = null
function item (l) {
var r
function parseClass (string) {
var m = string.split(/([\.#]?[a-zA-Z0-9_-]+)/)
m.forEach(function (v) {
var s = v.substring(1,v.length)
if(!v) return
if(!e)
e = document.createElement(v)
else if (v[0] === '.')
ClassList(e).add(s)
else if (v[0] === '#')
e.setAttribute('id', s)
})
}
if(l == null)
;
else if('string' === typeof l) {
if(!e)
parseClass(l)
else
e.appendChild(r = document.createTextNode(l))
}
else if('number' === typeof l
|| 'boolean' === typeof l
|| l instanceof Date
|| l instanceof RegExp ) {
e.appendChild(r = document.createTextNode(l.toString()))
}
//there might be a better way to handle this...
else if (Array.isArray(l))
l.forEach(item)
else if(l instanceof Node)
e.appendChild(r = l)
else if(l instanceof Text)
e.appendChild(r = l)
else if ('object' === typeof l) {
for (var k in l) {
if('function' === typeof l[k]) {
if(/^on\w+/.test(k)) {
e.addEventListener(k.substring(2), l[k])
} else {
e[k] = l[k]()
l[k](function (v) {
e[k] = v
})
}
}
else if(k === 'style') {
for (var s in l[k]) (function(s, v) {
if('function' === typeof v) {
e.style.setProperty(s, v())
v(function (val) {
e.style.setProperty(s, val)
})
} else
e.style.setProperty(s, l[k][s])
})(s, l[k][s])
} else
e[k] = l[k]
}
} else if ('function' === typeof l) {
//assume it's an observable!
var v = l()
e.appendChild(r = v instanceof Node ? v : document.createTextNode(v))
l(function (v) {
if(v instanceof Node && r.parentElement)
r.parentElement.replaceChild(v, r), r = v
else
r.textContent = v
})
}
return r
}
while(args.length)
item(args.shift())
return e
}
if(typeof module === 'object')
module.exports = h
else
this.hyperscript = h
})()