]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): avoid unset option's value (#10416)
authorDoctor Wu <44631608+Doctor-wu@users.noreply.github.com>
Wed, 28 Feb 2024 11:07:12 +0000 (19:07 +0800)
committerGitHub <noreply@github.com>
Wed, 28 Feb 2024 11:07:12 +0000 (19:07 +0800)
close #10412
re-fix #10396

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

index 7c356f1a66cb01d8f34f4ca9da4483c9a1b3013d..61dd98513ce3ad84eea2575cd94ec4545039d5a7 100644 (file)
@@ -291,6 +291,18 @@ describe('runtime-dom: props patching', () => {
     expect(el.value).toBe('baz')
   })
 
+  test('init empty value for option', () => {
+    const root = document.createElement('div')
+    render(
+      h('select', { value: 'foo' }, [h('option', { value: '' }, 'foo')]),
+      root,
+    )
+    const select = root.children[0] as HTMLSelectElement
+    const option = select.children[0] as HTMLOptionElement
+    expect(select.value).toBe('')
+    expect(option.value).toBe('')
+  })
+
   // #8780
   test('embedded tag with width and height', () => {
     // Width and height of some embedded element such as img、video、source、canvas
index e9b3bca357de636d3f0ef9b5b041337ef36e9caf..c4bb4987b8c595db5a2a598763e5aa35de2aba57 100644 (file)
@@ -34,20 +34,20 @@ export function patchDOMProp(
     // custom elements may use _value internally
     !tag.includes('-')
   ) {
-    // store value as _value as well since
-    // non-string values will be stringified.
-    el._value = value
     // #4956: <option> value will fallback to its text content so we need to
     // compare against its attribute value instead.
     const oldValue =
       tag === 'OPTION' ? el.getAttribute('value') || '' : el.value
     const newValue = value == null ? '' : value
-    if (oldValue !== newValue) {
+    if (oldValue !== newValue || !('_value' in el)) {
       el.value = newValue
     }
     if (value == null) {
       el.removeAttribute(key)
     }
+    // store value as _value as well since
+    // non-string values will be stringified.
+    el._value = value
     return
   }