]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): support Symbol for input value bindings (#10608)
authorXu Wei <616525957@qq.com>
Mon, 10 Jun 2024 09:01:56 +0000 (17:01 +0800)
committerGitHub <noreply@github.com>
Mon, 10 Jun 2024 09:01:56 +0000 (17:01 +0800)
close #10597

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

index f06fb4a5ea6ae741658d608c72a893baef0100a7..8436bbb4f960210b3398f0e3a274042222076405 100644 (file)
@@ -53,4 +53,20 @@ describe('runtime-dom: attrs patching', () => {
     patchProp(el, 'onwards', 'a', null)
     expect(el.getAttribute('onwards')).toBe(null)
   })
+
+  // #10597
+  test('should allow setting attribute to symbol', () => {
+    const el = document.createElement('div')
+    const symbol = Symbol('foo')
+    patchProp(el, 'foo', null, symbol)
+    expect(el.getAttribute('foo')).toBe(symbol.toString())
+  })
+
+  // #10598
+  test('should allow setting value to symbol', () => {
+    const el = document.createElement('input')
+    const symbol = Symbol('foo')
+    patchProp(el, 'value', null, symbol)
+    expect(el.value).toBe(symbol.toString())
+  })
 })
index 8d8c10c31bf391c60335d1fc6aca5b34ce97c50b..8218e182fb60175c6b8fdb23a6b491d59fe43a33 100644 (file)
@@ -36,7 +36,8 @@ export function patchAttr(
     if (value == null || (isBoolean && !includeBooleanAttr(value))) {
       el.removeAttribute(key)
     } else {
-      el.setAttribute(key, isBoolean ? '' : value)
+      // attribute value is a string https://html.spec.whatwg.org/multipage/dom.html#attributes
+      el.setAttribute(key, isBoolean ? '' : String(value))
     }
   }
 }
index c4bb4987b8c595db5a2a598763e5aa35de2aba57..2eb83ea39f74303312fdbcf609a3524f999258f5 100644 (file)
@@ -38,7 +38,7 @@ export function patchDOMProp(
     // compare against its attribute value instead.
     const oldValue =
       tag === 'OPTION' ? el.getAttribute('value') || '' : el.value
-    const newValue = value == null ? '' : value
+    const newValue = value == null ? '' : String(value)
     if (oldValue !== newValue || !('_value' in el)) {
       el.value = newValue
     }