From 32b8849c8f631859fcfc4f05720dd722990726e9 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Tue, 12 Jun 2018 23:26:59 +0100 Subject: [PATCH] fix: support text slots --- packages/create-instance/add-slots.js | 13 +++++++++++-- test/specs/mounting-options/slots.spec.js | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/create-instance/add-slots.js b/packages/create-instance/add-slots.js index 0236cb288..8bbc61ea0 100644 --- a/packages/create-instance/add-slots.js +++ b/packages/create-instance/add-slots.js @@ -2,11 +2,20 @@ import { compileToFunctions } from 'vue-template-compiler' +function startsWithTag (str) { + return str && str.trim()[0] === '<' +} + function createVNodesForSlot ( h: Function, slotValue: SlotValue, name: string -): Array { +): VNode | string { + if (typeof slotValue === 'string' && + !startsWithTag(slotValue)) { + return slotValue + } + const el = typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue @@ -19,7 +28,7 @@ function createVNodesForSlot ( export function createSlotVNodes ( h: Function, slots: SlotsObject -): Array { +): Array { return Object.keys(slots).reduce((acc, key) => { const content = slots[key] if (Array.isArray(content)) { diff --git a/test/specs/mounting-options/slots.spec.js b/test/specs/mounting-options/slots.spec.js index 0c7287bd7..442d1147f 100644 --- a/test/specs/mounting-options/slots.spec.js +++ b/test/specs/mounting-options/slots.spec.js @@ -164,6 +164,20 @@ describeWithMountingMethods('options.slots', (mountingMethod) => { } }) + it('mounts component with text slot', () => { + const wrapper = mountingMethod(ComponentWithSlots, { + slots: { + default: 'hello,', + header: 'world' + } + }) + if (mountingMethod.name === 'renderToString') { + expect(wrapper).contains('hello,world') + } else { + expect(wrapper.text()).to.contain('hello,world') + } + }) + it('mounts component with named slot if passed component in slot object', () => { const wrapper = mountingMethod(ComponentWithSlots, { slots: {