diff --git a/mithril.js b/mithril.js index 1946fd77..569b3c70 100644 --- a/mithril.js +++ b/mithril.js @@ -118,7 +118,7 @@ Mithril = m = new function app(window, undefined) { if (!existing[key]) existing[key] = {action: INSERTION, index: i} else existing[key] = {action: MOVE, index: i, from: existing[key].index, element: parentElement.childNodes[existing[key].index]} } - else unkeyed.push({index: i, element: parentElement.childNodes[i]}) + else unkeyed.push({index: i, element: parentElement.childNodes[i] || window.document.createElement("dummy")}) } } var actions = Object.keys(existing).map(function(key) {return existing[key]}) @@ -131,7 +131,7 @@ Mithril = m = new function app(window, undefined) { newCached.splice(change.index, 1) } if (change.action == INSERTION) { - var dummy = window.document.createElement("div") + var dummy = window.document.createElement("dummy2") dummy.key = data[change.index].attrs.key parentElement.insertBefore(dummy, parentElement.childNodes[change.index]) newCached.splice(change.index, 0, {attrs: {key: data[change.index].attrs.key}, nodes: [dummy]}) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 3159b8bb..e70e1bdc 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -584,6 +584,17 @@ function testMithril(mock) { var fifthAfter = root.childNodes[1] return firstBefore === firstAfter && secondBefore === secondAfter && thirdBefore === thirdAfter && fourthBefore === fourthAfter && fifthBefore === fifthAfter }) + test(function() { + //https://github.com/lhorie/mithril.js/issues/246 + //insert at beginning with non-keyed in the middle + var root = mock.document.createElement("div") + m.render(root, [m("a", {key: 1})]) + var firstBefore = root.childNodes[0] + m.render(root, [m("a", {key: 2}), m("br"), m("a", {key: 1})]) + var firstAfter = root.childNodes[2] + console.log(root.childNodes) + return firstBefore == firstAfter && root.childNodes[0].key == 2 && root.childNodes.length == 3 + }) test(function() { //https://github.com/lhorie/mithril.js/issues/134 var root = mock.document.createElement("div") diff --git a/tests/mock.js b/tests/mock.js index fae3574e..660f08f4 100644 --- a/tests/mock.js +++ b/tests/mock.js @@ -15,7 +15,11 @@ mock.window = new function() { insertBefore: function(node, reference) { node.parentNode = this var referenceIndex = this.childNodes.indexOf(reference) - if (referenceIndex < 0) this.childNodes.push(node) + if (referenceIndex < 0) { + var index = this.childNodes.indexOf(node) + if (index > -1) this.childNodes.splice(index, 1) + this.childNodes.push(node) + } else { var index = this.childNodes.indexOf(node) if (index > -1) this.childNodes.splice(index, 1)