Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsx/dom): find "insertBefore" node from next node list if not found #2017

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions deno_dist/jsx/dom/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ const applyNode = (node: Node, container: Container) => {
if (isNodeString(node)) {
container.textContent = node[0]
} else {
applyProps(container as HTMLElement, node.props)
applyNodeObject(node, container)
}
}
Expand All @@ -216,7 +215,7 @@ const applyNodeObject = (node: NodeObject, container: Container) => {
const callbacks: EffectData[] = []
getNextChildren(node, container, next, remove, callbacks)
let offset = container.childNodes.length
const insertBefore = findInsertBefore(node.nN)
const insertBefore = findInsertBefore(node.nN) || next.find((n) => n.e)?.e
if (insertBefore) {
for (let i = 0; i < offset; i++) {
if (container.childNodes[i] === insertBefore) {
Expand Down
12 changes: 11 additions & 1 deletion src/jsx/dom/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { JSDOM } from 'jsdom'
import { jsx, Fragment } from '..'
import type { RefObject } from '../hooks'
import { useState, useEffect, useCallback, useRef } from '../hooks'
import type { NodeObject } from './render'
import { render } from '.'

const getContainer = (element: JSX.Element): DocumentFragment | HTMLElement | undefined => {
return (element as unknown as NodeObject).c
}

describe('DOM', () => {
let dom: JSDOM
let root: HTMLElement
Expand Down Expand Up @@ -74,10 +79,15 @@ describe('DOM', () => {
setCount = _setCount
return <div>{count}</div>
}
render(<App />, root)
const app = <App />
render(app, root)
const container = getContainer(app) as HTMLElement
expect(root.innerHTML).toBe('<div>0</div>')

const insertBeforeSpy = vi.spyOn(container, 'insertBefore')
setCount(1)
expect(root.innerHTML).toBe('<div>1</div>')
expect(insertBeforeSpy).not.toHaveBeenCalled()
})

it('element to text to element', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/jsx/dom/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ const applyNode = (node: Node, container: Container) => {
if (isNodeString(node)) {
container.textContent = node[0]
} else {
applyProps(container as HTMLElement, node.props)
applyNodeObject(node, container)
}
}
Expand All @@ -216,7 +215,7 @@ const applyNodeObject = (node: NodeObject, container: Container) => {
const callbacks: EffectData[] = []
getNextChildren(node, container, next, remove, callbacks)
let offset = container.childNodes.length
const insertBefore = findInsertBefore(node.nN)
const insertBefore = findInsertBefore(node.nN) || next.find((n) => n.e)?.e
if (insertBefore) {
for (let i = 0; i < offset; i++) {
if (container.childNodes[i] === insertBefore) {
Expand Down