@talyaron1/mithril 0.0.0-stage → 2.4.0
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 +89 -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/render/render.js
ADDED
|
@@ -0,0 +1,964 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
var Vnode = require("./vnode")
|
|
4
|
+
var delayedRemoval = require("./delayedRemoval")
|
|
5
|
+
var domFor = require("./domFor")
|
|
6
|
+
var cachedAttrsIsStaticMap = require("./cachedAttrsIsStaticMap")
|
|
7
|
+
|
|
8
|
+
module.exports = function() {
|
|
9
|
+
var nameSpace = {
|
|
10
|
+
svg: "http://www.w3.org/2000/svg",
|
|
11
|
+
math: "http://www.w3.org/1998/Math/MathML"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
var currentRedraw
|
|
15
|
+
var currentRender
|
|
16
|
+
var currentErrors
|
|
17
|
+
|
|
18
|
+
function getDocument(dom) {
|
|
19
|
+
return dom.ownerDocument;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getNameSpace(vnode) {
|
|
23
|
+
return vnode.attrs && vnode.attrs.xmlns || nameSpace[vnode.tag]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//sanity check to discourage people from doing `vnode.state = ...`
|
|
27
|
+
function checkState(vnode, original) {
|
|
28
|
+
if (vnode.state !== original) throw new Error("'vnode.state' must not be modified.")
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//Note: the hook is passed as the `this` argument to allow proxying the
|
|
32
|
+
//arguments without requiring a full array allocation to do so. It also
|
|
33
|
+
//takes advantage of the fact the current `vnode` is the first argument in
|
|
34
|
+
//all lifecycle methods.
|
|
35
|
+
function callHook(vnode) {
|
|
36
|
+
var original = vnode.state
|
|
37
|
+
try {
|
|
38
|
+
return this.apply(original, arguments)
|
|
39
|
+
} finally {
|
|
40
|
+
checkState(vnode, original)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// IE11 (at least) throws an UnspecifiedError when accessing document.activeElement when
|
|
45
|
+
// inside an iframe. Catch and swallow this error, and heavy-handidly return null.
|
|
46
|
+
function activeElement(dom) {
|
|
47
|
+
try {
|
|
48
|
+
return getDocument(dom).activeElement
|
|
49
|
+
} catch (e) {
|
|
50
|
+
return null
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//create
|
|
54
|
+
function createNodes(parent, vnodes, start, end, hooks, nextSibling, ns) {
|
|
55
|
+
for (var i = start; i < end; i++) {
|
|
56
|
+
var vnode = vnodes[i]
|
|
57
|
+
if (vnode != null) {
|
|
58
|
+
createNode(parent, vnode, hooks, ns, nextSibling)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function createNode(parent, vnode, hooks, ns, nextSibling) {
|
|
63
|
+
var tag = vnode.tag
|
|
64
|
+
if (typeof tag === "string") {
|
|
65
|
+
vnode.state = {}
|
|
66
|
+
if (vnode.attrs != null) initLifecycle(vnode.attrs, vnode, hooks)
|
|
67
|
+
switch (tag) {
|
|
68
|
+
case "#": createText(parent, vnode, nextSibling); break
|
|
69
|
+
case "<": createHTML(parent, vnode, ns, nextSibling); break
|
|
70
|
+
case "[": createFragment(parent, vnode, hooks, ns, nextSibling); break
|
|
71
|
+
default: createElement(parent, vnode, hooks, ns, nextSibling)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else createComponent(parent, vnode, hooks, ns, nextSibling)
|
|
75
|
+
}
|
|
76
|
+
function createText(parent, vnode, nextSibling) {
|
|
77
|
+
vnode.dom = getDocument(parent).createTextNode(vnode.children)
|
|
78
|
+
insertDOM(parent, vnode.dom, nextSibling)
|
|
79
|
+
}
|
|
80
|
+
var possibleParents = {caption: "table", thead: "table", tbody: "table", tfoot: "table", tr: "tbody", th: "tr", td: "tr", colgroup: "table", col: "colgroup"}
|
|
81
|
+
function createHTML(parent, vnode, ns, nextSibling) {
|
|
82
|
+
var match = vnode.children.match(/^\s*?<(\w+)/im) || []
|
|
83
|
+
// not using the proper parent makes the child element(s) vanish.
|
|
84
|
+
// var div = document.createElement("div")
|
|
85
|
+
// div.innerHTML = "<td>i</td><td>j</td>"
|
|
86
|
+
// console.log(div.innerHTML)
|
|
87
|
+
// --> "ij", no <td> in sight.
|
|
88
|
+
var temp = getDocument(parent).createElement(possibleParents[match[1]] || "div")
|
|
89
|
+
if (ns === "http://www.w3.org/2000/svg") {
|
|
90
|
+
temp.innerHTML = "<svg xmlns=\"http://www.w3.org/2000/svg\">" + vnode.children + "</svg>"
|
|
91
|
+
temp = temp.firstChild
|
|
92
|
+
} else {
|
|
93
|
+
temp.innerHTML = vnode.children
|
|
94
|
+
}
|
|
95
|
+
vnode.dom = temp.firstChild
|
|
96
|
+
vnode.domSize = temp.childNodes.length
|
|
97
|
+
var fragment = getDocument(parent).createDocumentFragment()
|
|
98
|
+
var child
|
|
99
|
+
while (child = temp.firstChild) {
|
|
100
|
+
fragment.appendChild(child)
|
|
101
|
+
}
|
|
102
|
+
insertDOM(parent, fragment, nextSibling)
|
|
103
|
+
}
|
|
104
|
+
function createFragment(parent, vnode, hooks, ns, nextSibling) {
|
|
105
|
+
var fragment = getDocument(parent).createDocumentFragment()
|
|
106
|
+
if (vnode.children != null) {
|
|
107
|
+
var children = vnode.children
|
|
108
|
+
createNodes(fragment, children, 0, children.length, hooks, null, ns)
|
|
109
|
+
}
|
|
110
|
+
vnode.dom = fragment.firstChild
|
|
111
|
+
vnode.domSize = fragment.childNodes.length
|
|
112
|
+
insertDOM(parent, fragment, nextSibling)
|
|
113
|
+
}
|
|
114
|
+
function createElement(parent, vnode, hooks, ns, nextSibling) {
|
|
115
|
+
var tag = vnode.tag
|
|
116
|
+
var attrs = vnode.attrs
|
|
117
|
+
var is = vnode.is
|
|
118
|
+
|
|
119
|
+
ns = getNameSpace(vnode) || ns
|
|
120
|
+
|
|
121
|
+
var element = ns ?
|
|
122
|
+
is ? getDocument(parent).createElementNS(ns, tag, {is: is}) : getDocument(parent).createElementNS(ns, tag) :
|
|
123
|
+
is ? getDocument(parent).createElement(tag, {is: is}) : getDocument(parent).createElement(tag)
|
|
124
|
+
vnode.dom = element
|
|
125
|
+
|
|
126
|
+
if (attrs != null) {
|
|
127
|
+
setAttrs(vnode, attrs, ns)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
insertDOM(parent, element, nextSibling)
|
|
131
|
+
|
|
132
|
+
if (!maybeSetContentEditable(vnode)) {
|
|
133
|
+
if (vnode.children != null) {
|
|
134
|
+
var children = vnode.children
|
|
135
|
+
createNodes(element, children, 0, children.length, hooks, null, ns)
|
|
136
|
+
if (vnode.tag === "select" && attrs != null) setLateSelectAttrs(vnode, attrs)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// A component instance whose initialization failed is replaced by an inert one:
|
|
141
|
+
// it renders nothing and is never re-initialized (see #1311), but the rest of
|
|
142
|
+
// the tree keeps working and it can still be updated and removed safely.
|
|
143
|
+
function inertView() {}
|
|
144
|
+
function disableComponent(vnode) {
|
|
145
|
+
vnode.state = {view: inertView}
|
|
146
|
+
vnode.instance = undefined
|
|
147
|
+
}
|
|
148
|
+
// Errors thrown by a component are collected here so the render pass can finish
|
|
149
|
+
// and leave the vnode tree in sync with the DOM. They are rethrown at the end of
|
|
150
|
+
// `render()`.
|
|
151
|
+
function deferError(e) {
|
|
152
|
+
if (currentErrors == null) currentErrors = [e]
|
|
153
|
+
else currentErrors.push(e)
|
|
154
|
+
}
|
|
155
|
+
function initComponent(vnode, hooks) {
|
|
156
|
+
var sentinel
|
|
157
|
+
var isObject = typeof vnode.tag.view === "function"
|
|
158
|
+
if (isObject) {
|
|
159
|
+
vnode.state = Object.create(vnode.tag)
|
|
160
|
+
sentinel = vnode.state.view
|
|
161
|
+
if (sentinel.$$reentrantLock$$ != null) return
|
|
162
|
+
} else {
|
|
163
|
+
sentinel = vnode.tag
|
|
164
|
+
// There is no state to fall back to here, so make it inert instead of
|
|
165
|
+
// leaving `vnode.state` undefined.
|
|
166
|
+
if (sentinel.$$reentrantLock$$ != null) return disableComponent(vnode)
|
|
167
|
+
}
|
|
168
|
+
sentinel.$$reentrantLock$$ = true
|
|
169
|
+
try {
|
|
170
|
+
if (!isObject) {
|
|
171
|
+
vnode.state = (vnode.tag.prototype != null && typeof vnode.tag.prototype.view === "function") ? new vnode.tag(vnode) : vnode.tag(vnode)
|
|
172
|
+
}
|
|
173
|
+
initLifecycle(vnode.state, vnode, hooks)
|
|
174
|
+
if (vnode.attrs != null) initLifecycle(vnode.attrs, vnode, hooks)
|
|
175
|
+
vnode.instance = Vnode.normalize(callHook.call(vnode.state.view, vnode))
|
|
176
|
+
if (vnode.instance === vnode) throw Error("A view cannot return the vnode it received as argument")
|
|
177
|
+
} finally {
|
|
178
|
+
sentinel.$$reentrantLock$$ = null
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function createComponent(parent, vnode, hooks, ns, nextSibling) {
|
|
182
|
+
var hooksLength = hooks.length
|
|
183
|
+
try {
|
|
184
|
+
initComponent(vnode, hooks)
|
|
185
|
+
} catch (e) {
|
|
186
|
+
// Drop the `oncreate` hooks this component queued before it failed.
|
|
187
|
+
hooks.length = hooksLength
|
|
188
|
+
disableComponent(vnode)
|
|
189
|
+
deferError(e)
|
|
190
|
+
}
|
|
191
|
+
if (vnode.instance != null) {
|
|
192
|
+
createNode(parent, vnode.instance, hooks, ns, nextSibling)
|
|
193
|
+
vnode.dom = vnode.instance.dom
|
|
194
|
+
vnode.domSize = vnode.instance.domSize
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
vnode.domSize = 0
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
//update
|
|
202
|
+
/**
|
|
203
|
+
* @param {Element|Fragment} parent - the parent element
|
|
204
|
+
* @param {Vnode[] | null} old - the list of vnodes of the last `render()` call for
|
|
205
|
+
* this part of the tree
|
|
206
|
+
* @param {Vnode[] | null} vnodes - as above, but for the current `render()` call.
|
|
207
|
+
* @param {Function[]} hooks - an accumulator of post-render hooks (oncreate/onupdate)
|
|
208
|
+
* @param {Element | null} nextSibling - the next DOM node if we're dealing with a
|
|
209
|
+
* fragment that is not the last item in its
|
|
210
|
+
* parent
|
|
211
|
+
* @param {'svg' | 'math' | String | null} ns) - the current XML namespace, if any
|
|
212
|
+
* @returns void
|
|
213
|
+
*/
|
|
214
|
+
// This function diffs and patches lists of vnodes, both keyed and unkeyed.
|
|
215
|
+
//
|
|
216
|
+
// We will:
|
|
217
|
+
//
|
|
218
|
+
// 1. describe its general structure
|
|
219
|
+
// 2. focus on the diff algorithm optimizations
|
|
220
|
+
// 3. discuss DOM node operations.
|
|
221
|
+
|
|
222
|
+
// ## Overview:
|
|
223
|
+
//
|
|
224
|
+
// The updateNodes() function:
|
|
225
|
+
// - deals with trivial cases
|
|
226
|
+
// - determines whether the lists are keyed or unkeyed based on the first non-null node
|
|
227
|
+
// of each list.
|
|
228
|
+
// - diffs them and patches the DOM if needed (that's the brunt of the code)
|
|
229
|
+
// - manages the leftovers: after diffing, are there:
|
|
230
|
+
// - old nodes left to remove?
|
|
231
|
+
// - new nodes to insert?
|
|
232
|
+
// deal with them!
|
|
233
|
+
//
|
|
234
|
+
// The lists are only iterated over once, with an exception for the nodes in `old` that
|
|
235
|
+
// are visited in the fourth part of the diff and in the `removeNodes` loop.
|
|
236
|
+
|
|
237
|
+
// ## Diffing
|
|
238
|
+
//
|
|
239
|
+
// Reading https://github.com/localvoid/ivi/blob/ddc09d06abaef45248e6133f7040d00d3c6be853/packages/ivi/src/vdom/implementation.ts#L617-L837
|
|
240
|
+
// may be good for context on longest increasing subsequence-based logic for moving nodes.
|
|
241
|
+
//
|
|
242
|
+
// In order to diff keyed lists, one has to
|
|
243
|
+
//
|
|
244
|
+
// 1) match nodes in both lists, per key, and update them accordingly
|
|
245
|
+
// 2) create the nodes present in the new list, but absent in the old one
|
|
246
|
+
// 3) remove the nodes present in the old list, but absent in the new one
|
|
247
|
+
// 4) figure out what nodes in 1) to move in order to minimize the DOM operations.
|
|
248
|
+
//
|
|
249
|
+
// To achieve 1) one can create a dictionary of keys => index (for the old list), then iterate
|
|
250
|
+
// over the new list and for each new vnode, find the corresponding vnode in the old list using
|
|
251
|
+
// the map.
|
|
252
|
+
// 2) is achieved in the same step: if a new node has no corresponding entry in the map, it is new
|
|
253
|
+
// and must be created.
|
|
254
|
+
// For the removals, we actually remove the nodes that have been updated from the old list.
|
|
255
|
+
// The nodes that remain in that list after 1) and 2) have been performed can be safely removed.
|
|
256
|
+
// The fourth step is a bit more complex and relies on the longest increasing subsequence (LIS)
|
|
257
|
+
// algorithm.
|
|
258
|
+
//
|
|
259
|
+
// the longest increasing subsequence is the list of nodes that can remain in place. Imagine going
|
|
260
|
+
// from `1,2,3,4,5` to `4,5,1,2,3` where the numbers are not necessarily the keys, but the indices
|
|
261
|
+
// corresponding to the keyed nodes in the old list (keyed nodes `e,d,c,b,a` => `b,a,e,d,c` would
|
|
262
|
+
// match the above lists, for example).
|
|
263
|
+
//
|
|
264
|
+
// In there are two increasing subsequences: `4,5` and `1,2,3`, the latter being the longest. We
|
|
265
|
+
// can update those nodes without moving them, and only call `insertNode` on `4` and `5`.
|
|
266
|
+
//
|
|
267
|
+
// @localvoid adapted the algo to also support node deletions and insertions (the `lis` is actually
|
|
268
|
+
// the longest increasing subsequence *of old nodes still present in the new list*).
|
|
269
|
+
//
|
|
270
|
+
// It is a general algorithm that is fireproof in all circumstances, but it requires the allocation
|
|
271
|
+
// and the construction of a `key => oldIndex` map, and three arrays (one with `newIndex => oldIndex`,
|
|
272
|
+
// the `LIS` and a temporary one to create the LIS).
|
|
273
|
+
//
|
|
274
|
+
// So we cheat where we can: if the tails of the lists are identical, they are guaranteed to be part of
|
|
275
|
+
// the LIS and can be updated without moving them.
|
|
276
|
+
//
|
|
277
|
+
// If two nodes are swapped, they are guaranteed not to be part of the LIS, and must be moved (with
|
|
278
|
+
// the exception of the last node if the list is fully reversed).
|
|
279
|
+
//
|
|
280
|
+
// ## Finding the next sibling.
|
|
281
|
+
//
|
|
282
|
+
// `updateNode()` and `createNode()` expect a nextSibling parameter to perform DOM operations.
|
|
283
|
+
// When the list is being traversed top-down, at any index, the DOM nodes up to the previous
|
|
284
|
+
// vnode reflect the content of the new list, whereas the rest of the DOM nodes reflect the old
|
|
285
|
+
// list. The next sibling must be looked for in the old list using `getNextSibling(... oldStart + 1 ...)`.
|
|
286
|
+
//
|
|
287
|
+
// In the other scenarios (swaps, upwards traversal, map-based diff),
|
|
288
|
+
// the new vnodes list is traversed upwards. The DOM nodes at the bottom of the list reflect the
|
|
289
|
+
// bottom part of the new vnodes list, and we can use the `v.dom` value of the previous node
|
|
290
|
+
// as the next sibling (cached in the `nextSibling` variable).
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
// ## DOM node moves
|
|
294
|
+
//
|
|
295
|
+
// In most scenarios `updateNode()` and `createNode()` perform the DOM operations. However,
|
|
296
|
+
// this is not the case if the node moved (second and fourth part of the diff algo). We move
|
|
297
|
+
// the old DOM nodes before updateNode runs because it enables us to use the cached `nextSibling`
|
|
298
|
+
// variable rather than fetching it using `getNextSibling()`.
|
|
299
|
+
|
|
300
|
+
function updateNodes(parent, old, vnodes, hooks, nextSibling, ns) {
|
|
301
|
+
if (old === vnodes || old == null && vnodes == null) return
|
|
302
|
+
else if (old == null || old.length === 0) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, ns)
|
|
303
|
+
else if (vnodes == null || vnodes.length === 0) removeNodes(parent, old, 0, old.length)
|
|
304
|
+
else {
|
|
305
|
+
var isOldKeyed = old[0] != null && old[0].key != null
|
|
306
|
+
var isKeyed = vnodes[0] != null && vnodes[0].key != null
|
|
307
|
+
var start = 0, oldStart = 0, o, v
|
|
308
|
+
if (isOldKeyed !== isKeyed) {
|
|
309
|
+
removeNodes(parent, old, 0, old.length)
|
|
310
|
+
createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, ns)
|
|
311
|
+
} else if (!isKeyed) {
|
|
312
|
+
// Don't index past the end of either list (causes deopts).
|
|
313
|
+
var commonLength = old.length < vnodes.length ? old.length : vnodes.length
|
|
314
|
+
// Rewind if necessary to the first non-null index on either side.
|
|
315
|
+
// We could alternatively either explicitly create or remove nodes when `start !== oldStart`
|
|
316
|
+
// but that would be optimizing for sparse lists which are more rare than dense ones.
|
|
317
|
+
while (oldStart < old.length && old[oldStart] == null) oldStart++
|
|
318
|
+
while (start < vnodes.length && vnodes[start] == null) start++
|
|
319
|
+
start = start < oldStart ? start : oldStart
|
|
320
|
+
for (; start < commonLength; start++) {
|
|
321
|
+
o = old[start]
|
|
322
|
+
v = vnodes[start]
|
|
323
|
+
if (o === v || o == null && v == null) continue
|
|
324
|
+
else if (o == null) createNode(parent, v, hooks, ns, getNextSibling(old, start + 1, old.length, nextSibling))
|
|
325
|
+
else if (v == null) removeNode(parent, o)
|
|
326
|
+
else updateNode(parent, o, v, hooks, getNextSibling(old, start + 1, old.length, nextSibling), ns)
|
|
327
|
+
}
|
|
328
|
+
if (old.length > commonLength) removeNodes(parent, old, start, old.length)
|
|
329
|
+
if (vnodes.length > commonLength) createNodes(parent, vnodes, start, vnodes.length, hooks, nextSibling, ns)
|
|
330
|
+
} else {
|
|
331
|
+
// keyed diff
|
|
332
|
+
var oldEnd = old.length - 1, end = vnodes.length - 1, oe, ve, topSibling
|
|
333
|
+
|
|
334
|
+
// bottom-up
|
|
335
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
336
|
+
oe = old[oldEnd]
|
|
337
|
+
ve = vnodes[end]
|
|
338
|
+
if (oe.key !== ve.key) break
|
|
339
|
+
if (oe !== ve) updateNode(parent, oe, ve, hooks, nextSibling, ns)
|
|
340
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
341
|
+
oldEnd--, end--
|
|
342
|
+
}
|
|
343
|
+
// top-down
|
|
344
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
345
|
+
o = old[oldStart]
|
|
346
|
+
v = vnodes[start]
|
|
347
|
+
if (o.key !== v.key) break
|
|
348
|
+
oldStart++, start++
|
|
349
|
+
if (o !== v) updateNode(parent, o, v, hooks, getNextSibling(old, oldStart, oldEnd + 1, nextSibling), ns)
|
|
350
|
+
}
|
|
351
|
+
// swaps and list reversals
|
|
352
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
353
|
+
if (start === end) break
|
|
354
|
+
if (o.key !== ve.key || oe.key !== v.key) break
|
|
355
|
+
topSibling = getNextSibling(old, oldStart, oldEnd, nextSibling)
|
|
356
|
+
moveDOM(parent, oe, topSibling)
|
|
357
|
+
if (oe !== v) updateNode(parent, oe, v, hooks, topSibling, ns)
|
|
358
|
+
if (++start <= --end) moveDOM(parent, o, nextSibling)
|
|
359
|
+
if (o !== ve) updateNode(parent, o, ve, hooks, nextSibling, ns)
|
|
360
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
361
|
+
oldStart++; oldEnd--
|
|
362
|
+
oe = old[oldEnd]
|
|
363
|
+
ve = vnodes[end]
|
|
364
|
+
o = old[oldStart]
|
|
365
|
+
v = vnodes[start]
|
|
366
|
+
}
|
|
367
|
+
// bottom up once again
|
|
368
|
+
while (oldEnd >= oldStart && end >= start) {
|
|
369
|
+
if (oe.key !== ve.key) break
|
|
370
|
+
if (oe !== ve) updateNode(parent, oe, ve, hooks, nextSibling, ns)
|
|
371
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
372
|
+
oldEnd--, end--
|
|
373
|
+
oe = old[oldEnd]
|
|
374
|
+
ve = vnodes[end]
|
|
375
|
+
}
|
|
376
|
+
if (start > end) removeNodes(parent, old, oldStart, oldEnd + 1)
|
|
377
|
+
else if (oldStart > oldEnd) createNodes(parent, vnodes, start, end + 1, hooks, nextSibling, ns)
|
|
378
|
+
else {
|
|
379
|
+
// inspired by ivi https://github.com/ivijs/ivi/ by Boris Kaul
|
|
380
|
+
var originalNextSibling = nextSibling, pos = 2147483647, matched = 0
|
|
381
|
+
var oldIndices = new Array(end - start + 1).fill(-1)
|
|
382
|
+
var map = Object.create(null)
|
|
383
|
+
for (var i = start; i <= end; i++) map[vnodes[i].key] = i
|
|
384
|
+
for (var i = oldEnd; i >= oldStart; i--) {
|
|
385
|
+
oe = old[i]
|
|
386
|
+
var newIndex = map[oe.key]
|
|
387
|
+
if (newIndex != null) {
|
|
388
|
+
pos = (newIndex < pos) ? newIndex : -1 // becomes -1 if nodes were re-ordered
|
|
389
|
+
oldIndices[newIndex-start] = i
|
|
390
|
+
ve = vnodes[newIndex]
|
|
391
|
+
if (oe !== ve) updateNode(parent, oe, ve, hooks, nextSibling, ns)
|
|
392
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
393
|
+
matched++
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
nextSibling = originalNextSibling
|
|
397
|
+
// Remove the old nodes that have no match in the new list. The old list
|
|
398
|
+
// is left untouched, so it is still usable if something throws mid-diff.
|
|
399
|
+
if (matched !== oldEnd - oldStart + 1) {
|
|
400
|
+
for (var i = oldStart; i <= oldEnd; i++) {
|
|
401
|
+
if (map[old[i].key] == null) removeNode(parent, old[i])
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (matched === 0) createNodes(parent, vnodes, start, end + 1, hooks, nextSibling, ns)
|
|
405
|
+
else {
|
|
406
|
+
if (pos === -1) {
|
|
407
|
+
// the indices of the indices of the items that are part of the
|
|
408
|
+
// longest increasing subsequence in the oldIndices list
|
|
409
|
+
var lisIndices = makeLisIndices(oldIndices)
|
|
410
|
+
var li = lisIndices.length - 1
|
|
411
|
+
for (var i = end; i >= start; i--) {
|
|
412
|
+
ve = vnodes[i]
|
|
413
|
+
if (oldIndices[i-start] === -1) createNode(parent, ve, hooks, ns, nextSibling)
|
|
414
|
+
else {
|
|
415
|
+
if (lisIndices[li] === i - start) li--
|
|
416
|
+
else moveDOM(parent, ve, nextSibling)
|
|
417
|
+
}
|
|
418
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
for (var i = end; i >= start; i--) {
|
|
422
|
+
ve = vnodes[i]
|
|
423
|
+
if (oldIndices[i-start] === -1) createNode(parent, ve, hooks, ns, nextSibling)
|
|
424
|
+
if (ve.dom != null) nextSibling = ve.dom
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
function updateNode(parent, old, vnode, hooks, nextSibling, ns) {
|
|
433
|
+
var oldTag = old.tag, tag = vnode.tag
|
|
434
|
+
if (oldTag === tag && old.is === vnode.is) {
|
|
435
|
+
vnode.state = old.state
|
|
436
|
+
vnode.events = old.events
|
|
437
|
+
if (typeof oldTag === "string") {
|
|
438
|
+
if (shouldNotUpdate(vnode, old)) return
|
|
439
|
+
if (vnode.attrs != null) {
|
|
440
|
+
updateLifecycle(vnode.attrs, vnode, hooks)
|
|
441
|
+
}
|
|
442
|
+
switch (oldTag) {
|
|
443
|
+
case "#": updateText(old, vnode); break
|
|
444
|
+
case "<": updateHTML(parent, old, vnode, ns, nextSibling); break
|
|
445
|
+
case "[": updateFragment(parent, old, vnode, hooks, nextSibling, ns); break
|
|
446
|
+
default: updateElement(old, vnode, hooks, ns)
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
else updateComponent(parent, old, vnode, hooks, nextSibling, ns)
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
removeNode(parent, old)
|
|
453
|
+
createNode(parent, vnode, hooks, ns, nextSibling)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function updateText(old, vnode) {
|
|
457
|
+
if (old.children.toString() !== vnode.children.toString()) {
|
|
458
|
+
old.dom.nodeValue = vnode.children
|
|
459
|
+
}
|
|
460
|
+
vnode.dom = old.dom
|
|
461
|
+
}
|
|
462
|
+
function updateHTML(parent, old, vnode, ns, nextSibling) {
|
|
463
|
+
if (old.children !== vnode.children) {
|
|
464
|
+
removeDOM(parent, old)
|
|
465
|
+
createHTML(parent, vnode, ns, nextSibling)
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
vnode.dom = old.dom
|
|
469
|
+
vnode.domSize = old.domSize
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function updateFragment(parent, old, vnode, hooks, nextSibling, ns) {
|
|
473
|
+
updateNodes(parent, old.children, vnode.children, hooks, nextSibling, ns)
|
|
474
|
+
var domSize = 0, children = vnode.children
|
|
475
|
+
vnode.dom = null
|
|
476
|
+
if (children != null) {
|
|
477
|
+
for (var i = 0; i < children.length; i++) {
|
|
478
|
+
var child = children[i]
|
|
479
|
+
if (child != null && child.dom != null) {
|
|
480
|
+
if (vnode.dom == null) vnode.dom = child.dom
|
|
481
|
+
domSize += child.domSize || 1
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
vnode.domSize = domSize
|
|
486
|
+
}
|
|
487
|
+
function updateElement(old, vnode, hooks, ns) {
|
|
488
|
+
var element = vnode.dom = old.dom
|
|
489
|
+
ns = getNameSpace(vnode) || ns
|
|
490
|
+
|
|
491
|
+
if (old.attrs != vnode.attrs || (vnode.attrs != null && !cachedAttrsIsStaticMap.get(vnode.attrs))) {
|
|
492
|
+
updateAttrs(vnode, old.attrs, vnode.attrs, ns)
|
|
493
|
+
}
|
|
494
|
+
if (!maybeSetContentEditable(vnode)) {
|
|
495
|
+
updateNodes(element, old.children, vnode.children, hooks, null, ns)
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
function updateComponent(parent, old, vnode, hooks, nextSibling, ns) {
|
|
499
|
+
if (vnode.state.view === inertView) {
|
|
500
|
+
vnode.domSize = 0
|
|
501
|
+
return
|
|
502
|
+
}
|
|
503
|
+
try {
|
|
504
|
+
if (shouldNotUpdate(vnode, old)) return
|
|
505
|
+
vnode.instance = Vnode.normalize(callHook.call(vnode.state.view, vnode))
|
|
506
|
+
if (vnode.instance === vnode) throw Error("A view cannot return the vnode it received as argument")
|
|
507
|
+
} catch (e) {
|
|
508
|
+
// Keep what the component rendered last time, so the tree stays in sync
|
|
509
|
+
// with the DOM, and retry on the next render.
|
|
510
|
+
reuseOld(vnode, old)
|
|
511
|
+
deferError(e)
|
|
512
|
+
return
|
|
513
|
+
}
|
|
514
|
+
updateLifecycle(vnode.state, vnode, hooks)
|
|
515
|
+
if (vnode.attrs != null) updateLifecycle(vnode.attrs, vnode, hooks)
|
|
516
|
+
if (vnode.instance != null) {
|
|
517
|
+
if (old.instance == null) createNode(parent, vnode.instance, hooks, ns, nextSibling)
|
|
518
|
+
else updateNode(parent, old.instance, vnode.instance, hooks, nextSibling, ns)
|
|
519
|
+
vnode.dom = vnode.instance.dom
|
|
520
|
+
vnode.domSize = vnode.instance.domSize
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
if (old.instance != null) removeNode(parent, old.instance)
|
|
524
|
+
vnode.domSize = 0
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
// Lifted from ivi https://github.com/ivijs/ivi/
|
|
528
|
+
// takes a list of unique numbers (-1 is special and can
|
|
529
|
+
// occur multiple times) and returns an array with the indices
|
|
530
|
+
// of the items that are part of the longest increasing
|
|
531
|
+
// subsequence
|
|
532
|
+
var lisTemp = []
|
|
533
|
+
function makeLisIndices(a) {
|
|
534
|
+
var result = [0]
|
|
535
|
+
var u = 0, v = 0, i = 0
|
|
536
|
+
var il = lisTemp.length = a.length
|
|
537
|
+
for (var i = 0; i < il; i++) lisTemp[i] = a[i]
|
|
538
|
+
for (var i = 0; i < il; ++i) {
|
|
539
|
+
if (a[i] === -1) continue
|
|
540
|
+
var j = result[result.length - 1]
|
|
541
|
+
if (a[j] < a[i]) {
|
|
542
|
+
lisTemp[i] = j
|
|
543
|
+
result.push(i)
|
|
544
|
+
continue
|
|
545
|
+
}
|
|
546
|
+
u = 0
|
|
547
|
+
v = result.length - 1
|
|
548
|
+
while (u < v) {
|
|
549
|
+
// Fast integer average without overflow.
|
|
550
|
+
// eslint-disable-next-line no-bitwise
|
|
551
|
+
var c = (u >>> 1) + (v >>> 1) + (u & v & 1)
|
|
552
|
+
if (a[result[c]] < a[i]) {
|
|
553
|
+
u = c + 1
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
v = c
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (a[i] < a[result[u]]) {
|
|
560
|
+
if (u > 0) lisTemp[i] = result[u - 1]
|
|
561
|
+
result[u] = i
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
u = result.length
|
|
565
|
+
v = result[u - 1]
|
|
566
|
+
while (u-- > 0) {
|
|
567
|
+
result[u] = v
|
|
568
|
+
v = lisTemp[v]
|
|
569
|
+
}
|
|
570
|
+
lisTemp.length = 0
|
|
571
|
+
return result
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function getNextSibling(vnodes, i, end, nextSibling) {
|
|
575
|
+
for (; i < end; i++) {
|
|
576
|
+
if (vnodes[i] != null && vnodes[i].dom != null) return vnodes[i].dom
|
|
577
|
+
}
|
|
578
|
+
return nextSibling
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// This handles fragments with zombie children (removed from vdom, but persisted in DOM through onbeforeremove)
|
|
582
|
+
function moveDOM(parent, vnode, nextSibling) {
|
|
583
|
+
if (vnode.dom != null) {
|
|
584
|
+
var target
|
|
585
|
+
if (vnode.domSize == null || vnode.domSize === 1) {
|
|
586
|
+
// don't allocate for the common case
|
|
587
|
+
target = vnode.dom
|
|
588
|
+
} else {
|
|
589
|
+
target = getDocument(parent).createDocumentFragment()
|
|
590
|
+
for (var dom of domFor(vnode)) target.appendChild(dom)
|
|
591
|
+
}
|
|
592
|
+
insertDOM(parent, target, nextSibling)
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function insertDOM(parent, dom, nextSibling) {
|
|
597
|
+
if (nextSibling != null) parent.insertBefore(dom, nextSibling)
|
|
598
|
+
else parent.appendChild(dom)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function maybeSetContentEditable(vnode) {
|
|
602
|
+
if (vnode.attrs == null || (
|
|
603
|
+
vnode.attrs.contenteditable == null && // attribute
|
|
604
|
+
vnode.attrs.contentEditable == null // property
|
|
605
|
+
)) return false
|
|
606
|
+
var children = vnode.children
|
|
607
|
+
if (children != null && children.length === 1 && children[0].tag === "<") {
|
|
608
|
+
var content = children[0].children
|
|
609
|
+
if (vnode.dom.innerHTML !== content) vnode.dom.innerHTML = content
|
|
610
|
+
}
|
|
611
|
+
else if (children != null && children.length !== 0) throw new Error("Child node of a contenteditable must be trusted.")
|
|
612
|
+
return true
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
//remove
|
|
616
|
+
function removeNodes(parent, vnodes, start, end) {
|
|
617
|
+
for (var i = start; i < end; i++) {
|
|
618
|
+
var vnode = vnodes[i]
|
|
619
|
+
if (vnode != null) removeNode(parent, vnode)
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
function tryBlockRemove(parent, vnode, source, counter) {
|
|
623
|
+
var original = vnode.state
|
|
624
|
+
var result = callHook.call(source.onbeforeremove, vnode)
|
|
625
|
+
if (result == null) return
|
|
626
|
+
|
|
627
|
+
var generation = currentRender
|
|
628
|
+
for (var dom of domFor(vnode)) delayedRemoval.set(dom, generation)
|
|
629
|
+
counter.v++
|
|
630
|
+
|
|
631
|
+
Promise.resolve(result).finally(function () {
|
|
632
|
+
checkState(vnode, original)
|
|
633
|
+
tryResumeRemove(parent, vnode, counter)
|
|
634
|
+
})
|
|
635
|
+
}
|
|
636
|
+
function tryResumeRemove(parent, vnode, counter) {
|
|
637
|
+
if (--counter.v === 0) {
|
|
638
|
+
onremove(vnode)
|
|
639
|
+
removeDOM(parent, vnode)
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function removeNode(parent, vnode) {
|
|
643
|
+
var counter = {v: 1}
|
|
644
|
+
if (typeof vnode.tag !== "string" && typeof vnode.state.onbeforeremove === "function") tryBlockRemove(parent, vnode, vnode.state, counter)
|
|
645
|
+
if (vnode.attrs && typeof vnode.attrs.onbeforeremove === "function") tryBlockRemove(parent, vnode, vnode.attrs, counter)
|
|
646
|
+
tryResumeRemove(parent, vnode, counter)
|
|
647
|
+
}
|
|
648
|
+
function removeDOM(parent, vnode) {
|
|
649
|
+
if (vnode.dom == null) return
|
|
650
|
+
if (vnode.domSize == null || vnode.domSize === 1) {
|
|
651
|
+
parent.removeChild(vnode.dom)
|
|
652
|
+
} else {
|
|
653
|
+
for (var dom of domFor(vnode)) parent.removeChild(dom)
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function onremove(vnode) {
|
|
658
|
+
if (typeof vnode.tag !== "string" && typeof vnode.state.onremove === "function") callHook.call(vnode.state.onremove, vnode)
|
|
659
|
+
if (vnode.attrs && typeof vnode.attrs.onremove === "function") callHook.call(vnode.attrs.onremove, vnode)
|
|
660
|
+
if (typeof vnode.tag !== "string") {
|
|
661
|
+
if (vnode.instance != null) onremove(vnode.instance)
|
|
662
|
+
} else {
|
|
663
|
+
if (vnode.events != null) vnode.events._ = null
|
|
664
|
+
var children = vnode.children
|
|
665
|
+
if (Array.isArray(children)) {
|
|
666
|
+
for (var i = 0; i < children.length; i++) {
|
|
667
|
+
var child = children[i]
|
|
668
|
+
if (child != null) onremove(child)
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
//attrs
|
|
675
|
+
function setAttrs(vnode, attrs, ns) {
|
|
676
|
+
for (var key in attrs) {
|
|
677
|
+
setAttr(vnode, key, null, attrs[key], ns)
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
function setAttr(vnode, key, old, value, ns) {
|
|
681
|
+
if (key === "key" || value == null || isLifecycleMethod(key) || (old === value && !isFormAttribute(vnode, key)) && typeof value !== "object") return
|
|
682
|
+
if (key[0] === "o" && key[1] === "n") return updateEvent(vnode, key, value)
|
|
683
|
+
if (key.slice(0, 6) === "xlink:") vnode.dom.setAttributeNS("http://www.w3.org/1999/xlink", key.slice(6), value)
|
|
684
|
+
else if (key === "style") updateStyle(vnode.dom, old, value)
|
|
685
|
+
else if (hasPropertyKey(vnode, key, ns)) {
|
|
686
|
+
if (key === "value") {
|
|
687
|
+
// Only do the coercion if we're actually going to check the value.
|
|
688
|
+
/* eslint-disable no-implicit-coercion */
|
|
689
|
+
//setting input[value] to same value by typing on focused element moves cursor to end in Chrome
|
|
690
|
+
//setting input[type=file][value] to same value causes an error to be generated if it's non-empty
|
|
691
|
+
//minlength/maxlength validation isn't performed on script-set values(#2256)
|
|
692
|
+
if ((vnode.tag === "input" || vnode.tag === "textarea") && vnode.dom.value === "" + value) return
|
|
693
|
+
//setting select[value] to same value while having select open blinks select dropdown in Chrome
|
|
694
|
+
if (vnode.tag === "select" && old !== null && vnode.dom.value === "" + value) return
|
|
695
|
+
//setting option[value] to same value while having select open blinks select dropdown in Chrome
|
|
696
|
+
if (vnode.tag === "option" && old !== null && vnode.dom.value === "" + value) return
|
|
697
|
+
//setting input[type=file][value] to different value is an error if it's non-empty
|
|
698
|
+
// Not ideal, but it at least works around the most common source of uncaught exceptions for now.
|
|
699
|
+
if (vnode.tag === "input" && vnode.attrs.type === "file" && "" + value !== "") { console.error("`value` is read-only on file inputs!"); return }
|
|
700
|
+
/* eslint-enable no-implicit-coercion */
|
|
701
|
+
}
|
|
702
|
+
// If you assign an input type that is not supported by IE 11 with an assignment expression, an error will occur.
|
|
703
|
+
if (vnode.tag === "input" && key === "type") vnode.dom.setAttribute(key, value)
|
|
704
|
+
else vnode.dom[key] = value
|
|
705
|
+
} else {
|
|
706
|
+
if (typeof value === "boolean") {
|
|
707
|
+
if (value) vnode.dom.setAttribute(key, "")
|
|
708
|
+
else vnode.dom.removeAttribute(key)
|
|
709
|
+
}
|
|
710
|
+
else vnode.dom.setAttribute(key === "className" ? "class" : key, value)
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
function removeAttr(vnode, key, old, ns) {
|
|
714
|
+
if (key === "key" || old == null || isLifecycleMethod(key)) return
|
|
715
|
+
if (key[0] === "o" && key[1] === "n") updateEvent(vnode, key, undefined)
|
|
716
|
+
else if (key === "style") updateStyle(vnode.dom, old, null)
|
|
717
|
+
else if (
|
|
718
|
+
hasPropertyKey(vnode, key, ns)
|
|
719
|
+
&& key !== "className"
|
|
720
|
+
&& key !== "title" // creates "null" as title
|
|
721
|
+
&& !(key === "value" && (
|
|
722
|
+
vnode.tag === "option"
|
|
723
|
+
|| vnode.tag === "select" && vnode.dom.selectedIndex === -1 && vnode.dom === activeElement(vnode.dom)
|
|
724
|
+
))
|
|
725
|
+
&& !(vnode.tag === "input" && key === "type")
|
|
726
|
+
) {
|
|
727
|
+
vnode.dom[key] = null
|
|
728
|
+
} else {
|
|
729
|
+
var nsLastIndex = key.indexOf(":")
|
|
730
|
+
if (nsLastIndex !== -1) key = key.slice(nsLastIndex + 1)
|
|
731
|
+
if (old !== false) vnode.dom.removeAttribute(key === "className" ? "class" : key)
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
function setLateSelectAttrs(vnode, attrs) {
|
|
735
|
+
if ("value" in attrs) {
|
|
736
|
+
if(attrs.value === null) {
|
|
737
|
+
if (vnode.dom.selectedIndex !== -1) vnode.dom.value = null
|
|
738
|
+
} else {
|
|
739
|
+
var normalized = "" + attrs.value // eslint-disable-line no-implicit-coercion
|
|
740
|
+
if (vnode.dom.value !== normalized || vnode.dom.selectedIndex === -1) {
|
|
741
|
+
vnode.dom.value = normalized
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
if ("selectedIndex" in attrs) setAttr(vnode, "selectedIndex", null, attrs.selectedIndex, undefined)
|
|
746
|
+
}
|
|
747
|
+
function updateAttrs(vnode, old, attrs, ns) {
|
|
748
|
+
// Some attributes may NOT be case-sensitive (e.g. data-***),
|
|
749
|
+
// so removal should be done first to prevent accidental removal for newly setting values.
|
|
750
|
+
var val
|
|
751
|
+
if (old != null) {
|
|
752
|
+
if (old === attrs && !cachedAttrsIsStaticMap.has(attrs)) {
|
|
753
|
+
console.warn("Don't reuse attrs object, use new object for every redraw, this will throw in next major")
|
|
754
|
+
}
|
|
755
|
+
for (var key in old) {
|
|
756
|
+
if (((val = old[key]) != null) && (attrs == null || attrs[key] == null)) {
|
|
757
|
+
removeAttr(vnode, key, val, ns)
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (attrs != null) {
|
|
762
|
+
for (var key in attrs) {
|
|
763
|
+
setAttr(vnode, key, old && old[key], attrs[key], ns)
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
function isFormAttribute(vnode, attr) {
|
|
768
|
+
return attr === "value" || attr === "checked" || attr === "selectedIndex" || attr === "selected" && (vnode.dom === activeElement(vnode.dom) || vnode.tag === "option" && vnode.dom.parentNode === activeElement(vnode.dom))
|
|
769
|
+
}
|
|
770
|
+
function isLifecycleMethod(attr) {
|
|
771
|
+
return attr === "oninit" || attr === "oncreate" || attr === "onupdate" || attr === "onremove" || attr === "onbeforeremove" || attr === "onbeforeupdate"
|
|
772
|
+
}
|
|
773
|
+
function hasPropertyKey(vnode, key, ns) {
|
|
774
|
+
// Filter out namespaced keys
|
|
775
|
+
return ns === undefined && (
|
|
776
|
+
// If it's a custom element, just keep it.
|
|
777
|
+
vnode.tag.indexOf("-") > -1 || vnode.is ||
|
|
778
|
+
// If it's a normal element, let's try to avoid a few browser bugs.
|
|
779
|
+
key !== "href" && key !== "list" && key !== "form" && key !== "width" && key !== "height"// && key !== "type"
|
|
780
|
+
// Defer the property check until *after* we check everything.
|
|
781
|
+
) && key in vnode.dom
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
//style
|
|
785
|
+
function updateStyle(element, old, style) {
|
|
786
|
+
if (old === style) {
|
|
787
|
+
// Styles are equivalent, do nothing.
|
|
788
|
+
} else if (style == null) {
|
|
789
|
+
// New style is missing, just clear it.
|
|
790
|
+
element.style = ""
|
|
791
|
+
} else if (typeof style !== "object") {
|
|
792
|
+
// New style is a string, let engine deal with patching.
|
|
793
|
+
element.style = style
|
|
794
|
+
} else if (old == null || typeof old !== "object") {
|
|
795
|
+
// `old` is missing or a string, `style` is an object.
|
|
796
|
+
element.style = ""
|
|
797
|
+
// Add new style properties
|
|
798
|
+
for (var key in style) {
|
|
799
|
+
var value = style[key]
|
|
800
|
+
if (value != null) {
|
|
801
|
+
if (key.includes("-")) element.style.setProperty(key, String(value))
|
|
802
|
+
else element.style[key] = String(value)
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
} else {
|
|
806
|
+
// Both old & new are (different) objects.
|
|
807
|
+
// Remove style properties that no longer exist
|
|
808
|
+
// Style properties may have two cases(dash-case and camelCase),
|
|
809
|
+
// so removal should be done first to prevent accidental removal for newly setting values.
|
|
810
|
+
for (var key in old) {
|
|
811
|
+
if (old[key] != null && style[key] == null) {
|
|
812
|
+
if (key.includes("-")) element.style.removeProperty(key)
|
|
813
|
+
else element.style[key] = ""
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
// Update style properties that have changed
|
|
817
|
+
for (var key in style) {
|
|
818
|
+
var value = style[key]
|
|
819
|
+
if (value != null && (value = String(value)) !== String(old[key])) {
|
|
820
|
+
if (key.includes("-")) element.style.setProperty(key, value)
|
|
821
|
+
else element.style[key] = value
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// Here's an explanation of how this works:
|
|
828
|
+
// 1. The event names are always (by design) prefixed by `on`.
|
|
829
|
+
// 2. The EventListener interface accepts either a function or an object
|
|
830
|
+
// with a `handleEvent` method.
|
|
831
|
+
// 3. The object does not inherit from `Object.prototype`, to avoid
|
|
832
|
+
// any potential interference with that (e.g. setters).
|
|
833
|
+
// 4. The event name is remapped to the handler before calling it.
|
|
834
|
+
// 5. In function-based event handlers, `ev.target === this`. We replicate
|
|
835
|
+
// that below.
|
|
836
|
+
// 6. In function-based event handlers, `return false` prevents the default
|
|
837
|
+
// action and stops event propagation. We replicate that below.
|
|
838
|
+
function EventDict() {
|
|
839
|
+
// Save this, so the current redraw is correctly tracked.
|
|
840
|
+
this._ = currentRedraw
|
|
841
|
+
}
|
|
842
|
+
EventDict.prototype = Object.create(null)
|
|
843
|
+
EventDict.prototype.handleEvent = function (ev) {
|
|
844
|
+
var handler = this["on" + ev.type]
|
|
845
|
+
var result
|
|
846
|
+
if (typeof handler === "function") result = handler.call(ev.currentTarget, ev)
|
|
847
|
+
else if (typeof handler.handleEvent === "function") handler.handleEvent(ev)
|
|
848
|
+
var self = this
|
|
849
|
+
if (self._ != null) {
|
|
850
|
+
if (ev.redraw !== false) (0, self._)()
|
|
851
|
+
if (result != null && typeof result.then === "function") {
|
|
852
|
+
Promise.resolve(result).then(function () {
|
|
853
|
+
if (self._ != null && ev.redraw !== false) (0, self._)()
|
|
854
|
+
})
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if (result === false) {
|
|
858
|
+
ev.preventDefault()
|
|
859
|
+
ev.stopPropagation()
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
//event
|
|
864
|
+
function updateEvent(vnode, key, value) {
|
|
865
|
+
if (vnode.events != null) {
|
|
866
|
+
vnode.events._ = currentRedraw
|
|
867
|
+
if (vnode.events[key] === value) return
|
|
868
|
+
if (value != null && (typeof value === "function" || typeof value === "object")) {
|
|
869
|
+
if (vnode.events[key] == null) vnode.dom.addEventListener(key.slice(2), vnode.events, false)
|
|
870
|
+
vnode.events[key] = value
|
|
871
|
+
} else {
|
|
872
|
+
if (vnode.events[key] != null) vnode.dom.removeEventListener(key.slice(2), vnode.events, false)
|
|
873
|
+
vnode.events[key] = undefined
|
|
874
|
+
}
|
|
875
|
+
} else if (value != null && (typeof value === "function" || typeof value === "object")) {
|
|
876
|
+
vnode.events = new EventDict()
|
|
877
|
+
vnode.dom.addEventListener(key.slice(2), vnode.events, false)
|
|
878
|
+
vnode.events[key] = value
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
//lifecycle
|
|
883
|
+
function initLifecycle(source, vnode, hooks) {
|
|
884
|
+
if (typeof source.oninit === "function") callHook.call(source.oninit, vnode)
|
|
885
|
+
if (typeof source.oncreate === "function") hooks.push(callHook.bind(source.oncreate, vnode))
|
|
886
|
+
}
|
|
887
|
+
function updateLifecycle(source, vnode, hooks) {
|
|
888
|
+
if (typeof source.onupdate === "function") hooks.push(callHook.bind(source.onupdate, vnode))
|
|
889
|
+
}
|
|
890
|
+
function shouldNotUpdate(vnode, old) {
|
|
891
|
+
do {
|
|
892
|
+
if (vnode.attrs != null && typeof vnode.attrs.onbeforeupdate === "function") {
|
|
893
|
+
var force = callHook.call(vnode.attrs.onbeforeupdate, vnode, old)
|
|
894
|
+
if (force !== undefined && !force) break
|
|
895
|
+
}
|
|
896
|
+
if (typeof vnode.tag !== "string" && typeof vnode.state.onbeforeupdate === "function") {
|
|
897
|
+
var force = callHook.call(vnode.state.onbeforeupdate, vnode, old)
|
|
898
|
+
if (force !== undefined && !force) break
|
|
899
|
+
}
|
|
900
|
+
return false
|
|
901
|
+
} while (false); // eslint-disable-line no-constant-condition
|
|
902
|
+
reuseOld(vnode, old)
|
|
903
|
+
return true
|
|
904
|
+
}
|
|
905
|
+
function reuseOld(vnode, old) {
|
|
906
|
+
vnode.dom = old.dom
|
|
907
|
+
vnode.domSize = old.domSize
|
|
908
|
+
vnode.instance = old.instance
|
|
909
|
+
// One would think having the actual latest attributes would be ideal,
|
|
910
|
+
// but it doesn't let us properly diff based on our current internal
|
|
911
|
+
// representation. We have to save not only the old DOM info, but also
|
|
912
|
+
// the attributes used to create it, as we diff *that*, not against the
|
|
913
|
+
// DOM directly (with a few exceptions in `setAttr`). And, of course, we
|
|
914
|
+
// need to save the children and text as they are conceptually not
|
|
915
|
+
// unlike special "attributes" internally.
|
|
916
|
+
vnode.attrs = old.attrs
|
|
917
|
+
vnode.children = old.children
|
|
918
|
+
vnode.text = old.text
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
var currentDOM
|
|
922
|
+
|
|
923
|
+
return function(dom, vnodes, redraw) {
|
|
924
|
+
if (!dom) throw new TypeError("DOM element being rendered to does not exist.")
|
|
925
|
+
if (currentDOM != null && dom.contains(currentDOM)) {
|
|
926
|
+
throw new TypeError("Node is currently being rendered to and thus is locked.")
|
|
927
|
+
}
|
|
928
|
+
var prevRedraw = currentRedraw
|
|
929
|
+
var prevDOM = currentDOM
|
|
930
|
+
var prevErrors = currentErrors
|
|
931
|
+
var hooks = []
|
|
932
|
+
var active = activeElement(dom)
|
|
933
|
+
var namespace = dom.namespaceURI
|
|
934
|
+
var errors, completed = false
|
|
935
|
+
|
|
936
|
+
currentDOM = dom
|
|
937
|
+
currentRedraw = typeof redraw === "function" ? redraw : undefined
|
|
938
|
+
currentRender = {}
|
|
939
|
+
currentErrors = null
|
|
940
|
+
try {
|
|
941
|
+
// First time rendering into a node clears it out
|
|
942
|
+
if (dom.vnodes == null) dom.textContent = ""
|
|
943
|
+
vnodes = Vnode.normalizeChildren(Array.isArray(vnodes) ? vnodes : [vnodes])
|
|
944
|
+
updateNodes(dom, dom.vnodes, vnodes, hooks, null, namespace === "http://www.w3.org/1999/xhtml" ? undefined : namespace)
|
|
945
|
+
dom.vnodes = vnodes
|
|
946
|
+
// `document.activeElement` can return null: https://html.spec.whatwg.org/multipage/interaction.html#dom-document-activeelement
|
|
947
|
+
if (active != null && activeElement(dom) !== active && typeof active.focus === "function") active.focus()
|
|
948
|
+
for (var i = 0; i < hooks.length; i++) hooks[i]()
|
|
949
|
+
completed = true
|
|
950
|
+
} finally {
|
|
951
|
+
errors = currentErrors
|
|
952
|
+
currentErrors = prevErrors
|
|
953
|
+
currentRedraw = prevRedraw
|
|
954
|
+
currentDOM = prevDOM
|
|
955
|
+
// Only one error can be thrown. Report the others, so none is silently lost.
|
|
956
|
+
if (errors != null) {
|
|
957
|
+
for (var j = completed ? 1 : 0; j < errors.length; j++) console.error(errors[j])
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
// The tree is in sync with the DOM at this point, so it is safe to surface
|
|
961
|
+
// the first error a component threw during this pass.
|
|
962
|
+
if (errors != null) throw errors[0]
|
|
963
|
+
}
|
|
964
|
+
}
|