fix: determine if rendered or not by `node.vC[0]` instead of referring to `node.pP` (#4663)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix: determine if rendered or not by node.vC[0] instead of referring to node.pP * refactor: assign undefined to "pP" instead of deleting it Because that is slightly more likely to be optimized by the JIT * refactor: use while loop instead of for loop for shorter code
authored by Taku Amano · committed by GitHub · parent 0c1d4c7 · Browse files · 2 files·+157 added−10 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+157 added−10 removed
- tests:+151 added−0 removed
- other:+6 added−10 removed
Provenance
- Commit
3d536ff- Tree
3423271- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
0c1d4c7
2 files changed+157 added−10 removed
modified:src/jsx/dom/index.test.tsx+151 added
307 unmodified lines308 await Promise.resolve()309 expect(root.innerHTML).toBe('<div>2</div><div>1</div><button>+</button>')310 expect(Child).toBeCalledTimes(3)311 })312+313+ it('multiple children', async () => {314+ const Child = ({ name }: { name: string }) => {315+ const [count, setCount] = useState(0)316+ return (317+ <div>318+ <div>319+ {name} {count}320+ </div>321+ <button onClick={() => setCount(count + 1)}>+</button>322+ </div>323+ )324+ }325+ const App = () => {326+ const [count, setCount] = useState(0)327+ return (328+ <div>329+ <div>parent {count}</div>330+ <button onClick={() => setCount(count + 1)}>+</button>331+ <div>332+ <Child name='child 1' />333+ <Child name='child 2' />334+ <Child name='child 3' />335+ </div>336+ </div>337+ )338+ }339+ render(<App />, root)340+ expect(root.innerHTML).toBe(341+ '<div><div>parent 0</div><button>+</button><div><div><div>child 1 0</div><button>+</button></div><div><div>child 2 0</div><button>+</button></div><div><div>child 3 0</div><button>+</button></div></div></div>'342+ )343+ const [parentButton, child1Button, child2Button, child3Button] =344+ root.querySelectorAll('button')345+ parentButton?.click()346+ await Promise.resolve()347+ expect(root.innerHTML).toBe(348+ '<div><div>parent 1</div><button>+</button><div><div><div>child 1 0</div><button>+</button></div><div><div>child 2 0</div><button>+</button></div><div><div>child 3 0</div><button>+</button></div></div></div>'349+ )350+ child2Button?.click()351+ await Promise.resolve()352+ expect(root.innerHTML).toBe(353+ '<div><div>parent 1</div><button>+</button><div><div><div>child 1 0</div><button>+</button></div><div><div>child 2 1</div><button>+</button></div><div><div>child 3 0</div><button>+</button></div></div></div>'354+ )355+ child1Button?.click()356+ await Promise.resolve()357+ expect(root.innerHTML).toBe(358+ '<div><div>parent 1</div><button>+</button><div><div><div>child 1 1</div><button>+</button></div><div><div>child 2 1</div><button>+</button></div><div><div>child 3 0</div><button>+</button></div></div></div>'359+ )360+ child3Button?.click()361+ await Promise.resolve()362+ expect(root.innerHTML).toBe(363+ '<div><div>parent 1</div><button>+</button><div><div><div>child 1 1</div><button>+</button></div><div><div>child 2 1</div><button>+</button></div><div><div>child 3 1</div><button>+</button></div></div></div>'364+ )365+ })366+367+ it('keeps sibling order when a null sibling exists after parent update', async () => {368+ const Empty = () => null369+ const Child = () => {370+ const [count, setCount] = useState(0)371+ return count === 0 ? (372+ <>373+ <div>A0</div>374+ <button id='child' onClick={() => setCount(1)}>375+ child+376+ </button>377+ </>378+ ) : (379+ <>380+ <span>A1</span>381+ <span>A2</span>382+ </>383+ )384+ }385+ const App = () => {386+ const [count, setCount] = useState(0)387+ return (388+ <>389+ <Child />390+ <Empty />391+ <div id='tail'>T{count}</div>392+ <button id='parent' onClick={() => setCount(count + 1)}>393+ parent+394+ </button>395+ </>396+ )397+ }398+ render(<App />, root)399+ expect(root.innerHTML).toBe(400+ '<div>A0</div><button id="child">child+</button><div id="tail">T0</div><button id="parent">parent+</button>'401+ )402+ root.querySelector<HTMLButtonElement>('#parent')?.click()403+ await Promise.resolve()404+ expect(root.innerHTML).toBe(405+ '<div>A0</div><button id="child">child+</button><div id="tail">T1</div><button id="parent">parent+</button>'406+ )407+ root.querySelector<HTMLButtonElement>('#child')?.click()408+ await Promise.resolve()409+ expect(root.innerHTML).toBe(410+ '<span>A1</span><span>A2</span><div id="tail">T1</div><button id="parent">parent+</button>'411+ )412+ })413+414+ it('multiple children with dynamic addition and rerender', async () => {415+ const Child = ({ name }: { name: string }) => {416+ const [count, setCount] = useState(0)417+ return (418+ <div>419+ <div>420+ {name} {count}421+ </div>422+ <button onClick={() => setCount(count + 1)}>+</button>423+ </div>424+ )425+ }426+ const App = () => {427+ const [showThird, setShowThird] = useState(false)428+ return (429+ <div>430+ <Child name='child 1' />431+ <Child name='child 2' />432+ {showThird && <Child name='child 3' />}433+ <button onClick={() => setShowThird(true)}>add</button>434+ </div>435+ )436+ }437+ render(<App />, root)438+ expect(root.innerHTML).toBe(439+ '<div><div><div>child 1 0</div><button>+</button></div><div><div>child 2 0</div><button>+</button></div><button>add</button></div>'440+ )441+ // add child 3442+ let buttons = root.querySelectorAll('button')443+ buttons[2]?.click() // add444+ await Promise.resolve()445+ expect(root.innerHTML).toBe(446+ '<div><div><div>child 1 0</div><button>+</button></div><div><div>child 2 0</div><button>+</button></div><div><div>child 3 0</div><button>+</button></div><button>add</button></div>'447+ )448+ // click child 1449+ buttons = root.querySelectorAll('button')450+ buttons[0]?.click() // child 1451+ await Promise.resolve()452+ expect(root.innerHTML).toBe(453+ '<div><div><div>child 1 1</div><button>+</button></div><div><div>child 2 0</div><button>+</button></div><div><div>child 3 0</div><button>+</button></div><button>add</button></div>'454+ )455+ // click child 2 - verify child 2 and child 3 do not swap positions456+ buttons = root.querySelectorAll('button')457+ buttons[1]?.click() // child 2458+ await Promise.resolve()459+ expect(root.innerHTML).toBe(460+ '<div><div><div>child 1 1</div><button>+</button></div><div><div>child 2 1</div><button>+</button></div><div><div>child 3 0</div><button>+</button></div><button>add</button></div>'461+ )462+ })463 })464 465 describe('defaultProps', () => {466 it('simple', () => {2194 unmodified linesmodified:src/jsx/dom/render.ts+6 added-10 removed
300 unmodified lines301 }302 })303 }304 305-const findInsertBefore = (node: Node | undefined): SupportedElement | Text | null => {306- for (; ; node = node.tag === HONO_PORTAL_ELEMENT || !node.vC || !node.pP ? node.nN : node.vC[0]) {307- if (!node) {308- return null309- }310- if (node.tag !== HONO_PORTAL_ELEMENT && node.e) {311- return node.e312- }305+const findInsertBefore = (node: Node | undefined): SupportedElement | Text | undefined => {306+ while (node && (node.tag === HONO_PORTAL_ELEMENT || !node.e)) {307+ node = node.tag === HONO_PORTAL_ELEMENT || !node.vC?.[0] ? node.nN : node.vC[0]308 }309+ return node?.e310 }311 312 const removeNode = (node: Node): void => {313 if (!isNodeString(node)) {24 unmodified lines338 }339 340 const findChildNodeIndex = (341 childNodes: NodeListOf<ChildNode>,346- child: ChildNode | null | undefined342+ child: ChildNode | undefined343 ): number | undefined => {344 if (!child) {345 return346 }76 unmodified lines423 }424 }425 }426 if (node.pP) {431- delete node.pP427+ node.pP = undefined428 }429 if (callbacks.length) {430 const useLayoutEffectCbs: Array<() => void> = []431 const useEffectCbs: Array<() => void> = []359 unmodified lines