Merge pull request #68 from rayd/remove-single-text-nodes

Make sure we remove a single text node when patching it's parent element.
pull/71/head
Simon Friis Vindum 9 years ago
commit 1c0ce08e45

@ -195,9 +195,12 @@ function init(modules) {
if (isDef(oldCh) && isDef(ch)) {
if (oldCh !== ch) updateChildren(elm, oldCh, ch, insertedVnodeQueue);
} else if (isDef(ch)) {
if (isDef(oldVnode.text)) elm.textContent = '';
addVnodes(elm, null, ch, 0, ch.length - 1, insertedVnodeQueue);
} else if (isDef(oldCh)) {
removeVnodes(elm, oldCh, 0, oldCh.length - 1);
} else if (isDef(oldVnode.text)) {
elm.textContent = '';
}
} else if (oldVnode.text !== vnode.text) {
elm.textContent = vnode.text;

@ -448,6 +448,33 @@ describe('snabbdom', function() {
patch(vnode1, vnode2);
assert.deepEqual(map(inner, elm.children), ['One', 'Three']);
});
it('removes a single text node', function() {
var vnode1 = h('div', 'One');
var vnode2 = h('div');
patch(vnode0, vnode1);
assert.equal(elm.textContent, 'One');
patch(vnode1, vnode2);
assert.equal(elm.textContent, '');
});
it('removes a single text node when children are updated', function() {
var vnode1 = h('div', 'One');
var vnode2 = h('div', [ h('div', 'Two'), h('span', 'Three') ]);
patch(vnode0, vnode1);
assert.equal(elm.textContent, 'One');
patch(vnode1, vnode2);
console.log(elm.childNodes);
assert.deepEqual(map(prop('textContent'), elm.childNodes), ['Two', 'Three']);
});
it('removes a text node among other elements', function() {
var vnode1 = h('div', [ 'One', h('span', 'Two') ]);
var vnode2 = h('div', [ h('div', 'Three')]);
patch(vnode0, vnode1);
assert.deepEqual(map(prop('textContent'), elm.childNodes), ['One', 'Two']);
patch(vnode1, vnode2);
assert.equal(elm.childNodes.length, 1);
assert.equal(elm.childNodes[0].tagName, 'DIV');
assert.equal(elm.childNodes[0].textContent, 'Three');
});
it('reorders elements', function() {
var vnode1 = h('div', [h('span', 'One'), h('div', 'Two'), h('b', 'Three')]);
var vnode2 = h('div', [h('b', 'Three'), h('span', 'One'), h('div', 'Two')]);

Loading…
Cancel
Save