Skip to content

Commit

Permalink
Handle toString; add tests for other builtins
Browse files Browse the repository at this point in the history
This changes current support for __proto__ in key paths.

See: kazupon#1706
  • Loading branch information
alxndrsn committed May 3, 2024
1 parent 6895542 commit 93c7456
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ export default class VueI18n {
if (!message || !key) { return false }
if (!isNull(this._path.getPathValue(message, key))) { return true }
// fallback for flat key
if (message[key]) { return true }
return false
return Object.prototype.hasOwnProperty.call(message, key)
}

if (this._warnHtmlInMessage === 'warn' || this._warnHtmlInMessage === 'error') {
Expand Down
3 changes: 3 additions & 0 deletions src/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ export default class I18nPath {
let last: any = obj
let i: number = 0
while (i < length) {
if (!Object.prototype.hasOwnProperty.call(last, paths[i])) {
return null
}
const value: any = last[paths[i]]
if (value === undefined || value === null) {
return null
Expand Down
83 changes: 83 additions & 0 deletions test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,89 @@ describe('basic', () => {
assert(vm.$te('message.hello', 'xx') === false)
})
})

describe('builtin property handling', () => {
[
// '__proto__', // TODO: see https://github.com/kazupon/vue-i18n/issues/1706
'constructor',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'toLocaleString',
'toString',
'valueOf'
].forEach(k => {
describe('top-level props', () => {
describe('existing key', () => {
let vm;
beforeEach(() => {
const i18n = new VueI18n({
locale: 'en',
fallbackLocale: 'en',
messages: {
en: {
[k]: 'i exist'
},
ja: {
[k]: 'i exist (ja)'
},
},
modifiers: {
custom: str => str.replace(/[aeiou]/g, 'x')
}
})
vm = new Vue({ i18n })
})

it('should return true', () => {
assert(vm.$te(k) === true)
})

it('should return true with locale', () => {
assert(vm.$te(k, 'ja') === true)
})
})

describe('not existing key', () => {
it('should return false', () => {
const vm = new Vue({ i18n })
assert(vm.$te(k) === false)
})

it('should return false with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te(k, 'ja') === false)
})
})
})

describe('deep props', () => {
describe('existing key', () => {
it('should return true', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.existing.${k}`) === true)
})

it('should return true with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.existing.${k}`, 'ja') === true)
})
})

describe('not existing key', () => {
it('should return false', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.missing.${k}`) === false)
})

it('should return false with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te(`issues.builtins.missing.${k}`, 'ja') === false)
})
})
})
})
})
})

describe('i18n#locale', () => {
Expand Down
32 changes: 29 additions & 3 deletions test/unit/fixture/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
en: {
message: {
hello: 'the world',
Expand Down Expand Up @@ -70,7 +70,20 @@ export default {
]
],
issues: {
arrayBugs: ['bug1', 'bug2']
arrayBugs: ['bug1', 'bug2'],
builtins: {
existing: {
// '__proto__': 'i exist', // TODO
'constructor': 'i exist',
'hasOwnProperty': 'i exist',
'isPrototypeOf': 'i exist',
'propertyIsEnumerable': 'i exist',
'toLocaleString': 'i exist',
'toString': 'i exist',
'valueOf': 'i exist'
},
missing: {}
}
},
'foo.bar.buz': 'hello flat key!'
},
Expand Down Expand Up @@ -111,7 +124,20 @@ export default {
]
],
issues: {
arrayBugs: ['バグ1', 'バグ2']
arrayBugs: ['バグ1', 'バグ2'],
builtins: {
existing: {
// '__proto__': 'i exist (ja)', // TODO
'constructor': 'i exist (ja)',
'hasOwnProperty': 'i exist (ja)',
'isPrototypeOf': 'i exist (ja)',
'propertyIsEnumerable': 'i exist (ja)',
'toLocaleString': 'i exist (ja)',
'toString': 'i exist (ja)',
'valueOf': 'i exist (ja)'
},
missing: {}
}
},
'foo.bar.buz': 'こんにちは、フラットなキーさん!'
}
Expand Down

0 comments on commit 93c7456

Please sign in to comment.