Comparing version
@@ -5,2 +5,11 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.morphdom = f()}})(function(){var define,module,exports;module={exports:(exports={})}; | ||
function empty(o) { | ||
for (var k in o) { | ||
if (o.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
function toElement(str) { | ||
@@ -150,2 +159,3 @@ if (!range) { | ||
var childrenOnly = options.childrenOnly === true; | ||
var movedEls = []; | ||
@@ -212,6 +222,7 @@ function removeNodeHelper(node, nestedInSavedEl) { | ||
function morphEl(fromEl, toEl, alreadyVisited, childrenOnly) { | ||
if (getNodeKey(toEl)) { | ||
var toElKey = getNodeKey(toEl); | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it is will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete savedEls[getNodeKey(toEl)]; | ||
delete savedEls[toElKey]; | ||
} | ||
@@ -322,2 +333,11 @@ | ||
if (curToNodeChild.nodeType === 1 && (curToNodeId || curToNodeChild.firstChild)) { | ||
// The element that was just added to the original DOM may have | ||
// some nested elements with a key/ID that needs to be matched up | ||
// with other elements. We'll add the element to a list so that we | ||
// can later process the nested elements if there are any unmatched | ||
// keyed elements that were discarded | ||
movedEls.push(curToNodeChild); | ||
} | ||
curToNodeChild = toNextSibling; | ||
@@ -377,2 +397,56 @@ curFromNodeChild = fromNextSibling; | ||
/** | ||
* What we will do here is walk the tree for the DOM element | ||
* that was moved from the target DOM tree to the original | ||
* DOM tree and we will look for keyed elements that could | ||
* be matched to keyed elements that were earlier discarded. | ||
* If we find a match then we will move the saved element | ||
* into the final DOM tree | ||
*/ | ||
var handleMovedEl = function(el) { | ||
var curChild = el.firstChild; | ||
while(curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var savedEl = savedEls[key]; | ||
if (savedEl && (curChild.tagName === savedEl.tagName)) { | ||
curChild.parentNode.replaceChild(savedEl, curChild); | ||
morphEl(savedEl, curChild, true /* already visited the saved el tree */); | ||
curChild = nextSibling; | ||
if (empty(savedEls)) { | ||
return false; | ||
} | ||
continue; | ||
} | ||
} | ||
if (curChild.nodeType === 1) { | ||
handleMovedEl(curChild); | ||
} | ||
curChild = nextSibling; | ||
} | ||
}; | ||
// The loop below is used to possibly match up any discarded | ||
// elements in the original DOM tree with elemenets from the | ||
// target tree that were moved over without visiting their | ||
// children | ||
if (!empty(savedEls)) { | ||
handleMovedElsLoop: | ||
while (movedEls.length) { | ||
var movedElsTemp = movedEls; | ||
movedEls = []; | ||
for (var i=0; i<movedElsTemp.length; i++) { | ||
if (handleMovedEl(movedElsTemp[i]) === false) { | ||
// There are no more unmatched elements so completely end | ||
// the loop | ||
break handleMovedElsLoop; | ||
} | ||
} | ||
} | ||
} | ||
// Fire the "onNodeDiscarded" event for any saved elements | ||
@@ -379,0 +453,0 @@ // that never found a new home in the morphed DOM |
// Create a range object for efficently rendering strings to elements. | ||
var range; | ||
function empty(o) { | ||
for (var k in o) { | ||
if (o.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
function toElement(str) { | ||
@@ -148,2 +157,3 @@ if (!range) { | ||
var childrenOnly = options.childrenOnly === true; | ||
var movedEls = []; | ||
@@ -210,6 +220,7 @@ function removeNodeHelper(node, nestedInSavedEl) { | ||
function morphEl(fromEl, toEl, alreadyVisited, childrenOnly) { | ||
if (getNodeKey(toEl)) { | ||
var toElKey = getNodeKey(toEl); | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it is will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete savedEls[getNodeKey(toEl)]; | ||
delete savedEls[toElKey]; | ||
} | ||
@@ -320,2 +331,11 @@ | ||
if (curToNodeChild.nodeType === 1 && (curToNodeId || curToNodeChild.firstChild)) { | ||
// The element that was just added to the original DOM may have | ||
// some nested elements with a key/ID that needs to be matched up | ||
// with other elements. We'll add the element to a list so that we | ||
// can later process the nested elements if there are any unmatched | ||
// keyed elements that were discarded | ||
movedEls.push(curToNodeChild); | ||
} | ||
curToNodeChild = toNextSibling; | ||
@@ -375,2 +395,56 @@ curFromNodeChild = fromNextSibling; | ||
/** | ||
* What we will do here is walk the tree for the DOM element | ||
* that was moved from the target DOM tree to the original | ||
* DOM tree and we will look for keyed elements that could | ||
* be matched to keyed elements that were earlier discarded. | ||
* If we find a match then we will move the saved element | ||
* into the final DOM tree | ||
*/ | ||
var handleMovedEl = function(el) { | ||
var curChild = el.firstChild; | ||
while(curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var savedEl = savedEls[key]; | ||
if (savedEl && (curChild.tagName === savedEl.tagName)) { | ||
curChild.parentNode.replaceChild(savedEl, curChild); | ||
morphEl(savedEl, curChild, true /* already visited the saved el tree */); | ||
curChild = nextSibling; | ||
if (empty(savedEls)) { | ||
return false; | ||
} | ||
continue; | ||
} | ||
} | ||
if (curChild.nodeType === 1) { | ||
handleMovedEl(curChild); | ||
} | ||
curChild = nextSibling; | ||
} | ||
}; | ||
// The loop below is used to possibly match up any discarded | ||
// elements in the original DOM tree with elemenets from the | ||
// target tree that were moved over without visiting their | ||
// children | ||
if (!empty(savedEls)) { | ||
handleMovedElsLoop: | ||
while (movedEls.length) { | ||
var movedElsTemp = movedEls; | ||
movedEls = []; | ||
for (var i=0; i<movedElsTemp.length; i++) { | ||
if (handleMovedEl(movedElsTemp[i]) === false) { | ||
// There are no more unmatched elements so completely end | ||
// the loop | ||
break handleMovedElsLoop; | ||
} | ||
} | ||
} | ||
} | ||
// Fire the "onNodeDiscarded" event for any saved elements | ||
@@ -377,0 +451,0 @@ // that never found a new home in the morphed DOM |
@@ -38,3 +38,3 @@ { | ||
"dependencies": {}, | ||
"version": "1.0.3" | ||
"version": "1.0.4" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
18439133
17.88%792
13.79%815
19.68%