Comparing version 2.6.1 to 2.6.2
@@ -6,2 +6,7 @@ Changelog | ||
## 2.6.2 | ||
- Add new `addChild` and `skipFromChildren` callbacks to allow customization of how new children are | ||
added to a parent as well as preserving the from tree when indexing changes for diffing. | ||
## 2.5.12 | ||
@@ -8,0 +13,0 @@ |
@@ -299,3 +299,3 @@ var DOCUMENT_FRAGMENT_NODE = 11; | ||
if (node) { | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
} | ||
@@ -306,448 +306,452 @@ } | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
toNode = toNode.firstElementChild; | ||
} | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var childrenOnly = options.childrenOnly === true; | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var skipFromChildren = options.skipFromChildren || noop; | ||
var addChild = options.addChild || function(parent, child){ return parent.appendChild(child); }; | ||
var childrenOnly = options.childrenOnly === true; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = undefined; | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
/** | ||
* 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; | ||
} | ||
/** | ||
* 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; | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
// Walk recursively | ||
indexTree(curChild); | ||
// Walk recursively | ||
indexTree(curChild); | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
indexTree(fromNode); | ||
indexTree(fromNode); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
curChild = nextSibling; | ||
} | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
curChild = nextSibling; | ||
} | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
} | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
function morphChildren(fromEl, toEl) { | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
} | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
function morphChildren(fromEl, toEl) { | ||
var skipFrom = skipFromChildren(fromEl); | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
// walk the fromNode children all the way through | ||
while (curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// walk the fromNode children all the way through | ||
while (!skipFrom && curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
// fromNextSibling = curFromNodeChild.nextSibling; | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
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; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
} | ||
} | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
if (curFromNodeKey) { | ||
// Since the node is keyed it might be matched up later so we defer | ||
// the actual removal to later | ||
addKeyedRemoval(curFromNodeKey); | ||
} else { | ||
} 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; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. Therefore, we will just append the current "to" node | ||
// to the end | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
fromEl.appendChild(matchingFromEl); | ||
// MORPH | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
fromEl.appendChild(curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
curFromNodeChild = matchingFromEl; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
} | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
// 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 */); | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
curFromNodeChild = fromNextSibling; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
// MORPH | ||
addChild(fromEl, matchingFromEl); | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
addChild(fromEl, curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
} | ||
} | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
} | ||
} | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
onNodeDiscarded(fromNode); | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
}; | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
} | ||
return morphedNode; | ||
}; | ||
} | ||
@@ -754,0 +758,0 @@ |
@@ -237,3 +237,3 @@ 'use strict'; | ||
if (node) { | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
} | ||
@@ -244,450 +244,454 @@ } | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE) { | ||
toNode = toNode.firstElementChild; | ||
} | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var childrenOnly = options.childrenOnly === true; | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var skipFromChildren = options.skipFromChildren || noop; | ||
var addChild = options.addChild || function(parent, child){ return parent.appendChild(child); }; | ||
var childrenOnly = options.childrenOnly === true; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = undefined; | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
/** | ||
* 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; | ||
} | ||
/** | ||
* 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; | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
// Walk recursively | ||
indexTree(curChild); | ||
// Walk recursively | ||
indexTree(curChild); | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
indexTree(fromNode); | ||
indexTree(fromNode); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
curChild = nextSibling; | ||
} | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
curChild = nextSibling; | ||
} | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
} | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
function morphChildren(fromEl, toEl) { | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
} | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
function morphChildren(fromEl, toEl) { | ||
var skipFrom = skipFromChildren(fromEl); | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
// walk the fromNode children all the way through | ||
while (curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// walk the fromNode children all the way through | ||
while (!skipFrom && curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
// fromNextSibling = curFromNodeChild.nextSibling; | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
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; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
} | ||
} | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
if (curFromNodeKey) { | ||
// Since the node is keyed it might be matched up later so we defer | ||
// the actual removal to later | ||
addKeyedRemoval(curFromNodeKey); | ||
} else { | ||
} 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; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. Therefore, we will just append the current "to" node | ||
// to the end | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
fromEl.appendChild(matchingFromEl); | ||
// MORPH | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
fromEl.appendChild(curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
curFromNodeChild = matchingFromEl; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
} | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
// 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 */); | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
curFromNodeChild = fromNextSibling; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
// MORPH | ||
addChild(fromEl, matchingFromEl); | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
addChild(fromEl, curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
} | ||
} | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
} | ||
} | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
onNodeDiscarded(fromNode); | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
}; | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
} | ||
return morphedNode; | ||
}; | ||
} | ||
module.exports = morphdomFactory; |
@@ -305,3 +305,3 @@ (function (global, factory) { | ||
if (node) { | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
} | ||
@@ -312,448 +312,452 @@ } | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
toNode = toNode.firstElementChild; | ||
} | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var childrenOnly = options.childrenOnly === true; | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var skipFromChildren = options.skipFromChildren || noop; | ||
var addChild = options.addChild || function(parent, child){ return parent.appendChild(child); }; | ||
var childrenOnly = options.childrenOnly === true; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = undefined; | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
/** | ||
* 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; | ||
} | ||
/** | ||
* 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; | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
// Walk recursively | ||
indexTree(curChild); | ||
// Walk recursively | ||
indexTree(curChild); | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
indexTree(fromNode); | ||
indexTree(fromNode); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
curChild = nextSibling; | ||
} | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
curChild = nextSibling; | ||
} | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
} | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
function morphChildren(fromEl, toEl) { | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
} | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
function morphChildren(fromEl, toEl) { | ||
var skipFrom = skipFromChildren(fromEl); | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
// walk the fromNode children all the way through | ||
while (curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// walk the fromNode children all the way through | ||
while (!skipFrom && curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
// fromNextSibling = curFromNodeChild.nextSibling; | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
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; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
} | ||
} | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
if (curFromNodeKey) { | ||
// Since the node is keyed it might be matched up later so we defer | ||
// the actual removal to later | ||
addKeyedRemoval(curFromNodeKey); | ||
} else { | ||
} 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; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. Therefore, we will just append the current "to" node | ||
// to the end | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
fromEl.appendChild(matchingFromEl); | ||
// MORPH | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
fromEl.appendChild(curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
curFromNodeChild = matchingFromEl; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
} | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
// 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 */); | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
curFromNodeChild = fromNextSibling; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
// MORPH | ||
addChild(fromEl, matchingFromEl); | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
addChild(fromEl, curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
} | ||
} | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
} | ||
} | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
onNodeDiscarded(fromNode); | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
}; | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
} | ||
return morphedNode; | ||
}; | ||
} | ||
@@ -760,0 +764,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=global||self,global.morphdom=factory())})(this,function(){"use strict";var DOCUMENT_FRAGMENT_NODE=11;function morphAttrs(fromNode,toNode){var toNodeAttrs=toNode.attributes;var attr;var attrName;var attrNamespaceURI;var attrValue;var fromValue;if(toNode.nodeType===DOCUMENT_FRAGMENT_NODE||fromNode.nodeType===DOCUMENT_FRAGMENT_NODE){return}for(var i=toNodeAttrs.length-1;i>=0;i--){attr=toNodeAttrs[i];attrName=attr.name;attrNamespaceURI=attr.namespaceURI;attrValue=attr.value;if(attrNamespaceURI){attrName=attr.localName||attrName;fromValue=fromNode.getAttributeNS(attrNamespaceURI,attrName);if(fromValue!==attrValue){if(attr.prefix==="xmlns"){attrName=attr.name}fromNode.setAttributeNS(attrNamespaceURI,attrName,attrValue)}}else{fromValue=fromNode.getAttribute(attrName);if(fromValue!==attrValue){fromNode.setAttribute(attrName,attrValue)}}}var fromNodeAttrs=fromNode.attributes;for(var d=fromNodeAttrs.length-1;d>=0;d--){attr=fromNodeAttrs[d];attrName=attr.name;attrNamespaceURI=attr.namespaceURI;if(attrNamespaceURI){attrName=attr.localName||attrName;if(!toNode.hasAttributeNS(attrNamespaceURI,attrName)){fromNode.removeAttributeNS(attrNamespaceURI,attrName)}}else{if(!toNode.hasAttribute(attrName)){fromNode.removeAttribute(attrName)}}}}var range;var NS_XHTML="http://www.w3.org/1999/xhtml";var doc=typeof document==="undefined"?undefined:document;var HAS_TEMPLATE_SUPPORT=!!doc&&"content"in doc.createElement("template");var HAS_RANGE_SUPPORT=!!doc&&doc.createRange&&"createContextualFragment"in doc.createRange();function createFragmentFromTemplate(str){var template=doc.createElement("template");template.innerHTML=str;return template.content.childNodes[0]}function createFragmentFromRange(str){if(!range){range=doc.createRange();range.selectNode(doc.body)}var fragment=range.createContextualFragment(str);return fragment.childNodes[0]}function createFragmentFromWrap(str){var fragment=doc.createElement("body");fragment.innerHTML=str;return fragment.childNodes[0]}function toElement(str){str=str.trim();if(HAS_TEMPLATE_SUPPORT){return createFragmentFromTemplate(str)}else if(HAS_RANGE_SUPPORT){return createFragmentFromRange(str)}return createFragmentFromWrap(str)}function compareNodeNames(fromEl,toEl){var fromNodeName=fromEl.nodeName;var toNodeName=toEl.nodeName;var fromCodeStart,toCodeStart;if(fromNodeName===toNodeName){return true}fromCodeStart=fromNodeName.charCodeAt(0);toCodeStart=toNodeName.charCodeAt(0);if(fromCodeStart<=90&&toCodeStart>=97){return fromNodeName===toNodeName.toUpperCase()}else if(toCodeStart<=90&&fromCodeStart>=97){return toNodeName===fromNodeName.toUpperCase()}else{return false}}function createElementNS(name,namespaceURI){return!namespaceURI||namespaceURI===NS_XHTML?doc.createElement(name):doc.createElementNS(namespaceURI,name)}function moveChildren(fromEl,toEl){var curChild=fromEl.firstChild;while(curChild){var nextChild=curChild.nextSibling;toEl.appendChild(curChild);curChild=nextChild}return toEl}function syncBooleanAttrProp(fromEl,toEl,name){if(fromEl[name]!==toEl[name]){fromEl[name]=toEl[name];if(fromEl[name]){fromEl.setAttribute(name,"")}else{fromEl.removeAttribute(name)}}}var specialElHandlers={OPTION:function(fromEl,toEl){var parentNode=fromEl.parentNode;if(parentNode){var parentName=parentNode.nodeName.toUpperCase();if(parentName==="OPTGROUP"){parentNode=parentNode.parentNode;parentName=parentNode&&parentNode.nodeName.toUpperCase()}if(parentName==="SELECT"&&!parentNode.hasAttribute("multiple")){if(fromEl.hasAttribute("selected")&&!toEl.selected){fromEl.setAttribute("selected","selected");fromEl.removeAttribute("selected")}parentNode.selectedIndex=-1}}syncBooleanAttrProp(fromEl,toEl,"selected")},INPUT:function(fromEl,toEl){syncBooleanAttrProp(fromEl,toEl,"checked");syncBooleanAttrProp(fromEl,toEl,"disabled");if(fromEl.value!==toEl.value){fromEl.value=toEl.value}if(!toEl.hasAttribute("value")){fromEl.removeAttribute("value")}},TEXTAREA:function(fromEl,toEl){var newValue=toEl.value;if(fromEl.value!==newValue){fromEl.value=newValue}var firstChild=fromEl.firstChild;if(firstChild){var oldValue=firstChild.nodeValue;if(oldValue==newValue||!newValue&&oldValue==fromEl.placeholder){return}firstChild.nodeValue=newValue}},SELECT:function(fromEl,toEl){if(!toEl.hasAttribute("multiple")){var selectedIndex=-1;var i=0;var curChild=fromEl.firstChild;var optgroup;var nodeName;while(curChild){nodeName=curChild.nodeName&&curChild.nodeName.toUpperCase();if(nodeName==="OPTGROUP"){optgroup=curChild;curChild=optgroup.firstChild}else{if(nodeName==="OPTION"){if(curChild.hasAttribute("selected")){selectedIndex=i;break}i++}curChild=curChild.nextSibling;if(!curChild&&optgroup){curChild=optgroup.nextSibling;optgroup=null}}}fromEl.selectedIndex=selectedIndex}}};var ELEMENT_NODE=1;var DOCUMENT_FRAGMENT_NODE$1=11;var TEXT_NODE=3;var COMMENT_NODE=8;function noop(){}function defaultGetNodeKey(node){if(node){return node.getAttribute&&node.getAttribute("id")||node.id}}function morphdomFactory(morphAttrs){return function morphdom(fromNode,toNode,options){if(!options){options={}}if(typeof toNode==="string"){if(fromNode.nodeName==="#document"||fromNode.nodeName==="HTML"||fromNode.nodeName==="BODY"){var toNodeHtml=toNode;toNode=doc.createElement("html");toNode.innerHTML=toNodeHtml}else{toNode=toElement(toNode)}}var getNodeKey=options.getNodeKey||defaultGetNodeKey;var onBeforeNodeAdded=options.onBeforeNodeAdded||noop;var onNodeAdded=options.onNodeAdded||noop;var onBeforeElUpdated=options.onBeforeElUpdated||noop;var onElUpdated=options.onElUpdated||noop;var onBeforeNodeDiscarded=options.onBeforeNodeDiscarded||noop;var onNodeDiscarded=options.onNodeDiscarded||noop;var onBeforeElChildrenUpdated=options.onBeforeElChildrenUpdated||noop;var childrenOnly=options.childrenOnly===true;var fromNodesLookup=Object.create(null);var keyedRemovalList=[];function addKeyedRemoval(key){keyedRemovalList.push(key)}function walkDiscardedChildNodes(node,skipKeyedNodes){if(node.nodeType===ELEMENT_NODE){var curChild=node.firstChild;while(curChild){var key=undefined;if(skipKeyedNodes&&(key=getNodeKey(curChild))){addKeyedRemoval(key)}else{onNodeDiscarded(curChild);if(curChild.firstChild){walkDiscardedChildNodes(curChild,skipKeyedNodes)}}curChild=curChild.nextSibling}}}function removeNode(node,parentNode,skipKeyedNodes){if(onBeforeNodeDiscarded(node)===false){return}if(parentNode){parentNode.removeChild(node)}onNodeDiscarded(node);walkDiscardedChildNodes(node,skipKeyedNodes)}function indexTree(node){if(node.nodeType===ELEMENT_NODE||node.nodeType===DOCUMENT_FRAGMENT_NODE$1){var curChild=node.firstChild;while(curChild){var key=getNodeKey(curChild);if(key){fromNodesLookup[key]=curChild}indexTree(curChild);curChild=curChild.nextSibling}}}indexTree(fromNode);function handleNodeAdded(el){onNodeAdded(el);var curChild=el.firstChild;while(curChild){var nextSibling=curChild.nextSibling;var key=getNodeKey(curChild);if(key){var unmatchedFromEl=fromNodesLookup[key];if(unmatchedFromEl&&compareNodeNames(curChild,unmatchedFromEl)){curChild.parentNode.replaceChild(unmatchedFromEl,curChild);morphEl(unmatchedFromEl,curChild)}else{handleNodeAdded(curChild)}}else{handleNodeAdded(curChild)}curChild=nextSibling}}function cleanupFromEl(fromEl,curFromNodeChild,curFromNodeKey){while(curFromNodeChild){var fromNextSibling=curFromNodeChild.nextSibling;if(curFromNodeKey=getNodeKey(curFromNodeChild)){addKeyedRemoval(curFromNodeKey)}else{removeNode(curFromNodeChild,fromEl,true)}curFromNodeChild=fromNextSibling}}function morphEl(fromEl,toEl,childrenOnly){var toElKey=getNodeKey(toEl);if(toElKey){delete fromNodesLookup[toElKey]}if(!childrenOnly){if(onBeforeElUpdated(fromEl,toEl)===false){return}morphAttrs(fromEl,toEl);onElUpdated(fromEl);if(onBeforeElChildrenUpdated(fromEl,toEl)===false){return}}if(fromEl.nodeName!=="TEXTAREA"){morphChildren(fromEl,toEl)}else{specialElHandlers.TEXTAREA(fromEl,toEl)}}function morphChildren(fromEl,toEl){var curToNodeChild=toEl.firstChild;var curFromNodeChild=fromEl.firstChild;var curToNodeKey;var curFromNodeKey;var fromNextSibling;var toNextSibling;var matchingFromEl;outer:while(curToNodeChild){toNextSibling=curToNodeChild.nextSibling;curToNodeKey=getNodeKey(curToNodeChild);while(curFromNodeChild){fromNextSibling=curFromNodeChild.nextSibling;if(curToNodeChild.isSameNode&&curToNodeChild.isSameNode(curFromNodeChild)){curToNodeChild=toNextSibling;curFromNodeChild=fromNextSibling;continue outer}curFromNodeKey=getNodeKey(curFromNodeChild);var curFromNodeType=curFromNodeChild.nodeType;var isCompatible=undefined;if(curFromNodeType===curToNodeChild.nodeType){if(curFromNodeType===ELEMENT_NODE){if(curToNodeKey){if(curToNodeKey!==curFromNodeKey){if(matchingFromEl=fromNodesLookup[curToNodeKey]){if(fromNextSibling===matchingFromEl){isCompatible=false}else{fromEl.insertBefore(matchingFromEl,curFromNodeChild);if(curFromNodeKey){addKeyedRemoval(curFromNodeKey)}else{removeNode(curFromNodeChild,fromEl,true)}curFromNodeChild=matchingFromEl}}else{isCompatible=false}}}else if(curFromNodeKey){isCompatible=false}isCompatible=isCompatible!==false&&compareNodeNames(curFromNodeChild,curToNodeChild);if(isCompatible){morphEl(curFromNodeChild,curToNodeChild)}}else if(curFromNodeType===TEXT_NODE||curFromNodeType==COMMENT_NODE){isCompatible=true;if(curFromNodeChild.nodeValue!==curToNodeChild.nodeValue){curFromNodeChild.nodeValue=curToNodeChild.nodeValue}}}if(isCompatible){curToNodeChild=toNextSibling;curFromNodeChild=fromNextSibling;continue outer}if(curFromNodeKey){addKeyedRemoval(curFromNodeKey)}else{removeNode(curFromNodeChild,fromEl,true)}curFromNodeChild=fromNextSibling}if(curToNodeKey&&(matchingFromEl=fromNodesLookup[curToNodeKey])&&compareNodeNames(matchingFromEl,curToNodeChild)){fromEl.appendChild(matchingFromEl);morphEl(matchingFromEl,curToNodeChild)}else{var onBeforeNodeAddedResult=onBeforeNodeAdded(curToNodeChild);if(onBeforeNodeAddedResult!==false){if(onBeforeNodeAddedResult){curToNodeChild=onBeforeNodeAddedResult}if(curToNodeChild.actualize){curToNodeChild=curToNodeChild.actualize(fromEl.ownerDocument||doc)}fromEl.appendChild(curToNodeChild);handleNodeAdded(curToNodeChild)}}curToNodeChild=toNextSibling;curFromNodeChild=fromNextSibling}cleanupFromEl(fromEl,curFromNodeChild,curFromNodeKey);var specialElHandler=specialElHandlers[fromEl.nodeName];if(specialElHandler){specialElHandler(fromEl,toEl)}}var morphedNode=fromNode;var morphedNodeType=morphedNode.nodeType;var toNodeType=toNode.nodeType;if(!childrenOnly){if(morphedNodeType===ELEMENT_NODE){if(toNodeType===ELEMENT_NODE){if(!compareNodeNames(fromNode,toNode)){onNodeDiscarded(fromNode);morphedNode=moveChildren(fromNode,createElementNS(toNode.nodeName,toNode.namespaceURI))}}else{morphedNode=toNode}}else if(morphedNodeType===TEXT_NODE||morphedNodeType===COMMENT_NODE){if(toNodeType===morphedNodeType){if(morphedNode.nodeValue!==toNode.nodeValue){morphedNode.nodeValue=toNode.nodeValue}return morphedNode}else{morphedNode=toNode}}}if(morphedNode===toNode){onNodeDiscarded(fromNode)}else{if(toNode.isSameNode&&toNode.isSameNode(morphedNode)){return}morphEl(morphedNode,toNode,childrenOnly);if(keyedRemovalList){for(var i=0,len=keyedRemovalList.length;i<len;i++){var elToRemove=fromNodesLookup[keyedRemovalList[i]];if(elToRemove){removeNode(elToRemove,elToRemove.parentNode,false)}}}}if(!childrenOnly&&morphedNode!==fromNode&&fromNode.parentNode){if(morphedNode.actualize){morphedNode=morphedNode.actualize(fromNode.ownerDocument||doc)}fromNode.parentNode.replaceChild(morphedNode,fromNode)}return morphedNode}}var morphdom=morphdomFactory(morphAttrs);return morphdom}); | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=global||self,global.morphdom=factory())})(this,function(){"use strict";var DOCUMENT_FRAGMENT_NODE=11;function morphAttrs(fromNode,toNode){var toNodeAttrs=toNode.attributes;var attr;var attrName;var attrNamespaceURI;var attrValue;var fromValue;if(toNode.nodeType===DOCUMENT_FRAGMENT_NODE||fromNode.nodeType===DOCUMENT_FRAGMENT_NODE){return}for(var i=toNodeAttrs.length-1;i>=0;i--){attr=toNodeAttrs[i];attrName=attr.name;attrNamespaceURI=attr.namespaceURI;attrValue=attr.value;if(attrNamespaceURI){attrName=attr.localName||attrName;fromValue=fromNode.getAttributeNS(attrNamespaceURI,attrName);if(fromValue!==attrValue){if(attr.prefix==="xmlns"){attrName=attr.name}fromNode.setAttributeNS(attrNamespaceURI,attrName,attrValue)}}else{fromValue=fromNode.getAttribute(attrName);if(fromValue!==attrValue){fromNode.setAttribute(attrName,attrValue)}}}var fromNodeAttrs=fromNode.attributes;for(var d=fromNodeAttrs.length-1;d>=0;d--){attr=fromNodeAttrs[d];attrName=attr.name;attrNamespaceURI=attr.namespaceURI;if(attrNamespaceURI){attrName=attr.localName||attrName;if(!toNode.hasAttributeNS(attrNamespaceURI,attrName)){fromNode.removeAttributeNS(attrNamespaceURI,attrName)}}else{if(!toNode.hasAttribute(attrName)){fromNode.removeAttribute(attrName)}}}}var range;var NS_XHTML="http://www.w3.org/1999/xhtml";var doc=typeof document==="undefined"?undefined:document;var HAS_TEMPLATE_SUPPORT=!!doc&&"content"in doc.createElement("template");var HAS_RANGE_SUPPORT=!!doc&&doc.createRange&&"createContextualFragment"in doc.createRange();function createFragmentFromTemplate(str){var template=doc.createElement("template");template.innerHTML=str;return template.content.childNodes[0]}function createFragmentFromRange(str){if(!range){range=doc.createRange();range.selectNode(doc.body)}var fragment=range.createContextualFragment(str);return fragment.childNodes[0]}function createFragmentFromWrap(str){var fragment=doc.createElement("body");fragment.innerHTML=str;return fragment.childNodes[0]}function toElement(str){str=str.trim();if(HAS_TEMPLATE_SUPPORT){return createFragmentFromTemplate(str)}else if(HAS_RANGE_SUPPORT){return createFragmentFromRange(str)}return createFragmentFromWrap(str)}function compareNodeNames(fromEl,toEl){var fromNodeName=fromEl.nodeName;var toNodeName=toEl.nodeName;var fromCodeStart,toCodeStart;if(fromNodeName===toNodeName){return true}fromCodeStart=fromNodeName.charCodeAt(0);toCodeStart=toNodeName.charCodeAt(0);if(fromCodeStart<=90&&toCodeStart>=97){return fromNodeName===toNodeName.toUpperCase()}else if(toCodeStart<=90&&fromCodeStart>=97){return toNodeName===fromNodeName.toUpperCase()}else{return false}}function createElementNS(name,namespaceURI){return!namespaceURI||namespaceURI===NS_XHTML?doc.createElement(name):doc.createElementNS(namespaceURI,name)}function moveChildren(fromEl,toEl){var curChild=fromEl.firstChild;while(curChild){var nextChild=curChild.nextSibling;toEl.appendChild(curChild);curChild=nextChild}return toEl}function syncBooleanAttrProp(fromEl,toEl,name){if(fromEl[name]!==toEl[name]){fromEl[name]=toEl[name];if(fromEl[name]){fromEl.setAttribute(name,"")}else{fromEl.removeAttribute(name)}}}var specialElHandlers={OPTION:function(fromEl,toEl){var parentNode=fromEl.parentNode;if(parentNode){var parentName=parentNode.nodeName.toUpperCase();if(parentName==="OPTGROUP"){parentNode=parentNode.parentNode;parentName=parentNode&&parentNode.nodeName.toUpperCase()}if(parentName==="SELECT"&&!parentNode.hasAttribute("multiple")){if(fromEl.hasAttribute("selected")&&!toEl.selected){fromEl.setAttribute("selected","selected");fromEl.removeAttribute("selected")}parentNode.selectedIndex=-1}}syncBooleanAttrProp(fromEl,toEl,"selected")},INPUT:function(fromEl,toEl){syncBooleanAttrProp(fromEl,toEl,"checked");syncBooleanAttrProp(fromEl,toEl,"disabled");if(fromEl.value!==toEl.value){fromEl.value=toEl.value}if(!toEl.hasAttribute("value")){fromEl.removeAttribute("value")}},TEXTAREA:function(fromEl,toEl){var newValue=toEl.value;if(fromEl.value!==newValue){fromEl.value=newValue}var firstChild=fromEl.firstChild;if(firstChild){var oldValue=firstChild.nodeValue;if(oldValue==newValue||!newValue&&oldValue==fromEl.placeholder){return}firstChild.nodeValue=newValue}},SELECT:function(fromEl,toEl){if(!toEl.hasAttribute("multiple")){var selectedIndex=-1;var i=0;var curChild=fromEl.firstChild;var optgroup;var nodeName;while(curChild){nodeName=curChild.nodeName&&curChild.nodeName.toUpperCase();if(nodeName==="OPTGROUP"){optgroup=curChild;curChild=optgroup.firstChild}else{if(nodeName==="OPTION"){if(curChild.hasAttribute("selected")){selectedIndex=i;break}i++}curChild=curChild.nextSibling;if(!curChild&&optgroup){curChild=optgroup.nextSibling;optgroup=null}}}fromEl.selectedIndex=selectedIndex}}};var ELEMENT_NODE=1;var DOCUMENT_FRAGMENT_NODE$1=11;var TEXT_NODE=3;var COMMENT_NODE=8;function noop(){}function defaultGetNodeKey(node){if(node){return node.getAttribute&&node.getAttribute("id")||node.id}}function morphdomFactory(morphAttrs){return function morphdom(fromNode,toNode,options){if(!options){options={}}if(typeof toNode==="string"){if(fromNode.nodeName==="#document"||fromNode.nodeName==="HTML"||fromNode.nodeName==="BODY"){var toNodeHtml=toNode;toNode=doc.createElement("html");toNode.innerHTML=toNodeHtml}else{toNode=toElement(toNode)}}else if(toNode.nodeType===DOCUMENT_FRAGMENT_NODE$1){toNode=toNode.firstElementChild}var getNodeKey=options.getNodeKey||defaultGetNodeKey;var onBeforeNodeAdded=options.onBeforeNodeAdded||noop;var onNodeAdded=options.onNodeAdded||noop;var onBeforeElUpdated=options.onBeforeElUpdated||noop;var onElUpdated=options.onElUpdated||noop;var onBeforeNodeDiscarded=options.onBeforeNodeDiscarded||noop;var onNodeDiscarded=options.onNodeDiscarded||noop;var onBeforeElChildrenUpdated=options.onBeforeElChildrenUpdated||noop;var skipFromChildren=options.skipFromChildren||noop;var addChild=options.addChild||function(parent,child){return parent.appendChild(child)};var childrenOnly=options.childrenOnly===true;var fromNodesLookup=Object.create(null);var keyedRemovalList=[];function addKeyedRemoval(key){keyedRemovalList.push(key)}function walkDiscardedChildNodes(node,skipKeyedNodes){if(node.nodeType===ELEMENT_NODE){var curChild=node.firstChild;while(curChild){var key=undefined;if(skipKeyedNodes&&(key=getNodeKey(curChild))){addKeyedRemoval(key)}else{onNodeDiscarded(curChild);if(curChild.firstChild){walkDiscardedChildNodes(curChild,skipKeyedNodes)}}curChild=curChild.nextSibling}}}function removeNode(node,parentNode,skipKeyedNodes){if(onBeforeNodeDiscarded(node)===false){return}if(parentNode){parentNode.removeChild(node)}onNodeDiscarded(node);walkDiscardedChildNodes(node,skipKeyedNodes)}function indexTree(node){if(node.nodeType===ELEMENT_NODE||node.nodeType===DOCUMENT_FRAGMENT_NODE$1){var curChild=node.firstChild;while(curChild){var key=getNodeKey(curChild);if(key){fromNodesLookup[key]=curChild}indexTree(curChild);curChild=curChild.nextSibling}}}indexTree(fromNode);function handleNodeAdded(el){onNodeAdded(el);var curChild=el.firstChild;while(curChild){var nextSibling=curChild.nextSibling;var key=getNodeKey(curChild);if(key){var unmatchedFromEl=fromNodesLookup[key];if(unmatchedFromEl&&compareNodeNames(curChild,unmatchedFromEl)){curChild.parentNode.replaceChild(unmatchedFromEl,curChild);morphEl(unmatchedFromEl,curChild)}else{handleNodeAdded(curChild)}}else{handleNodeAdded(curChild)}curChild=nextSibling}}function cleanupFromEl(fromEl,curFromNodeChild,curFromNodeKey){while(curFromNodeChild){var fromNextSibling=curFromNodeChild.nextSibling;if(curFromNodeKey=getNodeKey(curFromNodeChild)){addKeyedRemoval(curFromNodeKey)}else{removeNode(curFromNodeChild,fromEl,true)}curFromNodeChild=fromNextSibling}}function morphEl(fromEl,toEl,childrenOnly){var toElKey=getNodeKey(toEl);if(toElKey){delete fromNodesLookup[toElKey]}if(!childrenOnly){if(onBeforeElUpdated(fromEl,toEl)===false){return}morphAttrs(fromEl,toEl);onElUpdated(fromEl);if(onBeforeElChildrenUpdated(fromEl,toEl)===false){return}}if(fromEl.nodeName!=="TEXTAREA"){morphChildren(fromEl,toEl)}else{specialElHandlers.TEXTAREA(fromEl,toEl)}}function morphChildren(fromEl,toEl){var skipFrom=skipFromChildren(fromEl);var curToNodeChild=toEl.firstChild;var curFromNodeChild=fromEl.firstChild;var curToNodeKey;var curFromNodeKey;var fromNextSibling;var toNextSibling;var matchingFromEl;outer:while(curToNodeChild){toNextSibling=curToNodeChild.nextSibling;curToNodeKey=getNodeKey(curToNodeChild);while(!skipFrom&&curFromNodeChild){fromNextSibling=curFromNodeChild.nextSibling;if(curToNodeChild.isSameNode&&curToNodeChild.isSameNode(curFromNodeChild)){curToNodeChild=toNextSibling;curFromNodeChild=fromNextSibling;continue outer}curFromNodeKey=getNodeKey(curFromNodeChild);var curFromNodeType=curFromNodeChild.nodeType;var isCompatible=undefined;if(curFromNodeType===curToNodeChild.nodeType){if(curFromNodeType===ELEMENT_NODE){if(curToNodeKey){if(curToNodeKey!==curFromNodeKey){if(matchingFromEl=fromNodesLookup[curToNodeKey]){if(fromNextSibling===matchingFromEl){isCompatible=false}else{fromEl.insertBefore(matchingFromEl,curFromNodeChild);if(curFromNodeKey){addKeyedRemoval(curFromNodeKey)}else{removeNode(curFromNodeChild,fromEl,true)}curFromNodeChild=matchingFromEl}}else{isCompatible=false}}}else if(curFromNodeKey){isCompatible=false}isCompatible=isCompatible!==false&&compareNodeNames(curFromNodeChild,curToNodeChild);if(isCompatible){morphEl(curFromNodeChild,curToNodeChild)}}else if(curFromNodeType===TEXT_NODE||curFromNodeType==COMMENT_NODE){isCompatible=true;if(curFromNodeChild.nodeValue!==curToNodeChild.nodeValue){curFromNodeChild.nodeValue=curToNodeChild.nodeValue}}}if(isCompatible){curToNodeChild=toNextSibling;curFromNodeChild=fromNextSibling;continue outer}if(curFromNodeKey){addKeyedRemoval(curFromNodeKey)}else{removeNode(curFromNodeChild,fromEl,true)}curFromNodeChild=fromNextSibling}if(curToNodeKey&&(matchingFromEl=fromNodesLookup[curToNodeKey])&&compareNodeNames(matchingFromEl,curToNodeChild)){addChild(fromEl,matchingFromEl);morphEl(matchingFromEl,curToNodeChild)}else{var onBeforeNodeAddedResult=onBeforeNodeAdded(curToNodeChild);if(onBeforeNodeAddedResult!==false){if(onBeforeNodeAddedResult){curToNodeChild=onBeforeNodeAddedResult}if(curToNodeChild.actualize){curToNodeChild=curToNodeChild.actualize(fromEl.ownerDocument||doc)}addChild(fromEl,curToNodeChild);handleNodeAdded(curToNodeChild)}}curToNodeChild=toNextSibling;curFromNodeChild=fromNextSibling}cleanupFromEl(fromEl,curFromNodeChild,curFromNodeKey);var specialElHandler=specialElHandlers[fromEl.nodeName];if(specialElHandler){specialElHandler(fromEl,toEl)}}var morphedNode=fromNode;var morphedNodeType=morphedNode.nodeType;var toNodeType=toNode.nodeType;if(!childrenOnly){if(morphedNodeType===ELEMENT_NODE){if(toNodeType===ELEMENT_NODE){if(!compareNodeNames(fromNode,toNode)){onNodeDiscarded(fromNode);morphedNode=moveChildren(fromNode,createElementNS(toNode.nodeName,toNode.namespaceURI))}}else{morphedNode=toNode}}else if(morphedNodeType===TEXT_NODE||morphedNodeType===COMMENT_NODE){if(toNodeType===morphedNodeType){if(morphedNode.nodeValue!==toNode.nodeValue){morphedNode.nodeValue=toNode.nodeValue}return morphedNode}else{morphedNode=toNode}}}if(morphedNode===toNode){onNodeDiscarded(fromNode)}else{if(toNode.isSameNode&&toNode.isSameNode(morphedNode)){return}morphEl(morphedNode,toNode,childrenOnly);if(keyedRemovalList){for(var i=0,len=keyedRemovalList.length;i<len;i++){var elToRemove=fromNodesLookup[keyedRemovalList[i]];if(elToRemove){removeNode(elToRemove,elToRemove.parentNode,false)}}}}if(!childrenOnly&&morphedNode!==fromNode&&fromNode.parentNode){if(morphedNode.actualize){morphedNode=morphedNode.actualize(fromNode.ownerDocument||doc)}fromNode.parentNode.replaceChild(morphedNode,fromNode)}return morphedNode}}var morphdom=morphdomFactory(morphAttrs);return morphdom}); |
@@ -301,3 +301,3 @@ 'use strict'; | ||
if (node) { | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
} | ||
@@ -308,448 +308,452 @@ } | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
toNode = toNode.firstElementChild; | ||
} | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var childrenOnly = options.childrenOnly === true; | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var skipFromChildren = options.skipFromChildren || noop; | ||
var addChild = options.addChild || function(parent, child){ return parent.appendChild(child); }; | ||
var childrenOnly = options.childrenOnly === true; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = undefined; | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
/** | ||
* 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; | ||
} | ||
/** | ||
* 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; | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE$1) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
// Walk recursively | ||
indexTree(curChild); | ||
// Walk recursively | ||
indexTree(curChild); | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
indexTree(fromNode); | ||
indexTree(fromNode); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
curChild = nextSibling; | ||
} | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
curChild = nextSibling; | ||
} | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
} | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
function morphChildren(fromEl, toEl) { | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
} | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
function morphChildren(fromEl, toEl) { | ||
var skipFrom = skipFromChildren(fromEl); | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
// walk the fromNode children all the way through | ||
while (curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// walk the fromNode children all the way through | ||
while (!skipFrom && curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
// fromNextSibling = curFromNodeChild.nextSibling; | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
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; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
} | ||
} | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
if (curFromNodeKey) { | ||
// Since the node is keyed it might be matched up later so we defer | ||
// the actual removal to later | ||
addKeyedRemoval(curFromNodeKey); | ||
} else { | ||
} 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; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. Therefore, we will just append the current "to" node | ||
// to the end | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
fromEl.appendChild(matchingFromEl); | ||
// MORPH | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
fromEl.appendChild(curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
curFromNodeChild = matchingFromEl; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
} | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
// 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 */); | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
curFromNodeChild = fromNextSibling; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
// MORPH | ||
addChild(fromEl, matchingFromEl); | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
addChild(fromEl, curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
} | ||
} | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
} | ||
} | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
onNodeDiscarded(fromNode); | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
}; | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
} | ||
return morphedNode; | ||
}; | ||
} | ||
@@ -756,0 +760,0 @@ |
@@ -55,3 +55,3 @@ { | ||
"dependencies": {}, | ||
"version": "2.6.1", | ||
"version": "2.6.2", | ||
"keywords": [ | ||
@@ -58,0 +58,0 @@ "dom", |
@@ -89,2 +89,3 @@ morphdom | ||
- **getNodeKey** (`Function(node)`) - Called to get the `Node`'s unique identifier. This is used by `morphdom` to rearrange elements rather than creating and destroying an element that already exists. This defaults to using the `Node`'s `id` property. (Note that form fields must not have a `name` corresponding to forms' DOM properties, e.g. `id`.) | ||
- **addChild** (`Function(parentNode, childNode)`) - Called when adding a new child to a parent. By default, `parentNode.appendChild(childNode)` is invoked. Use this callback to customize how a new child is added. | ||
- **onBeforeNodeAdded** (`Function(node)`) - Called before a `Node` in the `to` tree is added to the `from` tree. If this function returns `false` then the node will not be added. Should return the node to be added. | ||
@@ -98,2 +99,3 @@ - **onNodeAdded** (`Function(node)`) - Called after a `Node` in the `to` tree has been added to the `from` tree. | ||
- **childrenOnly** (`Boolean`) - If `true` then only the children of the `fromNode` and `toNode` nodes will be morphed (the containing element will be skipped). Defaults to `false`. | ||
- **skipFromChildren** (`Function(fromEl)`) - called when indexing a the `fromEl` tree. False by default. Return `true` to skip indexing the from tree, which will keep current items in place after patch rather than removing them when not found in the `toEl`. | ||
@@ -106,2 +108,5 @@ ```javascript | ||
}, | ||
addChild: function(parentNode, childNode) { | ||
parentNode.appendChild(childNode); | ||
}, | ||
onBeforeNodeAdded: function(node) { | ||
@@ -128,3 +133,6 @@ return node; | ||
}, | ||
childrenOnly: false | ||
childrenOnly: false, | ||
skipFromChildren: function(fromEl) { | ||
return false; | ||
} | ||
}); | ||
@@ -204,2 +212,3 @@ ``` | ||
- __[Phoenix Live View](https://github.com/phoenixframework/phoenix_live_view)__ (`v0.0.1+`) - Rich, real-time user experiences with server-rendered HTML | ||
- __[TS LiveView](https://github.com/beenotung/ts-liveview)__ (`v0.1.0+`) - Build SSR realtime SPA with Typescript | ||
- __[Omi.js](https://github.com/AlloyTeam/omi)__ (`v1.0.1+`) - Open and modern framework for building user interfaces. | ||
@@ -211,4 +220,5 @@ - __[Marko Widgets](https://github.com/marko-js/marko-widgets)__ (`v5.0.0-beta+`) - Marko Widgets is a high performance and lightweight UI components framework that uses the [Marko templating engine](https://github.com/marko-js/marko) for rendering UI components. You can see how Marko Widgets compares to React in performance by taking a look at the following benchmark: [Marko vs React: Performance Benchmark](https://github.com/patrick-steele-idem/marko-vs-react) | ||
- __[vomit.js](https://github.com/bredele/vomit)__ (`v0.9.19`) - A library that uses the power of ES6 template literals to quickly create DOM elements that you can update and compose with Objects, Arrays, other DOM elements, Functions, Promises and even Streams. All with the ease of a function call. | ||
- __[CableReady](https://github.com/hopsoft/cable_ready)__(`v4.0+`) - Server Rendered SPAs. CableReady provides a standard interface for invoking common client-side DOM operations from the server via ActionCable. | ||
- __[Integrated Haskell Platform](https://github.com/digitallyinduced/ihp)__(`all versions`) - A complete platform for developing server-rendered web applications in Haskell. | ||
_NOTE: If you are using a `morphdom` in your project please send a PR to add your project here_ | ||
@@ -215,0 +225,0 @@ |
@@ -15,3 +15,3 @@ 'use strict'; | ||
if (node) { | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
return (node.getAttribute && node.getAttribute('id')) || node.id; | ||
} | ||
@@ -22,448 +22,453 @@ } | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
return function morphdom(fromNode, toNode, options) { | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} | ||
if (typeof toNode === 'string') { | ||
if (fromNode.nodeName === '#document' || fromNode.nodeName === 'HTML' || fromNode.nodeName === 'BODY') { | ||
var toNodeHtml = toNode; | ||
toNode = doc.createElement('html'); | ||
toNode.innerHTML = toNodeHtml; | ||
} else { | ||
toNode = toElement(toNode); | ||
} | ||
} else if (toNode.nodeType === DOCUMENT_FRAGMENT_NODE) { | ||
toNode = toNode.firstElementChild; | ||
} | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var childrenOnly = options.childrenOnly === true; | ||
var getNodeKey = options.getNodeKey || defaultGetNodeKey; | ||
var onBeforeNodeAdded = options.onBeforeNodeAdded || noop; | ||
var onNodeAdded = options.onNodeAdded || noop; | ||
var onBeforeElUpdated = options.onBeforeElUpdated || noop; | ||
var onElUpdated = options.onElUpdated || noop; | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var onNodeDiscarded = options.onNodeDiscarded || noop; | ||
var onBeforeElChildrenUpdated = options.onBeforeElChildrenUpdated || noop; | ||
var skipFromChildren = options.skipFromChildren || noop; | ||
var addChild = options.addChild || function(parent, child){ return parent.appendChild(child); }; | ||
var childrenOnly = options.childrenOnly === true; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
// This object is used as a lookup to quickly find all keyed elements in the original DOM tree. | ||
var fromNodesLookup = Object.create(null); | ||
var keyedRemovalList = []; | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function addKeyedRemoval(key) { | ||
keyedRemovalList.push(key); | ||
} | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
function walkDiscardedChildNodes(node, skipKeyedNodes) { | ||
if (node.nodeType === ELEMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = undefined; | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
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, skipKeyedNodes); | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
/** | ||
* 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; | ||
} | ||
/** | ||
* 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; | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
onNodeDiscarded(node); | ||
walkDiscardedChildNodes(node, skipKeyedNodes); | ||
} | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // TreeWalker implementation is no faster, but keeping this around in case this changes in the future | ||
// function indexTree(root) { | ||
// var treeWalker = document.createTreeWalker( | ||
// root, | ||
// NodeFilter.SHOW_ELEMENT); | ||
// | ||
// var el; | ||
// while((el = treeWalker.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
// // NodeIterator implementation is no faster, but keeping this around in case this changes in the future | ||
// | ||
// function indexTree(node) { | ||
// var nodeIterator = document.createNodeIterator(node, NodeFilter.SHOW_ELEMENT); | ||
// var el; | ||
// while((el = nodeIterator.nextNode())) { | ||
// var key = getNodeKey(el); | ||
// if (key) { | ||
// fromNodesLookup[key] = el; | ||
// } | ||
// } | ||
// } | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
function indexTree(node) { | ||
if (node.nodeType === ELEMENT_NODE || node.nodeType === DOCUMENT_FRAGMENT_NODE) { | ||
var curChild = node.firstChild; | ||
while (curChild) { | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
fromNodesLookup[key] = curChild; | ||
} | ||
// Walk recursively | ||
indexTree(curChild); | ||
// Walk recursively | ||
indexTree(curChild); | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
curChild = curChild.nextSibling; | ||
} | ||
} | ||
} | ||
indexTree(fromNode); | ||
indexTree(fromNode); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
function handleNodeAdded(el) { | ||
onNodeAdded(el); | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var curChild = el.firstChild; | ||
while (curChild) { | ||
var nextSibling = curChild.nextSibling; | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
curChild = nextSibling; | ||
} | ||
var key = getNodeKey(curChild); | ||
if (key) { | ||
var unmatchedFromEl = fromNodesLookup[key]; | ||
// if we find a duplicate #id node in cache, replace `el` with cache value | ||
// and morph it to the child node. | ||
if (unmatchedFromEl && compareNodeNames(curChild, unmatchedFromEl)) { | ||
curChild.parentNode.replaceChild(unmatchedFromEl, curChild); | ||
morphEl(unmatchedFromEl, curChild); | ||
} else { | ||
handleNodeAdded(curChild); | ||
} | ||
} else { | ||
// recursively call for curChild and it's children to see if we find something in | ||
// fromNodesLookup | ||
handleNodeAdded(curChild); | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
curChild = nextSibling; | ||
} | ||
} | ||
function cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey) { | ||
// We have processed all of the "to nodes". If curFromNodeChild is | ||
// non-null then we still have some from nodes left over that need | ||
// to be removed | ||
while (curFromNodeChild) { | ||
var fromNextSibling = curFromNodeChild.nextSibling; | ||
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; | ||
} | ||
} | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
function morphEl(fromEl, toEl, childrenOnly) { | ||
var toElKey = getNodeKey(toEl); | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (toElKey) { | ||
// If an element with an ID is being morphed then it will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete fromNodesLookup[toElKey]; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
if (!childrenOnly) { | ||
// optional | ||
if (onBeforeElUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
// update attributes on original DOM element first | ||
morphAttrs(fromEl, toEl); | ||
// optional | ||
onElUpdated(fromEl); | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
if (onBeforeElChildrenUpdated(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
function morphChildren(fromEl, toEl) { | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
if (fromEl.nodeName !== 'TEXTAREA') { | ||
morphChildren(fromEl, toEl); | ||
} else { | ||
specialElHandlers.TEXTAREA(fromEl, toEl); | ||
} | ||
} | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
function morphChildren(fromEl, toEl) { | ||
var skipFrom = skipFromChildren(fromEl); | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeKey; | ||
var curFromNodeKey; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
var fromNextSibling; | ||
var toNextSibling; | ||
var matchingFromEl; | ||
// walk the fromNode children all the way through | ||
while (curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
// walk the children | ||
outer: while (curToNodeChild) { | ||
toNextSibling = curToNodeChild.nextSibling; | ||
curToNodeKey = getNodeKey(curToNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// walk the fromNode children all the way through | ||
while (!skipFrom && curFromNodeChild) { | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
if (curToNodeChild.isSameNode && curToNodeChild.isSameNode(curFromNodeChild)) { | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
curFromNodeKey = getNodeKey(curFromNodeChild); | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
var curFromNodeType = curFromNodeChild.nodeType; | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// this means if the curFromNodeChild doesnt have a match with the curToNodeChild | ||
var isCompatible = undefined; | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
if (curFromNodeType === curToNodeChild.nodeType) { | ||
if (curFromNodeType === ELEMENT_NODE) { | ||
// Both nodes being compared are Element nodes | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
if (curToNodeKey) { | ||
// The target node has a key so we want to match it up with the correct element | ||
// in the original DOM tree | ||
if (curToNodeKey !== curFromNodeKey) { | ||
// The current element in the original DOM tree does not have a matching key so | ||
// let's check our lookup to see if there is a matching element in the original | ||
// DOM tree | ||
if ((matchingFromEl = fromNodesLookup[curToNodeKey])) { | ||
if (fromNextSibling === matchingFromEl) { | ||
// Special case for single element removals. To avoid removing the original | ||
// DOM node out of the tree (since that can break CSS transitions, etc.), | ||
// we will instead discard the current node and wait until the next | ||
// iteration to properly match up the keyed target element with its matching | ||
// element in the original tree | ||
isCompatible = false; | ||
} else { | ||
// We found a matching keyed element somewhere in the original DOM tree. | ||
// Let's move the original DOM node into the current position and morph | ||
// it. | ||
// fromNextSibling = curFromNodeChild.nextSibling; | ||
// NOTE: We use insertBefore instead of replaceChild because we want to go through | ||
// the `removeNode()` function for the node that is being discarded so that | ||
// all lifecycle hooks are correctly invoked | ||
fromEl.insertBefore(matchingFromEl, curFromNodeChild); | ||
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; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
} | ||
} | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
if (curFromNodeKey) { | ||
// Since the node is keyed it might be matched up later so we defer | ||
// the actual removal to later | ||
addKeyedRemoval(curFromNodeKey); | ||
} else { | ||
} 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; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. Therefore, we will just append the current "to" node | ||
// to the end | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
fromEl.appendChild(matchingFromEl); | ||
// MORPH | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
fromEl.appendChild(curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
curFromNodeChild = matchingFromEl; | ||
} | ||
} else { | ||
// The nodes are not compatible since the "to" node has a key and there | ||
// is no matching keyed node in the source tree | ||
isCompatible = false; | ||
} | ||
} | ||
} else if (curFromNodeKey) { | ||
// The original has a key | ||
isCompatible = false; | ||
} | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
isCompatible = isCompatible !== false && compareNodeNames(curFromNodeChild, curToNodeChild); | ||
if (isCompatible) { | ||
// We found compatible DOM elements so transform | ||
// the current "from" node to match the current | ||
// target DOM node. | ||
// MORPH | ||
morphEl(curFromNodeChild, curToNodeChild); | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
} else if (curFromNodeType === TEXT_NODE || curFromNodeType == COMMENT_NODE) { | ||
// Both nodes being compared are Text or Comment nodes | ||
isCompatible = true; | ||
// Simply update nodeValue on the original node to | ||
// change the text value | ||
if (curFromNodeChild.nodeValue !== curToNodeChild.nodeValue) { | ||
curFromNodeChild.nodeValue = curToNodeChild.nodeValue; | ||
} | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
} | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (isCompatible) { | ||
// Advance both the "to" child and the "from" child since we found a match | ||
// Nothing else to do as we already recursively called morphChildren above | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
continue outer; | ||
} | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
// No compatible match so remove the old node from the DOM and continue trying to find a | ||
// match in the original DOM. However, we only do this if the from node is not keyed | ||
// since it is possible that a keyed node might match up with a node somewhere else in the | ||
// 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) { | ||
// 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 */); | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
curFromNodeChild = fromNextSibling; | ||
} // END: while(curFromNodeChild) {} | ||
// If we got this far then we did not find a candidate match for | ||
// our "to node" and we exhausted all of the children "from" | ||
// nodes. | ||
if (curToNodeKey && (matchingFromEl = fromNodesLookup[curToNodeKey]) && compareNodeNames(matchingFromEl, curToNodeChild)) { | ||
// MORPH | ||
addChild(fromEl, matchingFromEl); | ||
morphEl(matchingFromEl, curToNodeChild); | ||
} else { | ||
var onBeforeNodeAddedResult = onBeforeNodeAdded(curToNodeChild); | ||
if (onBeforeNodeAddedResult !== false) { | ||
if (onBeforeNodeAddedResult) { | ||
curToNodeChild = onBeforeNodeAddedResult; | ||
} | ||
if (curToNodeChild.actualize) { | ||
curToNodeChild = curToNodeChild.actualize(fromEl.ownerDocument || doc); | ||
} | ||
addChild(fromEl, curToNodeChild); | ||
handleNodeAdded(curToNodeChild); | ||
} | ||
} | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
curToNodeChild = toNextSibling; | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
cleanupFromEl(fromEl, curFromNodeChild, curFromNodeKey); | ||
var specialElHandler = specialElHandlers[fromEl.nodeName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} // END: morphChildren(...) | ||
var morphedNode = fromNode; | ||
var morphedNodeType = morphedNode.nodeType; | ||
var toNodeType = toNode.nodeType; | ||
if (!childrenOnly) { | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === ELEMENT_NODE) { | ||
if (toNodeType === ELEMENT_NODE) { | ||
if (!compareNodeNames(fromNode, toNode)) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, createElementNS(toNode.nodeName, toNode.namespaceURI)); | ||
} | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else if (morphedNodeType === TEXT_NODE || morphedNodeType === COMMENT_NODE) { // Text or comment node | ||
if (toNodeType === morphedNodeType) { | ||
if (morphedNode.nodeValue !== toNode.nodeValue) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
} | ||
} | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
if (morphedNode === toNode) { | ||
// The "to node" was not compatible with the "from node" so we had to | ||
// toss out the "from node" and use the "to node" | ||
onNodeDiscarded(fromNode); | ||
} else { | ||
if (toNode.isSameNode && toNode.isSameNode(morphedNode)) { | ||
return; | ||
} | ||
morphEl(morphedNode, toNode, childrenOnly); | ||
// 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); | ||
} | ||
} | ||
} | ||
} | ||
return morphedNode; | ||
}; | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
if (morphedNode.actualize) { | ||
morphedNode = morphedNode.actualize(fromNode.ownerDocument || doc); | ||
} | ||
// If we had to swap out the from node with a new node because the old | ||
// node was not compatible with the target node then we need to | ||
// replace the old DOM node in the original DOM tree. This is only | ||
// possible if the original DOM node was part of a DOM tree which | ||
// we know is the case if it has a parent node. | ||
fromNode.parentNode.replaceChild(morphedNode, fromNode); | ||
} | ||
return morphedNode; | ||
}; | ||
} | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3265
259
195588
1