maquette 3.5.3 → 4.0.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.
@@ -1,540 +1,558 @@
1
- var NAMESPACE_W3 = "http://www.w3.org/";
2
- var NAMESPACE_SVG = "".concat(NAMESPACE_W3, "2000/svg");
3
- var NAMESPACE_XLINK = "".concat(NAMESPACE_W3, "1999/xlink");
4
- var emptyArray = [];
5
- export var extend = function (base, overrides) {
6
- var result = {};
7
- Object.keys(base).forEach(function (key) {
8
- result[key] = base[key];
9
- });
10
- if (overrides) {
11
- Object.keys(overrides).forEach(function (key) {
12
- result[key] = overrides[key];
13
- });
14
- }
15
- return result;
16
- };
17
- var same = function (vnode1, vnode2) {
18
- if (vnode1.vnodeSelector !== vnode2.vnodeSelector) {
19
- return false;
20
- }
21
- if (vnode1.properties && vnode2.properties) {
22
- if (vnode1.properties.key !== vnode2.properties.key) {
23
- return false;
24
- }
25
- return vnode1.properties.bind === vnode2.properties.bind;
26
- }
27
- return !vnode1.properties && !vnode2.properties;
28
- };
29
- var checkStyleValue = function (styleValue) {
30
- if (typeof styleValue !== "string") {
31
- throw new Error("Style values must be strings");
32
- }
33
- };
34
- var findIndexOfChild = function (children, sameAs, start) {
35
- if (sameAs.vnodeSelector !== "") {
36
- // Never scan for text-nodes
37
- for (var i = start; i < children.length; i++) {
38
- if (same(children[i], sameAs)) {
39
- return i;
40
- }
41
- }
42
- }
43
- return -1;
44
- };
45
- var checkDistinguishable = function (childNodes, indexToCheck, parentVNode, operation) {
46
- var childNode = childNodes[indexToCheck];
47
- if (childNode.vnodeSelector === "") {
48
- return; // Text nodes need not be distinguishable
49
- }
50
- var properties = childNode.properties;
51
- var key = properties
52
- ? properties.key === undefined
53
- ? properties.bind
54
- : properties.key
55
- : undefined;
56
- if (!key) {
57
- // A key is just assumed to be unique
58
- for (var i = 0; i < childNodes.length; i++) {
59
- if (i !== indexToCheck) {
60
- var node = childNodes[i];
61
- if (same(node, childNode)) {
62
- throw {
63
- error: new Error("".concat(parentVNode.vnodeSelector, " had a ").concat(childNode.vnodeSelector, " child ").concat(operation === "added" ? operation : "removed", ", but there is now more than one. You must add unique key properties to make them distinguishable.")),
64
- parentNode: parentVNode,
65
- childNode: childNode,
66
- };
67
- }
68
- }
69
- }
70
- }
71
- };
72
- var nodeAdded = function (vNode) {
73
- if (vNode.properties) {
74
- var enterAnimation = vNode.properties.enterAnimation;
75
- if (enterAnimation) {
76
- enterAnimation(vNode.domNode, vNode.properties);
77
- }
78
- }
79
- };
80
- var removedNodes = [];
81
- var requestedIdleCallback = false;
82
- var visitRemovedNode = function (node) {
83
- (node.children || []).forEach(visitRemovedNode);
84
- if (node.properties && node.properties.afterRemoved) {
85
- node.properties.afterRemoved.apply(node.properties.bind || node.properties, [
86
- node.domNode,
87
- ]);
88
- }
89
- };
90
- var processPendingNodeRemovals = function () {
91
- requestedIdleCallback = false;
92
- removedNodes.forEach(visitRemovedNode);
93
- removedNodes.length = 0;
94
- };
95
- var scheduleNodeRemoval = function (vNode) {
96
- removedNodes.push(vNode);
97
- if (!requestedIdleCallback) {
98
- requestedIdleCallback = true;
99
- if (typeof window !== "undefined" && "requestIdleCallback" in window) {
100
- window.requestIdleCallback(processPendingNodeRemovals, { timeout: 16 });
101
- }
102
- else {
103
- setTimeout(processPendingNodeRemovals, 16);
104
- }
105
- }
106
- };
107
- var nodeToRemove = function (vNode) {
108
- var domNode = vNode.domNode;
109
- if (vNode.properties) {
110
- var exitAnimation = vNode.properties.exitAnimation;
111
- if (exitAnimation) {
112
- domNode.style.pointerEvents = "none";
113
- var removeDomNode = function () {
114
- if (domNode.parentNode) {
115
- domNode.parentNode.removeChild(domNode);
116
- scheduleNodeRemoval(vNode);
117
- }
118
- };
119
- exitAnimation(domNode, removeDomNode, vNode.properties);
120
- return;
121
- }
122
- }
123
- if (domNode.parentNode) {
124
- domNode.parentNode.removeChild(domNode);
125
- scheduleNodeRemoval(vNode);
126
- }
127
- };
128
- var setProperties = function (domNode, properties, projectionOptions) {
129
- if (!properties) {
130
- return;
131
- }
132
- var eventHandlerInterceptor = projectionOptions.eventHandlerInterceptor;
133
- var propNames = Object.keys(properties);
134
- var propCount = propNames.length;
135
- var _loop_1 = function (i) {
136
- var propName = propNames[i];
137
- var propValue = properties[propName];
138
- if (propName === "className") {
139
- throw new Error('Property "className" is not supported, use "class".');
140
- }
141
- else if (propName === "class") {
142
- toggleClasses(domNode, propValue, true);
143
- }
144
- else if (propName === "classes") {
145
- // object with string keys and boolean values
146
- var classNames = Object.keys(propValue);
147
- var classNameCount = classNames.length;
148
- for (var j = 0; j < classNameCount; j++) {
149
- var className = classNames[j];
150
- if (propValue[className]) {
151
- domNode.classList.add(className);
152
- }
153
- }
154
- }
155
- else if (propName === "styles") {
156
- // object with string keys and string (!) values
157
- var styleNames = Object.keys(propValue);
158
- var styleCount = styleNames.length;
159
- for (var j = 0; j < styleCount; j++) {
160
- var styleName = styleNames[j];
161
- var styleValue = propValue[styleName];
162
- if (styleValue) {
163
- checkStyleValue(styleValue);
164
- projectionOptions.styleApplyer(domNode, styleName, styleValue);
165
- }
166
- }
167
- }
168
- else if (propName !== "key" && propValue !== null && propValue !== undefined) {
169
- var type = typeof propValue;
170
- if (type === "function") {
171
- if (propName.lastIndexOf("on", 0) === 0) {
172
- // lastIndexOf(,0)===0 -> startsWith
173
- if (eventHandlerInterceptor) {
174
- propValue = eventHandlerInterceptor(propName, propValue, domNode, properties); // intercept eventhandlers
175
- }
176
- if (propName === "oninput") {
177
- (function () {
178
- // record the evt.target.value, because IE and Edge sometimes do a requestAnimationFrame between changing value and running oninput
179
- var oldPropValue = propValue;
180
- propValue = function (evt) {
181
- oldPropValue.apply(this, [evt]);
182
- evt.target["oninput-value"] = evt.target.value; // may be HTMLTextAreaElement as well
183
- };
184
- })();
185
- }
186
- }
187
- domNode[propName] = propValue;
188
- }
189
- else if (projectionOptions.namespace === NAMESPACE_SVG) {
190
- if (propName === "href") {
191
- domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue);
192
- }
193
- else {
194
- // all SVG attributes are read-only in DOM, so...
195
- domNode.setAttribute(propName, propValue);
196
- }
197
- }
198
- else if (type === "string" && propName !== "value" && propName !== "innerHTML") {
199
- domNode.setAttribute(propName, propValue);
200
- }
201
- else {
202
- domNode[propName] = propValue;
203
- }
204
- }
205
- };
206
- for (var i = 0; i < propCount; i++) {
207
- _loop_1(i);
208
- }
209
- };
210
- var addChildren = function (domNode, children, projectionOptions) {
211
- if (!children) {
212
- return;
213
- }
214
- for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
215
- var child = children_1[_i];
216
- createDom(child, domNode, undefined, projectionOptions);
217
- }
218
- };
219
- export var initPropertiesAndChildren = function (domNode, vnode, projectionOptions) {
220
- addChildren(domNode, vnode.children, projectionOptions); // children before properties, needed for value property of <select>.
221
- if (vnode.text) {
222
- domNode.textContent = vnode.text;
223
- }
224
- setProperties(domNode, vnode.properties, projectionOptions);
225
- if (vnode.properties && vnode.properties.afterCreate) {
226
- vnode.properties.afterCreate.apply(vnode.properties.bind || vnode.properties, [
227
- domNode,
228
- projectionOptions,
229
- vnode.vnodeSelector,
230
- vnode.properties,
231
- vnode.children,
232
- ]);
233
- }
234
- };
235
- export var createDom = function (vnode, parentNode, insertBefore, projectionOptions) {
236
- var domNode;
237
- var start = 0;
238
- var vnodeSelector = vnode.vnodeSelector;
239
- var doc = parentNode.ownerDocument;
240
- if (vnodeSelector === "") {
241
- if (vnode.domNode) {
242
- vnode.domNode.nodeValue = vnode.text;
243
- }
244
- else {
245
- domNode = vnode.domNode = doc.createTextNode(vnode.text);
246
- if (insertBefore !== undefined) {
247
- parentNode.insertBefore(domNode, insertBefore);
248
- }
249
- else {
250
- parentNode.appendChild(domNode);
251
- }
252
- }
253
- }
254
- else {
255
- for (var i = 0; i <= vnodeSelector.length; ++i) {
256
- var c = vnodeSelector.charAt(i);
257
- if (i === vnodeSelector.length || c === "." || c === "#") {
258
- var type = vnodeSelector.charAt(start - 1);
259
- var found = vnodeSelector.slice(start, i);
260
- if (type === ".") {
261
- domNode.classList.add(found);
262
- }
263
- else if (type === "#") {
264
- domNode.id = found;
265
- }
266
- else {
267
- if (found === "svg") {
268
- projectionOptions = extend(projectionOptions, {
269
- namespace: NAMESPACE_SVG,
270
- });
271
- }
272
- if (projectionOptions.namespace !== undefined) {
273
- domNode = vnode.domNode = doc.createElementNS(projectionOptions.namespace, found);
274
- }
275
- else {
276
- domNode = vnode.domNode = vnode.domNode || doc.createElement(found);
277
- if (found === "input" && vnode.properties && vnode.properties.type !== undefined) {
278
- // IE8 and older don't support setting input type after the DOM Node has been added to the document
279
- domNode.setAttribute("type", vnode.properties.type);
280
- }
281
- }
282
- if (insertBefore !== undefined) {
283
- parentNode.insertBefore(domNode, insertBefore);
284
- }
285
- else if (domNode.parentNode !== parentNode) {
286
- parentNode.appendChild(domNode);
287
- }
288
- }
289
- start = i + 1;
290
- }
291
- }
292
- initPropertiesAndChildren(domNode, vnode, projectionOptions);
293
- }
294
- };
295
- var updateDom;
296
- /**
297
- * Adds or removes classes from an Element
298
- * @param domNode the element
299
- * @param classes a string separated list of classes
300
- * @param on true means add classes, false means remove
301
- */
302
- var toggleClasses = function (domNode, classes, on) {
303
- if (!classes) {
304
- return;
305
- }
306
- classes.split(" ").forEach(function (classToToggle) {
307
- if (classToToggle) {
308
- domNode.classList.toggle(classToToggle, on);
309
- }
310
- });
311
- };
312
- var updateProperties = function (domNode, previousProperties, properties, projectionOptions) {
313
- if (!properties) {
314
- return;
315
- }
316
- var propertiesUpdated = false;
317
- var propNames = Object.keys(properties);
318
- var propCount = propNames.length;
319
- for (var i = 0; i < propCount; i++) {
320
- var propName = propNames[i];
321
- // assuming that properties will be nullified instead of missing is by design
322
- var propValue = properties[propName];
323
- var previousValue = previousProperties[propName];
324
- if (propName === "class") {
325
- if (previousValue !== propValue) {
326
- toggleClasses(domNode, previousValue, false);
327
- toggleClasses(domNode, propValue, true);
328
- }
329
- }
330
- else if (propName === "classes") {
331
- var classList = domNode.classList;
332
- var classNames = Object.keys(propValue);
333
- var classNameCount = classNames.length;
334
- for (var j = 0; j < classNameCount; j++) {
335
- var className = classNames[j];
336
- var on = !!propValue[className];
337
- var previousOn = !!previousValue[className];
338
- if (on === previousOn) {
339
- continue;
340
- }
341
- propertiesUpdated = true;
342
- if (on) {
343
- classList.add(className);
344
- }
345
- else {
346
- classList.remove(className);
347
- }
348
- }
349
- }
350
- else if (propName === "styles") {
351
- var styleNames = Object.keys(propValue);
352
- var styleCount = styleNames.length;
353
- for (var j = 0; j < styleCount; j++) {
354
- var styleName = styleNames[j];
355
- var newStyleValue = propValue[styleName];
356
- var oldStyleValue = previousValue[styleName];
357
- if (newStyleValue === oldStyleValue) {
358
- continue;
359
- }
360
- propertiesUpdated = true;
361
- if (newStyleValue) {
362
- checkStyleValue(newStyleValue);
363
- projectionOptions.styleApplyer(domNode, styleName, newStyleValue);
364
- }
365
- else {
366
- projectionOptions.styleApplyer(domNode, styleName, "");
367
- }
368
- }
369
- }
370
- else {
371
- if (!propValue && typeof previousValue === "string") {
372
- propValue = "";
373
- }
374
- if (propName === "value") {
375
- // value can be manipulated by the user directly and using event.preventDefault() is not an option
376
- var domValue = domNode[propName];
377
- if (domValue !== propValue && // The 'value' in the DOM tree !== newValue
378
- (domNode["oninput-value"]
379
- ? domValue === domNode["oninput-value"] // If the last reported value to 'oninput' does not match domValue, do nothing and wait for oninput
380
- : propValue !== previousValue) // Only update the value if the vdom changed
381
- ) {
382
- // The edge cases are described in the tests
383
- domNode[propName] = propValue; // Reset the value, even if the virtual DOM did not change
384
- domNode["oninput-value"] = undefined;
385
- } // else do not update the domNode, otherwise the cursor position would be changed
386
- if (propValue !== previousValue) {
387
- propertiesUpdated = true;
388
- }
389
- }
390
- else if (propValue !== previousValue) {
391
- var type = typeof propValue;
392
- if (type !== "function" || !projectionOptions.eventHandlerInterceptor) {
393
- // Function updates are expected to be handled by the EventHandlerInterceptor
394
- if (projectionOptions.namespace === NAMESPACE_SVG) {
395
- if (propName === "href") {
396
- domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue);
397
- }
398
- else {
399
- // all SVG attributes are read-only in DOM, so...
400
- domNode.setAttribute(propName, propValue);
401
- }
402
- }
403
- else if (type === "string" && propName !== "innerHTML") {
404
- if (propName === "role" && propValue === "") {
405
- domNode.removeAttribute(propName);
406
- }
407
- else {
408
- domNode.setAttribute(propName, propValue);
409
- }
410
- }
411
- else if (domNode[propName] !== propValue) {
412
- // Comparison is here for side-effects in Edge with scrollLeft and scrollTop
413
- domNode[propName] = propValue;
414
- }
415
- propertiesUpdated = true;
416
- }
417
- }
418
- }
419
- }
420
- return propertiesUpdated;
421
- };
422
- var updateChildren = function (vnode, domNode, oldChildren, newChildren, projectionOptions) {
423
- if (oldChildren === newChildren) {
424
- return false;
425
- }
426
- oldChildren = oldChildren || emptyArray;
427
- newChildren = newChildren || emptyArray;
428
- var oldChildrenLength = oldChildren.length;
429
- var newChildrenLength = newChildren.length;
430
- var oldIndex = 0;
431
- var newIndex = 0;
432
- var i;
433
- var textUpdated = false;
434
- while (newIndex < newChildrenLength) {
435
- var oldChild = oldIndex < oldChildrenLength ? oldChildren[oldIndex] : undefined;
436
- var newChild = newChildren[newIndex];
437
- if (oldChild !== undefined && same(oldChild, newChild)) {
438
- textUpdated = updateDom(oldChild, newChild, projectionOptions) || textUpdated;
439
- oldIndex++;
440
- }
441
- else {
442
- var findOldIndex = findIndexOfChild(oldChildren, newChild, oldIndex + 1);
443
- if (findOldIndex >= 0) {
444
- // Remove preceding missing children
445
- for (i = oldIndex; i < findOldIndex; i++) {
446
- nodeToRemove(oldChildren[i]);
447
- checkDistinguishable(oldChildren, i, vnode, "removed");
448
- }
449
- textUpdated =
450
- updateDom(oldChildren[findOldIndex], newChild, projectionOptions) || textUpdated;
451
- oldIndex = findOldIndex + 1;
452
- }
453
- else {
454
- // New child
455
- createDom(newChild, domNode, oldIndex < oldChildrenLength ? oldChildren[oldIndex].domNode : undefined, projectionOptions);
456
- nodeAdded(newChild);
457
- checkDistinguishable(newChildren, newIndex, vnode, "added");
458
- }
459
- }
460
- newIndex++;
461
- }
462
- if (oldChildrenLength > oldIndex) {
463
- // Remove child fragments
464
- for (i = oldIndex; i < oldChildrenLength; i++) {
465
- nodeToRemove(oldChildren[i]);
466
- checkDistinguishable(oldChildren, i, vnode, "removed");
467
- }
468
- }
469
- return textUpdated;
470
- };
471
- updateDom = function (previous, vnode, projectionOptions) {
472
- var domNode = previous.domNode;
473
- var textUpdated = false;
474
- if (previous === vnode) {
475
- return false; // By contract, VNode objects may not be modified anymore after passing them to maquette
476
- }
477
- var updated = false;
478
- if (vnode.vnodeSelector === "") {
479
- if (vnode.text !== previous.text) {
480
- var newTextNode = domNode.ownerDocument.createTextNode(vnode.text);
481
- domNode.parentNode.replaceChild(newTextNode, domNode);
482
- vnode.domNode = newTextNode;
483
- textUpdated = true;
484
- return textUpdated;
485
- }
486
- vnode.domNode = domNode;
487
- }
488
- else {
489
- if (vnode.vnodeSelector.lastIndexOf("svg", 0) === 0) {
490
- // lastIndexOf(needle,0)===0 means StartsWith
491
- projectionOptions = extend(projectionOptions, {
492
- namespace: NAMESPACE_SVG,
493
- });
494
- }
495
- if (previous.text !== vnode.text) {
496
- updated = true;
497
- if (vnode.text === undefined) {
498
- domNode.removeChild(domNode.firstChild); // the only textnode presumably
499
- }
500
- else {
501
- domNode.textContent = vnode.text;
502
- }
503
- }
504
- vnode.domNode = domNode;
505
- updated =
506
- updateChildren(vnode, domNode, previous.children, vnode.children, projectionOptions) ||
507
- updated;
508
- updated =
509
- updateProperties(domNode, previous.properties, vnode.properties, projectionOptions) ||
510
- updated;
511
- if (vnode.properties && vnode.properties.afterUpdate) {
512
- vnode.properties.afterUpdate.apply(vnode.properties.bind || vnode.properties, [
513
- domNode,
514
- projectionOptions,
515
- vnode.vnodeSelector,
516
- vnode.properties,
517
- vnode.children,
518
- ]);
519
- }
520
- }
521
- if (updated && vnode.properties && vnode.properties.updateAnimation) {
522
- vnode.properties.updateAnimation(domNode, vnode.properties, previous.properties);
523
- }
524
- return textUpdated;
525
- };
526
- export var createProjection = function (vnode, projectionOptions) {
527
- return {
528
- getLastRender: function () { return vnode; },
529
- update: function (updatedVnode) {
530
- if (vnode.vnodeSelector !== updatedVnode.vnodeSelector) {
531
- throw new Error("The selector for the root VNode may not be changed. (consider using dom.merge and add one extra level to the virtual DOM)");
532
- }
533
- var previousVNode = vnode;
534
- vnode = updatedVnode;
535
- updateDom(previousVNode, updatedVnode, projectionOptions);
536
- },
537
- domNode: vnode.domNode,
538
- };
539
- };
1
+ var NAMESPACE_W3 = "http://www.w3.org/";
2
+ var NAMESPACE_SVG = "".concat(NAMESPACE_W3, "2000/svg");
3
+ var NAMESPACE_XLINK = "".concat(NAMESPACE_W3, "1999/xlink");
4
+ var emptyArray = [];
5
+ export var extend = function (base, overrides) {
6
+ var result = {};
7
+ Object.keys(base).forEach(function (key) {
8
+ result[key] = base[key];
9
+ });
10
+ if (overrides) {
11
+ Object.keys(overrides).forEach(function (key) {
12
+ result[key] = overrides[key];
13
+ });
14
+ }
15
+ return result;
16
+ };
17
+ var same = function (vnode1, vnode2) {
18
+ if (vnode1.vnodeSelector !== vnode2.vnodeSelector) {
19
+ return false;
20
+ }
21
+ if (vnode1.properties && vnode2.properties) {
22
+ if (vnode1.properties.key !== vnode2.properties.key) {
23
+ return false;
24
+ }
25
+ return vnode1.properties.bind === vnode2.properties.bind;
26
+ }
27
+ return !vnode1.properties && !vnode2.properties;
28
+ };
29
+ var checkStyleValue = function (styleValue) {
30
+ if (typeof styleValue !== "string") {
31
+ throw new Error("Style values must be strings");
32
+ }
33
+ };
34
+ var findIndexOfChild = function (children, sameAs, start) {
35
+ if (sameAs.vnodeSelector !== "") {
36
+ // Never scan for text-nodes
37
+ for (var i = start; i < children.length; i++) {
38
+ if (same(children[i], sameAs)) {
39
+ return i;
40
+ }
41
+ }
42
+ }
43
+ return -1;
44
+ };
45
+ var checkDistinguishable = function (childNodes, indexToCheck, parentVNode, operation) {
46
+ var childNode = childNodes[indexToCheck];
47
+ if (childNode.vnodeSelector === "") {
48
+ return; // Text nodes need not be distinguishable
49
+ }
50
+ var properties = childNode.properties;
51
+ var key = properties
52
+ ? properties.key === undefined
53
+ ? properties.bind
54
+ : properties.key
55
+ : undefined;
56
+ if (!key) {
57
+ // A key is just assumed to be unique
58
+ for (var i = 0; i < childNodes.length; i++) {
59
+ if (i !== indexToCheck) {
60
+ var node = childNodes[i];
61
+ if (same(node, childNode)) {
62
+ throw {
63
+ error: new Error("".concat(parentVNode.vnodeSelector, " had a ").concat(childNode.vnodeSelector, " child ").concat(operation === "added" ? operation : "removed", ", but there is now more than one. You must add unique key properties to make them distinguishable.")),
64
+ parentNode: parentVNode,
65
+ childNode: childNode,
66
+ };
67
+ }
68
+ }
69
+ }
70
+ }
71
+ };
72
+ var nodeAdded = function (vNode) {
73
+ if (vNode.properties) {
74
+ var enterAnimation = vNode.properties.enterAnimation;
75
+ if (enterAnimation) {
76
+ enterAnimation(vNode.domNode, vNode.properties);
77
+ }
78
+ }
79
+ };
80
+ var removedNodes = [];
81
+ var requestedIdleCallback = false;
82
+ var visitRemovedNode = function (node) {
83
+ (node.children || []).forEach(visitRemovedNode);
84
+ if (node.properties && node.properties.afterRemoved) {
85
+ node.properties.afterRemoved.apply(node.properties.bind || node.properties, [
86
+ node.domNode,
87
+ ]);
88
+ }
89
+ };
90
+ var processPendingNodeRemovals = function () {
91
+ requestedIdleCallback = false;
92
+ removedNodes.forEach(visitRemovedNode);
93
+ removedNodes.length = 0;
94
+ };
95
+ var scheduleNodeRemoval = function (vNode) {
96
+ removedNodes.push(vNode);
97
+ if (!requestedIdleCallback) {
98
+ requestedIdleCallback = true;
99
+ if (typeof window !== "undefined" && "requestIdleCallback" in window) {
100
+ window.requestIdleCallback(processPendingNodeRemovals, { timeout: 16 });
101
+ }
102
+ else {
103
+ setTimeout(processPendingNodeRemovals, 16);
104
+ }
105
+ }
106
+ };
107
+ var nodeToRemove = function (vNode) {
108
+ var domNode = vNode.domNode;
109
+ if (vNode.properties) {
110
+ var exitAnimation = vNode.properties.exitAnimation;
111
+ if (exitAnimation) {
112
+ domNode.style.pointerEvents = "none";
113
+ var removeDomNode = function () {
114
+ if (domNode.parentNode) {
115
+ domNode.parentNode.removeChild(domNode);
116
+ scheduleNodeRemoval(vNode);
117
+ }
118
+ };
119
+ exitAnimation(domNode, removeDomNode, vNode.properties);
120
+ return;
121
+ }
122
+ }
123
+ if (domNode.parentNode) {
124
+ domNode.parentNode.removeChild(domNode);
125
+ scheduleNodeRemoval(vNode);
126
+ }
127
+ };
128
+ var setProperties = function (domNode, properties, projectionOptions) {
129
+ if (!properties) {
130
+ return;
131
+ }
132
+ var eventHandlerInterceptor = projectionOptions.eventHandlerInterceptor;
133
+ var propNames = Object.keys(properties);
134
+ var propCount = propNames.length;
135
+ var _loop_1 = function (i) {
136
+ var propName = propNames[i];
137
+ var propValue = properties[propName];
138
+ if (propName === "className") {
139
+ throw new Error('Property "className" is not supported, use "class".');
140
+ }
141
+ else if (propName === "class") {
142
+ toggleClasses(domNode, propValue, true);
143
+ }
144
+ else if (propName === "classes") {
145
+ // object with string keys and boolean values
146
+ var classNames = Object.keys(propValue);
147
+ var classNameCount = classNames.length;
148
+ for (var j = 0; j < classNameCount; j++) {
149
+ var className = classNames[j];
150
+ if (propValue[className]) {
151
+ domNode.classList.add(className);
152
+ }
153
+ }
154
+ }
155
+ else if (propName === "styles") {
156
+ // object with string keys and string (!) values
157
+ var styleNames = Object.keys(propValue);
158
+ var styleCount = styleNames.length;
159
+ for (var j = 0; j < styleCount; j++) {
160
+ var styleName = styleNames[j];
161
+ var styleValue = propValue[styleName];
162
+ if (styleValue) {
163
+ checkStyleValue(styleValue);
164
+ projectionOptions.styleApplyer(domNode, styleName, styleValue);
165
+ }
166
+ }
167
+ }
168
+ else if (propName === "on" && propValue) {
169
+ // object with string keys and function values
170
+ for (var _i = 0, _a = Object.entries(properties.on); _i < _a.length; _i++) {
171
+ var _b = _a[_i], key = _b[0], handler = _b[1];
172
+ var listener = typeof handler === "function" ? handler : handler.listener;
173
+ if (eventHandlerInterceptor) {
174
+ listener = eventHandlerInterceptor(key, listener, domNode, properties);
175
+ }
176
+ if (listener) {
177
+ domNode.addEventListener(key, listener, typeof handler === "function" ? undefined : handler.options);
178
+ }
179
+ }
180
+ }
181
+ else if (propName !== "key" && propValue !== null && propValue !== undefined) {
182
+ var type = typeof propValue;
183
+ if (type === "function") {
184
+ if (propName.lastIndexOf("on", 0) === 0) {
185
+ // lastIndexOf(,0)===0 -> startsWith
186
+ if (eventHandlerInterceptor) {
187
+ propValue = eventHandlerInterceptor(propName, propValue, domNode, properties); // intercept eventhandlers
188
+ }
189
+ if (propName === "oninput") {
190
+ (function () {
191
+ // record the evt.target.value, because IE and Edge sometimes do a requestAnimationFrame between changing value and running oninput
192
+ var oldPropValue = propValue;
193
+ propValue = function (evt) {
194
+ oldPropValue.apply(this, [evt]);
195
+ evt.target["oninput-value"] = evt.target.value; // may be HTMLTextAreaElement as well
196
+ };
197
+ })();
198
+ }
199
+ }
200
+ domNode[propName] = propValue;
201
+ }
202
+ else if (projectionOptions.namespace === NAMESPACE_SVG) {
203
+ if (propName === "href") {
204
+ domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue);
205
+ }
206
+ else {
207
+ // all SVG attributes are read-only in DOM, so...
208
+ domNode.setAttribute(propName, propValue);
209
+ }
210
+ }
211
+ else if (type === "string" && propName !== "value" && propName !== "innerHTML") {
212
+ domNode.setAttribute(propName, propValue);
213
+ }
214
+ else {
215
+ domNode[propName] = propValue;
216
+ }
217
+ }
218
+ };
219
+ for (var i = 0; i < propCount; i++) {
220
+ _loop_1(i);
221
+ }
222
+ };
223
+ var addChildren = function (domNode, children, projectionOptions) {
224
+ if (!children) {
225
+ return;
226
+ }
227
+ for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
228
+ var child = children_1[_i];
229
+ createDom(child, domNode, undefined, projectionOptions);
230
+ }
231
+ };
232
+ export var initPropertiesAndChildren = function (domNode, vnode, projectionOptions) {
233
+ addChildren(domNode, vnode.children, projectionOptions); // children before properties, needed for value property of <select>.
234
+ if (vnode.text) {
235
+ domNode.textContent = vnode.text;
236
+ }
237
+ setProperties(domNode, vnode.properties, projectionOptions);
238
+ if (vnode.properties && vnode.properties.afterCreate) {
239
+ vnode.properties.afterCreate.apply(vnode.properties.bind || vnode.properties, [
240
+ domNode,
241
+ projectionOptions,
242
+ vnode.vnodeSelector,
243
+ vnode.properties,
244
+ vnode.children,
245
+ ]);
246
+ }
247
+ };
248
+ export var createDom = function (vnode, parentNode, insertBefore, projectionOptions) {
249
+ var _a;
250
+ var domNode;
251
+ var start = 0;
252
+ var vnodeSelector = vnode.vnodeSelector;
253
+ var doc = parentNode.ownerDocument;
254
+ if (vnodeSelector === "") {
255
+ if (vnode.domNode) {
256
+ vnode.domNode.nodeValue = vnode.text;
257
+ }
258
+ else {
259
+ domNode = vnode.domNode = doc.createTextNode(vnode.text);
260
+ if (insertBefore !== undefined) {
261
+ parentNode.insertBefore(domNode, insertBefore);
262
+ }
263
+ else {
264
+ parentNode.appendChild(domNode);
265
+ }
266
+ }
267
+ }
268
+ else {
269
+ for (var i = 0; i <= vnodeSelector.length; ++i) {
270
+ var c = vnodeSelector.charAt(i);
271
+ if (i === vnodeSelector.length || c === "." || c === "#") {
272
+ var type = vnodeSelector.charAt(start - 1);
273
+ var found = vnodeSelector.slice(start, i);
274
+ if (type === ".") {
275
+ domNode.classList.add(found);
276
+ }
277
+ else if (type === "#") {
278
+ domNode.id = found;
279
+ }
280
+ else {
281
+ if (found === "svg") {
282
+ projectionOptions = extend(projectionOptions, {
283
+ namespace: NAMESPACE_SVG,
284
+ });
285
+ }
286
+ if (projectionOptions.namespace !== undefined) {
287
+ domNode = vnode.domNode = doc.createElementNS(projectionOptions.namespace, found);
288
+ }
289
+ else {
290
+ domNode = vnode.domNode =
291
+ vnode.domNode ||
292
+ (((_a = vnode.properties) === null || _a === void 0 ? void 0 : _a.is)
293
+ ? doc.createElement(found, { is: vnode.properties.is })
294
+ : doc.createElement(found));
295
+ if (found === "input" && vnode.properties && vnode.properties.type !== undefined) {
296
+ // IE8 and older don't support setting input type after the DOM Node has been added to the document
297
+ domNode.setAttribute("type", vnode.properties.type);
298
+ }
299
+ }
300
+ if (insertBefore !== undefined) {
301
+ parentNode.insertBefore(domNode, insertBefore);
302
+ }
303
+ else if (domNode.parentNode !== parentNode) {
304
+ parentNode.appendChild(domNode);
305
+ }
306
+ }
307
+ start = i + 1;
308
+ }
309
+ }
310
+ initPropertiesAndChildren(domNode, vnode, projectionOptions);
311
+ }
312
+ };
313
+ var updateDom;
314
+ /**
315
+ * Adds or removes classes from an Element
316
+ * @param domNode the element
317
+ * @param classes a string separated list of classes
318
+ * @param on true means add classes, false means remove
319
+ */
320
+ var toggleClasses = function (domNode, classes, on) {
321
+ if (!classes) {
322
+ return;
323
+ }
324
+ classes.split(" ").forEach(function (classToToggle) {
325
+ if (classToToggle) {
326
+ domNode.classList.toggle(classToToggle, on);
327
+ }
328
+ });
329
+ };
330
+ var updateProperties = function (domNode, previousProperties, properties, projectionOptions) {
331
+ if (!properties) {
332
+ return;
333
+ }
334
+ var propertiesUpdated = false;
335
+ var propNames = Object.keys(properties);
336
+ var propCount = propNames.length;
337
+ for (var i = 0; i < propCount; i++) {
338
+ var propName = propNames[i];
339
+ // assuming that properties will be nullified instead of missing is by design
340
+ var propValue = properties[propName];
341
+ var previousValue = previousProperties[propName];
342
+ if (propName === "class") {
343
+ if (previousValue !== propValue) {
344
+ toggleClasses(domNode, previousValue, false);
345
+ toggleClasses(domNode, propValue, true);
346
+ }
347
+ }
348
+ else if (propName === "classes") {
349
+ var classList = domNode.classList;
350
+ var classNames = Object.keys(propValue);
351
+ var classNameCount = classNames.length;
352
+ for (var j = 0; j < classNameCount; j++) {
353
+ var className = classNames[j];
354
+ var on = !!propValue[className];
355
+ var previousOn = !!previousValue[className];
356
+ if (on === previousOn) {
357
+ continue;
358
+ }
359
+ propertiesUpdated = true;
360
+ if (on) {
361
+ classList.add(className);
362
+ }
363
+ else {
364
+ classList.remove(className);
365
+ }
366
+ }
367
+ }
368
+ else if (propName === "styles") {
369
+ var styleNames = Object.keys(propValue);
370
+ var styleCount = styleNames.length;
371
+ for (var j = 0; j < styleCount; j++) {
372
+ var styleName = styleNames[j];
373
+ var newStyleValue = propValue[styleName];
374
+ var oldStyleValue = previousValue[styleName];
375
+ if (newStyleValue === oldStyleValue) {
376
+ continue;
377
+ }
378
+ propertiesUpdated = true;
379
+ if (newStyleValue) {
380
+ checkStyleValue(newStyleValue);
381
+ projectionOptions.styleApplyer(domNode, styleName, newStyleValue);
382
+ }
383
+ else {
384
+ projectionOptions.styleApplyer(domNode, styleName, "");
385
+ }
386
+ }
387
+ }
388
+ else {
389
+ if (!propValue && typeof previousValue === "string") {
390
+ propValue = "";
391
+ }
392
+ if (propName === "value") {
393
+ // value can be manipulated by the user directly and using event.preventDefault() is not an option
394
+ var domValue = domNode[propName];
395
+ if (domValue !== propValue && // The 'value' in the DOM tree !== newValue
396
+ (domNode["oninput-value"]
397
+ ? domValue === domNode["oninput-value"] // If the last reported value to 'oninput' does not match domValue, do nothing and wait for oninput
398
+ : propValue !== previousValue) // Only update the value if the vdom changed
399
+ ) {
400
+ // The edge cases are described in the tests
401
+ domNode[propName] = propValue; // Reset the value, even if the virtual DOM did not change
402
+ domNode["oninput-value"] = undefined;
403
+ } // else do not update the domNode, otherwise the cursor position would be changed
404
+ if (propValue !== previousValue) {
405
+ propertiesUpdated = true;
406
+ }
407
+ }
408
+ else if (propValue !== previousValue) {
409
+ var type = typeof propValue;
410
+ if (type !== "function" || !projectionOptions.eventHandlerInterceptor) {
411
+ // Function updates are expected to be handled by the EventHandlerInterceptor
412
+ if (projectionOptions.namespace === NAMESPACE_SVG) {
413
+ if (propName === "href") {
414
+ domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue);
415
+ }
416
+ else {
417
+ // all SVG attributes are read-only in DOM, so...
418
+ domNode.setAttribute(propName, propValue);
419
+ }
420
+ }
421
+ else if (type === "string" && propName !== "innerHTML") {
422
+ if (propName === "role" && propValue === "") {
423
+ domNode.removeAttribute(propName);
424
+ }
425
+ else {
426
+ domNode.setAttribute(propName, propValue);
427
+ }
428
+ }
429
+ else if (domNode[propName] !== propValue) {
430
+ // Comparison is here for side-effects in Edge with scrollLeft and scrollTop
431
+ domNode[propName] = propValue;
432
+ }
433
+ propertiesUpdated = true;
434
+ }
435
+ }
436
+ }
437
+ }
438
+ return propertiesUpdated;
439
+ };
440
+ var updateChildren = function (vnode, domNode, oldChildren, newChildren, projectionOptions) {
441
+ if (oldChildren === newChildren) {
442
+ return false;
443
+ }
444
+ oldChildren = oldChildren || emptyArray;
445
+ newChildren = newChildren || emptyArray;
446
+ var oldChildrenLength = oldChildren.length;
447
+ var newChildrenLength = newChildren.length;
448
+ var oldIndex = 0;
449
+ var newIndex = 0;
450
+ var i;
451
+ var textUpdated = false;
452
+ while (newIndex < newChildrenLength) {
453
+ var oldChild = oldIndex < oldChildrenLength ? oldChildren[oldIndex] : undefined;
454
+ var newChild = newChildren[newIndex];
455
+ if (oldChild !== undefined && same(oldChild, newChild)) {
456
+ textUpdated = updateDom(oldChild, newChild, projectionOptions) || textUpdated;
457
+ oldIndex++;
458
+ }
459
+ else {
460
+ var findOldIndex = findIndexOfChild(oldChildren, newChild, oldIndex + 1);
461
+ if (findOldIndex >= 0) {
462
+ // Remove preceding missing children
463
+ for (i = oldIndex; i < findOldIndex; i++) {
464
+ nodeToRemove(oldChildren[i]);
465
+ checkDistinguishable(oldChildren, i, vnode, "removed");
466
+ }
467
+ textUpdated =
468
+ updateDom(oldChildren[findOldIndex], newChild, projectionOptions) || textUpdated;
469
+ oldIndex = findOldIndex + 1;
470
+ }
471
+ else {
472
+ // New child
473
+ createDom(newChild, domNode, oldIndex < oldChildrenLength ? oldChildren[oldIndex].domNode : undefined, projectionOptions);
474
+ nodeAdded(newChild);
475
+ checkDistinguishable(newChildren, newIndex, vnode, "added");
476
+ }
477
+ }
478
+ newIndex++;
479
+ }
480
+ if (oldChildrenLength > oldIndex) {
481
+ // Remove child fragments
482
+ for (i = oldIndex; i < oldChildrenLength; i++) {
483
+ nodeToRemove(oldChildren[i]);
484
+ checkDistinguishable(oldChildren, i, vnode, "removed");
485
+ }
486
+ }
487
+ return textUpdated;
488
+ };
489
+ updateDom = function (previous, vnode, projectionOptions) {
490
+ var domNode = previous.domNode;
491
+ var textUpdated = false;
492
+ if (previous === vnode) {
493
+ return false; // By contract, VNode objects may not be modified anymore after passing them to maquette
494
+ }
495
+ var updated = false;
496
+ if (vnode.vnodeSelector === "") {
497
+ if (vnode.text !== previous.text) {
498
+ var newTextNode = domNode.ownerDocument.createTextNode(vnode.text);
499
+ domNode.parentNode.replaceChild(newTextNode, domNode);
500
+ vnode.domNode = newTextNode;
501
+ textUpdated = true;
502
+ return textUpdated;
503
+ }
504
+ vnode.domNode = domNode;
505
+ }
506
+ else {
507
+ if (vnode.vnodeSelector.lastIndexOf("svg", 0) === 0) {
508
+ // lastIndexOf(needle,0)===0 means StartsWith
509
+ projectionOptions = extend(projectionOptions, {
510
+ namespace: NAMESPACE_SVG,
511
+ });
512
+ }
513
+ if (previous.text !== vnode.text) {
514
+ updated = true;
515
+ if (vnode.text === undefined) {
516
+ domNode.removeChild(domNode.firstChild); // the only textnode presumably
517
+ }
518
+ else {
519
+ domNode.textContent = vnode.text;
520
+ }
521
+ }
522
+ vnode.domNode = domNode;
523
+ updated =
524
+ updateChildren(vnode, domNode, previous.children, vnode.children, projectionOptions) ||
525
+ updated;
526
+ updated =
527
+ updateProperties(domNode, previous.properties, vnode.properties, projectionOptions) ||
528
+ updated;
529
+ if (vnode.properties && vnode.properties.afterUpdate) {
530
+ vnode.properties.afterUpdate.apply(vnode.properties.bind || vnode.properties, [
531
+ domNode,
532
+ projectionOptions,
533
+ vnode.vnodeSelector,
534
+ vnode.properties,
535
+ vnode.children,
536
+ ]);
537
+ }
538
+ }
539
+ if (updated && vnode.properties && vnode.properties.updateAnimation) {
540
+ vnode.properties.updateAnimation(domNode, vnode.properties, previous.properties);
541
+ }
542
+ return textUpdated;
543
+ };
544
+ export var createProjection = function (vnode, projectionOptions) {
545
+ return {
546
+ getLastRender: function () { return vnode; },
547
+ update: function (updatedVnode) {
548
+ if (vnode.vnodeSelector !== updatedVnode.vnodeSelector) {
549
+ throw new Error("The selector for the root VNode may not be changed. (consider using dom.merge and add one extra level to the virtual DOM)");
550
+ }
551
+ var previousVNode = vnode;
552
+ vnode = updatedVnode;
553
+ updateDom(previousVNode, updatedVnode, projectionOptions);
554
+ },
555
+ domNode: vnode.domNode,
556
+ };
557
+ };
540
558
  //# sourceMappingURL=projection.js.map