-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathparse.ts
37 lines (29 loc) · 962 Bytes
/
parse.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
import type { ComponentValue } from "@csstools/css-parser-algorithms";
import { isTokenNode, parseListOfComponentValues } from "@csstools/css-parser-algorithms";
import { isTokenDelim, tokenize } from "@csstools/css-tokenizer";
export function parse(str: string): Array<Array<ComponentValue>> {
const componentValues = parseListOfComponentValues(
tokenize({ css: str })
);
const parts: Array<Array<ComponentValue>> = []
let lastSliceIndex = 0;
for (let i = (componentValues.length - 1); i >= 0; i--) {
const componentValue = componentValues[i];
if (!isTokenNode(componentValue)) {
continue;
}
const token = componentValue.value;
if (!isTokenDelim(token)) {
continue;
}
if (token[4].value !== '/') {
continue;
}
parts.push(componentValues.slice(lastSliceIndex, i));
lastSliceIndex = i + 1;
}
if (lastSliceIndex !== 0) {
parts.push(componentValues.slice(lastSliceIndex, componentValues.length));
}
return parts;
}