perf(runtime-vapor): improve traverse children

This commit is contained in:
三咲智子 Kevin Deng 2024-02-22 02:07:38 +08:00
parent fa0ca8a5af
commit 531f4f0052
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
2 changed files with 16 additions and 8 deletions

View File

@ -65,14 +65,6 @@ export function remove(block: Block, parent: ParentBlock) {
}
}
/*! #__NO_SIDE_EFFECTS__ */
export function children(node: Node | Node[], ...paths: number[]): Node {
for (const idx of paths) {
node = isArray(node) ? node[idx] : node.childNodes[idx]
}
return node as Node
}
/*! #__NO_SIDE_EFFECTS__ */
export function createTextNode(val?: unknown): Text {
// eslint-disable-next-line no-restricted-globals

View File

@ -1,3 +1,5 @@
import { isArray } from '@vue/shared'
/*! #__NO_SIDE_EFFECTS__ */
export function template(str: string): () => ChildNode[] {
let cached = false
@ -24,3 +26,17 @@ export function template(str: string): () => ChildNode[] {
function fragmentToNodes(node: DocumentFragment): ChildNode[] {
return Array.from((node.cloneNode(true) as DocumentFragment).childNodes)
}
/*! #__NO_SIDE_EFFECTS__ */
export function children(node: Node | Node[], ...paths: number[]): Node {
for (const idx of paths) {
if (isArray(node)) {
node = node[idx]
} else {
for (let i = 0; i <= idx; i++) {
node = (node as Node)[i === 0 ? 'firstChild' : 'nextSibling']!
}
}
}
return node as Node
}