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,18 +35,26 @@ export function patchDOMProp(
}
return
}
if (value === '' && typeof el[key] === 'boolean') {
if (value === '' || value == null) {
const type = typeof el[key]
if (value === '' && type === 'boolean') {
// e.g. <select multiple> compiles to { multiple: '' }
el[key] = true
} else if (value == null && typeof el[key] === 'string') {
return
} else if (value == null && type === 'string') {
// e.g. <div :id="null">
el[key] = ''
el.removeAttribute(key)
} else if ((value == null || value === '') && typeof el[key] === 'number') {
return
} else if (type === 'number') {
// e.g. <img :width="null">
el[key] = 0
el.removeAttribute(key)
} else {
return
}
}
// some properties perform value validation and throw
try {
el[key] = value
@ -59,4 +68,3 @@ export function patchDOMProp(
}
}
}
}