From afddbb90b3837dc070df9e7535088236026cbe2a Mon Sep 17 00:00:00 2001 From: Leonardo Cardoso Date: Fri, 2 Mar 2018 08:54:41 -0300 Subject: [PATCH 1/4] fix(v-model): v-model ended with spaces --- src/compiler/directives/model.js | 5 +++-- test/unit/features/directives/model-text.spec.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/compiler/directives/model.js b/src/compiler/directives/model.js index 8c1f55bf663..4ebc66d61e3 100644 --- a/src/compiler/directives/model.js +++ b/src/compiler/directives/model.js @@ -15,8 +15,8 @@ export function genComponentModel ( if (trim) { valueExpression = `(typeof ${baseValueExpression} === 'string'` + - `? ${baseValueExpression}.trim()` + - `: ${baseValueExpression})` + `? ${baseValueExpression}.trim()` + + `: ${baseValueExpression})` } if (number) { valueExpression = `_n(${valueExpression})` @@ -69,6 +69,7 @@ type ModelParseResult = { export function parseModel (val: string): ModelParseResult { len = val.length + val = val.trim() if (val.indexOf('[') < 0 || val.lastIndexOf(']') < len - 1) { index = val.lastIndexOf('.') diff --git a/test/unit/features/directives/model-text.spec.js b/test/unit/features/directives/model-text.spec.js index 7aae24b654e..ac648231e03 100644 --- a/test/unit/features/directives/model-text.spec.js +++ b/test/unit/features/directives/model-text.spec.js @@ -19,6 +19,16 @@ describe('Directive v-model text', () => { }).then(done) }) + it('should not create a new property ended with spaces when', () => { + const vm = new Vue({ + data: { + test: 'b' + }, + template: '' + }).$mount() + expect(vm['test ']).toBe(undefined) + }) + it('.lazy modifier', () => { const vm = new Vue({ data: { From 47afa07883c7323b2792b4e62986bfeff11473ec Mon Sep 17 00:00:00 2001 From: Leonardo Cardoso Date: Fri, 2 Mar 2018 12:22:16 -0300 Subject: [PATCH 2/4] fix(v-model): putting the value in a previous line and adding one more condition in the test --- src/compiler/directives/model.js | 2 +- test/unit/features/directives/model-text.spec.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compiler/directives/model.js b/src/compiler/directives/model.js index 4ebc66d61e3..2d0492465e6 100644 --- a/src/compiler/directives/model.js +++ b/src/compiler/directives/model.js @@ -68,8 +68,8 @@ type ModelParseResult = { } export function parseModel (val: string): ModelParseResult { - len = val.length val = val.trim() + len = val.length if (val.indexOf('[') < 0 || val.lastIndexOf(']') < len - 1) { index = val.lastIndexOf('.') diff --git a/test/unit/features/directives/model-text.spec.js b/test/unit/features/directives/model-text.spec.js index ac648231e03..39c889fe34f 100644 --- a/test/unit/features/directives/model-text.spec.js +++ b/test/unit/features/directives/model-text.spec.js @@ -26,7 +26,8 @@ describe('Directive v-model text', () => { }, template: '' }).$mount() - expect(vm['test ']).toBe(undefined) + expect(vm['test ']).toBe(undefined); + expect(vm.test).toBe('b'); }) it('.lazy modifier', () => { From 37aeb19d0e5a3b4fe6cd427f030c61cf68349c79 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 4 Mar 2018 11:53:58 +0100 Subject: [PATCH 3/4] test(model): use nested object in v-model + whitespace test Fix #7730 --- test/unit/features/directives/model-text.spec.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/unit/features/directives/model-text.spec.js b/test/unit/features/directives/model-text.spec.js index 39c889fe34f..e47fc93bbe9 100644 --- a/test/unit/features/directives/model-text.spec.js +++ b/test/unit/features/directives/model-text.spec.js @@ -19,15 +19,19 @@ describe('Directive v-model text', () => { }).then(done) }) - it('should not create a new property ended with spaces when', () => { + it('should work with space ended expression in v-model', () => { const vm = new Vue({ data: { - test: 'b' + obj: { + test: 'b' + } }, - template: '' + template: '' }).$mount() - expect(vm['test ']).toBe(undefined); - expect(vm.test).toBe('b'); + + triggerEvent(vm.$el, 'input') + expect(vm.obj['test ']).toBe(undefined) + expect(vm.obj.test).toBe('b') }) it('.lazy modifier', () => { From ea11fc4b465a3217c083d90c079d8e4a70b7abb0 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 4 Mar 2018 12:00:51 +0100 Subject: [PATCH 4/4] chore(model): add coment for val.trim() [skip ci] --- src/compiler/directives/model.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compiler/directives/model.js b/src/compiler/directives/model.js index 2d0492465e6..39bdbfda4c2 100644 --- a/src/compiler/directives/model.js +++ b/src/compiler/directives/model.js @@ -68,6 +68,8 @@ 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