fixing null reference when passing undefined to virtual dom tree

This commit is contained in:
Leo Horie 2014-03-20 15:36:52 -04:00
parent 960e9f2536
commit a7a9447cd6
8 changed files with 23 additions and 7 deletions

View file

@ -46,11 +46,12 @@ new function(window) {
var nodes = [], intact = cached.length === data.length
for (var i = 0; i < data.length; i++) {
var item = build(parent, data[i], cached[i])
if (item === undefined) continue
if (!item.nodes.intact) intact = false
cached[i] = item
}
if (!intact) {
for (var i = 0; i < data.length; i++) nodes = nodes.concat(cached[i].nodes)
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.removeChild(node)
for (var i = cached.nodes.length, node; node = nodes[i]; i++) if (node.parentNode === null) parent.appendChild(node)
cached.length = data.length
@ -522,6 +523,8 @@ function testMithril(mock) {
test(function() {return m("div", ["a", "b"]).children.length === 2})
test(function() {return m("div", [m("div")]).children[0].tag === "div"})
test(function() {return m("div", m("div")).attrs.tag === "div"}) //yes, this is expected behavior: see method signature
test(function() {return m("div", [undefined]).tag === "div"})
test(function() {return m("div", [{foo: "bar"}])}) //as long as it doesn't throw errors, it's fine
//m.module
test(function() {
@ -601,6 +604,11 @@ function testMithril(mock) {
var elementAfter = root.childNodes[0]
return elementBefore !== elementAfter
})
test(function() {
var root = mock.document.createElement("div")
m.render(root, m("div", [undefined]))
return root.childNodes[0].childNodes.length === 0
})
//m.redraw
test(function() {