]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(shared): support custom .toString() in text interpolation again (#4210)
authorRoan Kattouw <roan.kattouw@gmail.com>
Thu, 29 Jul 2021 14:51:03 +0000 (10:51 -0400)
committerGitHub <noreply@github.com>
Thu, 29 Jul 2021 14:51:03 +0000 (10:51 -0400)
fix #3944

packages/shared/__tests__/toDisplayString.spec.ts
packages/shared/src/toDisplayString.ts

index f9e20841e490283a6258eaa025bb74a7f2678123..8e76c4322f9c80358b770fe0d46b10ed0fba68d6 100644 (file)
@@ -31,10 +31,22 @@ describe('toDisplayString', () => {
       })
     ).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
   })
+  
+  test('objects with custom toString', () => {
+    class TestClass {
+      toString() {
+        return 'foo'
+      }
+    }
+    const instance = new TestClass()
+    expect(toDisplayString(instance)).toBe('foo')
+    const obj = { toString: () => 'bar' }
+    expect(toDisplayString(obj)).toBe('bar')
+  })
 
   test('native objects', () => {
     const div = document.createElement('div')
-    expect(toDisplayString(div)).toBe(`"[object HTMLDivElement]"`)
+    expect(toDisplayString(div)).toBe('[object HTMLDivElement]')
     expect(toDisplayString({ div })).toMatchInlineSnapshot(`
       "{
         \\"div\\": \\"[object HTMLDivElement]\\"
index bcfe1a90721e402cba4f68439b4462d28221ffb1..a374f525d6bb8589097b5a01c65cd8a609a38dc5 100644 (file)
@@ -1,4 +1,11 @@
-import { isArray, isMap, isObject, isPlainObject, isSet } from './index'
+import {
+  isArray,
+  isMap,
+  isObject,
+  isPlainObject,
+  isSet,
+  objectToString
+} from './index'
 
 /**
  * For converting {{ interpolation }} values to displayed strings.
@@ -7,7 +14,7 @@ import { isArray, isMap, isObject, isPlainObject, isSet } from './index'
 export const toDisplayString = (val: unknown): string => {
   return val == null
     ? ''
-    : isObject(val)
+    : isArray(val) || (isObject(val) && val.toString === objectToString)
     ? JSON.stringify(val, replacer, 2)
     : String(val)
 }