Skip to content

Commit fbe5f2b

Browse files
authored
Merge pull request vuejs#277 from vuejs/issue-221
fix: handle slots correctly
2 parents 49b38c4 + a3473c2 commit fbe5f2b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/mount.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { config } from './config'
2626
import { MountingOptions, Slot } from './types'
2727
import {
2828
isFunctionalComponent,
29+
isHTML,
2930
isObjectComponent,
3031
mergeGlobalProperties
3132
} from './utils'
@@ -289,8 +290,14 @@ export function mount(
289290
}
290291

291292
if (typeof slot === 'string') {
292-
// slot is most probably a scoped slot string or a plain string
293-
acc[name] = (props: VNodeProps) => h(processSlot(slot), props)
293+
// if it is HTML we process and render it using h
294+
if (isHTML(slot)) {
295+
acc[name] = (props: VNodeProps) => h(processSlot(slot), props)
296+
}
297+
// otherwise it is just a string so we just return it as-is
298+
else {
299+
acc[name] = () => slot
300+
}
294301
return acc
295302
}
296303

src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,17 @@ export function isFunctionalComponent(component: any) {
8282
export function isObjectComponent(component: any) {
8383
return typeof component !== 'function' && !isClassComponent(component)
8484
}
85+
86+
// https://stackoverflow.com/questions/15458876/check-if-a-string-is-html-or-not/15458987#answer-15458968
87+
export function isHTML(str: string) {
88+
var a = document.createElement('div')
89+
a.innerHTML = str
90+
91+
for (let c = a.childNodes, i = c.length; i--; ) {
92+
if (c[i].nodeType == 1) {
93+
return true
94+
}
95+
}
96+
97+
return false
98+
}

tests/mountingOptions/slots.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('slots', () => {
1515
named: namedString
1616
}
1717
})
18+
expect(wrapper.vm.$slots.default()[0].children).toBe(defaultString)
1819
expect(wrapper.find('.default').text()).toBe(defaultString)
1920
expect(wrapper.find('.named').text()).toBe(namedString)
2021
})

0 commit comments

Comments
 (0)