-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-selector.ts
355 lines (344 loc) · 10.6 KB
/
query-selector.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
export interface ISelNode {
readonly nodeType: number;
readonly parentNode: ISelNode | null;
readonly localName?: string;
readonly childNodes?: Iterable<ISelNode>;
readonly previousSibling: null | ISelNode;
getAttribute?(attr: string): null | string;
}
export function querySel<R extends ISelNode>(context: ISelNode, sel: string) {
return qSel<R>(context, parse(sel));
}
interface ISel {
a?: Map<string, (string|((v: string) => boolean)[]|null)>;
c?: Set<string>;
i?: string;
p?: [">" | "+" | "~" | " ", ISel];
t?: string;
}
function parse(sel: string): ISel[] {
sel = sel.trim();
const ret = [];
let curr: null | ISel = null;
do {
let join = "";
if (curr) {
const re = sel.match(/^\s*(>|\+|~|,| )\s*/);
if (!re) {
throw new Error("Unable to parse joiner at " + JSON.stringify(sel));
}
join = re[1] as ">" | "+" | "~" | " " | ",";
sel = sel.substring(re[0].length);
if (join == ",") {
ret.push(curr);
curr = null;
join = "";
}
}
if (!sel) {
throw new Error("unable to parse the empty string as a selector");
}
const res = parseStep(sel);
const next = res[0];
if (curr) {
next.p = [join as ">" | "+" | "~" | " ", curr];
}
curr = next;
sel = res[1];
} while(sel);
ret.push(curr);
return ret;
}
function parseStep(sel: string): [ISel, string] {
const ret: ISel = {};
{
const tagCheck = sel.match(/^[\p{L}_][0-9\p{L}_-]*/u);
if (tagCheck) {
ret.t = tagCheck[0];
sel = sel.substring(tagCheck[0].length);
} else if (sel.startsWith("*")) {
ret.t = undefined;
sel = sel.substring(1);
}
}
while (sel) {
if (sel.startsWith("#")) {
const tagCheck = sel.match(/^#[\p{L}_][0-9\p{L}_-]*/u);
if (tagCheck) {
const id = tagCheck[0].substring(1);
if (ret.i && ret.i != id) {
throw new Error("selector will always fail, elements can't contain multiple ids");
}
ret.i = id;
sel = sel.substring(tagCheck[0].length);
continue;
}
throw new Error("invalid id at " + JSON.stringify(sel));
}
if (sel.startsWith(".")) {
const tagCheck = sel.match(/^\.[\p{L}_][0-9\p{L}_-]*/u);
if (tagCheck) {
if (!ret.c) {
ret.c = new Set();
}
ret.c.add(tagCheck[0].substring(1));
sel = sel.substring(tagCheck[0].length);
continue;
}
throw new Error("invalid class at " + JSON.stringify(sel));
}
if (sel.startsWith("[")) {
sel = sel.substring(1);
const res = parseAttr(sel);
const n = res[0].n;
if (ret.a) {
const j = ret.a.get(n);
ret.a.set(n, joinAttr(j, res[0].v));
} else {
const a = new Map();
const v = res[0].v;
a.set(n, v === undefined ? null : typeof v == "string" ? v : [v]);
ret.a = a;
}
sel = res[1];
if (sel.startsWith("]")) {
sel = sel.substring(1);
continue;
}
throw new Error("unexpected characters in attribute section: " + JSON.stringify(sel));
}
}
if (Object.keys(ret).length) {
return [ret, sel];
}
throw new Error("unexpected part at " + JSON.stringify(sel));
}
interface IAttrRes {
n: string;
v?: string | ((v: string) => boolean);
}
function parseAttr(sel: string): [IAttrRes, string] {
const nameCheck = sel.match(/^[\p{L}_][0-9\p{L}._-]*/u);
if (!nameCheck) {
throw new Error("expected attribute name, found " + JSON.stringify(sel));
}
const ret: IAttrRes = { n: nameCheck[0] } as IAttrRes;
sel = sel.substring(nameCheck[0].length);
let op = undefined;
if (sel.startsWith("=")) {
op = "=";
sel = sel.substring(1);
} else if (sel.startsWith("*=")) {
op = "*";
sel = sel.substring(2);
} else if (sel.startsWith("~=")) {
op = "~";
sel = sel.substring(2);
} else if (sel.startsWith("|=")) {
op = "|";
sel = sel.substring(2);
} else if (sel.startsWith("$=")) {
op = "$";
sel = sel.substring(2);
} else if (sel.startsWith("^=")) {
op = "^";
sel = sel.substring(2);
}
if (op) {
const vres = parseNameOrString(sel);
sel = vres[1];
const va = vres[0];
if (op == "=") {
ret.v = va;
} else if (op == "*") {
ret.v = (v) => v.includes(va);
} else if (op == "|") {
ret.v = (v) => v == va || v.startsWith(va + "-");
} else if (op == "^") {
ret.v = (v) => v.startsWith(va);
} else if (op == "$") {
ret.v = (v) => v.endsWith(va);
} else if (op == "~") {
ret.v = (v) => spaceEq(v, va);
}
}
// TODO: add support for [n=v i/s] ?
if (sel.startsWith("]")) {
return [ret, sel.substring(1)];
}
throw new Error("expected end of attribute section but found " + JSON.stringify(sel));
}
function spaceEq(haystack: string, needle: string) {
let i = 0;
while (i != haystack.length) {
const next = haystack.indexOf(" ", i + 1);
const end = next == -1 ? haystack.length : next;
if ((end - i) == needle.length && haystack.substring(i, end) == needle) {
return true;
}
i = end;
}
return false;
}
function parseNameOrString(sel: string): [string, string] {
if (sel.startsWith("\"")) {
const e = sel.indexOf("\"", 1);
if (e != -1) {
return [sel.substring(1, e), sel.substring(e + 1)];
}
throw new Error("no end of string at " + JSON.stringify(sel));
}
const m = sel.match(/^[\d\p{L}._-]+/u);
if (m) {
return [m[0], sel.substring(m[0].length)];
}
throw new Error("unexpected unquoted data at " + JSON.stringify(sel));
}
function isMatch(context: ISelNode, match: ISel[], node: ISelNode): boolean {
branch: for (const m of match) {
if (m.t && m.t != node.localName) {
continue branch;
}
if (m.i) {
if (typeof node.getAttribute != "function") {
continue branch;
}
const cl = node.getAttribute("id");
if (!cl || cl != m.i) {
continue branch;
}
}
if (m.c) {
if (typeof node.getAttribute != "function") {
continue branch;
}
const cl = node.getAttribute("class");
if (!cl) {
continue branch;
}
for (const c of m.c) {
if (!spaceEq(cl, c)) {
continue branch;
}
}
}
if (m.a) {
if (typeof node.getAttribute != "function") {
continue branch;
}
for (const [a, av] of m.a) {
const cl = node.getAttribute(a);
if (!cl) {
continue branch;
}
if (typeof av == "string") {
if (cl == av) {
continue;
}
continue branch;
}
if (av) {
for (const c of av) {
if (!c(cl)) {
continue branch;
}
}
}
}
}
if (m.p) {
const op = m.p[0];
if (op == "+") {
const prev = node.previousSibling;
if (!prev) {
continue branch;
}
if (!isMatch(context, [m.p[1]], prev)) {
continue branch;
}
} else if (op == "~") {
let prev = node.previousSibling;
let mat = false;
const a = [m.p[1]];
while (!mat && prev) {
mat = isMatch(context, a, prev);
prev = prev.previousSibling;
}
if (!mat) {
continue branch;
}
} else if (op == ">") {
const prev = node.parentNode;
if (!prev) {
continue branch;
}
if (!isMatch(context, [m.p[1]], prev)) {
continue branch;
}
} else if (op == " ") {
let prev = node.parentNode;
let mat = false;
const a = [m.p[1]];
while (!mat && prev) {
mat = isMatch(context, a, prev);
prev = prev.parentNode;
}
if (!mat) {
continue branch;
}
}
}
return true;
}
return false;
}
function* qSel<R extends ISelNode>(context: ISelNode, match: ISel[]): IterableIterator<R> {
const cn = context.childNodes;
if (!cn) {
return;
}
for (const c of cn) {
if (c.nodeType != 1) {
continue;
}
if (isMatch(context, match, c)) {
yield c as R;
}
yield* qSel(c, match);
}
}
function joinAttr(attr: undefined | null | string | ((v: string) => boolean)[], add: undefined | string | ((v: string) => boolean)): null | string | ((v: string) => boolean)[] {
if (add === undefined) {
return attr !== undefined ? attr : null;
}
if (typeof add == "string") {
if (attr === undefined || attr === null) {
return add;
}
if (typeof attr == "string") {
if (attr == add) {
return attr;
}
} else {
let inv = false;
for (const f of attr) {
if (!f(add)) {
inv = true;
}
}
if (!inv) {
return add;
}
}
} else if (attr === undefined || attr === null) {
return [add];
} else if (typeof attr == "string") {
if (add(attr)) {
return attr;
}
} else {
attr.push(add);
return attr;
}
throw new Error("no possible attribute could ever match");
}