]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): ensure only symbols are explicitly stringified during attribute...
authorThorsten Lünborg <t.luenborg@googlemail.com>
Sat, 22 Jun 2024 09:04:41 +0000 (11:04 +0200)
committerGitHub <noreply@github.com>
Sat, 22 Jun 2024 09:04:41 +0000 (17:04 +0800)
close #11177

packages/runtime-dom/__tests__/patchAttrs.spec.ts
packages/runtime-dom/src/modules/attrs.ts

index 8436bbb4f960210b3398f0e3a274042222076405..393b685b0e9b3fbe7b3d7022baea0afb5f71ac33 100644 (file)
@@ -69,4 +69,23 @@ describe('runtime-dom: attrs patching', () => {
     patchProp(el, 'value', null, symbol)
     expect(el.value).toBe(symbol.toString())
   })
+
+  // #11177
+  test('should allow setting value to object, leaving stringification to the element/browser', () => {
+    // normal behavior
+    const el = document.createElement('div')
+    const obj = { toString: () => 'foo' }
+    patchProp(el, 'data-test', null, obj)
+    expect(el.dataset.test).toBe('foo')
+
+    const el2 = document.createElement('div')
+    let testvalue: null | typeof obj = null
+    // simulating a web component that implements its own setAttribute handler
+    el2.setAttribute = (name, value) => {
+      testvalue = value
+    }
+    patchProp(el2, 'data-test', null, obj)
+    expect(el2.dataset.test).toBe(undefined)
+    expect(testvalue).toBe(obj)
+  })
 })
index 3cc3468a7757902628a6d72cdb83cc598ced2f37..6e411f5b28726f777193980780db2594216c87c5 100644 (file)
@@ -2,6 +2,7 @@ import {
   NOOP,
   includeBooleanAttr,
   isSpecialBooleanAttr,
+  isSymbol,
   makeMap,
 } from '@vue/shared'
 import {
@@ -37,7 +38,10 @@ export function patchAttr(
       el.removeAttribute(key)
     } else {
       // attribute value is a string https://html.spec.whatwg.org/multipage/dom.html#attributes
-      el.setAttribute(key, isBoolean ? '' : String(value))
+      el.setAttribute(
+        key,
+        isBoolean ? '' : isSymbol(value) ? String(value) : value,
+      )
     }
   }
 }