Comparing version 0.1.14 to 1.0.0
@@ -104,10 +104,10 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.morphdom = f()}})(function(){var define,module,exports;module={exports:(exports={})}; | ||
*/ | ||
function moveChildren(from, to) { | ||
var curChild = from.firstChild; | ||
function moveChildren(fromEl, toEl) { | ||
var curChild = fromEl.firstChild; | ||
while(curChild) { | ||
var nextChild = curChild.nextSibling; | ||
to.appendChild(curChild); | ||
toEl.appendChild(curChild); | ||
curChild = nextChild; | ||
} | ||
return to; | ||
return toEl; | ||
} | ||
@@ -130,2 +130,3 @@ | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var childrenOnly = options.childrenOnly === true; | ||
@@ -191,21 +192,23 @@ function removeNodeHelper(node, nestedInSavedEl) { | ||
function morphEl(fromNode, toNode, alreadyVisited) { | ||
if (toNode.id) { | ||
function morphEl(fromEl, toEl, alreadyVisited, childrenOnly) { | ||
if (toEl.id) { | ||
// If an element with an ID is being morphed then it is will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete savedEls[toNode.id]; | ||
delete savedEls[toEl.id]; | ||
} | ||
if (onBeforeMorphEl(fromNode, toNode) === false) { | ||
return; | ||
} | ||
if (!childrenOnly) { | ||
if (onBeforeMorphEl(fromEl, toEl) === false) { | ||
return; | ||
} | ||
morphAttrs(fromNode, toNode); | ||
morphAttrs(fromEl, toEl); | ||
if (onBeforeMorphElChildren(fromNode, toNode) === false) { | ||
return; | ||
if (onBeforeMorphElChildren(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
var curToNodeChild = toNode.firstChild; | ||
var curFromNodeChild = fromNode.firstChild; | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeId; | ||
@@ -275,3 +278,3 @@ | ||
// to find a match in the original DOM | ||
removeNode(curFromNodeChild, fromNode, alreadyVisited); | ||
removeNode(curFromNodeChild, fromEl, alreadyVisited); | ||
curFromNodeChild = fromNextSibling; | ||
@@ -298,3 +301,3 @@ } | ||
// append the current "to node" to the end | ||
fromNode.appendChild(curToNodeChild); | ||
fromEl.appendChild(curToNodeChild); | ||
@@ -309,11 +312,11 @@ curToNodeChild = toNextSibling; | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
removeNode(curFromNodeChild, fromNode, alreadyVisited); | ||
removeNode(curFromNodeChild, fromEl, alreadyVisited); | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
var specialElHandler = specialElHandlers[fromNode.tagName]; | ||
var specialElHandler = specialElHandlers[fromEl.tagName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromNode, toNode); | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} | ||
} // END: morphEl(...) | ||
@@ -324,38 +327,45 @@ var morphedNode = fromNode; | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === 1) { | ||
if (toNodeType === 1) { | ||
if (morphedNode.tagName !== toNode.tagName) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(morphedNode, document.createElement(toNode.tagName)); | ||
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 === 1) { | ||
if (toNodeType === 1) { | ||
if (fromNode.tagName !== toNode.tagName) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, document.createElement(toNode.tagName)); | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
return toNode; | ||
} else if (morphedNodeType === 3) { // Text node | ||
if (toNodeType === 3) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
} | ||
} else if (morphedNodeType === 3) { // Text node | ||
if (toNodeType === 3) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
return morphedNode; | ||
} else { | ||
onNodeDiscarded(fromNode); | ||
// Text node to something else | ||
return toNode; | ||
} | ||
} | ||
morphEl(morphedNode, toNode, false); | ||
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 { | ||
morphEl(morphedNode, toNode, false, childrenOnly); | ||
// Fire the "onNodeDiscarded" event for any saved elements | ||
// that never found a new home in the morphed DOM | ||
for (var savedElId in savedEls) { | ||
if (savedEls.hasOwnProperty(savedElId)) { | ||
var savedEl = savedEls[savedElId]; | ||
onNodeDiscarded(savedEl); | ||
walkDiscardedChildNodes(savedEl); | ||
// Fire the "onNodeDiscarded" event for any saved elements | ||
// that never found a new home in the morphed DOM | ||
for (var savedElId in savedEls) { | ||
if (savedEls.hasOwnProperty(savedElId)) { | ||
var savedEl = savedEls[savedElId]; | ||
onNodeDiscarded(savedEl); | ||
walkDiscardedChildNodes(savedEl); | ||
} | ||
} | ||
} | ||
if (morphedNode !== fromNode && fromNode.parentNode) { | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
// If we had to swap out the from node with a new node because the old | ||
@@ -362,0 +372,0 @@ // node was not compatible with the target node then we need to |
108
lib/index.js
@@ -103,10 +103,10 @@ // Create a range object for efficently rendering strings to elements. | ||
*/ | ||
function moveChildren(from, to) { | ||
var curChild = from.firstChild; | ||
function moveChildren(fromEl, toEl) { | ||
var curChild = fromEl.firstChild; | ||
while(curChild) { | ||
var nextChild = curChild.nextSibling; | ||
to.appendChild(curChild); | ||
toEl.appendChild(curChild); | ||
curChild = nextChild; | ||
} | ||
return to; | ||
return toEl; | ||
} | ||
@@ -129,2 +129,3 @@ | ||
var onBeforeNodeDiscarded = options.onBeforeNodeDiscarded || noop; | ||
var childrenOnly = options.childrenOnly === true; | ||
@@ -190,21 +191,23 @@ function removeNodeHelper(node, nestedInSavedEl) { | ||
function morphEl(fromNode, toNode, alreadyVisited) { | ||
if (toNode.id) { | ||
function morphEl(fromEl, toEl, alreadyVisited, childrenOnly) { | ||
if (toEl.id) { | ||
// If an element with an ID is being morphed then it is will be in the final | ||
// DOM so clear it out of the saved elements collection | ||
delete savedEls[toNode.id]; | ||
delete savedEls[toEl.id]; | ||
} | ||
if (onBeforeMorphEl(fromNode, toNode) === false) { | ||
return; | ||
} | ||
if (!childrenOnly) { | ||
if (onBeforeMorphEl(fromEl, toEl) === false) { | ||
return; | ||
} | ||
morphAttrs(fromNode, toNode); | ||
morphAttrs(fromEl, toEl); | ||
if (onBeforeMorphElChildren(fromNode, toNode) === false) { | ||
return; | ||
if (onBeforeMorphElChildren(fromEl, toEl) === false) { | ||
return; | ||
} | ||
} | ||
var curToNodeChild = toNode.firstChild; | ||
var curFromNodeChild = fromNode.firstChild; | ||
var curToNodeChild = toEl.firstChild; | ||
var curFromNodeChild = fromEl.firstChild; | ||
var curToNodeId; | ||
@@ -274,3 +277,3 @@ | ||
// to find a match in the original DOM | ||
removeNode(curFromNodeChild, fromNode, alreadyVisited); | ||
removeNode(curFromNodeChild, fromEl, alreadyVisited); | ||
curFromNodeChild = fromNextSibling; | ||
@@ -297,3 +300,3 @@ } | ||
// append the current "to node" to the end | ||
fromNode.appendChild(curToNodeChild); | ||
fromEl.appendChild(curToNodeChild); | ||
@@ -308,11 +311,11 @@ curToNodeChild = toNextSibling; | ||
fromNextSibling = curFromNodeChild.nextSibling; | ||
removeNode(curFromNodeChild, fromNode, alreadyVisited); | ||
removeNode(curFromNodeChild, fromEl, alreadyVisited); | ||
curFromNodeChild = fromNextSibling; | ||
} | ||
var specialElHandler = specialElHandlers[fromNode.tagName]; | ||
var specialElHandler = specialElHandlers[fromEl.tagName]; | ||
if (specialElHandler) { | ||
specialElHandler(fromNode, toNode); | ||
specialElHandler(fromEl, toEl); | ||
} | ||
} | ||
} // END: morphEl(...) | ||
@@ -323,38 +326,45 @@ var morphedNode = fromNode; | ||
// Handle the case where we are given two DOM nodes that are not | ||
// compatible (e.g. <div> --> <span> or <div> --> TEXT) | ||
if (morphedNodeType === 1) { | ||
if (toNodeType === 1) { | ||
if (morphedNode.tagName !== toNode.tagName) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(morphedNode, document.createElement(toNode.tagName)); | ||
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 === 1) { | ||
if (toNodeType === 1) { | ||
if (fromNode.tagName !== toNode.tagName) { | ||
onNodeDiscarded(fromNode); | ||
morphedNode = moveChildren(fromNode, document.createElement(toNode.tagName)); | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
morphedNode = toNode; | ||
} | ||
} else { | ||
// Going from an element node to a text node | ||
return toNode; | ||
} else if (morphedNodeType === 3) { // Text node | ||
if (toNodeType === 3) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
return morphedNode; | ||
} else { | ||
// Text node to something else | ||
morphedNode = toNode; | ||
} | ||
} | ||
} else if (morphedNodeType === 3) { // Text node | ||
if (toNodeType === 3) { | ||
morphedNode.nodeValue = toNode.nodeValue; | ||
return morphedNode; | ||
} else { | ||
onNodeDiscarded(fromNode); | ||
// Text node to something else | ||
return toNode; | ||
} | ||
} | ||
morphEl(morphedNode, toNode, false); | ||
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 { | ||
morphEl(morphedNode, toNode, false, childrenOnly); | ||
// Fire the "onNodeDiscarded" event for any saved elements | ||
// that never found a new home in the morphed DOM | ||
for (var savedElId in savedEls) { | ||
if (savedEls.hasOwnProperty(savedElId)) { | ||
var savedEl = savedEls[savedElId]; | ||
onNodeDiscarded(savedEl); | ||
walkDiscardedChildNodes(savedEl); | ||
// Fire the "onNodeDiscarded" event for any saved elements | ||
// that never found a new home in the morphed DOM | ||
for (var savedElId in savedEls) { | ||
if (savedEls.hasOwnProperty(savedElId)) { | ||
var savedEl = savedEls[savedElId]; | ||
onNodeDiscarded(savedEl); | ||
walkDiscardedChildNodes(savedEl); | ||
} | ||
} | ||
} | ||
if (morphedNode !== fromNode && fromNode.parentNode) { | ||
if (!childrenOnly && morphedNode !== fromNode && fromNode.parentNode) { | ||
// If we had to swap out the from node with a new node because the old | ||
@@ -361,0 +371,0 @@ // node was not compatible with the target node then we need to |
@@ -38,3 +38,3 @@ { | ||
"dependencies": {}, | ||
"version": "0.1.14" | ||
"version": "1.0.0" | ||
} |
@@ -75,2 +75,3 @@ morphdom | ||
- **onBeforeMorphElChildren** (`Function(fromEl, toEl)`) - A function that will called when the children of an `HTMLElement` in the `from` tree are about to be morphed. If the listener function returns `false` then the child nodes will be skipped. | ||
- **childrenOnly** (`Boolean`) - If `true` then only the children of the `fromEl` and `toEl` nodes will be morphed (the containing element will be skipped). Defaults to `false`. | ||
@@ -82,3 +83,3 @@ ```javascript | ||
return true; | ||
} | ||
}, | ||
onNodeDiscarded: function(node) { | ||
@@ -92,3 +93,4 @@ | ||
return true; | ||
} | ||
}, | ||
childrenOnly: false | ||
}); | ||
@@ -95,0 +97,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
13083841
613
646
1
475
0