fixing null reference when passing undefined to virtual dom tree
This commit is contained in:
parent
960e9f2536
commit
a7a9447cd6
8 changed files with 23 additions and 7 deletions
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -182,7 +182,7 @@ m("a[href='/dashboard']", {config: m.route}, "Dashboard&q
|
|||
|
||||
where:
|
||||
VirtualElement :: Object { String tag, Attributes attributes, Children children }
|
||||
Attributes :: Object<any | void config(DOMElement element, Boolean isNew)>
|
||||
Attributes :: Object<any | void config(DOMElement element, Boolean isInitialized)>
|
||||
Children :: String text | Array<String text | VirtualElement virtualElement | Children children></code></pre>
|
||||
<ul>
|
||||
<li><p><strong>String selector</strong></p>
|
||||
|
|
@ -216,7 +216,7 @@ where:
|
|||
<p><code>{ style: {border: "1px solid red"} }</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) {
|
||||
|
|
|
|||
2
archive/v0.1.1/mithril.min.js
vendored
2
archive/v0.1.1/mithril.min.js
vendored
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.
|
|
@ -195,7 +195,7 @@ m("a[href='/dashboard/alicesmith']", {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><a></code> with an <code>href</code> attribute that points to a route</p>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue