Skip to content

Commit

Permalink
fix(parser): Fix for slot "name" property using interpolation as well…
Browse files Browse the repository at this point in the history
… as the "slot" property. (vuejs#9038)
  • Loading branch information
zleight1 committed Dec 4, 2018
1 parent 93850f4 commit a248607
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
36 changes: 29 additions & 7 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,28 @@ function processOnce (el) {

function processSlot (el) {
if (el.tag === 'slot') {
el.slotName = getBindingAttr(el, 'name')
if (process.env.NODE_ENV !== 'production' && el.key) {
warn(
`\`key\` does not work on <slot> because slots are abstract outlets ` +
`and can possibly expand into multiple elements. ` +
`Use the key on a wrapping element instead.`
)
let slotName
slotName = getBindingAttr(el, 'name')
if (process.env.NODE_ENV !== 'production') {
if(el.key) {
warn(
`\`key\` does not work on <slot> because slots are abstract outlets ` +
`and can possibly expand into multiple elements. ` +
`Use the key on a wrapping element instead.`
)
}
if (slotName) {
const res = parseText(slotName, delimiters);
if (res) {
warn(
`name="${slotName}": ` +
`Interpolation on <slot> "name" attribute has been removed. ` +
`Use v-bind or the colon shorthand instead.`
)
}
}
}
el.slotName = slotName
} else {
let slotScope
if (el.tag === 'template') {
Expand Down Expand Up @@ -502,6 +516,14 @@ function processSlot (el) {
}
const slotTarget = getBindingAttr(el, 'slot')
if (slotTarget) {
const res = parseText(slotTarget, delimiters);
if (process.env.NODE_ENV !== 'production' && res) {
warn(
`slot="${slotTarget}": "` +
`Interpolation on "slot" attribute has been removed. ` +
`Use v-bind or the colon shorthand instead.`
);
}
el.slotTarget = slotTarget === '""' ? '"default"' : slotTarget
// preserve slot as an attribute for native shadow DOM compat
// only for non-scoped slots.
Expand Down
18 changes: 15 additions & 3 deletions test/unit/modules/compiler/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,23 @@ describe('parser', () => {
expect(ast.children[0].slotName).toBe('"one"')
})

it('slot target', () => {
const ast = parse('<p slot="one">hello world</p>', baseOptions)
expect(ast.slotTarget).toBe('"one"')
it('slot tag name invalid syntax', () => {
// interpolation warning
parse('<div><slot name="{{error}}">hello world</slot></div>', baseOptions);
expect('Interpolation on <slot> "name" attribute has been removed.').toHaveBeenWarned();
})

it("slot target", () => {
const ast = parse('<p slot="one">hello world</p>', baseOptions);
expect(ast.slotTarget).toBe('"one"');
});

it("slot target invalid syntax", () => {
// interpolation warning
parse('<p slot="{{error}}">hello world</p>', baseOptions);
expect('Interpolation on "slot" attribute has been removed.').toHaveBeenWarned();
});

it('component properties', () => {
const ast = parse('<my-component :msg="hello"></my-component>', baseOptions)
expect(ast.attrs[0].name).toBe('msg')
Expand Down

0 comments on commit a248607

Please sign in to comment.