2019-11-09 11:06:53 +08:00
|
|
|
import { isString, hyphenate } from '@vue/shared'
|
2018-10-17 03:47:51 +08:00
|
|
|
|
2019-10-16 22:31:40 +08:00
|
|
|
type Style = string | Partial<CSSStyleDeclaration> | null
|
|
|
|
|
|
|
|
export function patchStyle(el: Element, prev: Style, next: Style) {
|
|
|
|
const style = (el as HTMLElement).style
|
2018-09-19 23:35:38 +08:00
|
|
|
if (!next) {
|
|
|
|
el.removeAttribute('style')
|
2018-10-17 03:47:51 +08:00
|
|
|
} 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) {
|
2019-11-09 11:06:53 +08:00
|
|
|
setStyle(style, key, next[key] as string)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2018-10-17 03:47:51 +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]) {
|
2019-11-09 11:06:53 +08:00
|
|
|
setStyle(style, key, '')
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 11:06:53 +08:00
|
|
|
|
|
|
|
const importantRE = /\s*!important$/
|
|
|
|
|
|
|
|
function setStyle(style: CSSStyleDeclaration, name: string, val: string) {
|
|
|
|
let rawName = hyphenate(name)
|
|
|
|
if (importantRE.test(val)) {
|
|
|
|
style.setProperty(rawName, val.replace(importantRE, ''), 'important')
|
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* TODO:
|
|
|
|
* 1. support values array created by autoprefixer.
|
|
|
|
* 2. support css variable, maybe should import 'csstype'.
|
|
|
|
* similar to https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L1450
|
|
|
|
*/
|
|
|
|
style.setProperty(rawName, val)
|
|
|
|
}
|
|
|
|
}
|