Skip to content

Commit

Permalink
feat: support custom toString() in text interpolation and v-html (#8217)
Browse files Browse the repository at this point in the history
close #8093
  • Loading branch information
mathieutu authored and yyx990803 committed Dec 21, 2018
1 parent 1933ee8 commit 0e4e45e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function isPromise (val: any): boolean {
export function toString (val: any): string {
return val == null
? ''
: typeof val === 'object'
: Array.isArray(val) || (isPlainObject(val) && val.toString === _toString)
? JSON.stringify(val, null, 2)
: String(val)
}
Expand Down
6 changes: 6 additions & 0 deletions test/unit/features/directives/html.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ describe('Directive v-html', () => {
vm.a = {}
}).then(() => {
expect(vm.$el.innerHTML).toBe('{}')
vm.a = { toString () { return 'foo' } }
}).then(() => {
expect(vm.$el.innerHTML).toBe('foo')
vm.a = { toJSON () { return { foo: 'bar' } } }
}).then(() => {
expect(vm.$el.innerHTML).toBe('{\n "foo": "bar"\n}')
vm.a = 123
}).then(() => {
expect(vm.$el.innerHTML).toBe('123')
Expand Down
6 changes: 6 additions & 0 deletions test/unit/features/directives/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ describe('Directive v-text', () => {
vm.a = {}
}).then(() => {
expect(vm.$el.innerHTML).toBe('{}')
vm.a = { toString () { return 'foo' } }
}).then(() => {
expect(vm.$el.innerHTML).toBe('foo')
vm.a = { toJSON () { return { foo: 'bar' } } }
}).then(() => {
expect(vm.$el.innerHTML).toBe('{\n "foo": "bar"\n}')
vm.a = 123
}).then(() => {
expect(vm.$el.innerHTML).toBe('123')
Expand Down

0 comments on commit 0e4e45e

Please sign in to comment.