From 1e829f16572c76a036c37f7318dd9bb0212517cd Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Fri, 11 Jul 2014 22:36:58 -0400 Subject: [PATCH] normalize arrays --- mithril.js | 14 +++++++++++++- tests/mithril-tests.js | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mithril.js b/mithril.js index 6053e545..9a8be835 100644 --- a/mithril.js +++ b/mithril.js @@ -20,7 +20,19 @@ Mithril = m = new function app(window) { } if (classes.length > 0) cell.attrs[classAttrName] = classes.join(" ") - cell.children = hasAttrs ? args[2] : args[1] + var children = hasAttrs ? args[2] : args[1] + if (children instanceof Array) { + cell.children = [] + for (var i = 0; i < children.length; i++) { + var child = children[i] + if (child instanceof Array) children.push.apply(cell.children, child) + else cell.children.push(child) + } + } + else { + cell.children = children + } + for (var attrName in attrs) { if (attrName == classAttrName) cell.attrs[attrName] = (cell.attrs[attrName] || "") + " " + attrs[attrName] else cell.attrs[attrName] = attrs[attrName] diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 316b0c03..4f6bd6a6 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -667,6 +667,17 @@ function testMithril(mock) { m.render(root, [[], m("div")]) return root.childNodes.length == 1 && root.childNodes[0].nodeName == "DIV" }) + test(function() { + //https://github.com/lhorie/mithril.js/issues/156 + var root = mock.document.createElement("div") + m.render(root, m("div", [ + ["a", "b", "c", "d"].map(function() { + return [m("div"), " "] + }), + m("span") + ])) + return root.childNodes[0].childNodes[8].nodeName == "SPAN" + }) //end m.render //m.redraw