-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathextract.mjs
91 lines (75 loc) · 1.58 KB
/
extract.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import decodeCSS from './decode.mjs';
/** Extract encoded selectors out of attribute selectors */
export default function extractEncodedSelectors(value) {
let out = [];
let depth = 0;
let candidate;
let quoted = false;
let quotedMark;
let containsUnescapedUnquotedHasAtDepth1 = false;
// Stryker disable next-line EqualityOperator
for (let i = 0; i < value.length; i++) {
const char = value[i];
switch (char) {
case '[':
if (quoted) {
candidate += char;
continue;
}
if (depth === 0) {
candidate = '';
} else {
candidate += char;
}
depth++;
continue;
case ']':
if (quoted) {
candidate += char;
continue;
}
{
depth--;
if (depth === 0) {
const decoded = decodeCSS(candidate);
if (containsUnescapedUnquotedHasAtDepth1) {
out.push(decoded);
}
} else {
candidate += char;
}
}
continue;
case '\\':
candidate += value[i];
candidate += value[i+1];
i++;
continue;
case '"':
case '\'':
if (quoted && char === quotedMark) {
quoted = false;
continue;
} else if (quoted) {
candidate += char;
continue;
}
quoted = true;
quotedMark = char;
continue;
default:
if (candidate === '' && depth === 1 && (value.slice(i, i + 13) === 'csstools-has-')) {
containsUnescapedUnquotedHasAtDepth1 = true;
}
candidate += char;
continue;
}
}
const unique = [];
for (let i = 0; i < out.length; i++) {
if (unique.indexOf(out[i]) === -1) {
unique.push(out[i]);
}
}
return unique;
}