Skip to content

Commit 11c7828

Browse files
fix(jsx/dom): handle empty arrays in render children loop (#4729)
* fix(jsx/dom): handle empty arrays in render children loop When an empty array is spliced into the children list, the loop index still increments, causing the next child to be skipped. This leads to a TypeError when accessing properties of undefined nodes. Add i-- and continue after splice so the loop re-examines the same index after flattening, correctly handling empty arrays. * ci: apply automated fixes --------- Co-authored-by: Maks Pikov <mixelburg@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a40d210 commit 11c7828

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/jsx/dom/index.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ describe('DOM', () => {
119119
expect(root.innerHTML).toBe('Hello')
120120
})
121121

122+
it('render with empty array followed by non-empty array', () => {
123+
const tags: string[] = []
124+
const terms: string[] = ['hello']
125+
const App = () => (
126+
<div>
127+
{tags.map((x) => (
128+
<span>{x}</span>
129+
))}
130+
{terms.map((x) => (
131+
<span>{x}</span>
132+
))}
133+
<input type='text' />
134+
</div>
135+
)
136+
render(<App />, root)
137+
expect(root.innerHTML).toBe('<div><span>hello</span><input type="text"></div>')
138+
})
139+
122140
describe('performance', () => {
123141
it('should be O(N) for each additional element', () => {
124142
const App = () => (

src/jsx/dom/render.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ export const build = (context: Context, node: NodeObject, children?: Child[]): v
487487
for (let i = 0; i < children.length; i++) {
488488
if (Array.isArray(children[i])) {
489489
children.splice(i, 1, ...(children[i] as Child[]).flat())
490+
i--
491+
continue
490492
}
491493
let child = buildNode(children[i])
492494
if (child) {

0 commit comments

Comments
 (0)