diff --git a/src/diff/children.js b/src/diff/children.js index 383b95cfdb..a6bc9e20d7 100644 --- a/src/diff/children.js +++ b/src/diff/children.js @@ -213,7 +213,7 @@ export function diffChildren( // If the newParentVNode.__nextDom points to a dom node that is about to // be unmounted, then get the next sibling of that vnode and set // _nextDom to it - newParentVNode._nextDom = newParentVNode._nextDom.nextSibling; + newParentVNode._nextDom = getLastDom(oldParentVNode).nextSibling; } unmount(oldChildren[i], oldChildren[i]); @@ -326,3 +326,26 @@ function placeChild( return oldDom; } + +/** + * @param {import('../internal').VNode} vnode + */ +function getLastDom(vnode) { + if (vnode.type == null || typeof vnode.type === 'string') { + return vnode._dom; + } + + if (vnode._children) { + for (let i = vnode._children.length - 1; i >= 0; i--) { + let child = vnode._children[i]; + if (child) { + let lastDom = getLastDom(child); + if (lastDom) { + return lastDom; + } + } + } + } + + return null; +} diff --git a/test/browser/fragments.test.js b/test/browser/fragments.test.js index 2dbbee6aab..e6df094d73 100644 --- a/test/browser/fragments.test.js +++ b/test/browser/fragments.test.js @@ -2700,12 +2700,7 @@ describe('Fragment', () => { render(, scratch); expect(scratch.innerHTML).to.equal(div([div(1), div('A'), div('B')])); - expectDomLogToBe([ - '
2.remove()', - '
3.remove()', - '
1AB.appendChild(
A)', - '
1BA.appendChild(
B)' - ]); + expectDomLogToBe(['
2.remove()', '
3.remove()']); }); it('should efficiently place new children and unmount nested Fragment children', () => { @@ -2754,9 +2749,7 @@ describe('Fragment', () => { '
.appendChild(#text)', '
123AB.insertBefore(
4,
2)', '
2.remove()', - '
3.remove()', - '
14AB.appendChild(
A)', - '
14BA.appendChild(
B)' + '
3.remove()' ]); });