Skip to content

Commit

Permalink
feat: support .property shorthand syntax for v-bind.prop modifier
Browse files Browse the repository at this point in the history
close #7582
  • Loading branch information
yyx990803 committed Jan 9, 2019
1 parent 23a1459 commit d2902ca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import {
} from '../helpers'

export const onRE = /^@|^v-on:/
export const dirRE = /^v-|^@|^:/
export const dirRE = /^v-|^@|^:|^\./
export const forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/
export const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
const stripParensRE = /^\(|\)$/g

const argRE = /:(.*)$/
export const bindRE = /^:|^v-bind:/
export const bindRE = /^:|^\.|^v-bind:/
const propBindRE = /^\./
const modifierRE = /\.[^.]+/g

const lineBreakRE = /[\r\n]/
Expand Down Expand Up @@ -683,8 +684,12 @@ function processAttrs (el) {
// mark element as dynamic
el.hasBindings = true
// modifiers
modifiers = parseModifiers(name)
if (modifiers) {
modifiers = parseModifiers(name.replace(dirRE, ''))
// support .foo shorthand syntax for the .prop modifier
if (propBindRE.test(name)) {
(modifiers || (modifiers = {})).prop = true
name = `.` + name.slice(1).replace(modifierRE, '')
} else if (modifiers) {
name = name.replace(modifierRE, '')
}
if (bindRE.test(name)) { // v-bind
Expand Down
12 changes: 12 additions & 0 deletions test/unit/features/directives/bind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ describe('Directive v-bind', () => {
expect(vm.$el.getAttribute('id')).toBe(null)
})

it('.prop modifier shorthand', () => {
const vm = new Vue({
template: '<div><span .text-content="foo"></span><span .inner-html="bar"></span></div>',
data: {
foo: 'hello',
bar: '<span>qux</span>'
}
}).$mount()
expect(vm.$el.children[0].textContent).toBe('hello')
expect(vm.$el.children[1].innerHTML).toBe('<span>qux</span>')
})

it('.camel modifier', () => {
const vm = new Vue({
template: '<svg :view-box.camel="viewBox"></svg>',
Expand Down
10 changes: 10 additions & 0 deletions test/unit/modules/compiler/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@ describe('parser', () => {
expect('The value for a v-bind expression cannot be empty. Found in "v-bind:empty-msg"').toHaveBeenWarned()
})

it('v-bind.prop shorthand syntax', () => {
const ast = parse('<div .id="foo"></div>', baseOptions)
expect(ast.props).toEqual([{ name: 'id', value: 'foo'}])
})

it('v-bind.prop shorthand syntax w/ modifiers', () => {
const ast = parse('<div .id.mod="foo"></div>', baseOptions)
expect(ast.props).toEqual([{ name: 'id', value: 'foo'}])
})

// #6887
it('special case static attribute that must be props', () => {
const ast = parse('<video muted></video>', baseOptions)
Expand Down

0 comments on commit d2902ca

Please sign in to comment.