Skip to content
Myr

fix(jsx/dom): fix finding element to insert before (#2230)

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

* fix(jsx/dom): fix finding element to insert before * chore: denoify

authored by Taku Amano · committed by GitHub · parent 7cf7432 · Browse files · 3 files+73 added−29 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+73 added−29 removed

  • tests:+29 added−9 removed
  • other:+44 added−20 removed

3 files+73 added−29 removed

modified:deno_dist/jsx/dom/render.ts+22 added-10 removed
238 unmodified lines
239 applyNodeObject(node, container)240 }241}242243const findChildNodeIndex = (244 childNodes: NodeListOf<ChildNode>,245 child: ChildNode | null | undefined246): number | undefined => {247 if (!child) {248 return249 }250251 for (let i = 0, len = childNodes.length; i < len; i++) {252 if (childNodes[i] === child) {253 return i254 }255 }256257 return258}259260const applyNodeObject = (node: NodeObject, container: Container) => {261 const next: Node[] = []262 const remove: Node[] = []263 const callbacks: EffectData[] = []264 getNextChildren(node, container, next, remove, callbacks)248 let offset = container.childNodes.length249 const insertBefore = findInsertBefore(node.nN) || next.find((n) => n.e)?.e250 if (insertBefore) {251 for (let i = 0; i < offset; i++) {252 if (container.childNodes[i] === insertBefore) {253 offset = i254 break255 }256 }257 }265266 const childNodes = container.childNodes267 let offset =268 findChildNodeIndex(childNodes, findInsertBefore(node.nN)) ??269 findChildNodeIndex(childNodes, next.find((n) => n.e)?.e) ??270 childNodes.length271272 for (let i = 0, len = next.length; i < len; i++, offset++) {273 const child = next[i]274275 let el: HTMLElement | Text
188 unmodified lines
modified:src/jsx/dom/index.test.tsx+29 added-9 removed
4 unmodified lines
5// eslint-disable-next-line @typescript-eslint/no-unused-vars6import { jsx, Fragment } from '..'7import type { RefObject } from '../hooks'8import { useState, useEffect, useLayoutEffect, useCallback, useRef, useMemo } from '../hooks'9import type { NodeObject } from './render'9import { memo, isValidElement, cloneElement } from '.'10import { render, cloneElement as cloneElementForDom } from '.'1113const getContainer = (element: JSX.Element): DocumentFragment | HTMLElement | undefined => {14 return (element as unknown as NodeObject).c15}1612describe('DOM', () => {13 beforeAll(() => {14 global.requestAnimationFrame = (cb) => setTimeout(cb)15 })
132 unmodified lines
148 return <div>{count}</div>149 }150 const app = <App />151 render(app, root)157 const container = getContainer(app) as HTMLElement152 expect(root.innerHTML).toBe('<div>0</div>')153160 const insertBeforeSpy = vi.spyOn(container, 'insertBefore')154 const insertBeforeSpy = vi.spyOn(dom.window.Node.prototype, 'insertBefore')155 setCount(1)156 await Promise.resolve()157 expect(root.innerHTML).toBe('<div>1</div>')158 expect(insertBeforeSpy).not.toHaveBeenCalled()
49 unmodified lines
208 <div>Footer</div>209 </>210 )211 render(app, root)218 const container = getContainer(app) as HTMLElement212 expect(root.innerHTML).toBe('<div>0</div><div>Footer</div>')213221 const insertBeforeSpy = vi.spyOn(container, 'insertBefore')214 const insertBeforeSpy = vi.spyOn(dom.window.Node.prototype, 'insertBefore')215 setCount(1)216 await Promise.resolve()217 expect(root.innerHTML).toBe('<div>1</div><div>Footer</div>')218 expect(insertBeforeSpy).not.toHaveBeenCalled()219 })220221 it('should not call insertBefore for unchanged complex dom tree', async () => {222 let setCount: (count: number) => void = () => {}223 const App = () => {224 const [count, _setCount] = useState(0)225 setCount = _setCount226 return (227 <form>228 <div>229 <label>label</label>230 <input />231 </div>232 <p>{count}</p>233 </form>234 )235 }236 const app = <App />237238 render(app, root)239 expect(root.innerHTML).toBe('<form><div><label>label</label><input></div><p>0</p></form>')240241 const insertBeforeSpy = vi.spyOn(dom.window.Node.prototype, 'insertBefore')242 setCount(1)243 await Promise.resolve()244 expect(root.innerHTML).toBe('<form><div><label>label</label><input></div><p>1</p></form>')245 expect(insertBeforeSpy).not.toHaveBeenCalled()246 })247 })248249 describe('Event', () => {250 it('bubbling phase', async () => {
817 unmodified lines
modified:src/jsx/dom/render.ts+22 added-10 removed
238 unmodified lines
239 applyNodeObject(node, container)240 }241}242243const findChildNodeIndex = (244 childNodes: NodeListOf<ChildNode>,245 child: ChildNode | null | undefined246): number | undefined => {247 if (!child) {248 return249 }250251 for (let i = 0, len = childNodes.length; i < len; i++) {252 if (childNodes[i] === child) {253 return i254 }255 }256257 return258}259260const applyNodeObject = (node: NodeObject, container: Container) => {261 const next: Node[] = []262 const remove: Node[] = []263 const callbacks: EffectData[] = []264 getNextChildren(node, container, next, remove, callbacks)248 let offset = container.childNodes.length249 const insertBefore = findInsertBefore(node.nN) || next.find((n) => n.e)?.e250 if (insertBefore) {251 for (let i = 0; i < offset; i++) {252 if (container.childNodes[i] === insertBefore) {253 offset = i254 break255 }256 }257 }265266 const childNodes = container.childNodes267 let offset =268 findChildNodeIndex(childNodes, findInsertBefore(node.nN)) ??269 findChildNodeIndex(childNodes, next.find((n) => n.e)?.e) ??270 childNodes.length271272 for (let i = 0, len = next.length; i < len; i++, offset++) {273 const child = next[i]274275 let el: HTMLElement | Text
188 unmodified lines