-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathtokenTypes.js
75 lines (64 loc) · 2.54 KB
/
tokenTypes.js
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
65
66
67
68
69
70
71
72
73
74
75
import { stringify } from 'postcss-value-parser'
import cssColorKeywords from 'css-color-keywords'
const matchString = node => {
if (node.type !== 'string') return null
return node.value
.replace(/\\([0-9a-f]{1,6})(?:\s|$)/gi, (match, charCode) =>
String.fromCharCode(parseInt(charCode, 16))
)
.replace(/\\/g, '')
}
const hexColorRe = /^(#(?:[0-9a-f]{3,4}){1,2})$/i
const cssFunctionNameRe = /^(rgba?|hsla?|hwb|lab|lch|gray|color)$/
const matchColor = node => {
if (
node.type === 'word' &&
(hexColorRe.test(node.value) ||
node.value in cssColorKeywords ||
node.value === 'transparent')
) {
return node.value
} else if (node.type === 'function' && cssFunctionNameRe.test(node.value)) {
return stringify(node)
}
return null
}
const noneRe = /^(none)$/i
const autoRe = /^(auto)$/i
const identRe = /(^-?[_a-z][_a-z0-9-]*$)/i
// Note if these are wrong, you'll need to change index.js too
const numberRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?)$/i
// Note lengthRe is sneaky: you can omit units for 0
const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?)(?=px$))/i
const unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?(ch|em|ex|rem|vh|vw|vmin|vmax|cm|mm|in|pc|pt))$/i
const angleRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?(?:deg|rad))$/i
const percentRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?%)$/i
const noopToken = predicate => node => (predicate(node) ? '<token>' : null)
const valueForTypeToken = type => node =>
node.type === type ? node.value : null
export const regExpToken = (regExp, transform = String) => node => {
if (node.type !== 'word') return null
const match = node.value.match(regExp)
if (match === null) return null
const value = transform(match[1])
return value
}
export const SPACE = noopToken(node => node.type === 'space')
export const SLASH = noopToken(
node => node.type === 'div' && node.value === '/'
)
export const COMMA = noopToken(
node => node.type === 'div' && node.value === ','
)
export const WORD = valueForTypeToken('word')
export const NONE = regExpToken(noneRe)
export const AUTO = regExpToken(autoRe)
export const NUMBER = regExpToken(numberRe, Number)
export const LENGTH = regExpToken(lengthRe, Number)
export const UNSUPPORTED_LENGTH_UNIT = regExpToken(unsupportedUnitRe)
export const ANGLE = regExpToken(angleRe, angle => angle.toLowerCase())
export const PERCENT = regExpToken(percentRe)
export const IDENT = regExpToken(identRe)
export const STRING = matchString
export const COLOR = matchColor
export const LINE = regExpToken(/^(none|underline|line-through)$/i)