forked from vuejs/test-utils
/
getRootNodes.ts
42 lines (39 loc) · 1.49 KB
/
getRootNodes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { isNotNullOrUndefined } from '../utils'
import { VNode, VNodeArrayChildren } from 'vue'
import { ShapeFlags } from './vueShared'
export function getRootNodes(vnode: VNode): Node[] {
if (vnode.shapeFlag & ShapeFlags.ELEMENT) {
return [vnode.el as Node]
} else if (vnode.shapeFlag & ShapeFlags.COMPONENT) {
const { subTree } = vnode.component!
return getRootNodes(subTree)
} else if (vnode.shapeFlag & ShapeFlags.SUSPENSE) {
return getRootNodes(vnode.suspense!.activeBranch!)
} else if (
vnode.shapeFlag &
(ShapeFlags.TEXT_CHILDREN | ShapeFlags.TELEPORT)
) {
// static node optimization, subTree.children will be static string and will not help us
const result = [vnode.el as Node]
if (vnode.anchor) {
let currentNode: Node | null = result[0].nextSibling
while (currentNode && currentNode.previousSibling !== vnode.anchor) {
result.push(currentNode)
currentNode = currentNode.nextSibling
}
}
return result
} else if (vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
const children = (
vnode.children as unknown as VNodeArrayChildren
).flat() as VNode[]
return children
.flatMap((vnode) => getRootNodes(vnode))
.filter(isNotNullOrUndefined)
}
// Missing cases which do not need special handling:
// ShapeFlags.SLOTS_CHILDREN comes with ShapeFlags.ELEMENT
// Will hit this default when ShapeFlags is 0
// This is the case for example for unresolved async component without loader
return []
}