-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (148 loc) · 4.65 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
export default (h, text, doc) => {
text = text || (str => str)
doc = doc || document
const isObj = X => X && typeof X == 'object' && !(X instanceof Array)
const merge = (X, Y) => isObj(X) && isObj(Y) ? {...X, ...Y} : Y
const setAttr = (old, value) =>
typeof old == "string" && typeof value == "string" ? old+value : value
const compile = element => {
if (element.nodeType != 1) {
return () => [text(element.textContent)]
}
const tag = element.tagName.toLowerCase()
const attributes = Array.from(element.attributes).map(({
nodeName,
nodeValue
}) => ({
key: nodeName,
value: element.getAttribute(nodeName)
}))
const childNodes = Array.from(
(tag == 'template' ? element.content : element).childNodes
).map(child => compile(child))
return (scope, word, rootChildren, rootScope) => attributes.reduce(
(nodes, attrs) => nodes.reduce((nodes, data) => {
const {scope, attributes} = data
var {key, value} = attrs
if (key.substr(0, 1) == ':' || key.substr(key.length - 1) == ':') {
key = key.substr(0, 1) == ':' ?
key.substr(1) : key.substr(0, key.length - 1)
if (value == '') {
value = scope
} else if (scope && typeof scope == 'object' && scope[value] != null) {
value = scope[value]
} else {
value = null
}
}
if (key == 'each') {
if (value instanceof Array) {
value.forEach(row => {
nodes.push({
attributes: {...attributes},
scope: merge(scope, row)
})
})
}
} else if (key == 'with') {
if (value != null) {
nodes.push({
...data,
scope: merge(scope, value)
})
}
} else if (key == 'if' || key == 'not') {
if ((key == 'if' && value) || (key == 'not' && !value)) {
nodes.push(data)
}
} else if (key == 'show' || key == 'hide') {
if ((key == 'hide' && value) || (key == 'show' && !value)) {
attributes.style = 'display: none;'+
(typeof attributes.style == 'string' ? attributes.style : '')
}
nodes.push({
...data,
attributes: attributes
})
} else if (key == 'case') {
if (word != null && word == value) {
nodes.push(data)
}
} else if (key == 'switch') {
nodes.push({
...data,
word: value
})
} else if (key == 'bind') {
if (value && typeof value == 'object') {
Object.keys(value).forEach(key => {
attributes[key] = setAttr(attributes[key], value[key])
})
}
nodes.push({
...data,
attributes: attributes
})
} else {
attributes[key] = setAttr(attributes[key], value)
nodes.push({
...data,
attributes: attributes
})
}
return nodes
}, [])
, [{
scope: tag == 'slot' ? rootScope : scope,
attributes: {}
}]).reduce((nodes, {scope, attributes, word}) => {
const children = (tag == 'slot' ? rootChildren : childNodes)
.reduce((result, render) => {
render(
scope, word, rootChildren || childNodes, rootScope
).forEach(child => {
result.push(child)
})
return result
}, []).filter(c => c)
const tpl = tag.indexOf('-') >= 0 ? doc.getElementById(tag) : null
if (attributes.text != null && tpl == null) {
while (children.length) {
children.pop()
}
children.push(text(attributes.text))
attributes = {...attributes}
delete attributes.text
}
var it = null
if (tag == 'template' || tag == 'slot') {
it = children
} else if (tpl != null) {
it = compile(tpl)(attributes, null, childNodes, scope)
} else {
it = [h(tag, attributes, children)]
}
it.forEach(child => {
nodes.push(child)
})
return nodes
}, [])
}
return (element, template) => {
if (template) {
const tag = element.tagName.toLowerCase()
const attributes = Array.from(element.attributes).reduce((X, {
attrName,
attrValue
}) => ({
...X,
[attrName]: attrValue
}), {})
const view = compile(template)
return scope => h(tag, attributes, view(scope))
} else {
const render = compile(element)
return scope => render(scope)[0]
}
}
}