fix node index displacement by null/undefined nodes
This commit is contained in:
parent
f589a33842
commit
a425bbca88
10 changed files with 56 additions and 20 deletions
|
|
@ -69,6 +69,11 @@
|
|||
<li>m.prop is now JSON-serializable <a href="https://github.com/lhorie/mithril.js/issues/54">#54</a></li>
|
||||
<li>added <code>extract</code> option to <code>m.request</code> to allow access to response metadata <a href="https://github.com/lhorie/mithril.js/issues/53">#53</a></li>
|
||||
</ul>
|
||||
<h3 id="bug-fixes-">Bug Fixes:</h3>
|
||||
<ul>
|
||||
<li>Fixed node index displacement by null/undefined nodes <a href="https://github.com/lhorie/mithril.js/issues/56">#56</a></li>
|
||||
<li>Fixed mock's insertBefore and appendChild when dealing w/ reattachments</li>
|
||||
</ul>
|
||||
<h3 id="breaking-changes-">Breaking changes:</h3>
|
||||
<ul>
|
||||
<li>changing an id in a virtual element now recreates the element, instead of recycling it <a href="https://github.com/lhorie/mithril.js/issues/55">#55</a></li>
|
||||
|
|
|
|||
|
|
@ -48,18 +48,18 @@ Mithril = m = new function app(window) {
|
|||
|
||||
if (dataType == "[object Array]") {
|
||||
var nodes = [], intact = cached.length === data.length, subArrayCount = 0
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
var item = build(parent, data[i], cached[i], shouldReattach, index + subArrayCount || subArrayCount, namespace)
|
||||
for (var i = 0, cacheCount = 0; i < data.length; i++) {
|
||||
var item = build(parent, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, namespace)
|
||||
if (item === undefined) continue
|
||||
if (!item.nodes.intact) intact = false
|
||||
subArrayCount += item instanceof Array ? item.length : 1
|
||||
cached[i] = item
|
||||
cached[cacheCount++] = item
|
||||
}
|
||||
if (!intact) {
|
||||
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
|
||||
if (data.length < cached.length) cached.length = data.length
|
||||
cached.nodes = nodes
|
||||
}
|
||||
}
|
||||
|
|
@ -498,9 +498,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
|
||||
|
|
@ -540,6 +543,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
|
||||
}
|
||||
|
|
@ -899,6 +904,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
|
||||
|
|
|
|||
|
|
@ -48,18 +48,18 @@ Mithril = m = new function app(window) {
|
|||
|
||||
if (dataType == "[object Array]") {
|
||||
var nodes = [], intact = cached.length === data.length, subArrayCount = 0
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
var item = build(parent, data[i], cached[i], shouldReattach, index + subArrayCount || subArrayCount, namespace)
|
||||
for (var i = 0, cacheCount = 0; i < data.length; i++) {
|
||||
var item = build(parent, data[i], cached[cacheCount], shouldReattach, index + subArrayCount || subArrayCount, namespace)
|
||||
if (item === undefined) continue
|
||||
if (!item.nodes.intact) intact = false
|
||||
subArrayCount += item instanceof Array ? item.length : 1
|
||||
cached[i] = item
|
||||
cached[cacheCount++] = item
|
||||
}
|
||||
if (!intact) {
|
||||
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
|
||||
if (data.length < cached.length) cached.length = data.length
|
||||
cached.nodes = nodes
|
||||
}
|
||||
}
|
||||
|
|
|
|||
2
archive/v0.1.9/mithril.min.js
vendored
2
archive/v0.1.9/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.
Loading…
Add table
Add a link
Reference in a new issue