-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
executable file
·226 lines (201 loc) · 5.62 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict'
const m = require('mithril/hyperscript')
const Vnode = require('mithril/render/vnode')
const VOID_TAGS = new RegExp(
'^(?:' +
'area|' +
'base|' +
'br|' +
'col|' +
'command|' +
'embed|' +
'hr|' +
'img|' +
'input|' +
'keygen|' +
'link|' +
'meta|' +
'param|' +
'source|' +
'track|' +
'wbr|' +
'!doctype' +
')$',
'i'
)
const hasOwn = {}.hasOwnProperty
function toStyleKey(str) {
if (str[0] === '-' && str[1] === '-') {
return str
}
return str
.replace(/\W+/g, '-')
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
.toLowerCase()
}
function replaceHtml(m) {
if (m === '&') return '&'
if (m === '<') return '<'
return '>'
}
function replaceAttribute(m) {
if (m === '&') return '&'
if (m === '<') return '<'
if (m === '>') return '>'
return '"'
}
const defaults = {
escapeText(s) {
return s.replace(/[&<>]/g, replaceHtml)
},
escapeAttribute(s) {
return s.replace(/[&<>"]/g, replaceAttribute)
},
}
function bindOpt(options, key) {
return options[key] ? options[key].bind(options) : defaults[key]
}
// Using a generator so I can just yield promises that need awaited. Can't just
// use `async`/`await` since this is used for both sync and async renders. At
// least I have generators (read: coroutines), or I'd have to implement this
// using a giant pushdown automaton. :-)
function* tryRender(view, attrs, options, allowAwait) {
// Fast-path a very simple case. Also lets me perform some renderer
// optimizations later.
if (view == null) return ''
if (view.view || typeof view === 'function') {
// root component
view = m(view, attrs)
options = options || {}
} else {
options = attrs || {}
}
const hooks = []
let result = ''
const escapeAttribute = bindOpt(options, 'escapeAttribute')
const escapeText = bindOpt(options, 'escapeText')
const xml = !!options.xml
const strict = xml || !!options.strict
function write(value) {
result = '' + result + value
}
function* setHooks(source, vnode) {
const promises = []
let waitFor
if (allowAwait)
waitFor = p => {
promises.push(p)
}
if (source.oninit) {
source.oninit.call(vnode.state, vnode, waitFor)
}
if (source.onremove) {
hooks.push(source.onremove.bind(vnode.state, vnode))
}
if (promises.length) yield promises
}
function createAttrString(view) {
for (const key in view.attrs) {
if (hasOwn.call(view.attrs, key)) {
let value = view.attrs[key]
if (value == null || typeof value === 'function') continue
const name = key === 'className' ? 'class' : key
if (name === 'style' && typeof value === 'object') {
const styles = value
const props = []
for (const key of Object.keys(styles)) {
const prop = styles[key]
if (prop) props.push(`${toStyleKey(key)}:${prop}`)
}
if (!props.length) continue
value = props.join(';')
}
if (typeof value === 'boolean') {
if (xml) value = value ? 'true' : 'false'
else if (!value) continue
else value = ''
} else {
value = '' + value
}
write(` ${name}`)
if (strict || value !== '') {
write(`="${escapeAttribute(value)}"`)
}
}
}
}
function* renderComponent(vnode) {
if (typeof vnode.tag !== 'function') {
vnode.state = Object.create(vnode.tag)
} else if (vnode.tag.prototype && vnode.tag.prototype.view) {
vnode.state = new vnode.tag(vnode)
} else {
vnode.state = vnode.tag(vnode)
}
yield* setHooks(vnode.state, vnode)
if (vnode.attrs != null) yield* setHooks(vnode.attrs, vnode)
vnode.instance = Vnode.normalize(vnode.state.view(vnode))
if (vnode.instance != null) yield* renderNode(vnode.instance)
}
function* renderElement(vnode) {
write(`<${vnode.tag}`)
createAttrString(vnode)
// Don't write children for void HTML elements
if (!xml && VOID_TAGS.test(vnode.tag)) {
write(strict ? '/>' : '>')
} else {
write('>')
if (vnode.text != null) {
const text = '' + vnode.text
if (text !== '') write(escapeText(text))
} else {
yield* renderChildren(vnode.children)
}
write(`</${vnode.tag}>`)
}
}
function* renderChildren(vnodes) {
for (const v of vnodes) {
if (v != null) yield* renderNode(v)
}
}
function* renderNode(vnode) {
if (vnode == null) return
if (typeof vnode.tag === 'string') {
vnode.state = {}
if (vnode.attrs != null) yield* setHooks(vnode.attrs, vnode)
switch (vnode.tag) {
case '#':
write(escapeText('' + vnode.children))
break
case '<':
write(vnode.children)
break
case '[':
yield* renderChildren(vnode.children)
break
default:
yield* renderElement(vnode)
}
} else {
yield* renderComponent(vnode)
}
}
yield* renderNode(Vnode.normalize(view))
for (const hook of hooks) hook()
return result.concat() // hint to flatten
}
module.exports = async (view, attrs, options) => {
const iter = tryRender(view, attrs, options, true)
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = iter.next()
if (done) return value
await Promise.all(value)
}
}
module.exports.sync = (view, attrs, options) => {
return tryRender(view, attrs, options, false).next().value
}
module.exports.escapeText = defaults.escapeText
module.exports.escapeAttribute = defaults.escapeAttribute