feat(runtime-dom): support !important for patchStyle. (#422)

This commit is contained in:
Cr
2019-11-09 11:06:53 +08:00
committed by Evan You
parent 3b8566149a
commit 34e2725e9b
2 changed files with 68 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
import { isString } from '@vue/shared'
import { isString, hyphenate } from '@vue/shared'
type Style = string | Partial<CSSStyleDeclaration> | null
@@ -10,14 +10,31 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
style.cssText = next
} else {
for (const key in next) {
style[key] = next[key] as string
setStyle(style, key, next[key] as string)
}
if (prev && !isString(prev)) {
for (const key in prev) {
if (!next[key]) {
style[key] = ''
setStyle(style, key, '')
}
}
}
}
}
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)
}
}