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

fix(runtime-dom): check attribute value before set option value #8246

Merged
merged 2 commits into from
May 8, 2023
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
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
19 changes: 9 additions & 10 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@ export function patchDOMProp(
return
}

const tag = el.tagName

if (
key === 'value' &&
el.tagName !== 'PROGRESS' &&
tag !== 'PROGRESS' &&
// custom elements may use _value internally
!el.tagName.includes('-')
!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 (
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'
) {
if (oldValue !== newValue) {
el.value = newValue
}
if (value == null) {
Expand Down Expand Up @@ -98,7 +97,7 @@ export function patchDOMProp(
// do not warn if value is auto-coerced from nullish values
if (__DEV__ && !needRemove) {
warn(
`Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
`Failed setting prop "${key}" on <${tag.toLowerCase()}>: ` +
`value ${value} is invalid.`,
e
)
Expand Down