fix node index displacement by null/undefined nodes

This commit is contained in:
Leo Horie 2014-04-20 21:48:33 -04:00
parent f589a33842
commit a425bbca88
10 changed files with 56 additions and 20 deletions

View file

@ -313,6 +313,14 @@ function testMithril(mock) {
var elementAfter = root.childNodes[0]
return elementBefore !== elementAfter
})
test(function() {
//https://github.com/lhorie/mithril.js/issues/56
var root = mock.document.createElement("div")
m.render(root, [null, "foo"])
m.render(root, ["bar"])
console.log(root.childNodes)
return root.childNodes.length == 1
})
//end m.render
//m.redraw

View file

@ -12,9 +12,12 @@ mock.window = new function() {
replaceChild: window.document.replaceChild,
insertBefore: function(node, reference) {
node.parentNode = this
var index = this.childNodes.indexOf(reference)
if (index < 0) this.childNodes.push(node)
else this.childNodes.splice(index, 0, node)
var referenceIndex = this.childNodes.indexOf(reference)
if (referenceIndex < 0) this.childNodes.push(node)
else {
var index = this.childNodes.indexOf(node)
this.childNodes.splice(referenceIndex, index < 0 ? 0 : 1, node)
}
},
insertAdjacentHTML: function(position, html) {
//todo: accept markup
@ -54,6 +57,8 @@ mock.window = new function() {
oldChild.parentNode = null
}
window.document.appendChild = function(child) {
var index = this.childNodes.indexOf(child)
if (index > -1) this.childNodes.splice(index, 1)
this.childNodes.push(child)
child.parentNode = this
}