maquette 3.5.2 → 3.6.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,535 +1,545 @@
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
- domNode = vnode.domNode = doc.createTextNode(vnode.text);
242
- if (insertBefore !== undefined) {
243
- parentNode.insertBefore(domNode, insertBefore);
244
- }
245
- else {
246
- parentNode.appendChild(domNode);
247
- }
248
- }
249
- else {
250
- for (var i = 0; i <= vnodeSelector.length; ++i) {
251
- var c = vnodeSelector.charAt(i);
252
- if (i === vnodeSelector.length || c === "." || c === "#") {
253
- var type = vnodeSelector.charAt(start - 1);
254
- var found = vnodeSelector.slice(start, i);
255
- if (type === ".") {
256
- domNode.classList.add(found);
257
- }
258
- else if (type === "#") {
259
- domNode.id = found;
260
- }
261
- else {
262
- if (found === "svg") {
263
- projectionOptions = extend(projectionOptions, {
264
- namespace: NAMESPACE_SVG,
265
- });
266
- }
267
- if (projectionOptions.namespace !== undefined) {
268
- domNode = vnode.domNode = doc.createElementNS(projectionOptions.namespace, found);
269
- }
270
- else {
271
- domNode = vnode.domNode = vnode.domNode || doc.createElement(found);
272
- if (found === "input" && vnode.properties && vnode.properties.type !== undefined) {
273
- // IE8 and older don't support setting input type after the DOM Node has been added to the document
274
- domNode.setAttribute("type", vnode.properties.type);
275
- }
276
- }
277
- if (insertBefore !== undefined) {
278
- parentNode.insertBefore(domNode, insertBefore);
279
- }
280
- else if (domNode.parentNode !== parentNode) {
281
- parentNode.appendChild(domNode);
282
- }
283
- }
284
- start = i + 1;
285
- }
286
- }
287
- initPropertiesAndChildren(domNode, vnode, projectionOptions);
288
- }
289
- };
290
- var updateDom;
291
- /**
292
- * Adds or removes classes from an Element
293
- * @param domNode the element
294
- * @param classes a string separated list of classes
295
- * @param on true means add classes, false means remove
296
- */
297
- var toggleClasses = function (domNode, classes, on) {
298
- if (!classes) {
299
- return;
300
- }
301
- classes.split(" ").forEach(function (classToToggle) {
302
- if (classToToggle) {
303
- domNode.classList.toggle(classToToggle, on);
304
- }
305
- });
306
- };
307
- var updateProperties = function (domNode, previousProperties, properties, projectionOptions) {
308
- if (!properties) {
309
- return;
310
- }
311
- var propertiesUpdated = false;
312
- var propNames = Object.keys(properties);
313
- var propCount = propNames.length;
314
- for (var i = 0; i < propCount; i++) {
315
- var propName = propNames[i];
316
- // assuming that properties will be nullified instead of missing is by design
317
- var propValue = properties[propName];
318
- var previousValue = previousProperties[propName];
319
- if (propName === "class") {
320
- if (previousValue !== propValue) {
321
- toggleClasses(domNode, previousValue, false);
322
- toggleClasses(domNode, propValue, true);
323
- }
324
- }
325
- else if (propName === "classes") {
326
- var classList = domNode.classList;
327
- var classNames = Object.keys(propValue);
328
- var classNameCount = classNames.length;
329
- for (var j = 0; j < classNameCount; j++) {
330
- var className = classNames[j];
331
- var on = !!propValue[className];
332
- var previousOn = !!previousValue[className];
333
- if (on === previousOn) {
334
- continue;
335
- }
336
- propertiesUpdated = true;
337
- if (on) {
338
- classList.add(className);
339
- }
340
- else {
341
- classList.remove(className);
342
- }
343
- }
344
- }
345
- else if (propName === "styles") {
346
- var styleNames = Object.keys(propValue);
347
- var styleCount = styleNames.length;
348
- for (var j = 0; j < styleCount; j++) {
349
- var styleName = styleNames[j];
350
- var newStyleValue = propValue[styleName];
351
- var oldStyleValue = previousValue[styleName];
352
- if (newStyleValue === oldStyleValue) {
353
- continue;
354
- }
355
- propertiesUpdated = true;
356
- if (newStyleValue) {
357
- checkStyleValue(newStyleValue);
358
- projectionOptions.styleApplyer(domNode, styleName, newStyleValue);
359
- }
360
- else {
361
- projectionOptions.styleApplyer(domNode, styleName, "");
362
- }
363
- }
364
- }
365
- else {
366
- if (!propValue && typeof previousValue === "string") {
367
- propValue = "";
368
- }
369
- if (propName === "value") {
370
- // value can be manipulated by the user directly and using event.preventDefault() is not an option
371
- var domValue = domNode[propName];
372
- if (domValue !== propValue && // The 'value' in the DOM tree !== newValue
373
- (domNode["oninput-value"]
374
- ? domValue === domNode["oninput-value"] // If the last reported value to 'oninput' does not match domValue, do nothing and wait for oninput
375
- : propValue !== previousValue) // Only update the value if the vdom changed
376
- ) {
377
- // The edge cases are described in the tests
378
- domNode[propName] = propValue; // Reset the value, even if the virtual DOM did not change
379
- domNode["oninput-value"] = undefined;
380
- } // else do not update the domNode, otherwise the cursor position would be changed
381
- if (propValue !== previousValue) {
382
- propertiesUpdated = true;
383
- }
384
- }
385
- else if (propValue !== previousValue) {
386
- var type = typeof propValue;
387
- if (type !== "function" || !projectionOptions.eventHandlerInterceptor) {
388
- // Function updates are expected to be handled by the EventHandlerInterceptor
389
- if (projectionOptions.namespace === NAMESPACE_SVG) {
390
- if (propName === "href") {
391
- domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue);
392
- }
393
- else {
394
- // all SVG attributes are read-only in DOM, so...
395
- domNode.setAttribute(propName, propValue);
396
- }
397
- }
398
- else if (type === "string" && propName !== "innerHTML") {
399
- if (propName === "role" && propValue === "") {
400
- domNode.removeAttribute(propName);
401
- }
402
- else {
403
- domNode.setAttribute(propName, propValue);
404
- }
405
- }
406
- else if (domNode[propName] !== propValue) {
407
- // Comparison is here for side-effects in Edge with scrollLeft and scrollTop
408
- domNode[propName] = propValue;
409
- }
410
- propertiesUpdated = true;
411
- }
412
- }
413
- }
414
- }
415
- return propertiesUpdated;
416
- };
417
- var updateChildren = function (vnode, domNode, oldChildren, newChildren, projectionOptions) {
418
- if (oldChildren === newChildren) {
419
- return false;
420
- }
421
- oldChildren = oldChildren || emptyArray;
422
- newChildren = newChildren || emptyArray;
423
- var oldChildrenLength = oldChildren.length;
424
- var newChildrenLength = newChildren.length;
425
- var oldIndex = 0;
426
- var newIndex = 0;
427
- var i;
428
- var textUpdated = false;
429
- while (newIndex < newChildrenLength) {
430
- var oldChild = oldIndex < oldChildrenLength ? oldChildren[oldIndex] : undefined;
431
- var newChild = newChildren[newIndex];
432
- if (oldChild !== undefined && same(oldChild, newChild)) {
433
- textUpdated = updateDom(oldChild, newChild, projectionOptions) || textUpdated;
434
- oldIndex++;
435
- }
436
- else {
437
- var findOldIndex = findIndexOfChild(oldChildren, newChild, oldIndex + 1);
438
- if (findOldIndex >= 0) {
439
- // Remove preceding missing children
440
- for (i = oldIndex; i < findOldIndex; i++) {
441
- nodeToRemove(oldChildren[i]);
442
- checkDistinguishable(oldChildren, i, vnode, "removed");
443
- }
444
- textUpdated =
445
- updateDom(oldChildren[findOldIndex], newChild, projectionOptions) || textUpdated;
446
- oldIndex = findOldIndex + 1;
447
- }
448
- else {
449
- // New child
450
- createDom(newChild, domNode, oldIndex < oldChildrenLength ? oldChildren[oldIndex].domNode : undefined, projectionOptions);
451
- nodeAdded(newChild);
452
- checkDistinguishable(newChildren, newIndex, vnode, "added");
453
- }
454
- }
455
- newIndex++;
456
- }
457
- if (oldChildrenLength > oldIndex) {
458
- // Remove child fragments
459
- for (i = oldIndex; i < oldChildrenLength; i++) {
460
- nodeToRemove(oldChildren[i]);
461
- checkDistinguishable(oldChildren, i, vnode, "removed");
462
- }
463
- }
464
- return textUpdated;
465
- };
466
- updateDom = function (previous, vnode, projectionOptions) {
467
- var domNode = previous.domNode;
468
- var textUpdated = false;
469
- if (previous === vnode) {
470
- return false; // By contract, VNode objects may not be modified anymore after passing them to maquette
471
- }
472
- var updated = false;
473
- if (vnode.vnodeSelector === "") {
474
- if (vnode.text !== previous.text) {
475
- var newTextNode = domNode.ownerDocument.createTextNode(vnode.text);
476
- domNode.parentNode.replaceChild(newTextNode, domNode);
477
- vnode.domNode = newTextNode;
478
- textUpdated = true;
479
- return textUpdated;
480
- }
481
- vnode.domNode = domNode;
482
- }
483
- else {
484
- if (vnode.vnodeSelector.lastIndexOf("svg", 0) === 0) {
485
- // lastIndexOf(needle,0)===0 means StartsWith
486
- projectionOptions = extend(projectionOptions, {
487
- namespace: NAMESPACE_SVG,
488
- });
489
- }
490
- if (previous.text !== vnode.text) {
491
- updated = true;
492
- if (vnode.text === undefined) {
493
- domNode.removeChild(domNode.firstChild); // the only textnode presumably
494
- }
495
- else {
496
- domNode.textContent = vnode.text;
497
- }
498
- }
499
- vnode.domNode = domNode;
500
- updated =
501
- updateChildren(vnode, domNode, previous.children, vnode.children, projectionOptions) ||
502
- updated;
503
- updated =
504
- updateProperties(domNode, previous.properties, vnode.properties, projectionOptions) ||
505
- updated;
506
- if (vnode.properties && vnode.properties.afterUpdate) {
507
- vnode.properties.afterUpdate.apply(vnode.properties.bind || vnode.properties, [
508
- domNode,
509
- projectionOptions,
510
- vnode.vnodeSelector,
511
- vnode.properties,
512
- vnode.children,
513
- ]);
514
- }
515
- }
516
- if (updated && vnode.properties && vnode.properties.updateAnimation) {
517
- vnode.properties.updateAnimation(domNode, vnode.properties, previous.properties);
518
- }
519
- return textUpdated;
520
- };
521
- export var createProjection = function (vnode, projectionOptions) {
522
- return {
523
- getLastRender: function () { return vnode; },
524
- update: function (updatedVnode) {
525
- if (vnode.vnodeSelector !== updatedVnode.vnodeSelector) {
526
- 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)");
527
- }
528
- var previousVNode = vnode;
529
- vnode = updatedVnode;
530
- updateDom(previousVNode, updatedVnode, projectionOptions);
531
- },
532
- domNode: vnode.domNode,
533
- };
534
- };
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 _a;
237
+ var domNode;
238
+ var start = 0;
239
+ var vnodeSelector = vnode.vnodeSelector;
240
+ var doc = parentNode.ownerDocument;
241
+ if (vnodeSelector === "") {
242
+ if (vnode.domNode) {
243
+ vnode.domNode.nodeValue = vnode.text;
244
+ }
245
+ else {
246
+ domNode = vnode.domNode = doc.createTextNode(vnode.text);
247
+ if (insertBefore !== undefined) {
248
+ parentNode.insertBefore(domNode, insertBefore);
249
+ }
250
+ else {
251
+ parentNode.appendChild(domNode);
252
+ }
253
+ }
254
+ }
255
+ else {
256
+ for (var i = 0; i <= vnodeSelector.length; ++i) {
257
+ var c = vnodeSelector.charAt(i);
258
+ if (i === vnodeSelector.length || c === "." || c === "#") {
259
+ var type = vnodeSelector.charAt(start - 1);
260
+ var found = vnodeSelector.slice(start, i);
261
+ if (type === ".") {
262
+ domNode.classList.add(found);
263
+ }
264
+ else if (type === "#") {
265
+ domNode.id = found;
266
+ }
267
+ else {
268
+ if (found === "svg") {
269
+ projectionOptions = extend(projectionOptions, {
270
+ namespace: NAMESPACE_SVG,
271
+ });
272
+ }
273
+ if (projectionOptions.namespace !== undefined) {
274
+ domNode = vnode.domNode = doc.createElementNS(projectionOptions.namespace, found);
275
+ }
276
+ else {
277
+ domNode = vnode.domNode =
278
+ vnode.domNode ||
279
+ (((_a = vnode.properties) === null || _a === void 0 ? void 0 : _a.is)
280
+ ? doc.createElement(found, { is: vnode.properties.is })
281
+ : doc.createElement(found));
282
+ if (found === "input" && vnode.properties && vnode.properties.type !== undefined) {
283
+ // IE8 and older don't support setting input type after the DOM Node has been added to the document
284
+ domNode.setAttribute("type", vnode.properties.type);
285
+ }
286
+ }
287
+ if (insertBefore !== undefined) {
288
+ parentNode.insertBefore(domNode, insertBefore);
289
+ }
290
+ else if (domNode.parentNode !== parentNode) {
291
+ parentNode.appendChild(domNode);
292
+ }
293
+ }
294
+ start = i + 1;
295
+ }
296
+ }
297
+ initPropertiesAndChildren(domNode, vnode, projectionOptions);
298
+ }
299
+ };
300
+ var updateDom;
301
+ /**
302
+ * Adds or removes classes from an Element
303
+ * @param domNode the element
304
+ * @param classes a string separated list of classes
305
+ * @param on true means add classes, false means remove
306
+ */
307
+ var toggleClasses = function (domNode, classes, on) {
308
+ if (!classes) {
309
+ return;
310
+ }
311
+ classes.split(" ").forEach(function (classToToggle) {
312
+ if (classToToggle) {
313
+ domNode.classList.toggle(classToToggle, on);
314
+ }
315
+ });
316
+ };
317
+ var updateProperties = function (domNode, previousProperties, properties, projectionOptions) {
318
+ if (!properties) {
319
+ return;
320
+ }
321
+ var propertiesUpdated = false;
322
+ var propNames = Object.keys(properties);
323
+ var propCount = propNames.length;
324
+ for (var i = 0; i < propCount; i++) {
325
+ var propName = propNames[i];
326
+ // assuming that properties will be nullified instead of missing is by design
327
+ var propValue = properties[propName];
328
+ var previousValue = previousProperties[propName];
329
+ if (propName === "class") {
330
+ if (previousValue !== propValue) {
331
+ toggleClasses(domNode, previousValue, false);
332
+ toggleClasses(domNode, propValue, true);
333
+ }
334
+ }
335
+ else if (propName === "classes") {
336
+ var classList = domNode.classList;
337
+ var classNames = Object.keys(propValue);
338
+ var classNameCount = classNames.length;
339
+ for (var j = 0; j < classNameCount; j++) {
340
+ var className = classNames[j];
341
+ var on = !!propValue[className];
342
+ var previousOn = !!previousValue[className];
343
+ if (on === previousOn) {
344
+ continue;
345
+ }
346
+ propertiesUpdated = true;
347
+ if (on) {
348
+ classList.add(className);
349
+ }
350
+ else {
351
+ classList.remove(className);
352
+ }
353
+ }
354
+ }
355
+ else if (propName === "styles") {
356
+ var styleNames = Object.keys(propValue);
357
+ var styleCount = styleNames.length;
358
+ for (var j = 0; j < styleCount; j++) {
359
+ var styleName = styleNames[j];
360
+ var newStyleValue = propValue[styleName];
361
+ var oldStyleValue = previousValue[styleName];
362
+ if (newStyleValue === oldStyleValue) {
363
+ continue;
364
+ }
365
+ propertiesUpdated = true;
366
+ if (newStyleValue) {
367
+ checkStyleValue(newStyleValue);
368
+ projectionOptions.styleApplyer(domNode, styleName, newStyleValue);
369
+ }
370
+ else {
371
+ projectionOptions.styleApplyer(domNode, styleName, "");
372
+ }
373
+ }
374
+ }
375
+ else {
376
+ if (!propValue && typeof previousValue === "string") {
377
+ propValue = "";
378
+ }
379
+ if (propName === "value") {
380
+ // value can be manipulated by the user directly and using event.preventDefault() is not an option
381
+ var domValue = domNode[propName];
382
+ if (domValue !== propValue && // The 'value' in the DOM tree !== newValue
383
+ (domNode["oninput-value"]
384
+ ? domValue === domNode["oninput-value"] // If the last reported value to 'oninput' does not match domValue, do nothing and wait for oninput
385
+ : propValue !== previousValue) // Only update the value if the vdom changed
386
+ ) {
387
+ // The edge cases are described in the tests
388
+ domNode[propName] = propValue; // Reset the value, even if the virtual DOM did not change
389
+ domNode["oninput-value"] = undefined;
390
+ } // else do not update the domNode, otherwise the cursor position would be changed
391
+ if (propValue !== previousValue) {
392
+ propertiesUpdated = true;
393
+ }
394
+ }
395
+ else if (propValue !== previousValue) {
396
+ var type = typeof propValue;
397
+ if (type !== "function" || !projectionOptions.eventHandlerInterceptor) {
398
+ // Function updates are expected to be handled by the EventHandlerInterceptor
399
+ if (projectionOptions.namespace === NAMESPACE_SVG) {
400
+ if (propName === "href") {
401
+ domNode.setAttributeNS(NAMESPACE_XLINK, propName, propValue);
402
+ }
403
+ else {
404
+ // all SVG attributes are read-only in DOM, so...
405
+ domNode.setAttribute(propName, propValue);
406
+ }
407
+ }
408
+ else if (type === "string" && propName !== "innerHTML") {
409
+ if (propName === "role" && propValue === "") {
410
+ domNode.removeAttribute(propName);
411
+ }
412
+ else {
413
+ domNode.setAttribute(propName, propValue);
414
+ }
415
+ }
416
+ else if (domNode[propName] !== propValue) {
417
+ // Comparison is here for side-effects in Edge with scrollLeft and scrollTop
418
+ domNode[propName] = propValue;
419
+ }
420
+ propertiesUpdated = true;
421
+ }
422
+ }
423
+ }
424
+ }
425
+ return propertiesUpdated;
426
+ };
427
+ var updateChildren = function (vnode, domNode, oldChildren, newChildren, projectionOptions) {
428
+ if (oldChildren === newChildren) {
429
+ return false;
430
+ }
431
+ oldChildren = oldChildren || emptyArray;
432
+ newChildren = newChildren || emptyArray;
433
+ var oldChildrenLength = oldChildren.length;
434
+ var newChildrenLength = newChildren.length;
435
+ var oldIndex = 0;
436
+ var newIndex = 0;
437
+ var i;
438
+ var textUpdated = false;
439
+ while (newIndex < newChildrenLength) {
440
+ var oldChild = oldIndex < oldChildrenLength ? oldChildren[oldIndex] : undefined;
441
+ var newChild = newChildren[newIndex];
442
+ if (oldChild !== undefined && same(oldChild, newChild)) {
443
+ textUpdated = updateDom(oldChild, newChild, projectionOptions) || textUpdated;
444
+ oldIndex++;
445
+ }
446
+ else {
447
+ var findOldIndex = findIndexOfChild(oldChildren, newChild, oldIndex + 1);
448
+ if (findOldIndex >= 0) {
449
+ // Remove preceding missing children
450
+ for (i = oldIndex; i < findOldIndex; i++) {
451
+ nodeToRemove(oldChildren[i]);
452
+ checkDistinguishable(oldChildren, i, vnode, "removed");
453
+ }
454
+ textUpdated =
455
+ updateDom(oldChildren[findOldIndex], newChild, projectionOptions) || textUpdated;
456
+ oldIndex = findOldIndex + 1;
457
+ }
458
+ else {
459
+ // New child
460
+ createDom(newChild, domNode, oldIndex < oldChildrenLength ? oldChildren[oldIndex].domNode : undefined, projectionOptions);
461
+ nodeAdded(newChild);
462
+ checkDistinguishable(newChildren, newIndex, vnode, "added");
463
+ }
464
+ }
465
+ newIndex++;
466
+ }
467
+ if (oldChildrenLength > oldIndex) {
468
+ // Remove child fragments
469
+ for (i = oldIndex; i < oldChildrenLength; i++) {
470
+ nodeToRemove(oldChildren[i]);
471
+ checkDistinguishable(oldChildren, i, vnode, "removed");
472
+ }
473
+ }
474
+ return textUpdated;
475
+ };
476
+ updateDom = function (previous, vnode, projectionOptions) {
477
+ var domNode = previous.domNode;
478
+ var textUpdated = false;
479
+ if (previous === vnode) {
480
+ return false; // By contract, VNode objects may not be modified anymore after passing them to maquette
481
+ }
482
+ var updated = false;
483
+ if (vnode.vnodeSelector === "") {
484
+ if (vnode.text !== previous.text) {
485
+ var newTextNode = domNode.ownerDocument.createTextNode(vnode.text);
486
+ domNode.parentNode.replaceChild(newTextNode, domNode);
487
+ vnode.domNode = newTextNode;
488
+ textUpdated = true;
489
+ return textUpdated;
490
+ }
491
+ vnode.domNode = domNode;
492
+ }
493
+ else {
494
+ if (vnode.vnodeSelector.lastIndexOf("svg", 0) === 0) {
495
+ // lastIndexOf(needle,0)===0 means StartsWith
496
+ projectionOptions = extend(projectionOptions, {
497
+ namespace: NAMESPACE_SVG,
498
+ });
499
+ }
500
+ if (previous.text !== vnode.text) {
501
+ updated = true;
502
+ if (vnode.text === undefined) {
503
+ domNode.removeChild(domNode.firstChild); // the only textnode presumably
504
+ }
505
+ else {
506
+ domNode.textContent = vnode.text;
507
+ }
508
+ }
509
+ vnode.domNode = domNode;
510
+ updated =
511
+ updateChildren(vnode, domNode, previous.children, vnode.children, projectionOptions) ||
512
+ updated;
513
+ updated =
514
+ updateProperties(domNode, previous.properties, vnode.properties, projectionOptions) ||
515
+ updated;
516
+ if (vnode.properties && vnode.properties.afterUpdate) {
517
+ vnode.properties.afterUpdate.apply(vnode.properties.bind || vnode.properties, [
518
+ domNode,
519
+ projectionOptions,
520
+ vnode.vnodeSelector,
521
+ vnode.properties,
522
+ vnode.children,
523
+ ]);
524
+ }
525
+ }
526
+ if (updated && vnode.properties && vnode.properties.updateAnimation) {
527
+ vnode.properties.updateAnimation(domNode, vnode.properties, previous.properties);
528
+ }
529
+ return textUpdated;
530
+ };
531
+ export var createProjection = function (vnode, projectionOptions) {
532
+ return {
533
+ getLastRender: function () { return vnode; },
534
+ update: function (updatedVnode) {
535
+ if (vnode.vnodeSelector !== updatedVnode.vnodeSelector) {
536
+ 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)");
537
+ }
538
+ var previousVNode = vnode;
539
+ vnode = updatedVnode;
540
+ updateDom(previousVNode, updatedVnode, projectionOptions);
541
+ },
542
+ domNode: vnode.domNode,
543
+ };
544
+ };
535
545
  //# sourceMappingURL=projection.js.map