Skip to content

fix #8862: cannot watch unicode properties like 'a.中文' #8925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/core/observer/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,28 @@ export default class Watcher {
if (typeof expOrFn === 'function') {
this.getter = expOrFn
} else {
this.getter = parsePath(expOrFn)
if (!this.getter) {
const bailRE = /[^\w.$]/
const delimiterRE = /[ ,/\\]/
if (bailRE.test(expOrFn)) {
if (process.env.NODE_ENV !== 'production') {
if (delimiterRE.test(expOrFn)) {
warn(
`Failed watching path: "${expOrFn}" ` +
'Watcher only accepts simple dot-delimited paths. ' +
'For full control, use a function instead.',
vm
)
} else {
warn(
`Failed watching path: "${expOrFn}" ` +
'Vue watchers only accept alphanumeric, $, or underscore naming of properties.',
vm
)
}
}
this.getter = function () {}
process.env.NODE_ENV !== 'production' && warn(
`Failed watching path: "${expOrFn}" ` +
'Watcher only accepts simple dot-delimited paths. ' +
'For full control, use a function instead.',
vm
)
} else {
this.getter = parsePath(expOrFn)
}
}
this.value = this.lazy
Expand Down
4 changes: 0 additions & 4 deletions src/core/util/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ export function def (obj: Object, key: string, val: any, enumerable?: boolean) {
/**
* Parse simple path.
*/
const bailRE = /[^\w.$]/
export function parsePath (path: string): any {
if (bailRE.test(path)) {
return
}
const segments = path.split('.')
return function (obj) {
for (let i = 0; i < segments.length; i++) {
Expand Down
5 changes: 5 additions & 0 deletions test/unit/modules/observer/watcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,9 @@ describe('Watcher', () => {
new Watcher(vm, 'd.e + c', spy)
expect('Failed watching path:').toHaveBeenWarned()
})

it('does not support non-alphanumeric paths and throws a warning', () => {
new Watcher(vm, '中文', spy)
expect('Failed watching path:').toHaveBeenWarned()
})
})