-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
Copy pathresolve-slots.ts
52 lines (50 loc) · 1.5 KB
/
resolve-slots.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
43
44
45
46
47
48
49
50
51
52
import type VNode from 'core/vdom/vnode'
import type { Component } from 'types/component'
/**
* Runtime helper for resolving raw children VNodes into a slot object.
*/
export function resolveSlots(
children: Array<VNode> | null | undefined,
context: Component | null
): { [key: string]: Array<VNode> } {
if (!children || !children.length) {
return {}
}
const slots: Record<string, any> = {}
for (let i = 0, l = children.length; i < l; i++) {
const child = children[i]
const data = child.data
// remove slot attribute if the node is resolved as a Vue slot node
if (data && data.attrs && data.attrs.slot) {
delete data.attrs.slot
}
// named slots should only be respected if the vnode was rendered in the
// same context.
if (
(child.context === context || child.fnContext === context) &&
data &&
data.slot != null
) {
const name = data.slot
const slot = slots[name] || (slots[name] = [])
if (child.tag === 'template') {
slot.push.apply(slot, child.children || [])
} else {
slot.push(child)
}
} else {
;(slots.default || (slots.default = [])).push(child)
}
}
// ignore slots that contains only whitespace
for (const name in slots) {
if (!slots.hasOwnProperty(name)) continue;
if (slots[name].every(isWhitespace)) {
delete slots[name]
}
}
return slots
}
function isWhitespace(node: VNode): boolean {
return (node.isComment && !node.asyncFactory) || node.text === ' '
}