From f3f8f45398a34a493fb8d08dfdedfcff1e9adbdf Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 23 Jan 2017 20:50:03 +0100 Subject: [PATCH] first part of implementation --- index.js | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index aa3ef09..8befb43 100644 --- a/index.js +++ b/index.js @@ -55,25 +55,38 @@ function updateChildren (newNode, oldNode) { var oldLength = oldNode.childNodes.length var length = Math.max(oldLength, newLength) - var iNew = 0 - var iOld = 0 - for (var i = 0; i < length; i++, iNew++, iOld++) { + for (var i = 0, iNew = 0, iOld = 0; i < length; i++, iNew++, iOld++) { var newChildNode = newNode.childNodes[iNew] var oldChildNode = oldNode.childNodes[iOld] - var retChildNode = walk(newChildNode, oldChildNode) - if (!retChildNode) { - if (oldChildNode) { - oldNode.removeChild(oldChildNode) - iOld-- - } - } else if (!oldChildNode) { - if (retChildNode) { - oldNode.appendChild(retChildNode) + + if (newChildNode) { + var newAttrs = newChildNode.attributes + if (newAttrs) var newKey = newAttrs['data-key'] + } + + if (oldChildNode) { + var oldAttrs = oldChildNode.attributes + if (oldAttrs) var oldKey = oldAttrs['data-key'] + } + + if (newKey || oldKey) { + console.log(newKey, oldKey) + } else { + var morphedNode = walk(newChildNode, oldChildNode) + if (!morphedNode) { + if (oldChildNode) { + oldNode.removeChild(oldChildNode) + iOld-- + } + } else if (!oldChildNode) { + if (morphedNode) { + oldNode.appendChild(morphedNode) + iNew-- + } + } else if (morphedNode !== oldChildNode) { + oldNode.replaceChild(morphedNode, oldChildNode) iNew-- } - } else if (retChildNode !== oldChildNode) { - oldNode.replaceChild(retChildNode, oldChildNode) - iNew-- } } }