-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathstyle.ts
64 lines (59 loc) · 1.62 KB
/
style.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { camelize, capitalize, hyphenate, isArray } from '@vue/shared'
export type Style = string | Record<string, string | string[]> | null
const semicolonRE = /[^\\];\s*$/
const importantRE = /\s*!important$/
export function setStyle(
style: CSSStyleDeclaration,
name: string,
val: string | string[],
warn: (msg: string, ...args: any[]) => void,
): void {
if (isArray(val)) {
val.forEach(v => setStyle(style, name, v, warn))
} else {
if (val == null) val = ''
if (__DEV__) {
if (semicolonRE.test(val)) {
warn(
`Unexpected semicolon at the end of '${name}' style value: '${val}'`,
)
}
}
if (name.startsWith('--')) {
// custom property definition
style.setProperty(name, val)
} else {
const prefixed = autoPrefix(style, name)
if (importantRE.test(val)) {
// !important
style.setProperty(
hyphenate(prefixed),
val.replace(importantRE, ''),
'important',
)
} else {
style[prefixed as any] = val
}
}
}
}
const prefixes = ['Webkit', 'Moz', 'ms']
const prefixCache: Record<string, string> = {}
function autoPrefix(style: CSSStyleDeclaration, rawName: string): string {
const cached = prefixCache[rawName]
if (cached) {
return cached
}
let name = camelize(rawName)
if (name !== 'filter' && name in style) {
return (prefixCache[rawName] = name)
}
name = capitalize(name)
for (let i = 0; i < prefixes.length; i++) {
const prefixed = prefixes[i] + name
if (prefixed in style) {
return (prefixCache[rawName] = prefixed)
}
}
return rawName
}