Skip to content

Commit

Permalink
Updated dist files
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-steele-idem committed Aug 10, 2016
1 parent dd1d8c1 commit 964dcd8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
84 changes: 68 additions & 16 deletions dist/morphdom-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,34 @@ function morphdom(fromNode, toNode, options) {

// This object is used as a lookup to quickly find all keyed elements in the original DOM tree.
var fromNodesLookup = {};
var keyedRemovalList;

function walkDiscardedChildNodes(node) {
function addKeyedRemoval(key) {
if (keyedRemovalList) {
keyedRemovalList.push(key);
} else {
keyedRemovalList = [key];
}
}

function walkDiscardedChildNodes(node, skipKeyedNodes) {
if (node.nodeType === ELEMENT_NODE) {
var curChild = node.firstChild;
while (curChild) {
if (!getNodeKey(curChild)) {

var key = undefined;

if (skipKeyedNodes && (key = getNodeKey(curChild))) {
// If we are skipping keyed nodes then we add the key
// to a list so that it can be handled at the very end.
addKeyedRemoval(key);
} else {
// Only report the node as discarded if it is not keyed. We do this because
// at the end we loop through all keyed elements that were unmatched
// and then discard them in one final pass.
onNodeDiscarded(curChild);
if (curChild.firstChild) {
walkDiscardedChildNodes(curChild);
walkDiscardedChildNodes(curChild, skipKeyedNodes);
}
}

Expand All @@ -258,7 +274,15 @@ function morphdom(fromNode, toNode, options) {
}
}

function removeNode(node, parentNode) {
/**
* Removes a DOM node out of the original DOM
*
* @param {Node} node The node to remove
* @param {Node} parentNode The nodes parent
* @param {Boolean} skipKeyedNodes If true then elements with keys will be skipped and not discarded.
* @return {undefined}
*/
function removeNode(node, parentNode, skipKeyedNodes) {
if (onBeforeNodeDiscarded(node) === false) {
return;
}
Expand All @@ -268,7 +292,7 @@ function morphdom(fromNode, toNode, options) {
}

onNodeDiscarded(node);
walkDiscardedChildNodes(node);
walkDiscardedChildNodes(node, skipKeyedNodes);
}

// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future
Expand Down Expand Up @@ -341,6 +365,8 @@ function morphdom(fromNode, toNode, options) {

function morphEl(fromEl, toEl, childrenOnly) {
var toElKey = getNodeKey(toEl);
var curFromNodeKey;

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
Expand Down Expand Up @@ -374,7 +400,7 @@ function morphdom(fromNode, toNode, options) {
curToNodeKey = getNodeKey(curToNodeChild);

while (curFromNodeChild) {
var curFromNodeKey = getNodeKey(curFromNodeChild);
curFromNodeKey = getNodeKey(curFromNodeChild);
fromNextSibling = curFromNodeChild.nextSibling;

var curFromNodeType = curFromNodeChild.nodeType;
Expand Down Expand Up @@ -410,8 +436,15 @@ function morphdom(fromNode, toNode, options) {
// all lifecycle hooks are correctly invoked
fromEl.insertBefore(matchingFromEl, curFromNodeChild);

if (!curFromNodeKey) {
removeNode(curFromNodeChild, fromEl);
if (curFromNodeKey) {
// Since the node is keyed it might be matched up later so we defer
// the actual removal to later
addKeyedRemoval(curFromNodeKey);
} else {
// NOTE: we skip nested keyed nodes from being removed since there is
// still a chance they will be matched up later
removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);

}
fromNextSibling = curFromNodeChild.nextSibling;
curFromNodeChild = matchingFromEl;
Expand Down Expand Up @@ -457,8 +490,14 @@ function morphdom(fromNode, toNode, options) {
// target tree and we don't want to discard it just yet since it still might find a
// home in the final DOM tree. After everything is done we will remove any keyed nodes
// that didn't find a home
if (!curFromNodeKey) {
removeNode(curFromNodeChild, fromEl);
if (curFromNodeKey) {
// Since the node is keyed it might be matched up later so we defer
// the actual removal to later
addKeyedRemoval(curFromNodeKey);
} else {
// NOTE: we skip nested keyed nodes from being removed since there is
// still a chance they will be matched up later
removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
}

curFromNodeChild = fromNextSibling;
Expand Down Expand Up @@ -487,8 +526,14 @@ function morphdom(fromNode, toNode, options) {
// to be removed
while (curFromNodeChild) {
fromNextSibling = curFromNodeChild.nextSibling;
if (!getNodeKey(curFromNodeChild)) {
removeNode(curFromNodeChild, fromEl);
if ((curFromNodeKey = getNodeKey(curFromNodeChild))) {
// Since the node is keyed it might be matched up later so we defer
// the actual removal to later
addKeyedRemoval(curFromNodeKey);
} else {
// NOTE: we skip nested keyed nodes from being removed since there is
// still a chance they will be matched up later
removeNode(curFromNodeChild, fromEl, true /* skip keyed nodes */);
}
curFromNodeChild = fromNextSibling;
}
Expand Down Expand Up @@ -535,10 +580,17 @@ function morphdom(fromNode, toNode, options) {
} else {
morphEl(morphedNode, toNode, childrenOnly);

for (var k in fromNodesLookup) {
if (fromNodesLookup.hasOwnProperty(k)) {
var elToRemove = fromNodesLookup[k];
removeNode(elToRemove, elToRemove.parentNode);
// We now need to loop over any keyed nodes that might need to be
// removed. We only do the removal if we know that the keyed node
// never found a match. When a keyed node is matched up we remove
// it out of fromNodesLookup and we use fromNodesLookup to determine
// if a keyed node has been matched up or not
if (keyedRemovalList) {
for (var i=0, len=keyedRemovalList.length; i<len; i++) {
var elToRemove = fromNodesLookup[keyedRemovalList[i]];
if (elToRemove) {
removeNode(elToRemove, elToRemove.parentNode, false);
}
}
}
}
Expand Down
Loading

0 comments on commit 964dcd8

Please sign in to comment.