From 79feb580c860bc85b8e9a46ceac2a7970b895b7b Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Sun, 20 May 2018 20:37:34 +0100 Subject: [PATCH 1/3] feat: support component as slot string --- packages/create-instance/add-slots.js | 16 +------------- test/specs/mounting-options/slots.spec.js | 26 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/create-instance/add-slots.js b/packages/create-instance/add-slots.js index 9b802ceaf..e47a242a4 100644 --- a/packages/create-instance/add-slots.js +++ b/packages/create-instance/add-slots.js @@ -4,16 +4,6 @@ import { compileToFunctions } from 'vue-template-compiler' import { throwError } from 'shared/util' import { validateSlots } from './validate-slots' -function isSingleElement (slotValue: string): boolean { - const _slotValue = slotValue.trim() - if (_slotValue[0] !== '<' || _slotValue[_slotValue.length - 1] !== '>') { - return false - } - const domParser = new window.DOMParser() - const _document = domParser.parseFromString(slotValue, 'text/html') - return _document.body.childElementCount === 1 -} - // see https://github.com/vuejs/vue-test-utils/pull/274 function createVNodes (vm: Component, slotValue: string) { const compiledResult = compileToFunctions(`
${slotValue}{{ }}
`) @@ -40,11 +30,7 @@ function addSlotToVm (vm: Component, slotName: string, slotValue: SlotValue): vo let elem if (typeof slotValue === 'string') { validateEnvironment() - if (isSingleElement(slotValue)) { - elem = vm.$createElement(compileToFunctions(slotValue)) - } else { - elem = createVNodes(vm, slotValue) - } + elem = createVNodes(vm, slotValue) } else { elem = vm.$createElement(slotValue) } diff --git a/test/specs/mounting-options/slots.spec.js b/test/specs/mounting-options/slots.spec.js index ab6dab1bc..f60254953 100644 --- a/test/specs/mounting-options/slots.spec.js +++ b/test/specs/mounting-options/slots.spec.js @@ -36,6 +36,32 @@ describeWithMountingMethods('options.slots', (mountingMethod) => { } }) + itDoNotRunIf(mountingMethod.name === 'shallowMount', + 'mounts component with default slot if passed component as string in slot object', () => { + const CustomComponent = { + render: h => h('time') + } + const TestComponent = { + template: '
', + components: { + 'custom-component': CustomComponent + } + } + const wrapper = mountingMethod(TestComponent, { + slots: { + default: '' + }, + components: { + 'custom-component': CustomComponent + } + }) + if (mountingMethod.name === 'renderToString') { + expect(wrapper).contains('