@talyaron1/mithril 0.0.0-stage → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +116 -2
- package/api/mount-redraw.js +49 -0
- package/api/router.js +252 -0
- package/browser.js +5 -0
- package/hyperscript.js +8 -0
- package/index.js +26 -0
- package/mithril.js +1775 -0
- package/mithril.min.js +1 -0
- package/mount-redraw.js +5 -0
- package/mount.js +3 -0
- package/package.json +36 -4
- package/pathname/build.js +45 -0
- package/pathname/compileTemplate.js +44 -0
- package/pathname/parse.js +25 -0
- package/querystring/build.js +26 -0
- package/querystring/parse.js +48 -0
- package/redraw.js +3 -0
- package/render/cachedAttrsIsStaticMap.js +12 -0
- package/render/delayedRemoval.js +3 -0
- package/render/domFor.js +24 -0
- package/render/emptyAttrs.js +4 -0
- package/render/fragment.js +13 -0
- package/render/hyperscript.js +101 -0
- package/render/hyperscriptVnode.js +23 -0
- package/render/render.js +964 -0
- package/render/trust.js +8 -0
- package/render/vnode.js +34 -0
- package/render.js +3 -0
- package/request/request.js +205 -0
- package/request.js +5 -0
- package/route.js +5 -0
- package/stream/stream.js +267 -0
- package/stream/stream.min.js +1 -0
- package/stream.js +3 -0
- package/util/censor.js +47 -0
- package/util/decodeURIComponentSafe.js +35 -0
- package/util/hasOwn.js +4 -0
package/mithril.js
ADDED
|
@@ -0,0 +1,1775 @@
|
|
|
1
|
+
;(function() {
|
|
2
|
+
"use strict"
|
|
3
|
+
function Vnode(tag, key, attrs0, children, text, dom) {
|
|
4
|
+
return {tag: tag, key: key, attrs: attrs0, children: children, text: text, dom: dom, is: undefined, domSize: undefined, state: undefined, events: undefined, instance: undefined}
|
|
5
|
+
}
|
|
6
|
+
Vnode.normalize = function(node) {
|
|
7
|
+
if (Array.isArray(node)) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined)
|
|
8
|
+
if (node == null || typeof node === "boolean") return null
|
|
9
|
+
if (typeof node === "object") return node
|
|
10
|
+
return Vnode("#", undefined, undefined, String(node), undefined, undefined)
|
|
11
|
+
}
|
|
12
|
+
Vnode.normalizeChildren = function(input) {
|
|
13
|
+
// Preallocate the array length (initially holey) and fill every index immediately in order.
|
|
14
|
+
// Benchmarking shows better performance on V8.
|
|
15
|
+
var children = new Array(input.length)
|
|
16
|
+
// Count the number of keyed normalized vnodes for consistency check.
|
|
17
|
+
// Note: this is a perf-sensitive check.
|
|
18
|
+
// Fun fact: merging the loop like this is somehow faster than splitting
|
|
19
|
+
// the check within updateNodes(), noticeably so.
|
|
20
|
+
var numKeyed = 0
|
|
21
|
+
for (var i = 0; i < input.length; i++) {
|
|
22
|
+
children[i] = Vnode.normalize(input[i])
|
|
23
|
+
if (children[i] !== null && children[i].key != null) numKeyed++
|
|
24
|
+
}
|
|
25
|
+
if (numKeyed !== 0 && numKeyed !== input.length) {
|
|
26
|
+
throw new TypeError(children.includes(null)
|
|
27
|
+
? "In fragments, vnodes must either all have keys or none have keys. You may wish to consider using an explicit keyed empty fragment, m.fragment({key: ...}), instead of a hole."
|
|
28
|
+
: "In fragments, vnodes must either all have keys or none have keys."
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
return children
|
|
32
|
+
}
|
|
33
|
+
// Note: the processing of variadic parameters is perf-sensitive.
|
|
34
|
+
//
|
|
35
|
+
// In native ES6, it might be preferable to define hyperscript and fragment
|
|
36
|
+
// factories with a final ...args parameter and call hyperscriptVnode(...args),
|
|
37
|
+
// since modern engines can optimize spread calls.
|
|
38
|
+
//
|
|
39
|
+
// However, benchmarks showed this was not faster. As a result, spread is used
|
|
40
|
+
// only in the parameter lists of hyperscript and fragment, while an array is
|
|
41
|
+
// passed to hyperscriptVnode.
|
|
42
|
+
var hyperscriptVnode = function(attrs1, children0) {
|
|
43
|
+
if (attrs1 == null || typeof attrs1 === "object" && attrs1.tag == null && !Array.isArray(attrs1)) {
|
|
44
|
+
if (children0.length === 1 && Array.isArray(children0[0])) children0 = children0[0]
|
|
45
|
+
} else {
|
|
46
|
+
children0 = children0.length === 0 && Array.isArray(attrs1) ? attrs1 : [attrs1, ...children0]
|
|
47
|
+
attrs1 = undefined
|
|
48
|
+
}
|
|
49
|
+
return Vnode("", attrs1 && attrs1.key, attrs1, children0)
|
|
50
|
+
}
|
|
51
|
+
// This exists so I'm only saving it once.
|
|
52
|
+
var hasOwn = {}.hasOwnProperty
|
|
53
|
+
// This is an attrs object that is used by default when attrs is undefined or null.
|
|
54
|
+
var emptyAttrs = {}
|
|
55
|
+
// This Map manages the following:
|
|
56
|
+
// - Whether an attrs is cached attrs generated by compileSelector().
|
|
57
|
+
// - Whether the cached attrs is "static", i.e., does not contain any form attributes.
|
|
58
|
+
// These information will be useful to skip updating attrs in render().
|
|
59
|
+
//
|
|
60
|
+
// Since the attrs used as keys in this map are not released from the selectorCache object,
|
|
61
|
+
// there is no risk of memory leaks. Therefore, Map is used here instead of WeakMap.
|
|
62
|
+
var cachedAttrsIsStaticMap = new Map([[emptyAttrs, true]])
|
|
63
|
+
var selectorParser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[(.+?)(?:\s*=\s*("|'|)((?:\\["'\]]|.)*?)\5)?\])/g
|
|
64
|
+
var selectorCache = Object.create(null)
|
|
65
|
+
function isEmpty(object) {
|
|
66
|
+
for (var key in object) if (hasOwn.call(object, key)) return false
|
|
67
|
+
return true
|
|
68
|
+
}
|
|
69
|
+
function isFormAttributeKey(key) {
|
|
70
|
+
return key === "value" || key === "checked" || key === "selectedIndex" || key === "selected"
|
|
71
|
+
}
|
|
72
|
+
function compileSelector(selector) {
|
|
73
|
+
var match, tag = "div", classes = [], attrs = {}, isStatic = true
|
|
74
|
+
while (match = selectorParser.exec(selector)) {
|
|
75
|
+
var type = match[1], value = match[2]
|
|
76
|
+
if (type === "" && value !== "") tag = value
|
|
77
|
+
else if (type === "#") attrs.id = value
|
|
78
|
+
else if (type === ".") classes.push(value)
|
|
79
|
+
else if (match[3][0] === "[") {
|
|
80
|
+
var attrValue = match[6]
|
|
81
|
+
if (attrValue) attrValue = attrValue.replace(/\\(["'])/g, "$1").replace(/\\\\/g, "\\")
|
|
82
|
+
if (match[4] === "class") classes.push(attrValue)
|
|
83
|
+
else {
|
|
84
|
+
attrs[match[4]] = attrValue === "" ? attrValue : attrValue || true
|
|
85
|
+
if (isFormAttributeKey(match[4])) isStatic = false
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (classes.length > 0) attrs.className = classes.join(" ")
|
|
90
|
+
if (isEmpty(attrs)) attrs = emptyAttrs
|
|
91
|
+
else cachedAttrsIsStaticMap.set(attrs, isStatic)
|
|
92
|
+
return selectorCache[selector] = {tag: tag, attrs: attrs, is: attrs.is}
|
|
93
|
+
}
|
|
94
|
+
function execSelector(state, vnode) {
|
|
95
|
+
vnode.tag = state.tag
|
|
96
|
+
var attrs = vnode.attrs
|
|
97
|
+
if (attrs == null) {
|
|
98
|
+
vnode.attrs = state.attrs
|
|
99
|
+
vnode.is = state.is
|
|
100
|
+
return vnode
|
|
101
|
+
}
|
|
102
|
+
if (hasOwn.call(attrs, "class")) {
|
|
103
|
+
if (attrs.class != null) attrs.className = attrs.class
|
|
104
|
+
attrs.class = null
|
|
105
|
+
}
|
|
106
|
+
if (state.attrs !== emptyAttrs) {
|
|
107
|
+
var className = attrs.className
|
|
108
|
+
attrs = Object.assign({}, state.attrs, attrs)
|
|
109
|
+
if (state.attrs.className != null) attrs.className =
|
|
110
|
+
className != null
|
|
111
|
+
? String(state.attrs.className) + " " + String(className)
|
|
112
|
+
: state.attrs.className
|
|
113
|
+
}
|
|
114
|
+
// workaround for #2622 (reorder keys in attrs to set "type" first)
|
|
115
|
+
// The DOM does things to inputs based on the "type", so it needs set first.
|
|
116
|
+
// See: https://github.com/MithrilJS/mithril.js/issues/2622
|
|
117
|
+
if (state.tag === "input" && hasOwn.call(attrs, "type")) {
|
|
118
|
+
attrs = Object.assign({type: attrs.type}, attrs)
|
|
119
|
+
}
|
|
120
|
+
// This reduces the complexity of the evaluation of "is" within the render function.
|
|
121
|
+
vnode.is = attrs.is
|
|
122
|
+
vnode.attrs = attrs
|
|
123
|
+
return vnode
|
|
124
|
+
}
|
|
125
|
+
function hyperscript(selector, attrs, ...children) {
|
|
126
|
+
if (selector == null || typeof selector !== "string" && typeof selector !== "function" && typeof selector.view !== "function") {
|
|
127
|
+
throw Error("The selector must be either a string or a component.");
|
|
128
|
+
}
|
|
129
|
+
var vnode = hyperscriptVnode(attrs, children)
|
|
130
|
+
if (typeof selector === "string") {
|
|
131
|
+
vnode.children = Vnode.normalizeChildren(vnode.children)
|
|
132
|
+
if (selector !== "[") return execSelector(selectorCache[selector] || compileSelector(selector), vnode)
|
|
133
|
+
}
|
|
134
|
+
if (vnode.attrs == null) vnode.attrs = {}
|
|
135
|
+
vnode.tag = selector
|
|
136
|
+
return vnode
|
|
137
|
+
}
|
|
138
|
+
hyperscript.trust = function(html) {
|
|
139
|
+
if (html == null) html = ""
|
|
140
|
+
return Vnode("<", undefined, undefined, html, undefined, undefined)
|
|
141
|
+
}
|
|
142
|
+
hyperscript.fragment = function(attrs4, ...children1) {
|
|
143
|
+
var vnode2 = hyperscriptVnode(attrs4, children1)
|
|
144
|
+
if (vnode2.attrs == null) vnode2.attrs = {}
|
|
145
|
+
vnode2.tag = "["
|
|
146
|
+
vnode2.children = Vnode.normalizeChildren(vnode2.children)
|
|
147
|
+
return vnode2
|
|
148
|
+
}
|
|
149
|
+
var delayedRemoval = new WeakMap
|
|
150
|
+
function *domFor(vnode4) {
|
|
151
|
+
// To avoid unintended mangling of the internal bundler,
|
|
152
|
+
// parameter destructuring is not used here.
|
|
153
|
+
var dom = vnode4.dom
|
|
154
|
+
var domSize0 = vnode4.domSize
|
|
155
|
+
var generation0 = delayedRemoval.get(dom)
|
|
156
|
+
if (dom != null) do {
|
|
157
|
+
var nextSibling = dom.nextSibling
|
|
158
|
+
if (delayedRemoval.get(dom) === generation0) {
|
|
159
|
+
yield dom
|
|
160
|
+
domSize0--
|
|
161
|
+
}
|
|
162
|
+
dom = nextSibling
|
|
163
|
+
}
|
|
164
|
+
while (domSize0)
|
|
165
|
+
}
|
|
166
|
+
var _14 = function() {
|
|
167
|
+
var nameSpace = {
|
|
168
|
+
svg: "http://www.w3.org/2000/svg",
|
|
169
|
+
math: "http://www.w3.org/1998/Math/MathML"
|
|
170
|
+
}
|
|
171
|
+
var currentRedraw
|
|
172
|
+
var currentRender
|
|
173
|
+
var currentErrors
|
|
174
|
+
function getDocument(dom) {
|
|
175
|
+
return dom.ownerDocument;
|
|
176
|
+
}
|
|
177
|
+
function getNameSpace(vnode3) {
|
|
178
|
+
return vnode3.attrs && vnode3.attrs.xmlns || nameSpace[vnode3.tag]
|
|
179
|
+
}
|
|
180
|
+
//sanity check to discourage people from doing `vnode.state = ...`
|
|
181
|
+
function checkState(vnode3, original) {
|
|
182
|
+
if (vnode3.state !== original) throw new Error("'vnode.state' must not be modified.")
|
|
183
|
+
}
|
|
184
|
+
//Note: the hook is passed as the `this` argument to allow proxying the
|
|
185
|
+
//arguments without requiring a full array allocation to do so. It also
|
|
186
|
+
//takes advantage of the fact the current `vnode` is the first argument in
|
|
187
|
+
//all lifecycle methods.
|
|
188
|
+
function callHook(vnode3) {
|
|
189
|
+
var original = vnode3.state
|
|
190
|
+
try {
|
|
191
|
+
return this.apply(original, arguments)
|
|
192
|
+
} finally {
|
|
193
|
+
checkState(vnode3, original)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// IE11 (at least) throws an UnspecifiedError when accessing document.activeElement when
|
|
197
|
+
// inside an iframe. Catch and swallow this error, and heavy-handidly return null.
|
|
198
|
+
function activeElement(dom) {
|
|
199
|
+
try {
|
|
200
|
+
return getDocument(dom).activeElement
|
|
201
|
+
} catch (e) {
|
|
202
|
+
return null
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
//create
|
|
206
|
+
function createNodes(parent, vnodes, start, end, hooks, nextSibling, ns) {
|
|
207
|
+
for (var i = start; i < end; i++) {
|
|
208
|
+
var vnode3 = vnodes[i]
|
|
209
|
+
if (vnode3 != null) {
|
|
210
|
+
createNode(parent, vnode3, hooks, ns, nextSibling)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function createNode(parent, vnode3, hooks, ns, nextSibling) {
|
|
215
|
+
var tag = vnode3.tag
|
|
216
|
+
if (typeof tag === "string") {
|
|
217
|
+
vnode3.state = {}
|
|
218
|
+
if (vnode3.attrs != null) initLifecycle(vnode3.attrs, vnode3, hooks)
|
|
219
|
+
switch (tag) {
|
|
220
|
+
case "#": createText(parent, vnode3, nextSibling); break
|
|
221
|
+
case "<": createHTML(parent, vnode3, ns, nextSibling); break
|
|
222
|
+
case "[": createFragment(parent, vnode3, hooks, ns, nextSibling); break
|
|
223
|
+
default: createElement(parent, vnode3, hooks, ns, nextSibling)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else createComponent(parent, vnode3, hooks, ns, nextSibling)
|
|
227
|
+
}
|
|
228
|
+
function createText(parent, vnode3, nextSibling) {
|
|
229
|
+
vnode3.dom = getDocument(parent).createTextNode(vnode3.children)
|
|
230
|
+
insertDOM(parent, vnode3.dom, nextSibling)
|
|
231
|
+
}
|
|
232
|
+
var possibleParents = {caption: "table", thead: "table", tbody: "table", tfoot: "table", tr: "tbody", th: "tr", td: "tr", colgroup: "table", col: "colgroup"}
|
|
233
|
+
function createHTML(parent, vnode3, ns, nextSibling) {
|
|
234
|
+
var match0 = vnode3.children.match(/^\s*?<(\w+)/im) || []
|
|
235
|
+
// not using the proper parent makes the child element(s) vanish.
|
|
236
|
+
// var div = document.createElement("div")
|
|
237
|
+
// div.innerHTML = "<td>i</td><td>j</td>"
|
|
238
|
+
// console.log(div.innerHTML)
|
|
239
|
+
// --> "ij", no <td> in sight.
|
|
240
|
+
var temp = getDocument(parent).createElement(possibleParents[match0[1]] || "div")
|
|
241
|
+
if (ns === "http://www.w3.org/2000/svg") {
|
|
242
|
+
temp.innerHTML = "<svg xmlns=\"http://www.w3.org/2000/svg\">" + vnode3.children + "</svg>"
|
|
243
|
+
temp = temp.firstChild
|
|
244
|
+
} else {
|
|
245
|
+
temp.innerHTML = vnode3.children
|
|
246
|
+
}
|
|
247
|
+
vnode3.dom = temp.firstChild
|
|
248
|
+
vnode3.domSize = temp.childNodes.length
|
|
249
|
+
var fragment = getDocument(parent).createDocumentFragment()
|
|
250
|
+
var child
|
|
251
|
+
while (child = temp.firstChild) {
|
|
252
|
+
fragment.appendChild(child)
|
|
253
|
+
}
|
|
254
|
+
insertDOM(parent, fragment, nextSibling)
|
|
255
|
+
}
|
|
256
|
+
function createFragment(parent, vnode3, hooks, ns, nextSibling) {
|
|
257
|
+
var fragment = getDocument(parent).createDocumentFragment()
|
|
258
|
+
if (vnode3.children != null) {
|
|
259
|
+
var children2 = vnode3.children
|
|
260
|
+
createNodes(fragment, children2, 0, children2.length, hooks, null, ns)
|
|
261
|
+
}
|
|
262
|
+
vnode3.dom = fragment.firstChild
|
|
263
|
+
vnode3.domSize = fragment.childNodes.length
|
|
264
|
+
insertDOM(parent, fragment, nextSibling)
|
|
265
|
+
}
|
|
266
|
+
function createElement(parent, vnode3, hooks, ns, nextSibling) {
|
|
267
|
+
var tag = vnode3.tag
|
|
268
|
+
var attrs5 = vnode3.attrs
|
|
269
|
+
var is = vnode3.is
|
|
270
|
+
ns = getNameSpace(vnode3) || ns
|
|
271
|
+
var element = ns ?
|
|
272
|
+
is ? getDocument(parent).createElementNS(ns, tag, {is: is}) : getDocument(parent).createElementNS(ns, tag) :
|
|
273
|
+
is ? getDocument(parent).createElement(tag, {is: is}) : getDocument(parent).createElement(tag)
|
|
274
|
+
vnode3.dom = element
|
|
275
|
+
if (attrs5 != null) {
|
|
276
|
+
setAttrs(vnode3, attrs5, ns)
|
|
277
|
+
}
|
|
278
|
+
insertDOM(parent, element, nextSibling)
|
|
279
|
+
if (!maybeSetContentEditable(vnode3)) {
|
|
280
|
+
if (vnode3.children != null) {
|
|
281
|
+
var children2 = vnode3.children
|
|
282
|
+
createNodes(element, children2, 0, children2.length, hooks, null, ns)
|
|
283
|
+
if (vnode3.tag === "select" && attrs5 != null) setLateSelectAttrs(vnode3, attrs5)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
// A component instance whose initialization failed is replaced by an inert one:
|
|
288
|
+
// it renders nothing and is never re-initialized (see #1311), but the rest of
|
|
289
|
+
// the tree keeps working and it can still be updated and removed safely.
|
|
290
|
+
function inertView() {}
|
|
291
|
+
function disableComponent(vnode3) {
|
|
292
|
+
vnode3.state = {view: inertView}
|
|
293
|
+
vnode3.instance = undefined
|
|
294
|
+
}
|
|
295
|
+
// Errors thrown by a component are collected here so the render pass can finish
|
|
296
|
+
// and leave the vnode tree in sync with the DOM. They are rethrown at the end of
|
|
297
|
+
// `render()`.
|
|
298
|
+
function deferError(e) {
|
|
299
|
+
if (currentErrors == null) currentErrors = [e]
|
|
300
|
+
else currentErrors.push(e)
|
|
301
|
+
}
|
|
302
|
+
function initComponent(vnode3, hooks) {
|
|
303
|
+
var sentinel
|
|
304
|
+
var isObject = typeof vnode3.tag.view === "function"
|
|
305
|
+
if (isObject) {
|
|
306
|
+
vnode3.state = Object.create(vnode3.tag)
|
|
307
|
+
sentinel = vnode3.state.view
|
|
308
|
+
if (sentinel.$$reentrantLock$$ != null) return
|
|
309
|
+
} else {
|
|
310
|
+
sentinel = vnode3.tag
|
|
311
|
+
// There is no state to fall back to here, so make it inert instead of
|
|
312
|
+
// leaving `vnode.state` undefined.
|
|
313
|
+
if (sentinel.$$reentrantLock$$ != null) return disableComponent(vnode3)
|
|
314
|
+
}
|
|
315
|
+
sentinel.$$reentrantLock$$ = true
|
|
316
|
+
try {
|
|
317
|
+
if (!isObject) {
|
|
318
|
+
vnode3.state = (vnode3.tag.prototype != null && typeof vnode3.tag.prototype.view === "function") ? new vnode3.tag(vnode3) : vnode3.tag(vnode3)
|
|
319
|
+
}
|
|
320
|
+
initLifecycle(vnode3.state, vnode3, hooks)
|
|
321
|
+
if (vnode3.attrs != null) initLifecycle(vnode3.attrs, vnode3, hooks)
|
|
322
|
+
vnode3.instance = Vnode.normalize(callHook.call(vnode3.state.view, vnode3))
|
|
323
|
+
if (vnode3.instance === vnode3) throw Error("A view cannot return the vnode it received as argument")
|
|
324
|
+
} finally {
|
|
325
|
+
sentinel.$$reentrantLock$$ = null
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function createComponent(parent, vnode3, hooks, ns, nextSibling) {
|
|
329
|
+
var hooksLength = hooks.length
|
|
330
|
+
try {
|
|
331
|
+
initComponent(vnode3, hooks)
|
|
332
|
+
} catch (e) {
|
|
333
|
+
// Drop the `oncreate` hooks this component queued before it failed.
|
|
334
|
+
hooks.length = hooksLength
|
|
335
|
+
disableComponent(vnode3)
|
|
336
|
+
deferError(e)
|
|
337
|
+
}
|
|
338
|
+
if (vnode3.instance != null) {
|
|
339
|
+
createNode(parent, vnode3.instance, hooks, ns, nextSibling)
|
|
340
|
+
vnode3.dom = vnode3.instance.dom
|
|
341
|
+
vnode3.domSize = vnode3.instance.domSize
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
vnode3.domSize = 0
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
//update
|
|
348
|
+
/**
|
|
349
|
+
* @param {Element|Fragment} parent - the parent element
|
|
350
|
+
* @param {Vnode[] | null} old - the list of vnodes of the last `render()` call for
|
|
351
|
+
* this part of the tree
|
|
352
|
+
* @param {Vnode[] | null} vnodes - as above, but for the current `render()` call.
|
|
353
|
+
* @param {Function[]} hooks - an accumulator of post-render hooks (oncreate/onupdate)
|
|
354
|
+
* @param {Element | null} nextSibling - the next DOM node if we're dealing with a
|
|
355
|
+
* fragment that is not the last item in its
|
|
356
|
+
* parent
|
|
357
|
+
* @param {'svg' | 'math' | String | null} ns) - the current XML namespace, if any
|
|
358
|
+
* @returns void
|
|
359
|
+
*/
|
|
360
|
+
// This function diffs and patches lists of vnodes, both keyed and unkeyed.
|
|
361
|
+
//
|
|
362
|
+
// We will:
|
|
363
|
+
//
|
|
364
|
+
// 1. describe its general structure
|
|
365
|
+
// 2. focus on the diff algorithm optimizations
|
|
366
|
+
// 3. discuss DOM node operations.
|
|
367
|
+
// ## Overview:
|
|
368
|
+
//
|
|
369
|
+
// The updateNodes() function:
|
|
370
|
+
// - deals with trivial cases
|
|
371
|
+
// - determines whether the lists are keyed or unkeyed based on the first non-null node
|
|
372
|
+
// of each list.
|
|
373
|
+
// - diffs them and patches the DOM if needed (that's the brunt of the code)
|
|
374
|
+
// - manages the leftovers: after diffing, are there:
|
|
375
|
+
// - old nodes left to remove?
|
|
376
|
+
// - new nodes to insert?
|
|
377
|
+
// deal with them!
|
|
378
|
+
//
|
|
379
|
+
// The lists are only iterated over once, with an exception for the nodes in `old` that
|
|
380
|
+
// are visited in the fourth part of the diff and in the `removeNodes` loop.
|
|
381
|
+
// ## Diffing
|
|
382
|
+
//
|
|
383
|
+
// Reading https://github.com/localvoid/ivi/blob/ddc09d06abaef45248e6133f7040d00d3c6be853/packages/ivi/src/vdom/implementation.ts#L617-L837
|
|
384
|
+
// may be good for context on longest increasing subsequence-based logic for moving nodes.
|
|
385
|
+
//
|
|
386
|
+
// In order to diff keyed lists, one has to
|
|
387
|
+
//
|
|
388
|
+
// 1) match nodes in both lists, per key, and update them accordingly
|
|
389
|
+
// 2) create the nodes present in the new list, but absent in the old one
|
|
390
|
+
// 3) remove the nodes present in the old list, but absent in the new one
|
|
391
|
+
// 4) figure out what nodes in 1) to move in order to minimize the DOM operations.
|
|
392
|
+
//
|
|
393
|
+
// To achieve 1) one can create a dictionary of keys => index (for the old list), then iterate
|
|
394
|
+
// over the new list and for each new vnode, find the corresponding vnode in the old list using
|
|
395
|
+
// the map.
|
|
396
|
+
// 2) is achieved in the same step: if a new node has no corresponding entry in the map, it is new
|
|
397
|
+
// and must be created.
|
|
398
|
+
// For the removals, we actually remove the nodes that have been updated from the old list.
|
|
399
|
+
// The nodes that remain in that list after 1) and 2) have been performed can be safely removed.
|
|
400
|
+
// The fourth step is a bit more complex and relies on the longest increasing subsequence (LIS)
|
|
401
|
+
// algorithm.
|
|
402
|
+
//
|
|
403
|
+
// the longest increasing subsequence is the list of nodes that can remain in place. Imagine going
|
|
404
|
+
// from `1,2,3,4,5` to `4,5,1,2,3` where the numbers are not necessarily the keys, but the indices
|
|
405
|
+
// corresponding to the keyed nodes in the old list (keyed nodes `e,d,c,b,a` => `b,a,e,d,c` would
|
|
406
|
+
// match the above lists, for example).
|
|
407
|
+
//
|
|
408
|
+
// In there are two increasing subsequences: `4,5` and `1,2,3`, the latter being the longest. We
|
|
409
|
+
// can update those nodes without moving them, and only call `insertNode` on `4` and `5`.
|
|
410
|
+
//
|
|
411
|
+
// @localvoid adapted the algo to also support node deletions and insertions (the `lis` is actually
|
|
412
|
+
// the longest increasing subsequence *of old nodes still present in the new list*).
|
|
413
|
+
//
|
|
414
|
+
// It is a general algorithm that is fireproof in all circumstances, but it requires the allocation
|
|
415
|
+
// and the construction of a `key => oldIndex` map, and three arrays (one with `newIndex => oldIndex`,
|
|
416
|
+
// the `LIS` and a temporary one to create the LIS).
|
|
417
|
+
//
|
|
418
|
+
// So we cheat where we can: if the tails of the lists are identical, they are guaranteed to be part of
|
|
419
|
+
// the LIS and can be updated without moving them.
|
|
420
|
+
//
|
|
421
|
+
// If two nodes are swapped, they are guaranteed not to be part of the LIS, and must be moved (with
|
|
422
|
+
// the exception of the last node if the list is fully reversed).
|
|
423
|
+
//
|
|
424
|
+
// ## Finding the next sibling.
|
|
425
|
+
//
|
|
426
|
+
// `updateNode()` and `createNode()` expect a nextSibling parameter to perform DOM operations.
|
|
427
|
+
// When the list is being traversed top-down, at any index, the DOM nodes up to the previous
|
|
428
|
+
// vnode reflect the content of the new list, whereas the rest of the DOM nodes reflect the old
|
|
429
|
+
// list. The next sibling must be looked for in the old list using `getNextSibling(... oldStart + 1 ...)`.
|
|
430
|
+
//
|
|
431
|
+
// In the other scenarios (swaps, upwards traversal, map-based diff),
|
|
432
|
+
// the new vnodes list is traversed upwards. The DOM nodes at the bottom of the list reflect the
|
|
433
|
+
// bottom part of the new vnodes list, and we can use the `v.dom` value of the previous node
|
|
434
|
+
// as the next sibling (cached in the `nextSibling` variable).
|
|
435
|
+
// ## DOM node moves
|
|
436
|
+
//
|
|
437
|
+
// In most scenarios `updateNode()` and `createNode()` perform the DOM operations. However,
|
|
438
|
+
// this is not the case if the node moved (second and fourth part of the diff algo). We move
|
|
439
|
+
// the old DOM nodes before updateNode runs because it enables us to use the cached `nextSibling`
|
|
440
|
+
// variable rather than fetching it using `getNextSibling()`.
|
|
441
|
+
function updateNodes(parent, old, vnodes, hooks, nextSibling, ns) {
|
|
442
|
+
if (old === vnodes || old == null && vnodes == null) return
|
|
443
|
+
else if (old == null || old.length === 0) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, ns)
|
|
444
|
+
else if (vnodes == null || vnodes.length === 0) removeNodes(parent, old, 0, old.length)
|
|
445
|
+
else {
|
|
446
|
+
var isOldKeyed = old[0] != null && old[0].key != null
|
|
447
|
+
var isKeyed = vnodes[0] != null && vnodes[0].key != null
|
|
448
|
+
var start = 0, oldStart = 0, o, v
|
|
449
|
+
if (isOldKeyed !== isKeyed) {
|
|
450
|
+
removeNodes(parent, old, 0, old.length)
|
|
451
|
+
createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, ns)
|
|
452
|
+
} else if (!isKeyed) {
|
|
453
|
+
// Don't index past the end of either list (causes deopts).
|
|
454
|
+
var commonLength = old.length < vnodes.length ? old.length : vnodes.length
|
|
455
|
+
// Rewind if necessary to the first non-null index on either side.
|
|
456
|
+
// We could alternatively either explicitly create or remove nodes when `start !== oldStart`
|
|
457
|
+
// but that would be optimizing for sparse lists which are more rare than dense ones.
|
|
458
|
+
while (oldStart < old.length && old[oldStart] == null) oldStart++
|
|
459
|
+
while (start < vnodes.length && vnodes[start] == null) start++
|
|
460
|
+
start = start < oldStart ? start : oldStart
|
|
461
|
+
for (; start < commonLength; start++) {
|
|
462
|
+
o = old[start]
|
|
463
|
+
v = vnodes[start]
|
|
464
|
+
if (o === v || o == null && v == null) continue
|
|
465
|
+
else if (o == null) createNode(parent, v, hooks, ns, getNextSibling(old, start + 1, old.length, nextSibling))
|
|
466
|
+
else if (v == null) removeNode(parent, o)
|
|
467
|
+
else updateNode(parent, o, v, hooks, getNextSibling(old, start + 1, old.length, nextSibling), ns)
|
|
468
|
+
}
|
|
469
|
+
if (old.length > commonLength) removeNodes(parent, old, start, old.length)
|
|
470
|
+
if (vnodes.length > commonLength) createNodes(parent, vnodes, start, vnodes.length, hooks, nextSibling, ns)
|
|
471
|
+
} else {
|
|
472
|
+
// keyed diff
|
|
473
|
+
var oldEnd = old.length - 1, end = vnodes.length - 1, oe, ve, topSibling
|
|
474
|
+
// bottom-up
|
|
475
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
476
|
+
oe = old[oldEnd]
|
|
477
|
+
ve = vnodes[end]
|
|
478
|
+
if (oe.key !== ve.key) break
|
|
479
|
+
if (oe !== ve) updateNode(parent, oe, ve, hooks, nextSibling, ns)
|
|
480
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
481
|
+
oldEnd--, end--
|
|
482
|
+
}
|
|
483
|
+
// top-down
|
|
484
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
485
|
+
o = old[oldStart]
|
|
486
|
+
v = vnodes[start]
|
|
487
|
+
if (o.key !== v.key) break
|
|
488
|
+
oldStart++, start++
|
|
489
|
+
if (o !== v) updateNode(parent, o, v, hooks, getNextSibling(old, oldStart, oldEnd + 1, nextSibling), ns)
|
|
490
|
+
}
|
|
491
|
+
// swaps and list reversals
|
|
492
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
493
|
+
if (start === end) break
|
|
494
|
+
if (o.key !== ve.key || oe.key !== v.key) break
|
|
495
|
+
topSibling = getNextSibling(old, oldStart, oldEnd, nextSibling)
|
|
496
|
+
moveDOM(parent, oe, topSibling)
|
|
497
|
+
if (oe !== v) updateNode(parent, oe, v, hooks, topSibling, ns)
|
|
498
|
+
if (++start <= --end) moveDOM(parent, o, nextSibling)
|
|
499
|
+
if (o !== ve) updateNode(parent, o, ve, hooks, nextSibling, ns)
|
|
500
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
501
|
+
oldStart++; oldEnd--
|
|
502
|
+
oe = old[oldEnd]
|
|
503
|
+
ve = vnodes[end]
|
|
504
|
+
o = old[oldStart]
|
|
505
|
+
v = vnodes[start]
|
|
506
|
+
}
|
|
507
|
+
// bottom up once again
|
|
508
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
509
|
+
if (oe.key !== ve.key) break
|
|
510
|
+
if (oe !== ve) updateNode(parent, oe, ve, hooks, nextSibling, ns)
|
|
511
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
512
|
+
oldEnd--, end--
|
|
513
|
+
oe = old[oldEnd]
|
|
514
|
+
ve = vnodes[end]
|
|
515
|
+
}
|
|
516
|
+
if (start > end) removeNodes(parent, old, oldStart, oldEnd + 1)
|
|
517
|
+
else if (oldStart > oldEnd) createNodes(parent, vnodes, start, end + 1, hooks, nextSibling, ns)
|
|
518
|
+
else {
|
|
519
|
+
// inspired by ivi https://github.com/ivijs/ivi/ by Boris Kaul
|
|
520
|
+
var originalNextSibling = nextSibling, pos = 2147483647, matched = 0
|
|
521
|
+
var oldIndices = new Array(end - start + 1).fill(-1)
|
|
522
|
+
var map = Object.create(null)
|
|
523
|
+
for (var i = start; i <= end; i++) map[vnodes[i].key] = i
|
|
524
|
+
for (var i = oldEnd; i >= oldStart; i--) {
|
|
525
|
+
oe = old[i]
|
|
526
|
+
var newIndex = map[oe.key]
|
|
527
|
+
if (newIndex != null) {
|
|
528
|
+
pos = (newIndex < pos) ? newIndex : -1 // becomes -1 if nodes were re-ordered
|
|
529
|
+
oldIndices[newIndex-start] = i
|
|
530
|
+
ve = vnodes[newIndex]
|
|
531
|
+
if (oe !== ve) updateNode(parent, oe, ve, hooks, nextSibling, ns)
|
|
532
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
533
|
+
matched++
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
nextSibling = originalNextSibling
|
|
537
|
+
// Remove the old nodes that have no match in the new list. The old list
|
|
538
|
+
// is left untouched, so it is still usable if something throws mid-diff.
|
|
539
|
+
if (matched !== oldEnd - oldStart + 1) {
|
|
540
|
+
for (var i = oldStart; i <= oldEnd; i++) {
|
|
541
|
+
if (map[old[i].key] == null) removeNode(parent, old[i])
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
if (matched === 0) createNodes(parent, vnodes, start, end + 1, hooks, nextSibling, ns)
|
|
545
|
+
else {
|
|
546
|
+
if (pos === -1) {
|
|
547
|
+
// the indices of the indices of the items that are part of the
|
|
548
|
+
// longest increasing subsequence in the oldIndices list
|
|
549
|
+
var lisIndices = makeLisIndices(oldIndices)
|
|
550
|
+
var li = lisIndices.length - 1
|
|
551
|
+
for (var i = end; i >= start; i--) {
|
|
552
|
+
ve = vnodes[i]
|
|
553
|
+
if (oldIndices[i-start] === -1) createNode(parent, ve, hooks, ns, nextSibling)
|
|
554
|
+
else {
|
|
555
|
+
if (lisIndices[li] === i - start) li--
|
|
556
|
+
else moveDOM(parent, ve, nextSibling)
|
|
557
|
+
}
|
|
558
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
559
|
+
}
|
|
560
|
+
} else {
|
|
561
|
+
for (var i = end; i >= start; i--) {
|
|
562
|
+
ve = vnodes[i]
|
|
563
|
+
if (oldIndices[i-start] === -1) createNode(parent, ve, hooks, ns, nextSibling)
|
|
564
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
function updateNode(parent, old, vnode3, hooks, nextSibling, ns) {
|
|
573
|
+
var oldTag = old.tag, tag = vnode3.tag
|
|
574
|
+
if (oldTag === tag && old.is === vnode3.is) {
|
|
575
|
+
vnode3.state = old.state
|
|
576
|
+
vnode3.events = old.events
|
|
577
|
+
if (typeof oldTag === "string") {
|
|
578
|
+
if (shouldNotUpdate(vnode3, old)) return
|
|
579
|
+
if (vnode3.attrs != null) {
|
|
580
|
+
updateLifecycle(vnode3.attrs, vnode3, hooks)
|
|
581
|
+
}
|
|
582
|
+
switch (oldTag) {
|
|
583
|
+
case "#": updateText(old, vnode3); break
|
|
584
|
+
case "<": updateHTML(parent, old, vnode3, ns, nextSibling); break
|
|
585
|
+
case "[": updateFragment(parent, old, vnode3, hooks, nextSibling, ns); break
|
|
586
|
+
default: updateElement(old, vnode3, hooks, ns)
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
else updateComponent(parent, old, vnode3, hooks, nextSibling, ns)
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
removeNode(parent, old)
|
|
593
|
+
createNode(parent, vnode3, hooks, ns, nextSibling)
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function updateText(old, vnode3) {
|
|
597
|
+
if (old.children.toString() !== vnode3.children.toString()) {
|
|
598
|
+
old.dom.nodeValue = vnode3.children
|
|
599
|
+
}
|
|
600
|
+
vnode3.dom = old.dom
|
|
601
|
+
}
|
|
602
|
+
function updateHTML(parent, old, vnode3, ns, nextSibling) {
|
|
603
|
+
if (old.children !== vnode3.children) {
|
|
604
|
+
removeDOM(parent, old)
|
|
605
|
+
createHTML(parent, vnode3, ns, nextSibling)
|
|
606
|
+
}
|
|
607
|
+
else {
|
|
608
|
+
vnode3.dom = old.dom
|
|
609
|
+
vnode3.domSize = old.domSize
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function updateFragment(parent, old, vnode3, hooks, nextSibling, ns) {
|
|
613
|
+
updateNodes(parent, old.children, vnode3.children, hooks, nextSibling, ns)
|
|
614
|
+
var domSize = 0, children2 = vnode3.children
|
|
615
|
+
vnode3.dom = null
|
|
616
|
+
if (children2 != null) {
|
|
617
|
+
for (var i = 0; i < children2.length; i++) {
|
|
618
|
+
var child = children2[i]
|
|
619
|
+
if (child != null && child.dom != null) {
|
|
620
|
+
if (vnode3.dom == null) vnode3.dom = child.dom
|
|
621
|
+
domSize += child.domSize || 1
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
vnode3.domSize = domSize
|
|
626
|
+
}
|
|
627
|
+
function updateElement(old, vnode3, hooks, ns) {
|
|
628
|
+
var element = vnode3.dom = old.dom
|
|
629
|
+
ns = getNameSpace(vnode3) || ns
|
|
630
|
+
if (old.attrs != vnode3.attrs || (vnode3.attrs != null && !cachedAttrsIsStaticMap.get(vnode3.attrs))) {
|
|
631
|
+
updateAttrs(vnode3, old.attrs, vnode3.attrs, ns)
|
|
632
|
+
}
|
|
633
|
+
if (!maybeSetContentEditable(vnode3)) {
|
|
634
|
+
updateNodes(element, old.children, vnode3.children, hooks, null, ns)
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
function updateComponent(parent, old, vnode3, hooks, nextSibling, ns) {
|
|
638
|
+
if (vnode3.state.view === inertView) {
|
|
639
|
+
vnode3.domSize = 0
|
|
640
|
+
return
|
|
641
|
+
}
|
|
642
|
+
try {
|
|
643
|
+
if (shouldNotUpdate(vnode3, old)) return
|
|
644
|
+
vnode3.instance = Vnode.normalize(callHook.call(vnode3.state.view, vnode3))
|
|
645
|
+
if (vnode3.instance === vnode3) throw Error("A view cannot return the vnode it received as argument")
|
|
646
|
+
} catch (e) {
|
|
647
|
+
// Keep what the component rendered last time, so the tree stays in sync
|
|
648
|
+
// with the DOM, and retry on the next render.
|
|
649
|
+
reuseOld(vnode3, old)
|
|
650
|
+
deferError(e)
|
|
651
|
+
return
|
|
652
|
+
}
|
|
653
|
+
updateLifecycle(vnode3.state, vnode3, hooks)
|
|
654
|
+
if (vnode3.attrs != null) updateLifecycle(vnode3.attrs, vnode3, hooks)
|
|
655
|
+
if (vnode3.instance != null) {
|
|
656
|
+
if (old.instance == null) createNode(parent, vnode3.instance, hooks, ns, nextSibling)
|
|
657
|
+
else updateNode(parent, old.instance, vnode3.instance, hooks, nextSibling, ns)
|
|
658
|
+
vnode3.dom = vnode3.instance.dom
|
|
659
|
+
vnode3.domSize = vnode3.instance.domSize
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
if (old.instance != null) removeNode(parent, old.instance)
|
|
663
|
+
vnode3.domSize = 0
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
// Lifted from ivi https://github.com/ivijs/ivi/
|
|
667
|
+
// takes a list of unique numbers (-1 is special and can
|
|
668
|
+
// occur multiple times) and returns an array with the indices
|
|
669
|
+
// of the items that are part of the longest increasing
|
|
670
|
+
// subsequence
|
|
671
|
+
var lisTemp = []
|
|
672
|
+
function makeLisIndices(a) {
|
|
673
|
+
var result = [0]
|
|
674
|
+
var u = 0, v = 0, i = 0
|
|
675
|
+
var il = lisTemp.length = a.length
|
|
676
|
+
for (var i = 0; i < il; i++) lisTemp[i] = a[i]
|
|
677
|
+
for (var i = 0; i < il; ++i) {
|
|
678
|
+
if (a[i] === -1) continue
|
|
679
|
+
var j = result[result.length - 1]
|
|
680
|
+
if (a[j] < a[i]) {
|
|
681
|
+
lisTemp[i] = j
|
|
682
|
+
result.push(i)
|
|
683
|
+
continue
|
|
684
|
+
}
|
|
685
|
+
u = 0
|
|
686
|
+
v = result.length - 1
|
|
687
|
+
while (u < v) {
|
|
688
|
+
// Fast integer average without overflow.
|
|
689
|
+
// eslint-disable-next-line no-bitwise
|
|
690
|
+
var c = (u >>> 1) + (v >>> 1) + (u & v & 1)
|
|
691
|
+
if (a[result[c]] < a[i]) {
|
|
692
|
+
u = c + 1
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
v = c
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
if (a[i] < a[result[u]]) {
|
|
699
|
+
if (u > 0) lisTemp[i] = result[u - 1]
|
|
700
|
+
result[u] = i
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
u = result.length
|
|
704
|
+
v = result[u - 1]
|
|
705
|
+
while (u-- > 0) {
|
|
706
|
+
result[u] = v
|
|
707
|
+
v = lisTemp[v]
|
|
708
|
+
}
|
|
709
|
+
lisTemp.length = 0
|
|
710
|
+
return result
|
|
711
|
+
}
|
|
712
|
+
function getNextSibling(vnodes, i, end, nextSibling) {
|
|
713
|
+
for (; i < end; i++) {
|
|
714
|
+
if (vnodes[i] != null && vnodes[i].dom != null) return vnodes[i].dom
|
|
715
|
+
}
|
|
716
|
+
return nextSibling
|
|
717
|
+
}
|
|
718
|
+
// This handles fragments with zombie children (removed from vdom, but persisted in DOM through onbeforeremove)
|
|
719
|
+
function moveDOM(parent, vnode3, nextSibling) {
|
|
720
|
+
if (vnode3.dom != null) {
|
|
721
|
+
var target
|
|
722
|
+
if (vnode3.domSize == null || vnode3.domSize === 1) {
|
|
723
|
+
// don't allocate for the common case
|
|
724
|
+
target = vnode3.dom
|
|
725
|
+
} else {
|
|
726
|
+
target = getDocument(parent).createDocumentFragment()
|
|
727
|
+
for (var dom of domFor(vnode3)) target.appendChild(dom)
|
|
728
|
+
}
|
|
729
|
+
insertDOM(parent, target, nextSibling)
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
function insertDOM(parent, dom, nextSibling) {
|
|
733
|
+
if (nextSibling != null) parent.insertBefore(dom, nextSibling)
|
|
734
|
+
else parent.appendChild(dom)
|
|
735
|
+
}
|
|
736
|
+
function maybeSetContentEditable(vnode3) {
|
|
737
|
+
if (vnode3.attrs == null || (
|
|
738
|
+
vnode3.attrs.contenteditable == null && // attribute
|
|
739
|
+
vnode3.attrs.contentEditable == null // property
|
|
740
|
+
)) return false
|
|
741
|
+
var children2 = vnode3.children
|
|
742
|
+
if (children2 != null && children2.length === 1 && children2[0].tag === "<") {
|
|
743
|
+
var content = children2[0].children
|
|
744
|
+
if (vnode3.dom.innerHTML !== content) vnode3.dom.innerHTML = content
|
|
745
|
+
}
|
|
746
|
+
else if (children2 != null && children2.length !== 0) throw new Error("Child node of a contenteditable must be trusted.")
|
|
747
|
+
return true
|
|
748
|
+
}
|
|
749
|
+
//remove
|
|
750
|
+
function removeNodes(parent, vnodes, start, end) {
|
|
751
|
+
for (var i = start; i < end; i++) {
|
|
752
|
+
var vnode3 = vnodes[i]
|
|
753
|
+
if (vnode3 != null) removeNode(parent, vnode3)
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
function tryBlockRemove(parent, vnode3, source, counter) {
|
|
757
|
+
var original = vnode3.state
|
|
758
|
+
var result = callHook.call(source.onbeforeremove, vnode3)
|
|
759
|
+
if (result == null) return
|
|
760
|
+
var generation = currentRender
|
|
761
|
+
for (var dom of domFor(vnode3)) delayedRemoval.set(dom, generation)
|
|
762
|
+
counter.v++
|
|
763
|
+
Promise.resolve(result).finally(function () {
|
|
764
|
+
checkState(vnode3, original)
|
|
765
|
+
tryResumeRemove(parent, vnode3, counter)
|
|
766
|
+
})
|
|
767
|
+
}
|
|
768
|
+
function tryResumeRemove(parent, vnode3, counter) {
|
|
769
|
+
if (--counter.v === 0) {
|
|
770
|
+
onremove(vnode3)
|
|
771
|
+
removeDOM(parent, vnode3)
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
function removeNode(parent, vnode3) {
|
|
775
|
+
var counter = {v: 1}
|
|
776
|
+
if (typeof vnode3.tag !== "string" && typeof vnode3.state.onbeforeremove === "function") tryBlockRemove(parent, vnode3, vnode3.state, counter)
|
|
777
|
+
if (vnode3.attrs && typeof vnode3.attrs.onbeforeremove === "function") tryBlockRemove(parent, vnode3, vnode3.attrs, counter)
|
|
778
|
+
tryResumeRemove(parent, vnode3, counter)
|
|
779
|
+
}
|
|
780
|
+
function removeDOM(parent, vnode3) {
|
|
781
|
+
if (vnode3.dom == null) return
|
|
782
|
+
if (vnode3.domSize == null || vnode3.domSize === 1) {
|
|
783
|
+
parent.removeChild(vnode3.dom)
|
|
784
|
+
} else {
|
|
785
|
+
for (var dom of domFor(vnode3)) parent.removeChild(dom)
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
function onremove(vnode3) {
|
|
789
|
+
if (typeof vnode3.tag !== "string" && typeof vnode3.state.onremove === "function") callHook.call(vnode3.state.onremove, vnode3)
|
|
790
|
+
if (vnode3.attrs && typeof vnode3.attrs.onremove === "function") callHook.call(vnode3.attrs.onremove, vnode3)
|
|
791
|
+
if (typeof vnode3.tag !== "string") {
|
|
792
|
+
if (vnode3.instance != null) onremove(vnode3.instance)
|
|
793
|
+
} else {
|
|
794
|
+
if (vnode3.events != null) vnode3.events._ = null
|
|
795
|
+
var children2 = vnode3.children
|
|
796
|
+
if (Array.isArray(children2)) {
|
|
797
|
+
for (var i = 0; i < children2.length; i++) {
|
|
798
|
+
var child = children2[i]
|
|
799
|
+
if (child != null) onremove(child)
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
//attrs
|
|
805
|
+
function setAttrs(vnode3, attrs5, ns) {
|
|
806
|
+
for (var key in attrs5) {
|
|
807
|
+
setAttr(vnode3, key, null, attrs5[key], ns)
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
function setAttr(vnode3, key, old, value, ns) {
|
|
811
|
+
if (key === "key" || value == null || isLifecycleMethod(key) || (old === value && !isFormAttribute(vnode3, key)) && typeof value !== "object") return
|
|
812
|
+
if (key[0] === "o" && key[1] === "n") return updateEvent(vnode3, key, value)
|
|
813
|
+
if (key.slice(0, 6) === "xlink:") vnode3.dom.setAttributeNS("http://www.w3.org/1999/xlink", key.slice(6), value)
|
|
814
|
+
else if (key === "style") updateStyle(vnode3.dom, old, value)
|
|
815
|
+
else if (hasPropertyKey(vnode3, key, ns)) {
|
|
816
|
+
if (key === "value") {
|
|
817
|
+
// Only do the coercion if we're actually going to check the value.
|
|
818
|
+
/* eslint-disable no-implicit-coercion */
|
|
819
|
+
//setting input[value] to same value by typing on focused element moves cursor to end in Chrome
|
|
820
|
+
//setting input[type=file][value] to same value causes an error to be generated if it's non-empty
|
|
821
|
+
//minlength/maxlength validation isn't performed on script-set values(#2256)
|
|
822
|
+
if ((vnode3.tag === "input" || vnode3.tag === "textarea") && vnode3.dom.value === "" + value) return
|
|
823
|
+
//setting select[value] to same value while having select open blinks select dropdown in Chrome
|
|
824
|
+
if (vnode3.tag === "select" && old !== null && vnode3.dom.value === "" + value) return
|
|
825
|
+
//setting option[value] to same value while having select open blinks select dropdown in Chrome
|
|
826
|
+
if (vnode3.tag === "option" && old !== null && vnode3.dom.value === "" + value) return
|
|
827
|
+
//setting input[type=file][value] to different value is an error if it's non-empty
|
|
828
|
+
// Not ideal, but it at least works around the most common source of uncaught exceptions for now.
|
|
829
|
+
if (vnode3.tag === "input" && vnode3.attrs.type === "file" && "" + value !== "") { console.error("`value` is read-only on file inputs!"); return }
|
|
830
|
+
/* eslint-enable no-implicit-coercion */
|
|
831
|
+
}
|
|
832
|
+
// If you assign an input type that is not supported by IE 11 with an assignment expression, an error will occur.
|
|
833
|
+
if (vnode3.tag === "input" && key === "type") vnode3.dom.setAttribute(key, value)
|
|
834
|
+
else vnode3.dom[key] = value
|
|
835
|
+
} else {
|
|
836
|
+
if (typeof value === "boolean") {
|
|
837
|
+
if (value) vnode3.dom.setAttribute(key, "")
|
|
838
|
+
else vnode3.dom.removeAttribute(key)
|
|
839
|
+
}
|
|
840
|
+
else vnode3.dom.setAttribute(key === "className" ? "class" : key, value)
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
function removeAttr(vnode3, key, old, ns) {
|
|
844
|
+
if (key === "key" || old == null || isLifecycleMethod(key)) return
|
|
845
|
+
if (key[0] === "o" && key[1] === "n") updateEvent(vnode3, key, undefined)
|
|
846
|
+
else if (key === "style") updateStyle(vnode3.dom, old, null)
|
|
847
|
+
else if (
|
|
848
|
+
hasPropertyKey(vnode3, key, ns)
|
|
849
|
+
&& key !== "className"
|
|
850
|
+
&& key !== "title" // creates "null" as title
|
|
851
|
+
&& !(key === "value" && (
|
|
852
|
+
vnode3.tag === "option"
|
|
853
|
+
|| vnode3.tag === "select" && vnode3.dom.selectedIndex === -1 && vnode3.dom === activeElement(vnode3.dom)
|
|
854
|
+
))
|
|
855
|
+
&& !(vnode3.tag === "input" && key === "type")
|
|
856
|
+
) {
|
|
857
|
+
vnode3.dom[key] = null
|
|
858
|
+
} else {
|
|
859
|
+
var nsLastIndex = key.indexOf(":")
|
|
860
|
+
if (nsLastIndex !== -1) key = key.slice(nsLastIndex + 1)
|
|
861
|
+
if (old !== false) vnode3.dom.removeAttribute(key === "className" ? "class" : key)
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
function setLateSelectAttrs(vnode3, attrs5) {
|
|
865
|
+
if ("value" in attrs5) {
|
|
866
|
+
if(attrs5.value === null) {
|
|
867
|
+
if (vnode3.dom.selectedIndex !== -1) vnode3.dom.value = null
|
|
868
|
+
} else {
|
|
869
|
+
var normalized = "" + attrs5.value // eslint-disable-line no-implicit-coercion
|
|
870
|
+
if (vnode3.dom.value !== normalized || vnode3.dom.selectedIndex === -1) {
|
|
871
|
+
vnode3.dom.value = normalized
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
if ("selectedIndex" in attrs5) setAttr(vnode3, "selectedIndex", null, attrs5.selectedIndex, undefined)
|
|
876
|
+
}
|
|
877
|
+
function updateAttrs(vnode3, old, attrs5, ns) {
|
|
878
|
+
// Some attributes may NOT be case-sensitive (e.g. data-***),
|
|
879
|
+
// so removal should be done first to prevent accidental removal for newly setting values.
|
|
880
|
+
var val
|
|
881
|
+
if (old != null) {
|
|
882
|
+
if (old === attrs5 && !cachedAttrsIsStaticMap.has(attrs5)) {
|
|
883
|
+
console.warn("Don't reuse attrs object, use new object for every redraw, this will throw in next major")
|
|
884
|
+
}
|
|
885
|
+
for (var key in old) {
|
|
886
|
+
if (((val = old[key]) != null) && (attrs5 == null || attrs5[key] == null)) {
|
|
887
|
+
removeAttr(vnode3, key, val, ns)
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
if (attrs5 != null) {
|
|
892
|
+
for (var key in attrs5) {
|
|
893
|
+
setAttr(vnode3, key, old && old[key], attrs5[key], ns)
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
function isFormAttribute(vnode3, attr) {
|
|
898
|
+
return attr === "value" || attr === "checked" || attr === "selectedIndex" || attr === "selected" && (vnode3.dom === activeElement(vnode3.dom) || vnode3.tag === "option" && vnode3.dom.parentNode === activeElement(vnode3.dom))
|
|
899
|
+
}
|
|
900
|
+
function isLifecycleMethod(attr) {
|
|
901
|
+
return attr === "oninit" || attr === "oncreate" || attr === "onupdate" || attr === "onremove" || attr === "onbeforeremove" || attr === "onbeforeupdate"
|
|
902
|
+
}
|
|
903
|
+
function hasPropertyKey(vnode3, key, ns) {
|
|
904
|
+
// Filter out namespaced keys
|
|
905
|
+
return ns === undefined && (
|
|
906
|
+
// If it's a custom element, just keep it.
|
|
907
|
+
vnode3.tag.indexOf("-") > -1 || vnode3.is ||
|
|
908
|
+
// If it's a normal element, let's try to avoid a few browser bugs.
|
|
909
|
+
key !== "href" && key !== "list" && key !== "form" && key !== "width" && key !== "height"// && key !== "type"
|
|
910
|
+
// Defer the property check until *after* we check everything.
|
|
911
|
+
) && key in vnode3.dom
|
|
912
|
+
}
|
|
913
|
+
//style
|
|
914
|
+
function updateStyle(element, old, style) {
|
|
915
|
+
if (old === style) {
|
|
916
|
+
// Styles are equivalent, do nothing.
|
|
917
|
+
} else if (style == null) {
|
|
918
|
+
// New style is missing, just clear it.
|
|
919
|
+
element.style = ""
|
|
920
|
+
} else if (typeof style !== "object") {
|
|
921
|
+
// New style is a string, let engine deal with patching.
|
|
922
|
+
element.style = style
|
|
923
|
+
} else if (old == null || typeof old !== "object") {
|
|
924
|
+
// `old` is missing or a string, `style` is an object.
|
|
925
|
+
element.style = ""
|
|
926
|
+
// Add new style properties
|
|
927
|
+
for (var key in style) {
|
|
928
|
+
var value = style[key]
|
|
929
|
+
if (value != null) {
|
|
930
|
+
if (key.includes("-")) element.style.setProperty(key, String(value))
|
|
931
|
+
else element.style[key] = String(value)
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
} else {
|
|
935
|
+
// Both old & new are (different) objects.
|
|
936
|
+
// Remove style properties that no longer exist
|
|
937
|
+
// Style properties may have two cases(dash-case and camelCase),
|
|
938
|
+
// so removal should be done first to prevent accidental removal for newly setting values.
|
|
939
|
+
for (var key in old) {
|
|
940
|
+
if (old[key] != null && style[key] == null) {
|
|
941
|
+
if (key.includes("-")) element.style.removeProperty(key)
|
|
942
|
+
else element.style[key] = ""
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
// Update style properties that have changed
|
|
946
|
+
for (var key in style) {
|
|
947
|
+
var value = style[key]
|
|
948
|
+
if (value != null && (value = String(value)) !== String(old[key])) {
|
|
949
|
+
if (key.includes("-")) element.style.setProperty(key, value)
|
|
950
|
+
else element.style[key] = value
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
// Here's an explanation of how this works:
|
|
956
|
+
// 1. The event names are always (by design) prefixed by `on`.
|
|
957
|
+
// 2. The EventListener interface accepts either a function or an object
|
|
958
|
+
// with a `handleEvent` method.
|
|
959
|
+
// 3. The object does not inherit from `Object.prototype`, to avoid
|
|
960
|
+
// any potential interference with that (e.g. setters).
|
|
961
|
+
// 4. The event name is remapped to the handler before calling it.
|
|
962
|
+
// 5. In function-based event handlers, `ev.target === this`. We replicate
|
|
963
|
+
// that below.
|
|
964
|
+
// 6. In function-based event handlers, `return false` prevents the default
|
|
965
|
+
// action and stops event propagation. We replicate that below.
|
|
966
|
+
function EventDict() {
|
|
967
|
+
// Save this, so the current redraw is correctly tracked.
|
|
968
|
+
this._ = currentRedraw
|
|
969
|
+
}
|
|
970
|
+
EventDict.prototype = Object.create(null)
|
|
971
|
+
EventDict.prototype.handleEvent = function (ev) {
|
|
972
|
+
var handler = this["on" + ev.type]
|
|
973
|
+
var result
|
|
974
|
+
if (typeof handler === "function") result = handler.call(ev.currentTarget, ev)
|
|
975
|
+
else if (typeof handler.handleEvent === "function") handler.handleEvent(ev)
|
|
976
|
+
var self = this
|
|
977
|
+
if (self._ != null) {
|
|
978
|
+
if (ev.redraw !== false) (0, self._)()
|
|
979
|
+
if (result != null && typeof result.then === "function") {
|
|
980
|
+
Promise.resolve(result).then(function () {
|
|
981
|
+
if (self._ != null && ev.redraw !== false) (0, self._)()
|
|
982
|
+
})
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
if (result === false) {
|
|
986
|
+
ev.preventDefault()
|
|
987
|
+
ev.stopPropagation()
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
//event
|
|
991
|
+
function updateEvent(vnode3, key, value) {
|
|
992
|
+
if (vnode3.events != null) {
|
|
993
|
+
vnode3.events._ = currentRedraw
|
|
994
|
+
if (vnode3.events[key] === value) return
|
|
995
|
+
if (value != null && (typeof value === "function" || typeof value === "object")) {
|
|
996
|
+
if (vnode3.events[key] == null) vnode3.dom.addEventListener(key.slice(2), vnode3.events, false)
|
|
997
|
+
vnode3.events[key] = value
|
|
998
|
+
} else {
|
|
999
|
+
if (vnode3.events[key] != null) vnode3.dom.removeEventListener(key.slice(2), vnode3.events, false)
|
|
1000
|
+
vnode3.events[key] = undefined
|
|
1001
|
+
}
|
|
1002
|
+
} else if (value != null && (typeof value === "function" || typeof value === "object")) {
|
|
1003
|
+
vnode3.events = new EventDict()
|
|
1004
|
+
vnode3.dom.addEventListener(key.slice(2), vnode3.events, false)
|
|
1005
|
+
vnode3.events[key] = value
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
//lifecycle
|
|
1009
|
+
function initLifecycle(source, vnode3, hooks) {
|
|
1010
|
+
if (typeof source.oninit === "function") callHook.call(source.oninit, vnode3)
|
|
1011
|
+
if (typeof source.oncreate === "function") hooks.push(callHook.bind(source.oncreate, vnode3))
|
|
1012
|
+
}
|
|
1013
|
+
function updateLifecycle(source, vnode3, hooks) {
|
|
1014
|
+
if (typeof source.onupdate === "function") hooks.push(callHook.bind(source.onupdate, vnode3))
|
|
1015
|
+
}
|
|
1016
|
+
function shouldNotUpdate(vnode3, old) {
|
|
1017
|
+
do {
|
|
1018
|
+
if (vnode3.attrs != null && typeof vnode3.attrs.onbeforeupdate === "function") {
|
|
1019
|
+
var force = callHook.call(vnode3.attrs.onbeforeupdate, vnode3, old)
|
|
1020
|
+
if (force !== undefined && !force) break
|
|
1021
|
+
}
|
|
1022
|
+
if (typeof vnode3.tag !== "string" && typeof vnode3.state.onbeforeupdate === "function") {
|
|
1023
|
+
var force = callHook.call(vnode3.state.onbeforeupdate, vnode3, old)
|
|
1024
|
+
if (force !== undefined && !force) break
|
|
1025
|
+
}
|
|
1026
|
+
return false
|
|
1027
|
+
} while (false); // eslint-disable-line no-constant-condition
|
|
1028
|
+
reuseOld(vnode3, old)
|
|
1029
|
+
return true
|
|
1030
|
+
}
|
|
1031
|
+
function reuseOld(vnode3, old) {
|
|
1032
|
+
vnode3.dom = old.dom
|
|
1033
|
+
vnode3.domSize = old.domSize
|
|
1034
|
+
vnode3.instance = old.instance
|
|
1035
|
+
// One would think having the actual latest attributes would be ideal,
|
|
1036
|
+
// but it doesn't let us properly diff based on our current internal
|
|
1037
|
+
// representation. We have to save not only the old DOM info, but also
|
|
1038
|
+
// the attributes used to create it, as we diff *that*, not against the
|
|
1039
|
+
// DOM directly (with a few exceptions in `setAttr`). And, of course, we
|
|
1040
|
+
// need to save the children and text as they are conceptually not
|
|
1041
|
+
// unlike special "attributes" internally.
|
|
1042
|
+
vnode3.attrs = old.attrs
|
|
1043
|
+
vnode3.children = old.children
|
|
1044
|
+
vnode3.text = old.text
|
|
1045
|
+
}
|
|
1046
|
+
var currentDOM
|
|
1047
|
+
return function(dom, vnodes, redraw) {
|
|
1048
|
+
if (!dom) throw new TypeError("DOM element being rendered to does not exist.")
|
|
1049
|
+
if (currentDOM != null && dom.contains(currentDOM)) {
|
|
1050
|
+
throw new TypeError("Node is currently being rendered to and thus is locked.")
|
|
1051
|
+
}
|
|
1052
|
+
var prevRedraw = currentRedraw
|
|
1053
|
+
var prevDOM = currentDOM
|
|
1054
|
+
var prevErrors = currentErrors
|
|
1055
|
+
var hooks = []
|
|
1056
|
+
var active = activeElement(dom)
|
|
1057
|
+
var namespace = dom.namespaceURI
|
|
1058
|
+
var errors, completed = false
|
|
1059
|
+
currentDOM = dom
|
|
1060
|
+
currentRedraw = typeof redraw === "function" ? redraw : undefined
|
|
1061
|
+
currentRender = {}
|
|
1062
|
+
currentErrors = null
|
|
1063
|
+
try {
|
|
1064
|
+
// First time rendering into a node clears it out
|
|
1065
|
+
if (dom.vnodes == null) dom.textContent = ""
|
|
1066
|
+
vnodes = Vnode.normalizeChildren(Array.isArray(vnodes) ? vnodes : [vnodes])
|
|
1067
|
+
updateNodes(dom, dom.vnodes, vnodes, hooks, null, namespace === "http://www.w3.org/1999/xhtml" ? undefined : namespace)
|
|
1068
|
+
dom.vnodes = vnodes
|
|
1069
|
+
// `document.activeElement` can return null: https://html.spec.whatwg.org/multipage/interaction.html#dom-document-activeelement
|
|
1070
|
+
if (active != null && activeElement(dom) !== active && typeof active.focus === "function") active.focus()
|
|
1071
|
+
for (var i = 0; i < hooks.length; i++) hooks[i]()
|
|
1072
|
+
completed = true
|
|
1073
|
+
} finally {
|
|
1074
|
+
errors = currentErrors
|
|
1075
|
+
currentErrors = prevErrors
|
|
1076
|
+
currentRedraw = prevRedraw
|
|
1077
|
+
currentDOM = prevDOM
|
|
1078
|
+
// Only one error can be thrown. Report the others, so none is silently lost.
|
|
1079
|
+
if (errors != null) {
|
|
1080
|
+
for (var j = completed ? 1 : 0; j < errors.length; j++) console.error(errors[j])
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
// The tree is in sync with the DOM at this point, so it is safe to surface
|
|
1084
|
+
// the first error a component threw during this pass.
|
|
1085
|
+
if (errors != null) throw errors[0]
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
var render = _14()
|
|
1089
|
+
var _21 = function(render2, schedule, console) {
|
|
1090
|
+
var subscriptions = []
|
|
1091
|
+
var pending = false
|
|
1092
|
+
var offset = -1
|
|
1093
|
+
function sync() {
|
|
1094
|
+
for (offset = 0; offset < subscriptions.length; offset += 2) {
|
|
1095
|
+
try { render2(subscriptions[offset], Vnode(subscriptions[offset + 1]), redraw) }
|
|
1096
|
+
catch (e) { console.error(e) }
|
|
1097
|
+
}
|
|
1098
|
+
offset = -1
|
|
1099
|
+
}
|
|
1100
|
+
function redraw() {
|
|
1101
|
+
if (!pending) {
|
|
1102
|
+
pending = true
|
|
1103
|
+
schedule(function() {
|
|
1104
|
+
pending = false
|
|
1105
|
+
sync()
|
|
1106
|
+
})
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
redraw.sync = sync
|
|
1110
|
+
function mount(root, component) {
|
|
1111
|
+
if (component != null && component.view == null && typeof component !== "function") {
|
|
1112
|
+
throw new TypeError("m.mount expects a component, not a vnode.")
|
|
1113
|
+
}
|
|
1114
|
+
var index = subscriptions.indexOf(root)
|
|
1115
|
+
if (index >= 0) {
|
|
1116
|
+
subscriptions.splice(index, 2)
|
|
1117
|
+
if (index <= offset) offset -= 2
|
|
1118
|
+
render2(root, [])
|
|
1119
|
+
}
|
|
1120
|
+
if (component != null) {
|
|
1121
|
+
subscriptions.push(root, component)
|
|
1122
|
+
render2(root, Vnode(component), redraw)
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
return {mount: mount, redraw: redraw}
|
|
1126
|
+
}
|
|
1127
|
+
var mountRedraw = _21(render, typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : null, typeof console !== "undefined" ? console : null)
|
|
1128
|
+
var buildQueryString = function(object) {
|
|
1129
|
+
if (Object.prototype.toString.call(object) !== "[object Object]") return ""
|
|
1130
|
+
var args = []
|
|
1131
|
+
for (var key in object) {
|
|
1132
|
+
destructure(key, object[key])
|
|
1133
|
+
}
|
|
1134
|
+
return args.join("&")
|
|
1135
|
+
function destructure(key, value1) {
|
|
1136
|
+
if (Array.isArray(value1)) {
|
|
1137
|
+
for (var i = 0; i < value1.length; i++) {
|
|
1138
|
+
destructure(key + "[" + i + "]", value1[i])
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
else if (Object.prototype.toString.call(value1) === "[object Object]") {
|
|
1142
|
+
for (var i in value1) {
|
|
1143
|
+
destructure(key + "[" + i + "]", value1[i])
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
else args.push(encodeURIComponent(key) + (value1 != null && value1 !== "" ? "=" + encodeURIComponent(value1) : ""))
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
// Returns `path` from `template` + `params`
|
|
1150
|
+
var buildPathname = function(template, params) {
|
|
1151
|
+
if ((/:([^\/\.-]+)(\.{3})?:/).test(template)) {
|
|
1152
|
+
throw new SyntaxError("Template parameter names must be separated by either a '/', '-', or '.'.")
|
|
1153
|
+
}
|
|
1154
|
+
if (params == null) return template
|
|
1155
|
+
var queryIndex = template.indexOf("?")
|
|
1156
|
+
var hashIndex = template.indexOf("#")
|
|
1157
|
+
var queryEnd = hashIndex < 0 ? template.length : hashIndex
|
|
1158
|
+
// A `?` after the `#` belongs to the fragment, it doesn't start a query string.
|
|
1159
|
+
if (queryIndex > queryEnd) queryIndex = -1
|
|
1160
|
+
var pathEnd = queryIndex < 0 ? queryEnd : queryIndex
|
|
1161
|
+
var path = template.slice(0, pathEnd)
|
|
1162
|
+
var query = {}
|
|
1163
|
+
Object.assign(query, params)
|
|
1164
|
+
var resolved = path.replace(/:([^\/\.-]+)(\.{3})?/g, function(m3, key, variadic) {
|
|
1165
|
+
delete query[key]
|
|
1166
|
+
// If no such parameter exists, don't interpolate it.
|
|
1167
|
+
if (params[key] == null) return m3
|
|
1168
|
+
// Escape normal parameters, but not variadic ones.
|
|
1169
|
+
return variadic ? params[key] : encodeURIComponent(String(params[key]))
|
|
1170
|
+
})
|
|
1171
|
+
// In case the template substitution adds new query/hash parameters.
|
|
1172
|
+
var newQueryIndex = resolved.indexOf("?")
|
|
1173
|
+
var newHashIndex = resolved.indexOf("#")
|
|
1174
|
+
var newQueryEnd = newHashIndex < 0 ? resolved.length : newHashIndex
|
|
1175
|
+
if (newQueryIndex > newQueryEnd) newQueryIndex = -1
|
|
1176
|
+
var newPathEnd = newQueryIndex < 0 ? newQueryEnd : newQueryIndex
|
|
1177
|
+
var result0 = resolved.slice(0, newPathEnd)
|
|
1178
|
+
if (queryIndex >= 0) result0 += template.slice(queryIndex, queryEnd)
|
|
1179
|
+
if (newQueryIndex >= 0) result0 += (queryIndex < 0 ? "?" : "&") + resolved.slice(newQueryIndex, newQueryEnd)
|
|
1180
|
+
var querystring = buildQueryString(query)
|
|
1181
|
+
if (querystring) result0 += (queryIndex < 0 && newQueryIndex < 0 ? "?" : "&") + querystring
|
|
1182
|
+
if (hashIndex >= 0) result0 += template.slice(hashIndex)
|
|
1183
|
+
if (newHashIndex >= 0) result0 += (hashIndex < 0 ? "" : "&") + resolved.slice(newHashIndex)
|
|
1184
|
+
return result0
|
|
1185
|
+
}
|
|
1186
|
+
var _25 = function($window, oncompletion) {
|
|
1187
|
+
function PromiseProxy(executor) {
|
|
1188
|
+
return new Promise(executor)
|
|
1189
|
+
}
|
|
1190
|
+
function makeRequest(url, args) {
|
|
1191
|
+
return new Promise(function(resolve, reject) {
|
|
1192
|
+
url = buildPathname(url, args.params)
|
|
1193
|
+
var method = args.method != null ? args.method.toUpperCase() : "GET"
|
|
1194
|
+
var body = args.body
|
|
1195
|
+
var assumeJSON = (args.serialize == null || args.serialize === JSON.stringify) && !(body instanceof $window.FormData || body instanceof $window.URLSearchParams)
|
|
1196
|
+
var responseType = args.responseType || (typeof args.extract === "function" ? "" : "json")
|
|
1197
|
+
var xhr = new $window.XMLHttpRequest(), aborted = false, isTimeout = false
|
|
1198
|
+
var original0 = xhr, replacedAbort
|
|
1199
|
+
var abort = xhr.abort
|
|
1200
|
+
xhr.abort = function() {
|
|
1201
|
+
aborted = true
|
|
1202
|
+
abort.call(this)
|
|
1203
|
+
}
|
|
1204
|
+
xhr.open(method, url, args.async !== false, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
|
|
1205
|
+
if (assumeJSON && body != null && !hasHeader(args, "content-type")) {
|
|
1206
|
+
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
|
|
1207
|
+
}
|
|
1208
|
+
if (typeof args.deserialize !== "function" && !hasHeader(args, "accept")) {
|
|
1209
|
+
xhr.setRequestHeader("Accept", "application/json, text/*")
|
|
1210
|
+
}
|
|
1211
|
+
if (args.withCredentials) xhr.withCredentials = args.withCredentials
|
|
1212
|
+
if (args.timeout) xhr.timeout = args.timeout
|
|
1213
|
+
xhr.responseType = responseType
|
|
1214
|
+
for (var key in args.headers) {
|
|
1215
|
+
if (hasOwn.call(args.headers, key)) {
|
|
1216
|
+
xhr.setRequestHeader(key, args.headers[key])
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
xhr.onreadystatechange = function(ev) {
|
|
1220
|
+
// Don't throw errors on xhr.abort().
|
|
1221
|
+
if (aborted) return
|
|
1222
|
+
if (ev.target.readyState === 4) {
|
|
1223
|
+
try {
|
|
1224
|
+
var success = (ev.target.status >= 200 && ev.target.status < 300) || ev.target.status === 304 || (/^file:\/\//i).test(url)
|
|
1225
|
+
// When the response type isn't "" or "text",
|
|
1226
|
+
// `xhr.responseText` is the wrong thing to use.
|
|
1227
|
+
// Browsers do the right thing and throw here, and we
|
|
1228
|
+
// should honor that and do the right thing by
|
|
1229
|
+
// preferring `xhr.response` where possible/practical.
|
|
1230
|
+
var response = ev.target.response, message
|
|
1231
|
+
if (responseType === "json") {
|
|
1232
|
+
// For IE and Edge, which don't implement
|
|
1233
|
+
// `responseType: "json"`.
|
|
1234
|
+
if (!ev.target.responseType && typeof args.extract !== "function") {
|
|
1235
|
+
// Handle no-content which will not parse.
|
|
1236
|
+
try { response = JSON.parse(ev.target.responseText) }
|
|
1237
|
+
catch (e) { response = null }
|
|
1238
|
+
}
|
|
1239
|
+
} else if (!responseType || responseType === "text") {
|
|
1240
|
+
// Only use this default if it's text. If a parsed
|
|
1241
|
+
// document is needed on old IE and friends (all
|
|
1242
|
+
// unsupported), the user should use a custom
|
|
1243
|
+
// `config` instead. They're already using this at
|
|
1244
|
+
// their own risk.
|
|
1245
|
+
if (response == null) response = ev.target.responseText
|
|
1246
|
+
}
|
|
1247
|
+
if (typeof args.extract === "function") {
|
|
1248
|
+
response = args.extract(ev.target, args)
|
|
1249
|
+
success = true
|
|
1250
|
+
} else if (typeof args.deserialize === "function") {
|
|
1251
|
+
response = args.deserialize(response)
|
|
1252
|
+
}
|
|
1253
|
+
if (success) {
|
|
1254
|
+
if (typeof args.type === "function") {
|
|
1255
|
+
if (Array.isArray(response)) {
|
|
1256
|
+
for (var i = 0; i < response.length; i++) {
|
|
1257
|
+
response[i] = new args.type(response[i])
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
else response = new args.type(response)
|
|
1261
|
+
}
|
|
1262
|
+
resolve(response)
|
|
1263
|
+
}
|
|
1264
|
+
else {
|
|
1265
|
+
var completeErrorResponse = function() {
|
|
1266
|
+
try { message = ev.target.responseText }
|
|
1267
|
+
catch (e) { message = response }
|
|
1268
|
+
// Error() stringifies non-strings poorly (e.g. [object Object]);
|
|
1269
|
+
// responseText is also unavailable for responseType json/arraybuffer/etc.
|
|
1270
|
+
if (typeof message !== "string") {
|
|
1271
|
+
try { message = JSON.stringify(message) }
|
|
1272
|
+
catch (stringifyError) { message = String(message) }
|
|
1273
|
+
}
|
|
1274
|
+
var error = new Error(message)
|
|
1275
|
+
error.code = ev.target.status
|
|
1276
|
+
error.response = response
|
|
1277
|
+
reject(error)
|
|
1278
|
+
}
|
|
1279
|
+
if (xhr.status === 0) {
|
|
1280
|
+
// Use setTimeout to push this code block onto the event queue
|
|
1281
|
+
// This allows `xhr.ontimeout` to run in the case that there is a timeout
|
|
1282
|
+
// Without this setTimeout, `xhr.ontimeout` doesn't have a chance to reject
|
|
1283
|
+
// as `xhr.onreadystatechange` will run before it
|
|
1284
|
+
setTimeout(function() {
|
|
1285
|
+
if (isTimeout) return
|
|
1286
|
+
completeErrorResponse()
|
|
1287
|
+
})
|
|
1288
|
+
} else completeErrorResponse()
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
catch (e) {
|
|
1292
|
+
reject(e)
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
xhr.ontimeout = function (ev) {
|
|
1297
|
+
isTimeout = true
|
|
1298
|
+
var error = new Error("Request timed out")
|
|
1299
|
+
error.code = ev.target.status
|
|
1300
|
+
reject(error)
|
|
1301
|
+
}
|
|
1302
|
+
if (typeof args.config === "function") {
|
|
1303
|
+
xhr = args.config(xhr, args, url) || xhr
|
|
1304
|
+
// Propagate the `abort` to any replacement XHR as well.
|
|
1305
|
+
if (xhr !== original0) {
|
|
1306
|
+
replacedAbort = xhr.abort
|
|
1307
|
+
xhr.abort = function() {
|
|
1308
|
+
aborted = true
|
|
1309
|
+
replacedAbort.call(this)
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
if (body == null) xhr.send()
|
|
1314
|
+
else if (typeof args.serialize === "function") xhr.send(args.serialize(body))
|
|
1315
|
+
else if (body instanceof $window.FormData || body instanceof $window.URLSearchParams) xhr.send(body)
|
|
1316
|
+
else xhr.send(JSON.stringify(body))
|
|
1317
|
+
})
|
|
1318
|
+
}
|
|
1319
|
+
// In case the global Promise is some userland library's where they rely on
|
|
1320
|
+
// `foo instanceof this.constructor`, `this.constructor.resolve(value)`, or
|
|
1321
|
+
// similar. Let's *not* break them.
|
|
1322
|
+
PromiseProxy.prototype = Promise.prototype
|
|
1323
|
+
PromiseProxy.__proto__ = Promise // eslint-disable-line no-proto
|
|
1324
|
+
function hasHeader(args, name) {
|
|
1325
|
+
for (var key in args.headers) {
|
|
1326
|
+
if (hasOwn.call(args.headers, key) && key.toLowerCase() === name) return true
|
|
1327
|
+
}
|
|
1328
|
+
return false
|
|
1329
|
+
}
|
|
1330
|
+
return {
|
|
1331
|
+
request: function(url, args) {
|
|
1332
|
+
if (typeof url !== "string") { args = url; url = url.url }
|
|
1333
|
+
else if (args == null) args = {}
|
|
1334
|
+
var promise = makeRequest(url, args)
|
|
1335
|
+
if (args.background === true) return promise
|
|
1336
|
+
var count = 0
|
|
1337
|
+
function complete() {
|
|
1338
|
+
if (--count === 0 && typeof oncompletion === "function") oncompletion()
|
|
1339
|
+
}
|
|
1340
|
+
return wrap(promise)
|
|
1341
|
+
function wrap(promise) {
|
|
1342
|
+
var then = promise.then
|
|
1343
|
+
// Set the constructor, so engines know to not await or resolve
|
|
1344
|
+
// this as a native promise. At the time of writing, this is
|
|
1345
|
+
// only necessary for V8, but their behavior is the correct
|
|
1346
|
+
// behavior per spec. See this spec issue for more details:
|
|
1347
|
+
// https://github.com/tc39/ecma262/issues/1577. Also, see the
|
|
1348
|
+
// corresponding comment in `request/tests/test-request.js` for
|
|
1349
|
+
// a bit more background on the issue at hand.
|
|
1350
|
+
promise.constructor = PromiseProxy
|
|
1351
|
+
promise.then = function() {
|
|
1352
|
+
count++
|
|
1353
|
+
var next = then.apply(promise, arguments)
|
|
1354
|
+
next.then(complete, function(e) {
|
|
1355
|
+
complete()
|
|
1356
|
+
if (count === 0) throw e
|
|
1357
|
+
})
|
|
1358
|
+
return wrap(next)
|
|
1359
|
+
}
|
|
1360
|
+
return promise
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
var request = _25(typeof window !== "undefined" ? window : null, mountRedraw.redraw)
|
|
1366
|
+
/*
|
|
1367
|
+
Percent encodings encode UTF-8 bytes, so this regexp needs to match that.
|
|
1368
|
+
Here's how UTF-8 encodes stuff:
|
|
1369
|
+
- `00-7F`: 1-byte, for U+0000-U+007F
|
|
1370
|
+
- `C2-DF 80-BF`: 2-byte, for U+0080-U+07FF
|
|
1371
|
+
- `E0-EF 80-BF 80-BF`: 3-byte, encodes U+0800-U+FFFF
|
|
1372
|
+
- `F0-F4 80-BF 80-BF 80-BF`: 4-byte, encodes U+10000-U+10FFFF
|
|
1373
|
+
In this, there's a number of invalid byte sequences:
|
|
1374
|
+
- `80-BF`: Continuation byte, invalid as start
|
|
1375
|
+
- `C0-C1 80-BF`: Overlong encoding for U+0000-U+007F
|
|
1376
|
+
- `E0 80-9F 80-BF`: Overlong encoding for U+0080-U+07FF
|
|
1377
|
+
- `ED A0-BF 80-BF`: Encoding for UTF-16 surrogate U+D800-U+DFFF
|
|
1378
|
+
- `F0 80-8F 80-BF 80-BF`: Overlong encoding for U+0800-U+FFFF
|
|
1379
|
+
- `F4 90-BF`: RFC 3629 restricted UTF-8 to only code points UTF-16 could encode.
|
|
1380
|
+
- `F5-FF`: RFC 3629 restricted UTF-8 to only code points UTF-16 could encode.
|
|
1381
|
+
So in reality, only the following sequences can encode are valid characters:
|
|
1382
|
+
- 00-7F
|
|
1383
|
+
- C2-DF 80-BF
|
|
1384
|
+
- E0 A0-BF 80-BF
|
|
1385
|
+
- E1-EC 80-BF 80-BF
|
|
1386
|
+
- ED 80-9F 80-BF
|
|
1387
|
+
- EE-EF 80-BF 80-BF
|
|
1388
|
+
- F0 90-BF 80-BF 80-BF
|
|
1389
|
+
- F1-F3 80-BF 80-BF 80-BF
|
|
1390
|
+
- F4 80-8F 80-BF 80-BF
|
|
1391
|
+
The regexp just tries to match this as compactly as possible.
|
|
1392
|
+
*/
|
|
1393
|
+
var validUtf8Encodings = /%(?:[0-7]|(?!c[01]|e0%[89]|ed%[ab]|f0%8|f4%[9ab])(?:c|d|(?:e|f[0-4]%[89ab])[\da-f]%[89ab])[\da-f]%[89ab])[\da-f]/gi
|
|
1394
|
+
var decodeURIComponentSafe = function(str) {
|
|
1395
|
+
return String(str).replace(validUtf8Encodings, decodeURIComponent)
|
|
1396
|
+
}
|
|
1397
|
+
var parseQueryString = function(string) {
|
|
1398
|
+
if (string === "" || string == null) return {}
|
|
1399
|
+
if (string.charAt(0) === "?") string = string.slice(1)
|
|
1400
|
+
var entries = string.split("&"), counters = {}, data0 = {}
|
|
1401
|
+
for (var i = 0; i < entries.length; i++) {
|
|
1402
|
+
// Only the first `=` separates the key from the value. The value may
|
|
1403
|
+
// contain more of them, e.g. base64 padding or a nested URL.
|
|
1404
|
+
var entry = entries[i]
|
|
1405
|
+
var separator = entry.indexOf("=")
|
|
1406
|
+
var key = decodeURIComponentSafe(separator < 0 ? entry : entry.slice(0, separator))
|
|
1407
|
+
var value2 = separator < 0 ? "" : decodeURIComponentSafe(entry.slice(separator + 1))
|
|
1408
|
+
if (value2 === "true") value2 = true
|
|
1409
|
+
else if (value2 === "false") value2 = false
|
|
1410
|
+
var levels = key.split(/\]\[?|\[/)
|
|
1411
|
+
var cursor = data0
|
|
1412
|
+
if (key.indexOf("[") > -1) levels.pop()
|
|
1413
|
+
for (var j0 = 0; j0 < levels.length; j0++) {
|
|
1414
|
+
var level = levels[j0], nextLevel = levels[j0 + 1]
|
|
1415
|
+
var isNumber = nextLevel == "" || !isNaN(parseInt(nextLevel, 10))
|
|
1416
|
+
if (level === "") {
|
|
1417
|
+
var key = levels.slice(0, j0).join()
|
|
1418
|
+
if (counters[key] == null) {
|
|
1419
|
+
counters[key] = Array.isArray(cursor) ? cursor.length : 0
|
|
1420
|
+
}
|
|
1421
|
+
level = counters[key]++
|
|
1422
|
+
}
|
|
1423
|
+
// Disallow direct prototype pollution
|
|
1424
|
+
else if (level === "__proto__") break
|
|
1425
|
+
if (j0 === levels.length - 1) cursor[level] = value2
|
|
1426
|
+
else {
|
|
1427
|
+
// Read own properties exclusively to disallow indirect
|
|
1428
|
+
// prototype pollution
|
|
1429
|
+
var desc = Object.getOwnPropertyDescriptor(cursor, level)
|
|
1430
|
+
if (desc != null) desc = desc.value
|
|
1431
|
+
if (desc == null) cursor[level] = desc = isNumber ? [] : {}
|
|
1432
|
+
cursor = desc
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
return data0
|
|
1437
|
+
}
|
|
1438
|
+
// Returns `{path, params}` from `url`
|
|
1439
|
+
var parsePathname = function(url) {
|
|
1440
|
+
var queryIndex0 = url.indexOf("?")
|
|
1441
|
+
var hashIndex0 = url.indexOf("#")
|
|
1442
|
+
var queryEnd0 = hashIndex0 < 0 ? url.length : hashIndex0
|
|
1443
|
+
// A `?` after the `#` belongs to the fragment, it doesn't start a query string.
|
|
1444
|
+
if (queryIndex0 > queryEnd0) queryIndex0 = -1
|
|
1445
|
+
var pathEnd0 = queryIndex0 < 0 ? queryEnd0 : queryIndex0
|
|
1446
|
+
var path1 = url.slice(0, pathEnd0).replace(/\/{2,}/g, "/")
|
|
1447
|
+
if (!path1) path1 = "/"
|
|
1448
|
+
else {
|
|
1449
|
+
if (path1[0] !== "/") path1 = "/" + path1
|
|
1450
|
+
}
|
|
1451
|
+
return {
|
|
1452
|
+
path: path1,
|
|
1453
|
+
params: queryIndex0 < 0
|
|
1454
|
+
? {}
|
|
1455
|
+
: parseQueryString(url.slice(queryIndex0 + 1, queryEnd0)),
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
// Compiles a template into a function that takes a resolved path (without query
|
|
1459
|
+
// strings) and returns an object containing the template parameters with their
|
|
1460
|
+
// parsed values. This expects the input of the compiled template to be the
|
|
1461
|
+
// output of `parsePathname`. Note that it does *not* remove query parameters
|
|
1462
|
+
// specified in the template.
|
|
1463
|
+
var compileTemplate = function(template) {
|
|
1464
|
+
var templateData = parsePathname(template)
|
|
1465
|
+
var templateKeys = Object.keys(templateData.params)
|
|
1466
|
+
var keys = []
|
|
1467
|
+
var regexp = new RegExp("^" + templateData.path.replace(
|
|
1468
|
+
// I escape literal text so people can use things like `:file.:ext` or
|
|
1469
|
+
// `:lang-:locale` in routes. This is all merged into one pass so I
|
|
1470
|
+
// don't also accidentally escape `-` and make it harder to detect it to
|
|
1471
|
+
// ban it from template parameters.
|
|
1472
|
+
/:([^\/.-]+)(\.{3}|\.(?!\.)|-)?|[\\^$*+.()|\[\]{}]/g,
|
|
1473
|
+
function(m4, key0, extra) {
|
|
1474
|
+
if (key0 == null) return "\\" + m4
|
|
1475
|
+
keys.push({k: key0, r: extra === "..."})
|
|
1476
|
+
if (extra === "...") return "(.*)"
|
|
1477
|
+
if (extra === ".") return "([^/]+)\\."
|
|
1478
|
+
return "([^/]+)" + (extra || "")
|
|
1479
|
+
}
|
|
1480
|
+
) + "\\/?$")
|
|
1481
|
+
return function(data1) {
|
|
1482
|
+
// First, check the params. Usually, there isn't any, and it's just
|
|
1483
|
+
// checking a static set.
|
|
1484
|
+
for (var i = 0; i < templateKeys.length; i++) {
|
|
1485
|
+
if (templateData.params[templateKeys[i]] !== data1.params[templateKeys[i]]) return false
|
|
1486
|
+
}
|
|
1487
|
+
// If no interpolations exist, let's skip all the ceremony
|
|
1488
|
+
if (!keys.length) return regexp.test(data1.path)
|
|
1489
|
+
var values = regexp.exec(data1.path)
|
|
1490
|
+
if (values == null) return false
|
|
1491
|
+
for (var i = 0; i < keys.length; i++) {
|
|
1492
|
+
data1.params[keys[i].k] = keys[i].r ? values[i + 1] : decodeURIComponentSafe(values[i + 1])
|
|
1493
|
+
}
|
|
1494
|
+
return true
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
// Note: this is mildly perf-sensitive.
|
|
1498
|
+
//
|
|
1499
|
+
// It does *not* use `delete` - dynamic `delete`s usually cause objects to bail
|
|
1500
|
+
// out into dictionary mode and just generally cause a bunch of optimization
|
|
1501
|
+
// issues within engines.
|
|
1502
|
+
//
|
|
1503
|
+
// Ideally, I would've preferred to do this, if it weren't for the optimization
|
|
1504
|
+
// issues:
|
|
1505
|
+
//
|
|
1506
|
+
// ```js
|
|
1507
|
+
// const hasOwn = hasOwn
|
|
1508
|
+
// const magic = [
|
|
1509
|
+
// "key", "oninit", "oncreate", "onbeforeupdate", "onupdate",
|
|
1510
|
+
// "onbeforeremove", "onremove",
|
|
1511
|
+
// ]
|
|
1512
|
+
// var censor = (attrs, extras) => {
|
|
1513
|
+
// const result = Object.assign(Object.create(null), attrs)
|
|
1514
|
+
// for (const key of magic) delete result[key]
|
|
1515
|
+
// if (extras != null) for (const key of extras) delete result[key]
|
|
1516
|
+
// return result
|
|
1517
|
+
// }
|
|
1518
|
+
// ```
|
|
1519
|
+
var magic = /^(?:key|oninit|oncreate|onbeforeupdate|onupdate|onbeforeremove|onremove)$/
|
|
1520
|
+
var censor = function(attrs7, extras) {
|
|
1521
|
+
var result2 = {}
|
|
1522
|
+
if (extras != null) {
|
|
1523
|
+
for (var key1 in attrs7) {
|
|
1524
|
+
if (hasOwn.call(attrs7, key1) && !magic.test(key1) && extras.indexOf(key1) < 0) {
|
|
1525
|
+
result2[key1] = attrs7[key1]
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
} else {
|
|
1529
|
+
for (var key1 in attrs7) {
|
|
1530
|
+
if (hasOwn.call(attrs7, key1) && !magic.test(key1)) {
|
|
1531
|
+
result2[key1] = attrs7[key1]
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
return result2
|
|
1536
|
+
}
|
|
1537
|
+
var _31 = function($window, mountRedraw0) {
|
|
1538
|
+
var p = Promise.resolve()
|
|
1539
|
+
var scheduled = false
|
|
1540
|
+
var ready = false
|
|
1541
|
+
var hasBeenResolved = false
|
|
1542
|
+
var dom0, compiled, fallbackRoute
|
|
1543
|
+
var currentResolver, component, attrs6, currentPath, lastUpdate
|
|
1544
|
+
var RouterRoot = {
|
|
1545
|
+
onremove: function() {
|
|
1546
|
+
ready = hasBeenResolved = false
|
|
1547
|
+
$window.removeEventListener("popstate", fireAsync, false)
|
|
1548
|
+
},
|
|
1549
|
+
view: function() {
|
|
1550
|
+
// The route has already been resolved.
|
|
1551
|
+
// Therefore, the following early return is not needed.
|
|
1552
|
+
// if (!hasBeenResolved) return
|
|
1553
|
+
var vnode6 = Vnode(component, attrs6.key, attrs6)
|
|
1554
|
+
if (currentResolver) return currentResolver.render(vnode6)
|
|
1555
|
+
// Wrap in a fragment to preserve existing key semantics
|
|
1556
|
+
return [vnode6]
|
|
1557
|
+
},
|
|
1558
|
+
}
|
|
1559
|
+
var SKIP = route.SKIP = {}
|
|
1560
|
+
function resolveRoute() {
|
|
1561
|
+
scheduled = false
|
|
1562
|
+
// Consider the pathname holistically. The prefix might even be invalid,
|
|
1563
|
+
// but that's not our problem.
|
|
1564
|
+
var prefix = $window.location.hash
|
|
1565
|
+
if (route.prefix[0] !== "#") {
|
|
1566
|
+
prefix = $window.location.search + prefix
|
|
1567
|
+
if (route.prefix[0] !== "?") {
|
|
1568
|
+
prefix = $window.location.pathname + prefix
|
|
1569
|
+
if (prefix[0] !== "/") prefix = "/" + prefix
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
// This seemingly useless `.concat()` speeds up the tests quite a bit,
|
|
1573
|
+
// since the representation is consistently a relatively poorly
|
|
1574
|
+
// optimized cons string.
|
|
1575
|
+
var path0 = prefix.concat()
|
|
1576
|
+
.replace(/(?:%[a-f89][a-f0-9])+/gim, decodeURIComponentSafe)
|
|
1577
|
+
.slice(route.prefix.length)
|
|
1578
|
+
var data = parsePathname(path0)
|
|
1579
|
+
Object.assign(data.params, $window.history.state)
|
|
1580
|
+
function reject(e) {
|
|
1581
|
+
console.error(e)
|
|
1582
|
+
route.set(fallbackRoute, null, {replace: true})
|
|
1583
|
+
}
|
|
1584
|
+
loop(0)
|
|
1585
|
+
function loop(i) {
|
|
1586
|
+
for (; i < compiled.length; i++) {
|
|
1587
|
+
if (compiled[i].check(data)) {
|
|
1588
|
+
var payload = compiled[i].component
|
|
1589
|
+
var matchedRoute = compiled[i].route
|
|
1590
|
+
var localComp = payload
|
|
1591
|
+
var update = lastUpdate = function(comp) {
|
|
1592
|
+
if (update !== lastUpdate) return
|
|
1593
|
+
if (comp === SKIP) return loop(i + 1)
|
|
1594
|
+
component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div"
|
|
1595
|
+
attrs6 = data.params, currentPath = path0, lastUpdate = null
|
|
1596
|
+
currentResolver = payload.render ? payload : null
|
|
1597
|
+
if (hasBeenResolved) mountRedraw0.redraw()
|
|
1598
|
+
else {
|
|
1599
|
+
hasBeenResolved = true
|
|
1600
|
+
mountRedraw0.mount(dom0, RouterRoot)
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
// There's no understating how much I *wish* I could
|
|
1604
|
+
// use `async`/`await` here...
|
|
1605
|
+
if (payload.view || typeof payload === "function") {
|
|
1606
|
+
payload = {}
|
|
1607
|
+
update(localComp)
|
|
1608
|
+
}
|
|
1609
|
+
else if (payload.onmatch) {
|
|
1610
|
+
p.then(function () {
|
|
1611
|
+
return payload.onmatch(data.params, path0, matchedRoute)
|
|
1612
|
+
}).then(update, path0 === fallbackRoute ? null : function (e) {
|
|
1613
|
+
// Only the latest navigation may redirect to the fallback
|
|
1614
|
+
// route. A stale rejection is still reported.
|
|
1615
|
+
if (update === lastUpdate) reject(e)
|
|
1616
|
+
else console.error(e)
|
|
1617
|
+
})
|
|
1618
|
+
}
|
|
1619
|
+
else update(/* "div" */)
|
|
1620
|
+
return
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
if (path0 === fallbackRoute) {
|
|
1624
|
+
throw new Error("Could not resolve default route " + fallbackRoute + ".")
|
|
1625
|
+
}
|
|
1626
|
+
route.set(fallbackRoute, null, {replace: true})
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
function fireAsync() {
|
|
1630
|
+
if (!scheduled) {
|
|
1631
|
+
scheduled = true
|
|
1632
|
+
// TODO: just do `mountRedraw.redraw()` here and elide the timer
|
|
1633
|
+
// dependency. Note that this will muck with tests a *lot*, so it's
|
|
1634
|
+
// not as easy of a change as it sounds.
|
|
1635
|
+
setTimeout(resolveRoute)
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
function route(root, defaultRoute, routes) {
|
|
1639
|
+
if (!root) throw new TypeError("DOM element being rendered to does not exist.")
|
|
1640
|
+
compiled = Object.keys(routes).map(function(route) {
|
|
1641
|
+
if (route[0] !== "/") throw new SyntaxError("Routes must start with a '/'.")
|
|
1642
|
+
if ((/:([^\/\.-]+)(\.{3})?:/).test(route)) {
|
|
1643
|
+
throw new SyntaxError("Route parameter names must be separated with either '/', '.', or '-'.")
|
|
1644
|
+
}
|
|
1645
|
+
return {
|
|
1646
|
+
route: route,
|
|
1647
|
+
component: routes[route],
|
|
1648
|
+
check: compileTemplate(route),
|
|
1649
|
+
}
|
|
1650
|
+
})
|
|
1651
|
+
fallbackRoute = defaultRoute
|
|
1652
|
+
if (defaultRoute != null) {
|
|
1653
|
+
var defaultData = parsePathname(defaultRoute)
|
|
1654
|
+
if (!compiled.some(function (i) { return i.check(defaultData) })) {
|
|
1655
|
+
throw new ReferenceError("Default route doesn't match any known routes.")
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
dom0 = root
|
|
1659
|
+
$window.addEventListener("popstate", fireAsync, false)
|
|
1660
|
+
ready = true
|
|
1661
|
+
// The RouterRoot component is mounted when the route is first resolved.
|
|
1662
|
+
resolveRoute()
|
|
1663
|
+
}
|
|
1664
|
+
route.set = function(path0, data, options) {
|
|
1665
|
+
// A navigation that interrupts a pending one replaces its history entry.
|
|
1666
|
+
// Note: `options` belongs to the caller, so it must not be modified.
|
|
1667
|
+
var replace = lastUpdate != null || options != null && options.replace
|
|
1668
|
+
lastUpdate = null
|
|
1669
|
+
path0 = buildPathname(path0, data)
|
|
1670
|
+
if (ready) {
|
|
1671
|
+
fireAsync()
|
|
1672
|
+
var state = options ? options.state : null
|
|
1673
|
+
var title = options ? options.title : null
|
|
1674
|
+
if (replace) $window.history.replaceState(state, title, route.prefix + path0)
|
|
1675
|
+
else $window.history.pushState(state, title, route.prefix + path0)
|
|
1676
|
+
}
|
|
1677
|
+
else {
|
|
1678
|
+
$window.location.href = route.prefix + path0
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
route.get = function() {return currentPath}
|
|
1682
|
+
route.prefix = "#!"
|
|
1683
|
+
route.Link = {
|
|
1684
|
+
view: function(vnode6) {
|
|
1685
|
+
// Omit the used parameters from the rendered element - they are
|
|
1686
|
+
// internal. Also, censor the various lifecycle methods.
|
|
1687
|
+
//
|
|
1688
|
+
// We don't strip the other parameters because for convenience we
|
|
1689
|
+
// let them be specified in the selector as well.
|
|
1690
|
+
var child0 = hyperscript(
|
|
1691
|
+
vnode6.attrs.selector || "a",
|
|
1692
|
+
censor(vnode6.attrs, ["options", "params", "selector", "onclick"]),
|
|
1693
|
+
vnode6.children
|
|
1694
|
+
)
|
|
1695
|
+
var options, onclick, href
|
|
1696
|
+
// Let's provide a *right* way to disable a route link, rather than
|
|
1697
|
+
// letting people screw up accessibility on accident.
|
|
1698
|
+
//
|
|
1699
|
+
// The attribute is coerced so users don't get surprised over
|
|
1700
|
+
// `disabled: 0` resulting in a button that's somehow routable
|
|
1701
|
+
// despite being visibly disabled.
|
|
1702
|
+
if (child0.attrs.disabled = Boolean(child0.attrs.disabled)) {
|
|
1703
|
+
child0.attrs.href = null
|
|
1704
|
+
child0.attrs["aria-disabled"] = "true"
|
|
1705
|
+
// If you *really* do want add `onclick` on a disabled link, use
|
|
1706
|
+
// an `oncreate` hook to add it.
|
|
1707
|
+
} else {
|
|
1708
|
+
options = vnode6.attrs.options
|
|
1709
|
+
onclick = vnode6.attrs.onclick
|
|
1710
|
+
// Easier to build it now to keep it isomorphic.
|
|
1711
|
+
href = buildPathname(child0.attrs.href, vnode6.attrs.params)
|
|
1712
|
+
child0.attrs.href = route.prefix + href
|
|
1713
|
+
child0.attrs.onclick = function(e) {
|
|
1714
|
+
var result1
|
|
1715
|
+
if (typeof onclick === "function") {
|
|
1716
|
+
result1 = onclick.call(e.currentTarget, e)
|
|
1717
|
+
} else if (onclick == null || typeof onclick !== "object") {
|
|
1718
|
+
// do nothing
|
|
1719
|
+
} else if (typeof onclick.handleEvent === "function") {
|
|
1720
|
+
onclick.handleEvent(e)
|
|
1721
|
+
}
|
|
1722
|
+
// Adapted from React Router's implementation:
|
|
1723
|
+
// https://github.com/ReactTraining/react-router/blob/520a0acd48ae1b066eb0b07d6d4d1790a1d02482/packages/react-router-dom/modules/Link.js
|
|
1724
|
+
//
|
|
1725
|
+
// Try to be flexible and intuitive in how we handle links.
|
|
1726
|
+
// Fun fact: links aren't as obvious to get right as you
|
|
1727
|
+
// would expect. There's a lot more valid ways to click a
|
|
1728
|
+
// link than this, and one might want to not simply click a
|
|
1729
|
+
// link, but right click or command-click it to copy the
|
|
1730
|
+
// link target, etc. Nope, this isn't just for blind people.
|
|
1731
|
+
if (
|
|
1732
|
+
// Skip if `onclick` prevented default
|
|
1733
|
+
result1 !== false && !e.defaultPrevented &&
|
|
1734
|
+
// Ignore everything but left clicks
|
|
1735
|
+
(e.button === 0 || e.which === 0 || e.which === 1) &&
|
|
1736
|
+
// Let the browser handle `target=_blank`, etc.
|
|
1737
|
+
(!e.currentTarget.target || e.currentTarget.target === "_self") &&
|
|
1738
|
+
// No modifier keys
|
|
1739
|
+
!e.ctrlKey && !e.metaKey && !e.shiftKey && !e.altKey
|
|
1740
|
+
) {
|
|
1741
|
+
e.preventDefault()
|
|
1742
|
+
e.redraw = false
|
|
1743
|
+
route.set(href, null, options)
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
return child0
|
|
1748
|
+
},
|
|
1749
|
+
}
|
|
1750
|
+
route.param = function(key) {
|
|
1751
|
+
return attrs6 && key != null ? attrs6[key] : attrs6
|
|
1752
|
+
}
|
|
1753
|
+
return route
|
|
1754
|
+
}
|
|
1755
|
+
var router = _31(typeof window !== "undefined" ? window : null, mountRedraw)
|
|
1756
|
+
var m = function m() { return hyperscript.apply(this, arguments) }
|
|
1757
|
+
m.m = hyperscript
|
|
1758
|
+
m.trust = hyperscript.trust
|
|
1759
|
+
m.fragment = hyperscript.fragment
|
|
1760
|
+
m.Fragment = "["
|
|
1761
|
+
m.mount = mountRedraw.mount
|
|
1762
|
+
m.route = router
|
|
1763
|
+
m.render = render
|
|
1764
|
+
m.redraw = mountRedraw.redraw
|
|
1765
|
+
m.request = request.request
|
|
1766
|
+
m.parseQueryString = parseQueryString
|
|
1767
|
+
m.buildQueryString = buildQueryString
|
|
1768
|
+
m.parsePathname = parsePathname
|
|
1769
|
+
m.buildPathname = buildPathname
|
|
1770
|
+
m.vnode = Vnode
|
|
1771
|
+
m.censor = censor
|
|
1772
|
+
m.domFor = domFor
|
|
1773
|
+
if (typeof module !== "undefined") module["exports"] = m
|
|
1774
|
+
else window.m = m
|
|
1775
|
+
}());
|