lively 0.22.0 → 0.24.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,697 +0,0 @@
1
- 'use strict';
2
-
3
- var range; // Create a range object for efficently rendering strings to elements.
4
- var NS_XHTML = 'http://www.w3.org/1999/xhtml';
5
-
6
- var doc = typeof document === 'undefined' ? undefined : document;
7
- var HAS_TEMPLATE_SUPPORT = !!doc && 'content' in doc.createElement('template');
8
- var HAS_RANGE_SUPPORT = !!doc && doc.createRange && 'createContextualFragment' in doc.createRange();
9
-
10
- function createFragmentFromTemplate(str) {
11
- var template = doc.createElement('template');
12
- template.innerHTML = str;
13
- return template.content.childNodes[0];
14
- }
15
-
16
- function createFragmentFromRange(str) {
17
- if (!range) {
18
- range = doc.createRange();
19
- range.selectNode(doc.body);
20
- }
21
-
22
- var fragment = range.createContextualFragment(str);
23
- return fragment.childNodes[0];
24
- }
25
-
26
- function createFragmentFromWrap(str) {
27
- var fragment = doc.createElement('body');
28
- fragment.innerHTML = str;
29
- return fragment.childNodes[0];
30
- }
31
-
32
- /**
33
- * This is about the same
34
- * var html = new DOMParser().parseFromString(str, 'text/html');
35
- * return html.body.firstChild;
36
- *
37
- * @method toElement
38
- * @param {String} str
39
- */
40
- function toElement(str) {
41
- str = str.trim();
42
- if (HAS_TEMPLATE_SUPPORT) {
43
- // avoid restrictions on content for things like `<tr><th>Hi</th></tr>` which
44
- // createContextualFragment doesn't support
45
- // <template> support not available in IE
46
- return createFragmentFromTemplate(str);
47
- } else if (HAS_RANGE_SUPPORT) {
48
- return createFragmentFromRange(str);
49
- }
50
-
51
- return createFragmentFromWrap(str);
52
- }
53
-
54
- /**
55
- * Returns true if two node's names are the same.
56
- *
57
- * NOTE: We don't bother checking `namespaceURI` because you will never find two HTML elements with the same
58
- * nodeName and different namespace URIs.
59
- *
60
- * @param {Element} a
61
- * @param {Element} b The target element
62
- * @return {boolean}
63
- */
64
- function compareNodeNames(fromEl, toEl) {
65
- var fromNodeName = fromEl.nodeName;
66
- var toNodeName = toEl.nodeName;
67
- var fromCodeStart, toCodeStart;
68
-
69
- if (fromNodeName === toNodeName) {
70
- return true;
71
- }
72
-
73
- fromCodeStart = fromNodeName.charCodeAt(0);
74
- toCodeStart = toNodeName.charCodeAt(0);
75
-
76
- // If the target element is a virtual DOM node or SVG node then we may
77
- // need to normalize the tag name before comparing. Normal HTML elements that are
78
- // in the "http://www.w3.org/1999/xhtml"
79
- // are converted to upper case
80
- if (fromCodeStart <= 90 && toCodeStart >= 97) { // from is upper and to is lower
81
- return fromNodeName === toNodeName.toUpperCase();
82
- } else if (toCodeStart <= 90 && fromCodeStart >= 97) { // to is upper and from is lower
83
- return toNodeName === fromNodeName.toUpperCase();
84
- } else {
85
- return false;
86
- }
87
- }
88
-
89
- /**
90
- * Create an element, optionally with a known namespace URI.
91
- *
92
- * @param {string} name the element name, e.g. 'div' or 'svg'
93
- * @param {string} [namespaceURI] the element's namespace URI, i.e. the value of
94
- * its `xmlns` attribute or its inferred namespace.
95
- *
96
- * @return {Element}
97
- */
98
- function createElementNS(name, namespaceURI) {
99
- return !namespaceURI || namespaceURI === NS_XHTML ?
100
- doc.createElement(name) :
101
- doc.createElementNS(namespaceURI, name);
102
- }
103
-
104
- /**
105
- * Copies the children of one DOM element to another DOM element
106
- */
107
- function moveChildren(fromEl, toEl) {
108
- var curChild = fromEl.firstChild;
109
- while (curChild) {
110
- var nextChild = curChild.nextSibling;
111
- toEl.appendChild(curChild);
112
- curChild = nextChild;
113
- }
114
- return toEl;
115
- }
116
-
117
- function syncBooleanAttrProp(fromEl, toEl, name) {
118
- if (fromEl[name] !== toEl[name]) {
119
- fromEl[name] = toEl[name];
120
- if (fromEl[name]) {
121
- fromEl.setAttribute(name, '');
122
- } else {
123
- fromEl.removeAttribute(name);
124
- }
125
- }
126
- }
127
-
128
- var specialElHandlers = {
129
- OPTION: function(fromEl, toEl) {
130
- var parentNode = fromEl.parentNode;
131
- if (parentNode) {
132
- var parentName = parentNode.nodeName.toUpperCase();
133
- if (parentName === 'OPTGROUP') {
134
- parentNode = parentNode.parentNode;
135
- parentName = parentNode && parentNode.nodeName.toUpperCase();
136
- }
137
- if (parentName === 'SELECT' && !parentNode.hasAttribute('multiple')) {
138
- if (fromEl.hasAttribute('selected') && !toEl.selected) {
139
- // Workaround for MS Edge bug where the 'selected' attribute can only be
140
- // removed if set to a non-empty value:
141
- // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12087679/
142
- fromEl.setAttribute('selected', 'selected');
143
- fromEl.removeAttribute('selected');
144
- }
145
- // We have to reset select element's selectedIndex to -1, otherwise setting
146
- // fromEl.selected using the syncBooleanAttrProp below has no effect.
147
- // The correct selectedIndex will be set in the SELECT special handler below.
148
- parentNode.selectedIndex = -1;
149
- }
150
- }
151
- syncBooleanAttrProp(fromEl, toEl, 'selected');
152
- },
153
- /**
154
- * The "value" attribute is special for the <input> element since it sets
155
- * the initial value. Changing the "value" attribute without changing the
156
- * "value" property will have no effect since it is only used to the set the
157
- * initial value. Similar for the "checked" attribute, and "disabled".
158
- */
159
- INPUT: function(fromEl, toEl) {
160
- syncBooleanAttrProp(fromEl, toEl, 'checked');
161
- syncBooleanAttrProp(fromEl, toEl, 'disabled');
162
-
163
- if (fromEl.value !== toEl.value) {
164
- fromEl.value = toEl.value;
165
- }
166
-
167
- if (!toEl.hasAttribute('value')) {
168
- fromEl.removeAttribute('value');
169
- }
170
- },
171
-
172
- TEXTAREA: function(fromEl, toEl) {
173
- var newValue = toEl.value;
174
- if (fromEl.value !== newValue) {
175
- fromEl.value = newValue;
176
- }
177
-
178
- var firstChild = fromEl.firstChild;
179
- if (firstChild) {
180
- // Needed for IE. Apparently IE sets the placeholder as the
181
- // node value and vise versa. This ignores an empty update.
182
- var oldValue = firstChild.nodeValue;
183
-
184
- if (oldValue == newValue || (!newValue && oldValue == fromEl.placeholder)) {
185
- return;
186
- }
187
-
188
- firstChild.nodeValue = newValue;
189
- }
190
- },
191
- SELECT: function(fromEl, toEl) {
192
- if (!toEl.hasAttribute('multiple')) {
193
- var selectedIndex = -1;
194
- var i = 0;
195
- // We have to loop through children of fromEl, not toEl since nodes can be moved
196
- // from toEl to fromEl directly when morphing.
197
- // At the time this special handler is invoked, all children have already been morphed
198
- // and appended to / removed from fromEl, so using fromEl here is safe and correct.
199
- var curChild = fromEl.firstChild;
200
- var optgroup;
201
- var nodeName;
202
- while(curChild) {
203
- nodeName = curChild.nodeName && curChild.nodeName.toUpperCase();
204
- if (nodeName === 'OPTGROUP') {
205
- optgroup = curChild;
206
- curChild = optgroup.firstChild;
207
- } else {
208
- if (nodeName === 'OPTION') {
209
- if (curChild.hasAttribute('selected')) {
210
- selectedIndex = i;
211
- break;
212
- }
213
- i++;
214
- }
215
- curChild = curChild.nextSibling;
216
- if (!curChild && optgroup) {
217
- curChild = optgroup.nextSibling;
218
- optgroup = null;
219
- }
220
- }
221
- }
222
-
223
- fromEl.selectedIndex = selectedIndex;
224
- }
225
- }
226
- };
227
-
228
- var ELEMENT_NODE = 1;
229
- var DOCUMENT_FRAGMENT_NODE = 11;
230
- var TEXT_NODE = 3;
231
- var COMMENT_NODE = 8;
232
-
233
- function noop() {}
234
-
235
- function defaultGetNodeKey(node) {
236
- if (node) {
237
- return (node.getAttribute && node.getAttribute('id')) || node.id;
238
- }
239
- }
240
-
241
- function morphdomFactory(morphAttrs) {
242
-
243
- return function morphdom(fromNode, toNode, options) {
244
- if (!options) {
245
- options = {};
246
- }
247
-
248
- if (typeof toNode === 'string') {
249
- if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') {
250
- var toNodeHtml = toNode;
251
- toNode = doc.createElement('html');
252
- toNode.innerHTML = toNodeHtml;
253
- } else {
254
- toNode = toElement(toNode);
255
- }
256
- } else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE) {
257
- toNode = toNode.firstElementChild;
258
- }
259
-
260
- var getNodeKey = options.getNodeKey || defaultGetNodeKey;
261
- var onBeforeNodeAdded = options.onBeforeNodeAdded || noop;
262
- var onNodeAdded = options.onNodeAdded || noop;
263
- var onBeforeElUpdated = options.onBeforeElUpdated || noop;
264
- var onElUpdated = options.onElUpdated || noop;
265
- var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop;
266
- var onNodeDiscarded = options.onNodeDiscarded || noop;
267
- var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop;
268
- var skipFromChildren = options.skipFromChildren || noop;
269
- var addChild = options.addChild || function(parent, child){ return parent.appendChild(child); };
270
- var childrenOnly = options.childrenOnly === true;
271
-
272
- // This object is used as a lookup to quickly find all keyed elements in the original DOM tree.
273
- var fromNodesLookup = Object.create(null);
274
- var keyedRemovalList = [];
275
-
276
- function addKeyedRemoval(key) {
277
- keyedRemovalList.push(key);
278
- }
279
-
280
- function walkDiscardedChildNodes(node, skipKeyedNodes) {
281
- if (node.nodeType === ELEMENT_NODE) {
282
- var curChild = node.firstChild;
283
- while (curChild) {
284
-
285
- var key = undefined;
286
-
287
- if (skipKeyedNodes && (key = getNodeKey(curChild))) {
288
- // If we are skipping keyed nodes then we add the key
289
- // to a list so that it can be handled at the very end.
290
- addKeyedRemoval(key);
291
- } else {
292
- // Only report the node as discarded if it is not keyed. We do this because
293
- // at the end we loop through all keyed elements that were unmatched
294
- // and then discard them in one final pass.
295
- onNodeDiscarded(curChild);
296
- if (curChild.firstChild) {
297
- walkDiscardedChildNodes(curChild, skipKeyedNodes);
298
- }
299
- }
300
-
301
- curChild = curChild.nextSibling;
302
- }
303
- }
304
- }
305
-
306
- /**
307
- * Removes a DOM node out of the original DOM
308
- *
309
- * @param {Node} node The node to remove
310
- * @param {Node} parentNode The nodes parent
311
- * @param {Boolean} skipKeyedNodes If true then elements with keys will be skipped and not discarded.
312
- * @return {undefined}
313
- */
314
- function removeNode(node, parentNode, skipKeyedNodes) {
315
- if (onBeforeNodeDiscarded(node) === false) {
316
- return;
317
- }
318
-
319
- if (parentNode) {
320
- parentNode.removeChild(node);
321
- }
322
-
323
- onNodeDiscarded(node);
324
- walkDiscardedChildNodes(node, skipKeyedNodes);
325
- }
326
-
327
- // // TreeWalker implementation is no faster, but keeping this around in case this changes in the future
328
- // function indexTree(root) {
329
- // var treeWalker = document.createTreeWalker(
330
- // root,
331
- // NodeFilter.SHOW_ELEMENT);
332
- //
333
- // var el;
334
- // while((el = treeWalker.nextNode())) {
335
- // var key = getNodeKey(el);
336
- // if (key) {
337
- // fromNodesLookup[key] = el;
338
- // }
339
- // }
340
- // }
341
-
342
- // // NodeIterator implementation is no faster, but keeping this around in case this changes in the future
343
- //
344
- // function indexTree(node) {
345
- // var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT);
346
- // var el;
347
- // while((el = nodeIterator.nextNode())) {
348
- // var key = getNodeKey(el);
349
- // if (key) {
350
- // fromNodesLookup[key] = el;
351
- // }
352
- // }
353
- // }
354
-
355
- function indexTree(node) {
356
- if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE) {
357
- var curChild = node.firstChild;
358
- while (curChild) {
359
- var key = getNodeKey(curChild);
360
- if (key) {
361
- fromNodesLookup[key] = curChild;
362
- }
363
-
364
- // Walk recursively
365
- indexTree(curChild);
366
-
367
- curChild = curChild.nextSibling;
368
- }
369
- }
370
- }
371
-
372
- indexTree(fromNode);
373
-
374
- function handleNodeAdded(el) {
375
- onNodeAdded(el);
376
-
377
- var curChild = el.firstChild;
378
- while (curChild) {
379
- var nextSibling = curChild.nextSibling;
380
-
381
- var key = getNodeKey(curChild);
382
- if (key) {
383
- var unmatchedFromEl = fromNodesLookup[key];
384
- // if we find a duplicate #id node in cache, replace `el` with cache value
385
- // and morph it to the child node.
386
- if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) {
387
- curChild.parentNode.replaceChild(unmatchedFromEl, curChild);
388
- morphEl(unmatchedFromEl, curChild);
389
- } else {
390
- handleNodeAdded(curChild);
391
- }
392
- } else {
393
- // recursively call for curChild and it's children to see if we find something in
394
- // fromNodesLookup
395
- handleNodeAdded(curChild);
396
- }
397
-
398
- curChild = nextSibling;
399
- }
400
- }
401
-
402
- function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) {
403
- // We have processed all of the "to nodes". If curFromNodeChild is
404
- // non-null then we still have some from nodes left over that need
405
- // to be removed
406
- while (curFromNodeChild) {
407
- var fromNextSibling = curFromNodeChild.nextSibling;
408
- if ((curFromNodeKey = getNodeKey(curFromNodeChild))) {
409
- // Since the node is keyed it might be matched up later so we defer
410
- // the actual removal to later
411
- addKeyedRemoval(curFromNodeKey);
412
- } else {
413
- // NOTE: we skip nested keyed nodes from being removed since there is
414
- // still a chance they will be matched up later
415
- removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
416
- }
417
- curFromNodeChild = fromNextSibling;
418
- }
419
- }
420
-
421
- function morphEl(fromEl, toEl, childrenOnly) {
422
- var toElKey = getNodeKey(toEl);
423
-
424
- if (toElKey) {
425
- // If an element with an ID is being morphed then it will be in the final
426
- // DOM so clear it out of the saved elements collection
427
- delete fromNodesLookup[toElKey];
428
- }
429
-
430
- if (!childrenOnly) {
431
- // optional
432
- if (onBeforeElUpdated(fromEl, toEl) === false) {
433
- return;
434
- }
435
-
436
- // update attributes on original DOM element first
437
- morphAttrs(fromEl, toEl);
438
- // optional
439
- onElUpdated(fromEl);
440
-
441
- if (onBeforeElChildrenUpdated(fromEl, toEl) === false) {
442
- return;
443
- }
444
- }
445
-
446
- if (fromEl.nodeName !== 'TEXTAREA') {
447
- morphChildren(fromEl, toEl);
448
- } else {
449
- specialElHandlers.TEXTAREA(fromEl, toEl);
450
- }
451
- }
452
-
453
- function morphChildren(fromEl, toEl) {
454
- var skipFrom = skipFromChildren(fromEl, toEl);
455
- var curToNodeChild = toEl.firstChild;
456
- var curFromNodeChild = fromEl.firstChild;
457
- var curToNodeKey;
458
- var curFromNodeKey;
459
-
460
- var fromNextSibling;
461
- var toNextSibling;
462
- var matchingFromEl;
463
-
464
- // walk the children
465
- outer: while (curToNodeChild) {
466
- toNextSibling = curToNodeChild.nextSibling;
467
- curToNodeKey = getNodeKey(curToNodeChild);
468
-
469
- // walk the fromNode children all the way through
470
- while (!skipFrom && curFromNodeChild) {
471
- fromNextSibling = curFromNodeChild.nextSibling;
472
-
473
- if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) {
474
- curToNodeChild = toNextSibling;
475
- curFromNodeChild = fromNextSibling;
476
- continue outer;
477
- }
478
-
479
- curFromNodeKey = getNodeKey(curFromNodeChild);
480
-
481
- var curFromNodeType = curFromNodeChild.nodeType;
482
-
483
- // this means if the curFromNodeChild doesnt have a match with the curToNodeChild
484
- var isCompatible = undefined;
485
-
486
- if (curFromNodeType === curToNodeChild.nodeType) {
487
- if (curFromNodeType === ELEMENT_NODE) {
488
- // Both nodes being compared are Element nodes
489
-
490
- if (curToNodeKey) {
491
- // The target node has a key so we want to match it up with the correct element
492
- // in the original DOM tree
493
- if (curToNodeKey !== curFromNodeKey) {
494
- // The current element in the original DOM tree does not have a matching key so
495
- // let's check our lookup to see if there is a matching element in the original
496
- // DOM tree
497
- if ((matchingFromEl = fromNodesLookup[curToNodeKey])) {
498
- if (fromNextSibling === matchingFromEl) {
499
- // Special case for single element removals. To avoid removing the original
500
- // DOM node out of the tree (since that can break CSS transitions, etc.),
501
- // we will instead discard the current node and wait until the next
502
- // iteration to properly match up the keyed target element with its matching
503
- // element in the original tree
504
- isCompatible = false;
505
- } else {
506
- // We found a matching keyed element somewhere in the original DOM tree.
507
- // Let's move the original DOM node into the current position and morph
508
- // it.
509
-
510
- // NOTE: We use insertBefore instead of replaceChild because we want to go through
511
- // the `removeNode()` function for the node that is being discarded so that
512
- // all lifecycle hooks are correctly invoked
513
- fromEl.insertBefore(matchingFromEl, curFromNodeChild);
514
-
515
- // fromNextSibling = curFromNodeChild.nextSibling;
516
-
517
- if (curFromNodeKey) {
518
- // Since the node is keyed it might be matched up later so we defer
519
- // the actual removal to later
520
- addKeyedRemoval(curFromNodeKey);
521
- } else {
522
- // NOTE: we skip nested keyed nodes from being removed since there is
523
- // still a chance they will be matched up later
524
- removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
525
- }
526
-
527
- curFromNodeChild = matchingFromEl;
528
- curFromNodeKey = getNodeKey(curFromNodeChild);
529
- }
530
- } else {
531
- // The nodes are not compatible since the "to" node has a key and there
532
- // is no matching keyed node in the source tree
533
- isCompatible = false;
534
- }
535
- }
536
- } else if (curFromNodeKey) {
537
- // The original has a key
538
- isCompatible = false;
539
- }
540
-
541
- isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild);
542
- if (isCompatible) {
543
- // We found compatible DOM elements so transform
544
- // the current "from" node to match the current
545
- // target DOM node.
546
- // MORPH
547
- morphEl(curFromNodeChild, curToNodeChild);
548
- }
549
-
550
- } else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) {
551
- // Both nodes being compared are Text or Comment nodes
552
- isCompatible = true;
553
- // Simply update nodeValue on the original node to
554
- // change the text value
555
- if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) {
556
- curFromNodeChild.nodeValue = curToNodeChild.nodeValue;
557
- }
558
-
559
- }
560
- }
561
-
562
- if (isCompatible) {
563
- // Advance both the "to" child and the "from" child since we found a match
564
- // Nothing else to do as we already recursively called morphChildren above
565
- curToNodeChild = toNextSibling;
566
- curFromNodeChild = fromNextSibling;
567
- continue outer;
568
- }
569
-
570
- // No compatible match so remove the old node from the DOM and continue trying to find a
571
- // match in the original DOM. However, we only do this if the from node is not keyed
572
- // since it is possible that a keyed node might match up with a node somewhere else in the
573
- // target tree and we don't want to discard it just yet since it still might find a
574
- // home in the final DOM tree. After everything is done we will remove any keyed nodes
575
- // that didn't find a home
576
- if (curFromNodeKey) {
577
- // Since the node is keyed it might be matched up later so we defer
578
- // the actual removal to later
579
- addKeyedRemoval(curFromNodeKey);
580
- } else {
581
- // NOTE: we skip nested keyed nodes from being removed since there is
582
- // still a chance they will be matched up later
583
- removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
584
- }
585
-
586
- curFromNodeChild = fromNextSibling;
587
- } // END: while(curFromNodeChild) {}
588
-
589
- // If we got this far then we did not find a candidate match for
590
- // our "to node" and we exhausted all of the children "from"
591
- // nodes. Therefore, we will just append the current "to" node
592
- // to the end
593
- if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) {
594
- // MORPH
595
- if(!skipFrom){ addChild(fromEl, matchingFromEl); }
596
- morphEl(matchingFromEl, curToNodeChild);
597
- } else {
598
- var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild);
599
- if (onBeforeNodeAddedResult !== false) {
600
- if (onBeforeNodeAddedResult) {
601
- curToNodeChild = onBeforeNodeAddedResult;
602
- }
603
-
604
- if (curToNodeChild.actualize) {
605
- curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc);
606
- }
607
- addChild(fromEl, curToNodeChild);
608
- handleNodeAdded(curToNodeChild);
609
- }
610
- }
611
-
612
- curToNodeChild = toNextSibling;
613
- curFromNodeChild = fromNextSibling;
614
- }
615
-
616
- cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey);
617
-
618
- var specialElHandler = specialElHandlers[fromEl.nodeName];
619
- if (specialElHandler) {
620
- specialElHandler(fromEl, toEl);
621
- }
622
- } // END: morphChildren(...)
623
-
624
- var morphedNode = fromNode;
625
- var morphedNodeType = morphedNode.nodeType;
626
- var toNodeType = toNode.nodeType;
627
-
628
- if (!childrenOnly) {
629
- // Handle the case where we are given two DOM nodes that are not
630
- // compatible (e.g. <div> --> <span> or <div> --> TEXT)
631
- if (morphedNodeType === ELEMENT_NODE) {
632
- if (toNodeType === ELEMENT_NODE) {
633
- if (!compareNodeNames(fromNode, toNode)) {
634
- onNodeDiscarded(fromNode);
635
- morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI));
636
- }
637
- } else {
638
- // Going from an element node to a text node
639
- morphedNode = toNode;
640
- }
641
- } else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node
642
- if (toNodeType === morphedNodeType) {
643
- if (morphedNode.nodeValue !== toNode.nodeValue) {
644
- morphedNode.nodeValue = toNode.nodeValue;
645
- }
646
-
647
- return morphedNode;
648
- } else {
649
- // Text node to something else
650
- morphedNode = toNode;
651
- }
652
- }
653
- }
654
-
655
- if (morphedNode === toNode) {
656
- // The "to node" was not compatible with the "from node" so we had to
657
- // toss out the "from node" and use the "to node"
658
- onNodeDiscarded(fromNode);
659
- } else {
660
- if (toNode.isSameNode && toNode.isSameNode(morphedNode)) {
661
- return;
662
- }
663
-
664
- morphEl(morphedNode, toNode, childrenOnly);
665
-
666
- // We now need to loop over any keyed nodes that might need to be
667
- // removed. We only do the removal if we know that the keyed node
668
- // never found a match. When a keyed node is matched up we remove
669
- // it out of fromNodesLookup and we use fromNodesLookup to determine
670
- // if a keyed node has been matched up or not
671
- if (keyedRemovalList) {
672
- for (var i=0, len=keyedRemovalList.length; i<len; i++) {
673
- var elToRemove = fromNodesLookup[keyedRemovalList[i]];
674
- if (elToRemove) {
675
- removeNode(elToRemove, elToRemove.parentNode, false);
676
- }
677
- }
678
- }
679
- }
680
-
681
- if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) {
682
- if (morphedNode.actualize) {
683
- morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc);
684
- }
685
- // If we had to swap out the from node with a new node because the old
686
- // node was not compatible with the target node then we need to
687
- // replace the old DOM node in the original DOM tree. This is only
688
- // possible if the original DOM node was part of a DOM tree which
689
- // we know is the case if it has a parent node.
690
- fromNode.parentNode.replaceChild(morphedNode, fromNode);
691
- }
692
-
693
- return morphedNode;
694
- };
695
- }
696
-
697
- module.exports = morphdomFactory;