-
Notifications
You must be signed in to change notification settings - Fork 25
/
parsel.ts
607 lines (537 loc) · 13 KB
/
parsel.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
export const TOKENS: Record<string, RegExp> = {
attribute:
/\[\s*(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?(?<name>[-\w\P{ASCII}]+)\s*(?:(?<operator>\W?=)\s*(?<value>.+?)\s*(\s(?<caseSensitive>[iIsS]))?\s*)?\]/gu,
id: /#(?<name>[-\w\P{ASCII}]+)/gu,
class: /\.(?<name>[-\w\P{ASCII}]+)/gu,
comma: /\s*,\s*/g, // must be before combinator
combinator: /\s*[\s>+~]\s*/g, // this must be after attribute
'pseudo-element': /::(?<name>[-\w\P{ASCII}]+)(?:\((?<argument>¶*)\))?/gu, // this must be before pseudo-class
'pseudo-class': /:(?<name>[-\w\P{ASCII}]+)(?:\((?<argument>¶*)\))?/gu,
universal: /(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?\*/gu,
type: /(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?(?<name>[-\w\P{ASCII}]+)/gu, // this must be last
};
export const TRIM_TOKENS = new Set<string>(['combinator', 'comma']);
export const RECURSIVE_PSEUDO_CLASSES = new Set<string>([
'not',
'is',
'where',
'has',
'matches',
'-moz-any',
'-webkit-any',
'nth-child',
'nth-last-child',
]);
const nthChildRegExp = /(?<index>[\dn+-]+)\s+of\s+(?<subtree>.+)/;
export const RECURSIVE_PSEUDO_CLASSES_ARGS: Record<string, RegExp> = {
'nth-child': nthChildRegExp,
'nth-last-child': nthChildRegExp,
};
const getArgumentPatternByType = (type: string) => {
switch (type) {
case 'pseudo-element':
case 'pseudo-class':
return new RegExp(
TOKENS[type]!.source.replace(
'(?<argument>¶*)',
'(?<argument>.*)'
),
'gu'
);
default:
return TOKENS[type];
}
};
export function gobbleParens(text: string, offset: number): string {
let nesting = 0;
let result = '';
for (; offset < text.length; offset++) {
const char = text[offset];
switch (char) {
case '(':
++nesting;
break;
case ')':
--nesting;
break;
}
result += char;
if (nesting === 0) {
return result;
}
}
return result;
}
export function tokenizeBy(text: string, grammar = TOKENS): Token[] {
if (!text) {
return [];
}
const tokens: (Token | string)[] = [text];
for (const [type, pattern] of Object.entries(grammar)) {
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (typeof token !== 'string') {
continue;
}
pattern.lastIndex = 0;
const match = pattern.exec(token);
if (!match) {
continue;
}
const from = match.index - 1;
const args: typeof tokens = [];
const content = match[0];
const before = token.slice(0, from + 1);
if (before) {
args.push(before);
}
args.push({
...match.groups,
type,
content,
} as unknown as Token);
const after = token.slice(from + content.length + 1);
if (after) {
args.push(after);
}
tokens.splice(i, 1, ...args);
}
}
let offset = 0;
for (const token of tokens) {
switch (typeof token) {
case 'string':
throw new Error(
`Unexpected sequence ${token} found at index ${offset}`
);
case 'object':
offset += token.content.length;
token.pos = [offset - token.content.length, offset];
if (TRIM_TOKENS.has(token.type)) {
token.content = token.content.trim() || ' ';
}
break;
}
}
return tokens as Token[];
}
const STRING_PATTERN = /(['"])([^\\\n]+?)\1/g;
const ESCAPE_PATTERN = /\\./g;
export function tokenize(selector: string, grammar = TOKENS): Token[] {
// Prevent leading/trailing whitespaces from being interpreted as combinators
selector = selector.trim();
if (selector === '') {
return [];
}
type Replacement = { value: string; offset: number };
const replacements: Replacement[] = [];
// Replace escapes with placeholders.
selector = selector.replace(
ESCAPE_PATTERN,
(value: string, offset: number) => {
replacements.push({ value, offset });
return '\uE000'.repeat(value.length);
}
);
// Replace strings with placeholders.
selector = selector.replace(
STRING_PATTERN,
(value: string, quote: string, content: string, offset: number) => {
replacements.push({ value, offset });
return `${quote}${'\uE001'.repeat(content.length)}${quote}`;
}
);
// Replace parentheses with placeholders.
{
let pos = 0;
let offset: number;
while ((offset = selector.indexOf('(', pos)) > -1) {
const value = gobbleParens(selector, offset);
replacements.push({ value, offset });
selector = `${selector.substring(0, offset)}(${'¶'.repeat(
value.length - 2
)})${selector.substring(offset + value.length)}`;
pos = offset + value.length;
}
}
// Now we have no nested structures and we can parse with regexes
const tokens = tokenizeBy(selector, grammar);
// Replace placeholders in reverse order.
const changedTokens = new Set<Token>();
for (const replacement of replacements.reverse()) {
for (const token of tokens) {
const { offset, value } = replacement;
if (
!(
token.pos[0] <= offset &&
offset + value.length <= token.pos[1]
)
) {
continue;
}
const { content } = token;
const tokenOffset = offset - token.pos[0];
token.content =
content.slice(0, tokenOffset) +
value +
content.slice(tokenOffset + value.length);
if (token.content !== content) {
changedTokens.add(token);
}
}
}
// Update changed tokens.
for (const token of changedTokens) {
const pattern = getArgumentPatternByType(token.type);
if (!pattern) {
throw new Error(`Unknown token type: ${token.type}`);
}
pattern.lastIndex = 0;
const match = pattern.exec(token.content);
if (!match) {
throw new Error(
`Unable to parse content for ${token.type}: ${token.content}`
);
}
Object.assign(token, match.groups);
}
return tokens;
}
/**
* Convert a flat list of tokens into a tree of complex & compound selectors
*/
function nestTokens(tokens: Token[], { list = true } = {}): AST {
if (list && tokens.find((t: { type: string }) => t.type === 'comma')) {
const selectors: AST[] = [];
const temp: Token[] = [];
for (let i = 0; i < tokens.length; i++) {
if (tokens[i].type === 'comma') {
if (temp.length === 0) {
throw new Error('Incorrect comma at ' + i);
}
selectors.push(nestTokens(temp, { list: false }));
temp.length = 0;
} else {
temp.push(tokens[i]);
}
}
if (temp.length === 0) {
throw new Error('Trailing comma');
} else {
selectors.push(nestTokens(temp, { list: false }));
}
return { type: 'list', list: selectors };
}
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) {
return {
type: 'relative',
combinator: token.content,
right: nestTokens(right),
};
}
return {
type: 'complex',
combinator: token.content,
left: nestTokens(left),
right: nestTokens(right),
};
}
}
switch (tokens.length) {
case 0:
throw new Error('Could not build AST.');
case 1:
// If we're here, there are no combinators, so it's just a list.
return tokens[0];
default:
return {
type: 'compound',
list: [...tokens], // clone to avoid pointers messing up the AST
};
}
}
/**
* Traverse an AST in depth-first order
*/
export function* flatten(
node: AST,
/**
* @internal
*/
parent?: AST
): IterableIterator<[Token, AST | undefined]> {
switch (node.type) {
case 'list':
for (let child of node.list) {
yield* flatten(child, node);
}
break;
case 'complex':
yield* flatten(node.left, node);
yield* flatten(node.right, node);
break;
case 'relative':
yield* flatten(node.right, node);
break;
case 'compound':
yield* node.list.map((token): [Token, Compound] => [token, node]);
break;
default:
yield [node, parent];
}
}
/**
* Traverse an AST (or part thereof), in depth-first order
*/
export function walk(
node: AST | undefined,
visit: (node: AST, parentNode?: AST) => void,
/**
* @internal
*/
parent?: AST
) {
if (!node) {
return;
}
for (const [token, ast] of flatten(node, parent)) {
visit(token, ast);
}
}
export interface ParserOptions {
recursive?: boolean;
list?: boolean;
}
/**
* Parse a CSS selector
*
* @param selector - The selector to parse
* @param options.recursive - Whether to parse the arguments of pseudo-classes like :is(), :has() etc. Defaults to true.
* @param options.list - Whether this can be a selector list (A, B, C etc). Defaults to true.
*/
export function parse(
selector: string,
{ recursive = true, list = true }: ParserOptions = {}
): AST | undefined {
const tokens = tokenize(selector);
if (!tokens) {
return;
}
const ast = nestTokens(tokens, { list });
if (!recursive) {
return ast;
}
for (const [token] of flatten(ast)) {
if (token.type !== 'pseudo-class' || !token.argument) {
continue;
}
if (!RECURSIVE_PSEUDO_CLASSES.has(token.name)) {
continue;
}
let argument = token.argument;
const childArg = RECURSIVE_PSEUDO_CLASSES_ARGS[token.name];
if (childArg) {
const match = childArg.exec(argument);
if (!match) {
continue;
}
Object.assign(token, match.groups);
argument = match.groups!['subtree'];
}
if (!argument) {
continue;
}
Object.assign(token, {
subtree: parse(argument, {
recursive: true,
list: true,
}),
});
}
return ast;
}
/**
* Converts the given list or (sub)tree to a string.
*/
export function stringify(listOrNode: Token[] | AST): string {
if (Array.isArray(listOrNode)) {
return listOrNode.map((token) => token.content).join("");
}
switch (listOrNode.type) {
case "list":
return listOrNode.list.map(stringify).join(",");
case "relative":
return (
listOrNode.combinator +
stringify(listOrNode.right)
);
case "complex":
return (
stringify(listOrNode.left) +
listOrNode.combinator +
stringify(listOrNode.right)
);
case "compound":
return listOrNode.list.map(stringify).join("");
default:
return listOrNode.content;
}
}
/**
* To convert the specificity array to a number
*/
export function specificityToNumber(
specificity: number[],
base: number
): number {
base = base || Math.max(...specificity) + 1;
return (
specificity[0] * (base << 1) + specificity[1] * base + specificity[2]
);
}
/**
* Calculate specificity of a selector.
*
* If the selector is a list, the max specificity is returned.
*/
export function specificity(selector: string | AST): number[] {
let ast: string | AST | undefined = selector;
if (typeof ast === 'string') {
ast = parse(ast, { recursive: true });
}
if (!ast) {
return [];
}
if (ast.type === 'list' && 'list' in ast) {
let base = 10;
const specificities = ast.list.map((ast) => {
const sp = specificity(ast);
base = Math.max(base, ...specificity(ast));
return sp;
});
const numbers = specificities.map((ast) =>
specificityToNumber(ast, base)
);
return specificities[numbers.indexOf(Math.max(...numbers))];
}
const ret = [0, 0, 0];
for (const [token] of flatten(ast)) {
switch (token.type) {
case 'id':
ret[0]++;
break;
case 'class':
case 'attribute':
ret[1]++;
break;
case 'pseudo-element':
case 'type':
ret[2]++;
break;
case 'pseudo-class':
if (token.name === 'where') {
break;
}
if (
!RECURSIVE_PSEUDO_CLASSES.has(token.name) ||
!token.subtree
) {
ret[1]++;
break;
}
const sub = specificity(token.subtree);
sub.forEach((s, i) => (ret[i] += s));
// :nth-child() & :nth-last-child() add (0, 1, 0) to the specificity of their most complex selector
if (
token.name === 'nth-child' ||
token.name === 'nth-last-child'
) {
ret[1]++;
}
}
}
return ret;
}
export interface BaseToken {
type: string;
content: string;
pos: [number, number];
}
export interface CommaToken extends BaseToken {
type: 'comma';
}
export interface CombinatorToken extends BaseToken {
type: 'combinator';
}
export interface NamedToken extends BaseToken {
name: string;
}
export interface IdToken extends NamedToken {
type: 'id';
}
export interface ClassToken extends NamedToken {
type: 'class';
}
export interface PseudoElementToken extends NamedToken {
type: 'pseudo-element';
argument?: string;
}
export interface PseudoClassToken extends NamedToken {
type: 'pseudo-class';
argument?: string;
subtree?: AST;
}
export interface NamespacedToken extends BaseToken {
namespace?: string;
}
export interface UniversalToken extends NamespacedToken {
type: 'universal';
}
export interface AttributeToken extends NamespacedToken, NamedToken {
type: 'attribute';
operator?: string;
value?: string;
caseSensitive?: 'i' | 'I' | 's' | 'S';
}
export interface TypeToken extends NamespacedToken, NamedToken {
type: 'type';
}
export interface UnknownToken extends BaseToken {
type: never;
}
export type Token =
| AttributeToken
| IdToken
| ClassToken
| CommaToken
| CombinatorToken
| PseudoElementToken
| PseudoClassToken
| UniversalToken
| TypeToken
| UnknownToken;
export interface Complex {
type: 'complex';
combinator: string;
right: AST;
left: AST;
}
export interface Relative {
type: 'relative';
combinator: string;
right: AST;
}
export interface Compound {
type: 'compound';
list: Token[];
}
export interface List {
type: 'list';
list: AST[];
}
export type AST = Complex | Relative | Compound | List | Token;