-
-
Notifications
You must be signed in to change notification settings - Fork 779
/
variables.ts
45 lines (40 loc) · 1.03 KB
/
variables.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
import type { Rule } from '@unocss/core'
import { handler as h } from '../utils'
const variablesAbbrMap: Record<string, string> = {
backface: 'backface-visibility',
break: 'word-break',
case: 'text-transform',
content: 'align-content',
fw: 'font-weight',
items: 'align-items',
justify: 'justify-content',
select: 'user-select',
self: 'align-self',
vertical: 'vertical-align',
visible: 'visibility',
whitespace: 'white-space',
ws: 'white-space',
}
export const cssVariables: Rule[] = [
[/^(.+?)-(\$.+)$/, ([, name, varname]) => {
const prop = variablesAbbrMap[name]
if (prop)
return { [prop]: h.cssvar(varname) }
}],
]
export const cssProperty: Rule[] = [
[/^\[(--(\w|\\\W)+|[\w-]+):(.+)\]$/, ([match, prop,, value]) => {
if (!isURI(match.slice(1, -1)))
return { [prop]: h.bracket(`[${value}]`) }
}],
]
function isURI(declaration: string) {
if (!declaration.includes('://'))
return false
try {
return new URL(declaration).host !== ''
}
catch (err) {
return false
}
}