forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-slot-vnodes.js
60 lines (52 loc) · 1.4 KB
/
create-slot-vnodes.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
52
53
54
55
56
57
58
59
60
// @flow
import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'
function startsWithTag (str: SlotValue): boolean {
return typeof str === 'string' && str.trim()[0] === '<'
}
function createVNodesForSlot (
h: Function,
slotValue: SlotValue,
name: string,
_Vue: any
): VNode | string {
if (typeof slotValue === 'string' && !startsWithTag(slotValue)) {
return slotValue
}
const el =
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue
let vnode = h(el)
if (typeof slotValue === 'string') {
const vue = new Vue()
const _vue = new _Vue()
for (const key in _vue._renderProxy) {
if (!(vue._renderProxy[key])) {
vue._renderProxy[key] = _vue._renderProxy[key]
}
}
try {
// $FlowIgnore
vnode = el.render.call(vue._renderProxy, h)
vnode = h(vnode.tag, vnode.data || {}, vnode.children)
} catch (e) {
}
}
vnode.data.slot = name
return vnode
}
export function createSlotVNodes (
h: Function,
slots: SlotsObject,
_Vue: any
): Array<VNode | string> {
return Object.keys(slots).reduce((acc, key) => {
const content = slots[key]
if (Array.isArray(content)) {
const nodes = content.map(
slotDef => createVNodesForSlot(h, slotDef, key, _Vue)
)
return acc.concat(nodes)
}
return acc.concat(createVNodesForSlot(h, content, key, _Vue))
}, [])
}