From cbd531c295b1b782113497dcfc2532adbd695b2b Mon Sep 17 00:00:00 2001 From: jdecroock Date: Tue, 18 Jun 2024 16:54:15 +0200 Subject: [PATCH] graciously handle array shuffling --- src/diff/children.js | 24 +++++++++++++++++++----- test/browser/render.test.js | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/diff/children.js b/src/diff/children.js index 912cc72d7c..757eb33e8a 100644 --- a/src/diff/children.js +++ b/src/diff/children.js @@ -302,20 +302,34 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) { childVNode._flags |= INSERT_VNODE; } } else if (matchingIndex !== skewedIndex) { - if (matchingIndex === skewedIndex + 1) { + if (matchingIndex == skewedIndex - 1) { + skew = matchingIndex - skewedIndex; + } else if (matchingIndex == skewedIndex + 1) { skew++; } else if (matchingIndex > skewedIndex) { + // Our matched DOM-node is further in the list of children than + // where it's at now. + + // When the remaining old children is bigger than the new-children + // minus our skewed index we know we are dealing with a shrinking list + // we have to increase our skew with the matchedIndex - the skewed index if (remainingOldChildren > newChildrenLength - skewedIndex) { skew += matchingIndex - skewedIndex; } else { + // If we have matched all the children just decrease the skew skew--; } } else if (matchingIndex < skewedIndex) { - if (matchingIndex == skewedIndex - 1) { - skew = matchingIndex - skewedIndex; + // Our matched DOM-node is further in the negative way in the list of children + // than where it's at now. + + // When the remaining old chiildren is less than the new children + // plus our skewed index we know we are dealing with a growing list + if (remainingOldChildren < newChildrenLength + skewedIndex) { + skew += matchingIndex + skewedIndex; + } else { + skew++; } - } else { - skew = 0; } // Move this VNode's DOM if the original index (matchingIndex) doesn't diff --git a/test/browser/render.test.js b/test/browser/render.test.js index f440962f70..5241cfaf29 100644 --- a/test/browser/render.test.js +++ b/test/browser/render.test.js @@ -474,17 +474,17 @@ describe('render()', () => { it('should support popover auto', () => { render(
, scratch); - expect(scratch.innerHTML).to.equal("
"); + expect(scratch.innerHTML).to.equal('
'); }); it('should support popover true boolean', () => { render(
, scratch); - expect(scratch.innerHTML).to.equal("
"); + expect(scratch.innerHTML).to.equal('
'); }); it('should support popover false boolean', () => { render(
, scratch); - expect(scratch.innerHTML).to.equal("
"); + expect(scratch.innerHTML).to.equal('
'); }); // Test for preactjs/preact#4340 @@ -1625,4 +1625,31 @@ describe('render()', () => { '
One
Six
Seven
' ); }); + + it('shuffle', function () { + const App = ({ items }) => ( +
+ {items.map(key => ( +
{key}
+ ))} +
+ ); + const a = ['0', '1', '2', '3', '4', '5', '6']; + const b = ['1', '3', '5', '2', '6', '4', '0']; + render(, scratch); + expect(scratch.innerHTML).to.equal( + `
${a.map(n => `
${n}
`).join('')}
` + ); + + render(, scratch); + + expect(scratch.innerHTML).to.equal( + `
${b.map(n => `
${n}
`).join('')}
` + ); + + render(, scratch); + expect(scratch.innerHTML).to.equal( + `
${a.map(n => `
${n}
`).join('')}
` + ); + }); });