vue3-core/packages/runtime-vapor/src/dom.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-01-20 13:38:20 +08:00
import { isArray, toDisplayString } from '@vue/shared'
2023-12-06 14:59:11 +08:00
import type { Block, ParentBlock } from './render'
2024-01-20 13:38:20 +08:00
export * from './dom/patchProp'
2024-01-20 23:48:10 +08:00
export * from './dom/templateRef'
2024-01-21 02:16:30 +08:00
export * from './dom/on'
2024-01-20 13:38:20 +08:00
export function insert(block: Block, parent: Node, anchor: Node | null = null) {
2023-12-06 14:59:11 +08:00
// if (!isHydrating) {
if (block instanceof Node) {
parent.insertBefore(block, anchor)
} else if (isArray(block)) {
for (const child of block) insert(child, parent, anchor)
} else {
insert(block.nodes, parent, anchor)
parent.insertBefore(block.anchor, anchor)
}
// }
}
export function prepend(parent: ParentBlock, ...nodes: Node[]) {
if (parent instanceof Node) {
// TODO use insertBefore for better performance https://jsbench.me/rolpg250hh/1
parent.prepend(...nodes)
} else if (isArray(parent)) {
parent.unshift(...nodes)
}
}
export function append(parent: ParentBlock, ...nodes: Node[]) {
if (parent instanceof Node) {
// TODO use insertBefore for better performance
parent.append(...nodes)
} else if (isArray(parent)) {
parent.push(...nodes)
}
}
export function remove(block: Block, parent: ParentNode) {
if (block instanceof DocumentFragment) {
remove(Array.from(block.childNodes), parent)
} else if (block instanceof Node) {
2023-12-06 14:59:11 +08:00
parent.removeChild(block)
} else if (isArray(block)) {
for (const child of block) remove(child, parent)
} else {
remove(block.nodes, parent)
block.anchor && parent.removeChild(block.anchor)
}
}
type Children = Record<number, [ChildNode, Children]>
export function children(n: Node): Children {
2023-12-29 22:05:33 +08:00
const result: Children = {}
const array = Array.from(n.childNodes)
for (let i = 0; i < array.length; i++) {
const n = array[i]
result[i] = [n, children(n)]
}
return result
2023-12-06 14:59:11 +08:00
}
export function createTextNode(val: unknown): Text {
2023-12-29 22:05:33 +08:00
// eslint-disable-next-line no-restricted-globals
2023-12-06 14:59:11 +08:00
return document.createTextNode(toDisplayString(val))
}