-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
/
resolve-slots.js
51 lines (48 loc) · 1.26 KB
/
resolve-slots.js
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
/* @flow */
/**
* Runtime helper for resolving raw children VNodes into a slot object.
*/
export function resolveSlots (
children: ?Array<VNode>,
context: ?Component
): { [key: string]: Array<VNode> } {
const slots = {}
if (!children) {
return slots
}
const defaultSlot = []
let name, child
for (let i = 0, l = children.length; i < l; i++) {
child = children[i]
// named slots should only be respected if the vnode was rendered in the
// same context.
if ((child.context === context || child.functionalContext === context) &&
child.data && (name = child.data.slot) != null) {
const slot = (slots[name] || (slots[name] = []))
if (child.tag === 'template') {
slot.push.apply(slot, child.children)
} else {
slot.push(child)
}
} else {
defaultSlot.push(child)
}
}
// ignore whitespace
if (!defaultSlot.every(isWhitespace)) {
slots.default = defaultSlot
}
return slots
}
function isWhitespace (node: VNode): boolean {
return node.isComment || node.text === ' '
}
export function resolveScopedSlots (
fns: Array<[string, Function]>
): { [key: string]: Function } {
const res = {}
for (let i = 0; i < fns.length; i++) {
res[fns[i][0]] = fns[i][1]
}
return res
}