Skip to content
Myr

fix(jsx/dom): Do not call insertBefore if the element position does not change (#2196)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx/dom): Do not call insertBefore if the element position does not change * chore: denoify

authored by Taku Amano · committed by GitHub · parent fab6f54 · Browse files · 3 files+30 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+30 added−4 removed

  • tests:+24 added−0 removed
  • other:+6 added−4 removed

3 files+30 added−4 removed

modified:deno_dist/jsx/dom/render.ts+3 added-2 removed
254 unmodified lines
255 }256 }257 }258259 const childNodes = container.childNodes260 for (let i = 0, len = next.length; i < len; i++, offset++) {261 const child = next[i]262263 let el: HTMLElement | Text
6 unmodified lines
270 el = child.e ||= document.createElement(child.tag as string)271 applyProps(el as HTMLElement, child.props, child.pP)272 applyNode(child, el as HTMLElement)273 }273 if (container.childNodes[offset] !== el) {274 container.insertBefore(el, container.childNodes[offset] || null)274 if (childNodes[offset] !== el && childNodes[offset - 1] !== child.e) {275 container.insertBefore(el, childNodes[offset] || null)276 }277 }278 remove.forEach(removeNode)279 callbacks.forEach(([, cb]) => cb?.())
172 unmodified lines
modified:src/jsx/dom/index.test.tsx+24 added
198 unmodified lines
199 setCount(2)200 await Promise.resolve()201 expect(root.innerHTML).toBe('2')202 })203204 it('one child is updated', async () => {205 let setCount: (count: number) => void = () => {}206 const App = () => {207 const [count, _setCount] = useState(0)208 setCount = _setCount209 return <div>{count}</div>210 }211 const app = (212 <>213 <App />214 <div>Footer</div>215 </>216 )217 render(app, root)218 const container = getContainer(app) as HTMLElement219 expect(root.innerHTML).toBe('<div>0</div><div>Footer</div>')220221 const insertBeforeSpy = vi.spyOn(container, 'insertBefore')222 setCount(1)223 await Promise.resolve()224 expect(root.innerHTML).toBe('<div>1</div><div>Footer</div>')225 expect(insertBeforeSpy).not.toHaveBeenCalled()226 })227 })228229 describe('Event', () => {230 it('bubbling phase', async () => {
817 unmodified lines
modified:src/jsx/dom/render.ts+3 added-2 removed
254 unmodified lines
255 }256 }257 }258259 const childNodes = container.childNodes260 for (let i = 0, len = next.length; i < len; i++, offset++) {261 const child = next[i]262263 let el: HTMLElement | Text
6 unmodified lines
270 el = child.e ||= document.createElement(child.tag as string)271 applyProps(el as HTMLElement, child.props, child.pP)272 applyNode(child, el as HTMLElement)273 }273 if (container.childNodes[offset] !== el) {274 container.insertBefore(el, container.childNodes[offset] || null)274 if (childNodes[offset] !== el && childNodes[offset - 1] !== child.e) {275 container.insertBefore(el, childNodes[offset] || null)276 }277 }278 remove.forEach(removeNode)279 callbacks.forEach(([, cb]) => cb?.())
172 unmodified lines