Skip to content

Commit 8294453

Browse files
authored
feat: support component slot string (#633)
1 parent c972b8c commit 8294453

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

Diff for: packages/create-instance/add-slots.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@ import { compileToFunctions } from 'vue-template-compiler'
44
import { throwError } from 'shared/util'
55
import { validateSlots } from './validate-slots'
66

7-
function isSingleElement (slotValue: string): boolean {
8-
const _slotValue = slotValue.trim()
9-
if (_slotValue[0] !== '<' || _slotValue[_slotValue.length - 1] !== '>') {
10-
return false
11-
}
12-
const domParser = new window.DOMParser()
13-
const _document = domParser.parseFromString(slotValue, 'text/html')
14-
return _document.body.childElementCount === 1
15-
}
16-
177
// see https://github.com/vuejs/vue-test-utils/pull/274
188
function createVNodes (vm: Component, slotValue: string) {
199
const compiledResult = compileToFunctions(`<div>${slotValue}{{ }}</div>`)
@@ -40,11 +30,7 @@ function addSlotToVm (vm: Component, slotName: string, slotValue: SlotValue): vo
4030
let elem
4131
if (typeof slotValue === 'string') {
4232
validateEnvironment()
43-
if (isSingleElement(slotValue)) {
44-
elem = vm.$createElement(compileToFunctions(slotValue))
45-
} else {
46-
elem = createVNodes(vm, slotValue)
47-
}
33+
elem = createVNodes(vm, slotValue)
4834
} else {
4935
elem = vm.$createElement(slotValue)
5036
}

Diff for: packages/create-instance/create-instance.js

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ export default function createInstance (
8686

8787
const vm = new Constructor(instanceOptions)
8888

89+
// Workaround for Vue < 2.5
90+
vm._staticTrees = []
91+
8992
addAttrs(vm, options.attrs)
9093
addListeners(vm, options.listeners)
9194

Diff for: test/specs/mounting-options/slots.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@ describeWithMountingMethods('options.slots', (mountingMethod) => {
3636
}
3737
})
3838

39+
itDoNotRunIf(
40+
mountingMethod.name === 'shallowMount' ||
41+
isRunningPhantomJS ||
42+
process.env.TEST_ENV === 'node',
43+
'mounts component with default slot if passed component as string in slot object', () => {
44+
const CustomComponent = {
45+
render: h => h('time')
46+
}
47+
const TestComponent = {
48+
template: '<div><slot /></div>',
49+
components: {
50+
'custom-component': CustomComponent
51+
}
52+
}
53+
const wrapper = mountingMethod(TestComponent, {
54+
slots: {
55+
default: '<custom-component />'
56+
},
57+
components: {
58+
'custom-component': CustomComponent
59+
}
60+
})
61+
if (mountingMethod.name === 'renderToString') {
62+
expect(wrapper).contains('<time>')
63+
} else {
64+
expect(wrapper.contains('time')).to.equal(true)
65+
}
66+
})
67+
3968
it('mounts component with default slot if passed component in array in slot object', () => {
4069
const wrapper = mountingMethod(ComponentWithSlots, { slots: { default: [Component] }})
4170
if (mountingMethod.name === 'renderToString') {

0 commit comments

Comments
 (0)