-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsslex-to-universal.mjs
68 lines (63 loc) · 1.72 KB
/
csslex-to-universal.mjs
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
import { types, value } from "csslex";
export function cssLexToUniversal(token, source) {
const tokenValue = value(source, token);
const tokenType = typeMapping[token[0]] ?? token[0];
let structured = null;
if (tokenType === 'comment') {
structured = null;
} else if (tokenType === 'percentage-token') {
structured = {
value: tokenValue.value
};
} else if (typeof tokenValue !== "object") {
structured = {
value: tokenValue
};
} else {
structured = tokenValue;
}
if (
tokenType === 'percentage-token' ||
tokenType === 'number-token' ||
tokenType === 'dimension-token'
) {
const firstChar = source.slice(token[1], token[1] + 1);
if (firstChar === '+' || firstChar === '-') {
structured.signCharacter = firstChar;
}
}
return {
type: tokenType,
raw: source.slice(token[1], token[2]),
startIndex: token[1],
endIndex: token[2],
structured,
}
}
const typeMapping = {
[types.LEFT_PAREN]: '(-token',
[types.RIGHT_PAREN]: ')-token',
[types.CDC]: 'CDC-token',
[types.CDO]: 'CDO-token',
[types.LEFT_SQUARE]: '[-token',
[types.RIGHT_SQUARE]: ']-token',
[types.AT_KEYWORD]: 'at-keyword-token',
[types.BAD_STRING]: 'bad-string-token',
[types.BAD_URL]: 'bad-url-token',
[types.COLON]: 'colon-token',
[types.COMMA]: 'comma-token',
[types.COMMENT]: 'comment',
[types.DELIM]: 'delim-token',
[types.DIMENSION]: 'dimension-token',
[types.FUNCTION]: 'function-token',
[types.HASH]: 'hash-token',
[types.IDENT]: 'ident-token',
[types.NUMBER]: 'number-token',
[types.PERCENTAGE]: 'percentage-token',
[types.SEMICOLON]: 'semicolon-token',
[types.STRING]: 'string-token',
[types.URL]: 'url-token',
[types.WHITESPACE]: 'whitespace-token',
[types.LEFT_CURLY]: '{-token',
[types.RIGHT_CURLY]: '}-token',
}