forked from LeaVerou/parsel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsel_aborted.js
225 lines (191 loc) · 4.32 KB
/
parsel_aborted.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Initial attempt to write a proper parser for CSS selectors.
* Aborted in favor of a hybrid regex approach which is smaller
*/
const COMBINATORS = [">", "+", "~"];
const PSEUDOS_WITH_SELECTOR_ARG = ["not", "is", "where", "matches"];
const SPECIAL = [...COMBINATORS, ..."#.[]=^$*:|()"];
// https://drafts.csswg.org/css-syntax-3/#ident-token-diagram
const IDENT_CHAR = /^[-\w\u{0080}-\u{FFFF}]+$/ui;
/*
TODO:
- attr selectors
- type selectors
- selector lists
- args
- consolidate logic, e.g. escapes should only be implemented in one place
*/
export function tokenize(selector) {
selector = selector.trim();
let str = "";
let stack = [];
let ret = [];
let i = 0;
let escape = false;
function gobble({test, until, escapes = true}) {
let str = "";
let char = selector[i];
while (i < selector.length) {
let doBreak;
if (test && !(test.test? test.test(char) : char === test)) {
doBreak = true;
}
else if (until && (until.test? until.test(char) : char === until)) {
doBreak = true;
}
if (doBreak && (!escapes || selector[i - 1] !== "\\")) {
break;
}
str += char;
char = selector[++i];
}
return str;
}
function gobbleIdent() {
return gobble({test: IDENT_CHAR, escapes: false});
}
function gobbleParens() {
let str = "";
let stack = [];
let char = selector[i];
for (; i < selector.length; i++) {
if (char === "(" && selector[i - 1] !== "\\") {
stack.push(char);
}
else if (char === ")" && selector[i - 1] !== "\\") {
if (stack.length > 0) {
stack.pop();
}
else {
throw new Error("Closing paren without opening paren at " + i);
}
if (stack.length === 0) {
return str.slice(1); // remove "(" at start
}
}
if (char === "'" || char === '"') {
str += gobbleString(false);
}
else {
str += char;
}
}
return str;
}
function gobbleSpaces() {
gobble({test: /\s/});
}
// Read a (potentially unquoted) string
function gobbleString(quoted) {
let char = selector[i];
if (!quoted && (char !== '"' && char !== "'")) {
// Unquoted string
return gobbleIdent();
}
return gobble({until: char});
}
while (i < selector.length) {
let char = selector[i];
if (char === "\\") {
// Next char is literal
i++;
str += selector[i];
}
else if (char === "#") {
i++;
ret.push({
type: "id",
name: gobbleIdent()
});
}
else if (char === ".") {
i++;
ret.push({
type: "class",
name: gobbleIdent()
});
}
else if (char === "[") {
// TODO handle spaces between parts
i++;
let o = {
type: "attribute",
name: gobbleIdent(),
operator: gobble({until: "="}) + "=",
value: gobbleString()
};
gobbleSpaces();
if (selector[i] === "i") {
// https://www.w3.org/TR/selectors/#attribute-case
o.i = true;
}
ret.push(o);
gobbleSpaces();
i++; // advance past ]
}
else if (char === ":") {
let o = {type: "pseudo-class"};
if (selector[i + 1] === ":") {
o.type = "pseudo-element";
i++;
}
i++;
o.name = gobbleIdent();
if (selector[i + 1] === "(") {
o.argument = gobbleParens();
}
ret.push(o);
}
else if (/\s/.test(char) || COMBINATORS.includes(char)) {
gobbleSpaces();
char = selector[i];
if (COMBINATORS.includes(char)) {
ret.push({
type: "combinator",
combinator: char
});
i++;
gobbleSpaces();
}
else {
ret.push({
type: "combinator",
combinator: " "
});
}
}
else {
str += char;
i++;
}
}
return ret;
}
// Convert a flat list of tokens into a tree of complex & compound selectors
function nestTokens(tokens) {
for (let i=tokens.length - 1; i>=0; i--) {
let token = tokens[i];
if (token.type === "combinator") {
let left = tokens.slice(0, i);
let right = tokens.slice(i + 1);
if (left.length === 0 || right.length === 0) {
throw new Error(`Combinator ${token.combinator} used in selector ${left.length === 0? "start" : "end"}`);
}
return {
type: "complex",
combinator: token.combinator,
left: nestTokens(left),
right: nestTokens(right)
};
}
}
// If we're here, there are no combinators, so it's just a list
return tokens.length === 1? tokens[0] : {
type: "compound",
list: tokens
};
}
export function parseSelector(selector) {
let tokens = tokenize(selector);
return nestTokens(tokens);
}