8000 do not sync boolean literal props + cast number literal props · Snoopbobb/vue@14e01eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 14e01eb

Browse files
committed
do not sync boolean literal props + cast number literal props
1 parent 21bfe7b commit 14e01eb

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/compiler/compile.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ function makeChildLinkFn (linkFns) {
392392
// regex to test if a path is "settable"
393393
// if not the prop binding is automatically one-way.
394394
var settablePathRE = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*|\[[^\[\]]+\])*$/
395+
var literalValueRE = /^true|false|\d+$/
395396

396397
function compileProps (el, attrs, propNames) {
397398
var props = []
@@ -426,7 +427,8 @@ function compileProps (el, attrs, propNames) {
426427
prop.oneTime =
427428
tokens.length > 1 ||
428429
tokens[0].oneTime ||
429-
!settablePathRE.test(prop.value)
430+
!settablePathRE.test(prop.value) ||
431+
literalValueRE.test(prop.value)
430432
}
431433
props.push(prop)
432434
}
@@ -469,7 +471,7 @@ function makePropsLinkFn (props) {
469471
}
470472
} else {
471473
// just set once
472-
vm.$set(path, prop.raw)
474+
vm.$set(path, _.toNumber(prop.raw))
473475
}
474476
}
475477
}

test/unit/specs/compiler/compile_spec.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ if (_.inBrowser) {
154154
'multiple-attrs',
155155
'oneway',
156156
'with-filter',
157-
'camelCase'
157+
'camelCase',
158+
'boolean-literal'
158159
]
159160
})
160161
var def = Vue.options.directives._prop
@@ -164,11 +165,12 @@ if (_.inBrowser) {
164165
el.setAttribute('multiple-attrs', 'a {{b}} c')
165166
el.setAttribute('oneway', '{{*a}}')
166167
el.setAttribute('with-filter', '{{a | filter}}')
168+
el.setAttribute('boolean-literal', '{{true}}')
167169
transclude(el, options)
168170
var linker = compile(el, options)
169171
linker(vm, el)
170172
// should skip literals and one-time bindings
171-
expect(vm._bindDir.calls.count()).toBe(4)
173+
expect(vm._bindDir.calls.count()).toBe(5)
172174
// data-some-attr
173175
var args = vm._bindDir.calls.argsFor(0)
174176
expect(args[0]).toBe('prop')
@@ -198,11 +200,19 @@ if (_.inBrowser) {
198200
expect(args[2].arg).toBe('withFilter')
199201
expect(args[2].expression).toBe('this._applyFilters(a,null,[{"name":"filter"}],false)')
200202
expect(args[3]).toBe(def)
203+
// boolean-literal
204+
args = vm._bindDir.calls.argsFor(4)
205+
expect(args[0]).toBe('prop')
206+
expect(args[1]).toBe(null)
207+
expect(args[2].arg).toBe('booleanLiteral')
208+
expect(args[2].expression).toBe('true')
209+
expect(args[2].oneWay).toBe(true)
201210
// camelCase should've warn
202211
expect(hasWarned(_, 'using camelCase')).toBe(true)
203212
// literal and one time should've called vm.$set
204-
expect(vm.$set).toHaveBeenCalledWith('a', '1')
205-
expect(vm.$set).toHaveBeenCalledWith('someOtherAttr', '2')
213+
// and numbers should be casted
214+
expect(vm.$set).toHaveBeenCalledWith('a', 1)
215+
expect(vm.$set).toHaveBeenCalledWith('someOtherAttr', 2)
206216
})
207217

208218
it('props on root instance', function () {

0 commit comments

Comments
 (0)
0