feat: use Array for Fragment, instead of native DocumentFragment

This commit is contained in:
三咲智子 Kevin Deng 2023-11-27 05:28:50 +08:00
parent 07373d41fd
commit 6ff8b1bf0d
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
5 changed files with 17 additions and 11 deletions

View File

@ -36,7 +36,7 @@ See the To-do list below or `// TODO` comments in code (`compiler-vapor` and `ru
- [ ] Fragment
- [x] multiple root nodes
- [x] all dynamic children
- [ ] return `Node[]` for all dynamic children, instead of using `fragment` API
- [x] return `Node[]` for all dynamic children, instead of using `fragment` API
- [ ] Built-in Components
- [ ] Transition
- [ ] TransitionGroup

View File

@ -18,12 +18,13 @@ describe('api: template', () => {
test('create fragment', () => {
const frag = fragment()
const root = frag()
expect(root).toBeInstanceOf(DocumentFragment)
expect(root.childNodes.length).toBe(0)
const div2 = frag()
expect(div2).toBeInstanceOf(DocumentFragment)
expect(div2).not.toBe(root)
const root = frag()
expect(root).toBeInstanceOf(Array)
expect(root.length).toBe(0)
const root2 = frag()
expect(root2).toBeInstanceOf(Array)
expect(root2).not.toBe(root)
})
})

View File

@ -7,6 +7,7 @@ import {
import { isArray } from '@vue/shared'
export type Block = Node | Fragment | Block[]
export type ParentBlock = ParentNode | Node[]
export type Fragment = { nodes: Block; anchor: Node }
export type BlockFn = (props?: any) => Block
@ -57,8 +58,12 @@ export function insert(
// }
}
export function append(parent: ParentNode, ...nodes: (Node | string)[]) {
parent.append(...nodes)
export function append(parent: ParentBlock, ...nodes: Node[]) {
if (parent instanceof Node) {
parent.append(...nodes)
} else if (isArray(parent)) {
parent.push(...nodes)
}
}
export function remove(block: Block, parent: ParentNode) {

View File

@ -19,6 +19,6 @@ export const template = (str: string): (() => Node) => {
}
}
export function fragment(): () => DocumentFragment {
return () => document.createDocumentFragment()
export function fragment(): () => Node[] {
return () => []
}