refactor(runtime-dom): avoid unnecessary typeof checks during props patching

This commit is contained in:
Evan You 2020-11-30 20:05:02 -05:00
parent fb6b9f8e8f
commit 5a19bb5320

View File

@ -24,6 +24,7 @@ export function patchDOMProp(
el[key] = value == null ? '' : value
return
}
if (key === 'value' && el.tagName !== 'PROGRESS') {
// store value as _value as well since
// non-string values will be stringified.
@ -34,29 +35,36 @@ export function patchDOMProp(
}
return
}
if (value === '' && typeof el[key] === 'boolean') {
// e.g. <select multiple> compiles to { multiple: '' }
el[key] = true
} else if (value == null && typeof el[key] === 'string') {
// e.g. <div :id="null">
el[key] = ''
el.removeAttribute(key)
} else if ((value == null || value === '') && typeof el[key] === 'number') {
// e.g. <img :width="null">
el[key] = 0
el.removeAttribute(key)
} else {
// some properties perform value validation and throw
try {
el[key] = value
} catch (e) {
if (__DEV__) {
warn(
`Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
`value ${value} is invalid.`,
e
)
}
if (value === '' || value == null) {
const type = typeof el[key]
if (value === '' && type === 'boolean') {
// e.g. <select multiple> compiles to { multiple: '' }
el[key] = true
return
} else if (value == null && type === 'string') {
// e.g. <div :id="null">
el[key] = ''
el.removeAttribute(key)
return
} else if (type === 'number') {
// e.g. <img :width="null">
el[key] = 0
el.removeAttribute(key)
return
}
}
// some properties perform value validation and throw
try {
el[key] = value
} catch (e) {
if (__DEV__) {
warn(
`Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
`value ${value} is invalid.`,
e
)
}
}
}