refactor: avoid unnecessary style normalization

This commit is contained in:
Evan You 2018-09-20 02:17:52 -04:00
parent 1ef0d03829
commit 369997bb1d

View File

@ -4,12 +4,6 @@ import { isObservable } from '@vue/core'
const nonNumericRE = /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i const nonNumericRE = /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i
export function patchStyle(el: any, prev: any, next: any, data: any) { export function patchStyle(el: any, prev: any, next: any, data: any) {
// If next is observed, the user is likely to mutate the style object.
// We need to normalize + clone it and replace data.style with the clone.
if (isObservable(next)) {
data.style = normalizeStyle(next)
}
const { style } = el const { style } = el
if (!next) { if (!next) {
el.removeAttribute('style') el.removeAttribute('style')
@ -17,9 +11,14 @@ export function patchStyle(el: any, prev: any, next: any, data: any) {
style.cssText = next style.cssText = next
} else { } else {
// TODO: warn invalid value in dev // TODO: warn invalid value in dev
next = normalizeStyle(next) const normalizedNext: any = normalizeStyle(next)
for (const key in next) { // If next is observed, the user is likely to mutate the style object.
let value = next[key] // We need to replace data.style with the normalized clone.
if (isObservable(next)) {
data.style = normalizedNext
}
for (const key in normalizedNext) {
let value = normalizedNext[key]
if (typeof value === 'number' && !nonNumericRE.test(key)) { if (typeof value === 'number' && !nonNumericRE.test(key)) {
value = value + 'px' value = value + 'px'
} }
@ -28,7 +27,7 @@ export function patchStyle(el: any, prev: any, next: any, data: any) {
if (prev && typeof prev !== 'string') { if (prev && typeof prev !== 'string') {
prev = normalizeStyle(prev) prev = normalizeStyle(prev)
for (const key in prev) { for (const key in prev) {
if (!next[key]) { if (!normalizedNext[key]) {
style.setProperty(key, '') style.setProperty(key, '')
} }
} }