From 0354a4e25138dd268dd3fad900a4666844379639 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sun, 6 Jul 2014 22:36:49 -0400 Subject: [PATCH] fix diff when changing from element to empty array --- mithril.js | 10 ++++------ tests/mithril-tests.js | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mithril.js b/mithril.js index a77af48f..0c063d2b 100644 --- a/mithril.js +++ b/mithril.js @@ -104,18 +104,16 @@ Mithril = m = new function app(window) { var item = build(parentElement, parentTag, cached, index, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, editable, namespace, configs) if (item === undefined) continue if (!item.nodes.intact) intact = false - subArrayCount += item instanceof Array ? item.length : 1 + var isArray = item instanceof Array + subArrayCount += isArray ? item.length : 1 cached[cacheCount++] = item } if (!intact) { for (var i = 0; i < data.length; i++) { if (cached[i] !== undefined) nodes = nodes.concat(cached[i].nodes) } - for (var i = nodes.length, node; node = cached.nodes[i]; i++) { - if (node.parentNode !== null && node.parentNode.childNodes.length != nodes.length) { - node.parentNode.removeChild(node) - if (cached[i]) unload(cached[i]) - } + for (var i = 0, node; node = cached.nodes[i]; i++) { + if (node.parentNode !== null && nodes.indexOf(node) < 0) node.parentNode.removeChild(node) } for (var i = cached.nodes.length, node; node = nodes[i]; i++) { if (node.parentNode === null) parentElement.appendChild(node) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 1e0efded..c2b00dad 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -660,6 +660,13 @@ function testMithril(mock) { m.render(root, m("main", {config: configParent}, m("b", {config: configChild}))) return unloadedParent === 1 && unloadedChild === 1 }) + test(function() { + //https://github.com/lhorie/mithril.js/issues/150 + var root = mock.document.createElement("div") + m.render(root, [m("a"), m("div")]) + m.render(root, [[], m("div")]) + return root.childNodes.length == 1 && root.childNodes[0].nodeName == "DIV" + }) //end m.render //m.redraw