From 987b7bbf5393d10b4dccd27ba67bea58c395ec1f Mon Sep 17 00:00:00 2001 From: Andre Wiggins Date: Wed, 21 Feb 2024 16:05:49 -0800 Subject: [PATCH] Match null placeholders using skewed index If we've skewed our matching before hitting a null placeholder (e.g. we've inserted or removed an unmatched node) then let's pick up matching null placeholders from the skewedIndex. Note I don't think we need to adjust the skew when we find a null placeholder, we treat it as "matching" the current node. --- src/diff/children.js | 9 ++++----- test/browser/fragments.test.js | 2 +- test/browser/render.test.js | 15 ++++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/diff/children.js b/src/diff/children.js index c4faed9cfe..ed08514f4d 100644 --- a/src/diff/children.js +++ b/src/diff/children.js @@ -227,9 +227,11 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) { childVNode = newParentVNode._children[i] = childVNode; } + const skewedIndex = i + skew; + // Handle unmounting null placeholders, i.e. VNode => null in unkeyed children if (childVNode == null) { - oldVNode = oldChildren[i]; + oldVNode = oldChildren[skewedIndex]; if ( oldVNode && oldVNode.key == null && @@ -238,8 +240,6 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) { ) { if (oldVNode._dom == newParentVNode._nextDom) { newParentVNode._nextDom = getDomSibling(oldVNode); - } else { - skew++; } unmount(oldVNode, oldVNode, false); @@ -252,7 +252,7 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) { // to unmount this VNode again seeing `_match==true`. Further, // getDomSibling doesn't know about _match and so would incorrectly // assume DOM nodes in this subtree are mounted and usable. - oldChildren[i] = null; + oldChildren[skewedIndex] = null; remainingOldChildren--; } continue; @@ -261,7 +261,6 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) { childVNode._parent = newParentVNode; childVNode._depth = newParentVNode._depth + 1; - const skewedIndex = i + skew; const matchingIndex = findMatchingIndex( childVNode, oldChildren, diff --git a/test/browser/fragments.test.js b/test/browser/fragments.test.js index 4634a4320e..20fed865e4 100644 --- a/test/browser/fragments.test.js +++ b/test/browser/fragments.test.js @@ -1304,7 +1304,7 @@ describe('Fragment', () => { 'rendering from true to false' ); expectDomLogToBe( - ['
  • 1.remove()', '
  • 2.remove()', '
      043.appendChild(
    1. 4)'], + ['
    2. 1.remove()', '
    3. 2.remove()'], 'rendering from true to false' ); diff --git a/test/browser/render.test.js b/test/browser/render.test.js index 9daf4b6099..075b6ad39f 100644 --- a/test/browser/render.test.js +++ b/test/browser/render.test.js @@ -1360,6 +1360,7 @@ describe('render()', () => { it('should not crash or repeatedly add the same child when replacing a matched vnode with null (mixed dom-types)', () => { const B = () =>
      B
      ; + /** @type {() => void} */ let update; class App extends Component { constructor(props) { @@ -1375,38 +1376,38 @@ describe('render()', () => { return (
      -
      +
      C
      ); } return (
      - + A {null} -
      +
      C
      ); } } render(, scratch); - expect(scratch.innerHTML).to.equal('
      B
      '); + expect(scratch.innerHTML).to.equal('
      B
      C
      '); update(); rerender(); expect(scratch.innerHTML).to.equal( - '
      B
      ' + '
      A
      B
      C
      ' ); update(); rerender(); - expect(scratch.innerHTML).to.equal('
      B
      '); + expect(scratch.innerHTML).to.equal('
      B
      C
      '); update(); rerender(); expect(scratch.innerHTML).to.equal( - '
      B
      ' + '
      A
      B
      C
      ' ); }); });