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() {

View file

@ -182,7 +182,7 @@ m(&quot;a[href=&#39;/dashboard&#39;]&quot;, {config: m.route}, &quot;Dashboard&q
where:
VirtualElement :: Object { String tag, Attributes attributes, Children children }
Attributes :: Object&lt;any | void config(DOMElement element, Boolean isNew)&gt;
Attributes :: Object&lt;any | void config(DOMElement element, Boolean isInitialized)&gt;
Children :: String text | Array&lt;String text | VirtualElement virtualElement | Children children&gt;</code></pre>
<ul>
<li><p><strong>String selector</strong></p>
@ -216,7 +216,7 @@ where:
<p><code>{ style: {border: &quot;1px solid red&quot;} }</code></p>
</li>
<li><h4 id="the-config-attribute">The <code>config</code> attribute</h4>
<p><strong>void config(DOMElement element, Boolean isNew)</strong> (optional)</p>
<p><strong>void config(DOMElement element, Boolean isInitialized)</strong> (optional)</p>
<p>You can define a non-HTML-standard attribute called <code>config</code>. This special parameter allows you to call methods on the DOM element after it gets created.</p>
<p>This is useful, for example, if you declare a <code>canvas</code> element and want to use the Javascript API to draw:</p>
<pre><code class="lang-javascript">function draw(element, isInitialized) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

View file

@ -195,7 +195,7 @@ m(&quot;a[href=&#39;/dashboard/alicesmith&#39;]&quot;, {config: m.route});</code
<hr>
<h4 id="signature">Signature</h4>
<p><a href="how-to-read-signatures.html">How to read signatures</a></p>
<pre><code class="lang-clike">void route(DOMElement element, Boolean isNew)</code></pre>
<pre><code class="lang-clike">void route(DOMElement element, Boolean isInitialized)</code></pre>
<ul>
<li><p><strong>DOMElement element</strong></p>
<p>an anchor element <code>&lt;a&gt;</code> with an <code>href</code> attribute that points to a route</p>

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

View file

@ -19,6 +19,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() {
@ -98,6 +100,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() {