Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure value for OPTION elements is properly set in edge case (fix #4956) #4959

Merged
merged 2 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ describe('runtime-dom: props patching', () => {
// so we need to add tests for other elements
test('value for non-text input', () => {
const el = document.createElement('option')
el.textContent = 'foo' // #4956
patchProp(el, 'value', null, 'foo')
expect(el.getAttribute('value')).toBe('foo')
expect(el.value).toBe('foo')
patchProp(el, 'value', null, null)
el.textContent = ''
expect(el.value).toBe('')
// #3475
expect(el.getAttribute('value')).toBe(null)
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export function patchDOMProp(
// non-string values will be stringified.
el._value = value
const newValue = value == null ? '' : value
if (el.value !== newValue) {
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
el.tagName === 'OPTION'
) {
el.value = newValue
}
if (value == null) {
Expand Down