fix(jsx/dom): fix dom rendering bugs for fragment. (#3569)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent ea3d799 · Browse files · 2 files·+54 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+54 added−9 removed
- tests:+40 added−0 removed
- other:+14 added−9 removed
Provenance
- Commit
3164326- Tree
bdf0eca- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
ea3d799
2 files changed+54 added−9 removed
modified:src/jsx/dom/index.test.tsx+40 added
1117 unmodified lines1118 '<div><p>Count: 5</p><button>+</button><div><p>Child Count: 6</p><button>+</button></div></div>'1119 )1120 })1121 1122+ it('consecutive fragment', async () => {1123+ const ComponentA = () => {1124+ const [count, setCount] = useState(0)1125+ return (1126+ <>1127+ <div>A: {count}</div>1128+ <button id='a-button' onClick={() => setCount(count + 1)}>1129+ A: +1130+ </button>1131+ </>1132+ )1133+ }1134+ const App = () => {1135+ const [count, setCount] = useState(0)1136+ return (1137+ <>1138+ <ComponentA />1139+ <div>B: {count}</div>1140+ <button id='b-button' onClick={() => setCount(count + 1)}>1141+ B: +1142+ </button>1143+ </>1144+ )1145+ }1146+ render(<App />, root)1147+ expect(root.innerHTML).toBe(1148+ '<div>A: 0</div><button id="a-button">A: +</button><div>B: 0</div><button id="b-button">B: +</button>'1149+ )1150+ root.querySelector<HTMLButtonElement>('#b-button')?.click()1151+ await Promise.resolve()1152+ expect(root.innerHTML).toBe(1153+ '<div>A: 0</div><button id="a-button">A: +</button><div>B: 1</div><button id="b-button">B: +</button>'1154+ )1155+ root.querySelector<HTMLButtonElement>('#a-button')?.click()1156+ await Promise.resolve()1157+ expect(root.innerHTML).toBe(1158+ '<div>A: 1</div><button id="a-button">A: +</button><div>B: 1</div><button id="b-button">B: +</button>'1159+ )1160+ })1161+1162 it('switch child component', async () => {1163 const Even = () => <p>Even</p>1164 const Odd = () => <div>Odd</div>1165 const Counter = () => {1312 unmodified linesmodified:src/jsx/dom/render.ts+14 added-9 removed
366 unmodified lines367 remove.forEach(removeNode)368 369 const childNodes = (isNew ? undefined : container.childNodes) as NodeListOf<ChildNode>370 let offset: number371+ let insertBeforeNode: ChildNode | null = null372 if (isNew) {373 offset = -1374+ } else if (!childNodes.length) {375+ offset = 0376 } else {374- offset =375- (childNodes.length &&376- (findChildNodeIndex(childNodes, findInsertBefore(node.nN)) ??377- findChildNodeIndex(378- childNodes,379- next.find((n) => n.tag !== HONO_PORTAL_ELEMENT && n.e)?.e380- ))) ??381- -1377+ const offsetByNextNode = findChildNodeIndex(childNodes, findInsertBefore(node.nN))378+ if (offsetByNextNode !== undefined) {379+ insertBeforeNode = childNodes[offsetByNextNode]380+ offset = offsetByNextNode381+ } else {382+ offset =383+ findChildNodeIndex(childNodes, next.find((n) => n.tag !== HONO_PORTAL_ELEMENT && n.e)?.e) ??384+ -1385+ }386+387 if (offset === -1) {388 isNew = true389 }390 }31 unmodified lines422 if (childNodes[offset + 1] === el) {423 // Move extra elements to the back of the container. This is to be done efficiently when elements are swapped.424 container.appendChild(childNodes[offset])425 } else {421- container.insertBefore(el, childNodes[offset] || null)426+ container.insertBefore(el, insertBeforeNode || childNodes[offset] || null)427 }428 }429 }430 if (node.pP) {352 unmodified lines