Skip to content

Commit

Permalink
fix(runtime-dom): check attribute value before set option value
Browse files Browse the repository at this point in the history
  • Loading branch information
linghaoSu committed May 7, 2023
1 parent 862edfd commit 13a2dfb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ describe('runtime-dom: props patching', () => {
patchProp(el, 'value', null, obj)
expect(el.value).toBe(obj.toString())
expect((el as any)._value).toBe(obj)

const option = document.createElement('option')
patchProp(option, 'textContent', null, 'foo')
expect(option.value).toBe('foo')
expect(option.getAttribute('value')).toBe(null)
patchProp(option, 'value', null, 'foo')
expect(option.value).toBe('foo')
expect(option.getAttribute('value')).toBe('foo')
})

test('value for custom elements', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ export function patchDOMProp(
// non-string values will be stringified.
el._value = value
const newValue = value == null ? '' : value

if (
el.value !== newValue ||
// #4956: always set for OPTION elements because its value falls back to
// textContent if no value attribute is present. And setting .value for
// OPTION has no side effect
// #8227: set value will trigger autocomplete,
// so only set when attribute value !== newValue
el.tagName === 'OPTION'
) {
const attrValue = el.getAttribute('value')

if (attrValue !== newValue) {
el.value = newValue
}
} else if (el.value !== newValue) {
el.value = newValue
}
if (value == null) {
Expand Down

0 comments on commit 13a2dfb

Please sign in to comment.