vue3-core/packages/runtime-dom/src/modules/style.ts

29 lines
733 B
TypeScript
Raw Normal View History

import { isString } from '@vue/shared'
2018-09-19 23:35:38 +08:00
// style properties that should NOT have "px" added when numeric
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) {
const { style } = el
if (!next) {
el.removeAttribute('style')
} else if (isString(next)) {
2018-09-19 23:35:38 +08:00
style.cssText = next
} else {
2018-09-25 07:11:14 +08:00
for (const key in next) {
let value = next[key]
2018-09-19 23:35:38 +08:00
if (typeof value === 'number' && !nonNumericRE.test(key)) {
value = value + 'px'
}
2018-09-25 06:51:58 +08:00
style[key] = value
2018-09-19 23:35:38 +08:00
}
if (prev && !isString(prev)) {
2018-09-19 23:35:38 +08:00
for (const key in prev) {
2018-09-25 07:11:14 +08:00
if (!next[key]) {
2018-09-25 06:51:58 +08:00
style[key] = ''
2018-09-19 23:35:38 +08:00
}
}
}
}
}