1 |
|
2 |
|
3 | import type VNode from 'core/vdom/vnode'
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export 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 |
|
20 | if (data && data.attrs && data.attrs.slot) {
|
21 | delete data.attrs.slot
|
22 | }
|
23 |
|
24 |
|
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 |
|
40 | for (const name in slots) {
|
41 | if (slots[name].every(isWhitespace)) {
|
42 | delete slots[name]
|
43 | }
|
44 | }
|
45 | return slots
|
46 | }
|
47 |
|
48 | function isWhitespace (node: VNode): boolean {
|
49 | return (node.isComment && !node.asyncFactory) || node.text === ' '
|
50 | }
|