UNPKG

1.4 kBJavaScriptView Raw
1/* @flow */
2
3import type VNode from 'core/vdom/vnode'
4
5/**
6 * Runtime helper for resolving raw children VNodes into a slot object.
7 */
8export function resolveSlots (
9 children: ?Array<VNode>,
10 context: ?Component
11): { [key: string]: Array<VNode> } {
12 if (!children || !children.length) {
13 return {}
14 }
15 const slots = {}
16 for (let i = 0, l = children.length; i < l; i++) {
17 const child = children[i]
18 const data = child.data
19 // remove slot attribute if the node is resolved as a Vue slot node
20 if (data && data.attrs && data.attrs.slot) {
21 delete data.attrs.slot
22 }
23 // named slots should only be respected if the vnode was rendered in the
24 // same context.
25 if ((child.context === context || child.fnContext === context) &&
26 data && data.slot != null
27 ) {
28 const name = data.slot
29 const slot = (slots[name] || (slots[name] = []))
30 if (child.tag === 'template') {
31 slot.push.apply(slot, child.children || [])
32 } else {
33 slot.push(child)
34 }
35 } else {
36 (slots.default || (slots.default = [])).push(child)
37 }
38 }
39 // ignore slots that contains only whitespace
40 for (const name in slots) {
41 if (slots[name].every(isWhitespace)) {
42 delete slots[name]
43 }
44 }
45 return slots
46}
47
48function isWhitespace (node: VNode): boolean {
49 return (node.isComment && !node.asyncFactory) || node.text === ' '
50}