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
Provenance
- Commit
e355089- Tree
a2eb86a- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
7cf7432
3 files changed+73 added−29 removed
modified:deno_dist/jsx/dom/render.ts+22 added-10 removed
238 unmodified lines239 applyNodeObject(node, container)240 }241 }242 243+const findChildNodeIndex = (244+ childNodes: NodeListOf<ChildNode>,245+ child: ChildNode | null | undefined246+): number | undefined => {247+ if (!child) {248+ return249+ }250+251+ for (let i = 0, len = childNodes.length; i < len; i++) {252+ if (childNodes[i] === child) {253+ return i254+ }255+ }256+257+ return258+}259+260 const 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- }265 266 const childNodes = container.childNodes267+ let offset =268+ findChildNodeIndex(childNodes, findInsertBefore(node.nN)) ??269+ findChildNodeIndex(childNodes, next.find((n) => n.e)?.e) ??270+ childNodes.length271+272 for (let i = 0, len = next.length; i < len; i++, offset++) {273 const child = next[i]274 275 let el: HTMLElement | Text188 unmodified linesmodified:src/jsx/dom/index.test.tsx+29 added-9 removed
4 unmodified lines5 // eslint-disable-next-line @typescript-eslint/no-unused-vars6 import { jsx, Fragment } from '..'7 import type { RefObject } from '../hooks'8 import { useState, useEffect, useLayoutEffect, useCallback, useRef, useMemo } from '../hooks'9-import type { NodeObject } from './render'9 import { memo, isValidElement, cloneElement } from '.'10 import { render, cloneElement as cloneElementForDom } from '.'11 13-const getContainer = (element: JSX.Element): DocumentFragment | HTMLElement | undefined => {14- return (element as unknown as NodeObject).c15-}16-12 describe('DOM', () => {13 beforeAll(() => {14 global.requestAnimationFrame = (cb) => setTimeout(cb)15 })132 unmodified lines148 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>')153 160- 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 lines208 <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>')213 221- 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 })220+221+ 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 />237+238+ render(app, root)239+ expect(root.innerHTML).toBe('<form><div><label>label</label><input></div><p>0</p></form>')240+241+ 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 })248 249 describe('Event', () => {250 it('bubbling phase', async () => {817 unmodified linesmodified:src/jsx/dom/render.ts+22 added-10 removed
238 unmodified lines239 applyNodeObject(node, container)240 }241 }242 243+const findChildNodeIndex = (244+ childNodes: NodeListOf<ChildNode>,245+ child: ChildNode | null | undefined246+): number | undefined => {247+ if (!child) {248+ return249+ }250+251+ for (let i = 0, len = childNodes.length; i < len; i++) {252+ if (childNodes[i] === child) {253+ return i254+ }255+ }256+257+ return258+}259+260 const 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- }265 266 const childNodes = container.childNodes267+ let offset =268+ findChildNodeIndex(childNodes, findInsertBefore(node.nN)) ??269+ findChildNodeIndex(childNodes, next.find((n) => n.e)?.e) ??270+ childNodes.length271+272 for (let i = 0, len = next.length; i < len; i++, offset++) {273 const child = next[i]274 275 let el: HTMLElement | Text188 unmodified lines