Skip to content

Commit 684cd7d

Browse files
committed
fix: preserve slot attribute if not resolved by Vue
close #6553
1 parent 844a540 commit 684cd7d

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/compiler/parser/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ function processSlot (el) {
431431
const slotTarget = getBindingAttr(el, 'slot')
432432
if (slotTarget) {
433433
el.slotTarget = slotTarget === '""' ? '"default"' : slotTarget
434+
// preserve slot as an attribute for native shadow DOM compat
435+
addAttr(el, 'slot', slotTarget)
434436
}
435437
if (el.tag === 'template') {
436438
el.slotScope = getAndRemoveAttr(el, 'scope')

src/core/instance/render-helpers/resolve-slots.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ export function resolveSlots (
1414
const defaultSlot = []
1515
for (let i = 0, l = children.length; i < l; i++) {
1616
const child = children[i]
17+
const data = child.data
18+
// remove slot attribute if the node is resolved as a Vue slot node
19+
if (data && data.attrs && data.attrs.slot) {
20+
delete data.attrs.slot
21+
}
1722
// named slots should only be respected if the vnode was rendered in the
1823
// same context.
1924
if ((child.context === context || child.functionalContext === context) &&
20-
child.data && child.data.slot != null
25+
data && data.slot != null
2126
) {
2227
const name = child.data.slot
2328
const slot = (slots[name] || (slots[name] = []))

test/unit/features/component/component-slot.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -728,4 +728,15 @@ describe('Component slot', () => {
728728
expect(vm.$el.innerHTML).toBe(`<i><div><div>foo</div></div><div>bar</div></i>`)
729729
}).then(done)
730730
})
731+
732+
it('should preserve slot attribute if not absorbed by a Vue component', () => {
733+
const vm = new Vue({
734+
template: `
735+
<div>
736+
<div slot="foo"></div>
737+
</div>
738+
`
739+
}).$mount()
740+
expect(vm.$el.children[0].getAttribute('slot')).toBe('foo')
741+
})
731742
})

test/unit/modules/compiler/codegen.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe('codegen', () => {
175175
it('generate slot target', () => {
176176
assertCodegen(
177177
'<p slot="one">hello world</p>',
178-
`with(this){return _c('p',{slot:"one"},[_v("hello world")])}`
178+
`with(this){return _c('p',{attrs:{"slot":"one"},slot:"one"},[_v("hello world")])}`
179179
)
180180
})
181181

0 commit comments

Comments
 (0)