Skip to content

Commit

Permalink
fix(v-model): handle trailing whitespaces in expression (#7737)
Browse files Browse the repository at this point in the history
  • Loading branch information
posva authored and yyx990803 committed Mar 8, 2018
1 parent 550c3c0 commit db58493
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function genComponentModel (
if (trim) {
valueExpression =
`(typeof ${baseValueExpression} === 'string'` +
`? ${baseValueExpression}.trim()` +
`: ${baseValueExpression})`
`? ${baseValueExpression}.trim()` +
`: ${baseValueExpression})`
}
if (number) {
valueExpression = `_n(${valueExpression})`
Expand Down Expand Up @@ -68,6 +68,9 @@ type ModelParseResult = {
}

export function parseModel (val: string): ModelParseResult {
// Fix https://github.com/vuejs/vue/pull/7730
// allow v-model="obj.val " (trailing whitespace)
val = val.trim()
len = val.length

if (val.indexOf('[') < 0 || val.lastIndexOf(']') < len - 1) {
Expand Down
15 changes: 15 additions & 0 deletions test/unit/features/directives/model-text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ describe('Directive v-model text', () => {
}).then(done)
})

it('should work with space ended expression in v-model', () => {
const vm = new Vue({
data: {
obj: {
test: 'b'
}
},
template: '<input v-model="obj.test ">'
}).$mount()

triggerEvent(vm.$el, 'input')
expect(vm.obj['test ']).toBe(undefined)
expect(vm.obj.test).toBe('b')
})

it('.lazy modifier', () => {
const vm = new Vue({
data: {
Expand Down

0 comments on commit db58493

Please sign in to comment.