@aljazmc/syntax 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -1
- package/bin/index.js +1305 -16
- package/bin/types/css/ast.d.ts +231 -0
- package/bin/types/css/parser.d.ts +10 -0
- package/bin/types/css/tokens.d.ts +27 -0
- package/bin/types/index.d.ts +14 -0
- package/package.json +2 -2
package/bin/index.js
CHANGED
|
@@ -271,6 +271,1275 @@ var Scanner = class _Scanner {
|
|
|
271
271
|
}
|
|
272
272
|
};
|
|
273
273
|
|
|
274
|
+
// shared/syntax/src/css/tokens.ts
|
|
275
|
+
var isDigit = /* @__PURE__ */ __name((char) => char !== void 0 && char >= "0" && char <= "9", "isDigit");
|
|
276
|
+
var isHex = /* @__PURE__ */ __name((char) => char !== void 0 && /^[0-9a-fA-F]$/.test(char), "isHex");
|
|
277
|
+
var isNameStart = /* @__PURE__ */ __name((char) => char !== void 0 && (/^[A-Za-z_]$/.test(char) || char.charCodeAt(0) >= 128), "isNameStart");
|
|
278
|
+
var isName = /* @__PURE__ */ __name((char) => char !== void 0 && (isNameStart(char) || isDigit(char) || char === "-"), "isName");
|
|
279
|
+
var isWhitespace = /* @__PURE__ */ __name((char) => char === " " || char === " " || char === "\n" || char === "\r" || char === "\f", "isWhitespace");
|
|
280
|
+
var Tokenizer = class {
|
|
281
|
+
static {
|
|
282
|
+
__name(this, "Tokenizer");
|
|
283
|
+
}
|
|
284
|
+
source;
|
|
285
|
+
comments = [];
|
|
286
|
+
/** Errors of a tokenizer that recovers from them, others throw them. */
|
|
287
|
+
errors;
|
|
288
|
+
#position;
|
|
289
|
+
constructor(source, start = 0, recover = false) {
|
|
290
|
+
this.source = source;
|
|
291
|
+
this.#position = start;
|
|
292
|
+
this.errors = recover ? [] : null;
|
|
293
|
+
}
|
|
294
|
+
#fail(message, position) {
|
|
295
|
+
const error = new ParseError(message, position);
|
|
296
|
+
if (!this.errors) throw error;
|
|
297
|
+
this.errors.push(error);
|
|
298
|
+
}
|
|
299
|
+
/** Every token to the end, the last one is `eof`. */
|
|
300
|
+
tokenize() {
|
|
301
|
+
const tokens = [];
|
|
302
|
+
for (; ; ) {
|
|
303
|
+
const token = this.next();
|
|
304
|
+
tokens.push(token);
|
|
305
|
+
if (token.type === "eof") return tokens;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
#validEscape(at) {
|
|
309
|
+
return this.source[at] === "\\" && this.source[at + 1] !== "\n";
|
|
310
|
+
}
|
|
311
|
+
#identStart(at) {
|
|
312
|
+
const source = this.source;
|
|
313
|
+
const char = source[at];
|
|
314
|
+
if (char === "-")
|
|
315
|
+
return isNameStart(source[at + 1]) || source[at + 1] === "-" || this.#validEscape(at + 1);
|
|
316
|
+
if (isNameStart(char)) return true;
|
|
317
|
+
return this.#validEscape(at);
|
|
318
|
+
}
|
|
319
|
+
#numberStart(at) {
|
|
320
|
+
const source = this.source;
|
|
321
|
+
const char = source[at];
|
|
322
|
+
if (char === "+" || char === "-")
|
|
323
|
+
return isDigit(source[at + 1]) || source[at + 1] === "." && isDigit(source[at + 2]);
|
|
324
|
+
if (char === ".") return isDigit(source[at + 1]);
|
|
325
|
+
return isDigit(char);
|
|
326
|
+
}
|
|
327
|
+
#escape() {
|
|
328
|
+
const source = this.source;
|
|
329
|
+
if (isHex(source[this.#position])) {
|
|
330
|
+
let count = 0;
|
|
331
|
+
while (count < 6 && isHex(source[this.#position])) {
|
|
332
|
+
this.#position++;
|
|
333
|
+
count++;
|
|
334
|
+
}
|
|
335
|
+
if (isWhitespace(source[this.#position])) this.#position++;
|
|
336
|
+
} else if (this.#position < source.length) this.#position++;
|
|
337
|
+
}
|
|
338
|
+
#name() {
|
|
339
|
+
const source = this.source;
|
|
340
|
+
const start = this.#position;
|
|
341
|
+
for (; ; ) {
|
|
342
|
+
const char = source[this.#position];
|
|
343
|
+
if (isName(char)) this.#position++;
|
|
344
|
+
else if (this.#validEscape(this.#position)) {
|
|
345
|
+
this.#position++;
|
|
346
|
+
this.#escape();
|
|
347
|
+
} else break;
|
|
348
|
+
}
|
|
349
|
+
return source.slice(start, this.#position);
|
|
350
|
+
}
|
|
351
|
+
#number() {
|
|
352
|
+
const source = this.source;
|
|
353
|
+
const start = this.#position;
|
|
354
|
+
if (source[this.#position] === "+" || source[this.#position] === "-")
|
|
355
|
+
this.#position++;
|
|
356
|
+
while (isDigit(source[this.#position])) this.#position++;
|
|
357
|
+
if (source[this.#position] === "." && isDigit(source[this.#position + 1])) {
|
|
358
|
+
this.#position++;
|
|
359
|
+
while (isDigit(source[this.#position])) this.#position++;
|
|
360
|
+
}
|
|
361
|
+
const e = source[this.#position];
|
|
362
|
+
if ((e === "e" || e === "E") && (isDigit(source[this.#position + 1]) || (source[this.#position + 1] === "+" || source[this.#position + 1] === "-") && isDigit(source[this.#position + 2]))) {
|
|
363
|
+
this.#position += 2;
|
|
364
|
+
while (isDigit(source[this.#position])) this.#position++;
|
|
365
|
+
}
|
|
366
|
+
return source.slice(start, this.#position);
|
|
367
|
+
}
|
|
368
|
+
#comment() {
|
|
369
|
+
const source = this.source;
|
|
370
|
+
const start = this.#position;
|
|
371
|
+
const close = source.indexOf("*/", start + 2);
|
|
372
|
+
if (close < 0) this.#fail("Unterminated block comment", start);
|
|
373
|
+
this.#position = close < 0 ? source.length : close + 2;
|
|
374
|
+
this.comments.push({
|
|
375
|
+
type: "Comment",
|
|
376
|
+
start,
|
|
377
|
+
end: this.#position,
|
|
378
|
+
value: source.slice(start, this.#position)
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
next() {
|
|
382
|
+
const source = this.source;
|
|
383
|
+
while (source.startsWith("/*", this.#position)) this.#comment();
|
|
384
|
+
const start = this.#position;
|
|
385
|
+
const char = source[start];
|
|
386
|
+
const token = /* @__PURE__ */ __name((type, value = "") => ({
|
|
387
|
+
type,
|
|
388
|
+
start,
|
|
389
|
+
end: this.#position,
|
|
390
|
+
value
|
|
391
|
+
}), "token");
|
|
392
|
+
if (char === void 0) return token("eof");
|
|
393
|
+
if (isWhitespace(char)) {
|
|
394
|
+
while (isWhitespace(source[this.#position])) this.#position++;
|
|
395
|
+
return token("whitespace");
|
|
396
|
+
}
|
|
397
|
+
if (char === '"' || char === "'") return this.#string(char);
|
|
398
|
+
if (char === "#") {
|
|
399
|
+
this.#position++;
|
|
400
|
+
if (isName(source[this.#position]) || this.#validEscape(this.#position))
|
|
401
|
+
return token("hash", this.#name());
|
|
402
|
+
return token("delim", "#");
|
|
403
|
+
}
|
|
404
|
+
if (this.#numberStart(start)) {
|
|
405
|
+
const value = this.#number();
|
|
406
|
+
if (this.#identStart(this.#position)) {
|
|
407
|
+
const unit = this.#name();
|
|
408
|
+
return { ...token("dimension", value), unit };
|
|
409
|
+
}
|
|
410
|
+
if (source[this.#position] === "%") {
|
|
411
|
+
this.#position++;
|
|
412
|
+
return token("percentage", value);
|
|
413
|
+
}
|
|
414
|
+
return token("number", value);
|
|
415
|
+
}
|
|
416
|
+
if ((char === "u" || char === "U") && source[start + 1] === "+" && (isHex(source[start + 2]) || source[start + 2] === "?")) {
|
|
417
|
+
this.#position += 2;
|
|
418
|
+
let count = 0;
|
|
419
|
+
while (count < 6 && isHex(source[this.#position])) {
|
|
420
|
+
this.#position++;
|
|
421
|
+
count++;
|
|
422
|
+
}
|
|
423
|
+
while (count < 6 && source[this.#position] === "?") {
|
|
424
|
+
this.#position++;
|
|
425
|
+
count++;
|
|
426
|
+
}
|
|
427
|
+
if (source[this.#position] === "-" && isHex(source[this.#position + 1])) {
|
|
428
|
+
this.#position++;
|
|
429
|
+
count = 0;
|
|
430
|
+
while (count < 6 && isHex(source[this.#position])) {
|
|
431
|
+
this.#position++;
|
|
432
|
+
count++;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return token("unicode-range", source.slice(start, this.#position));
|
|
436
|
+
}
|
|
437
|
+
if (this.#identStart(start)) {
|
|
438
|
+
const name = this.#name();
|
|
439
|
+
if (source[this.#position] === "(") {
|
|
440
|
+
this.#position++;
|
|
441
|
+
if (/^url$/i.test(name)) {
|
|
442
|
+
let at = this.#position;
|
|
443
|
+
while (isWhitespace(source[at])) at++;
|
|
444
|
+
if (source[at] !== '"' && source[at] !== "'")
|
|
445
|
+
return this.#url(start, name);
|
|
446
|
+
}
|
|
447
|
+
return token("function", name);
|
|
448
|
+
}
|
|
449
|
+
return token("ident", name);
|
|
450
|
+
}
|
|
451
|
+
if (char === "@") {
|
|
452
|
+
this.#position++;
|
|
453
|
+
if (this.#identStart(this.#position))
|
|
454
|
+
return token("at", this.#name());
|
|
455
|
+
return token("delim", "@");
|
|
456
|
+
}
|
|
457
|
+
if (source.startsWith("<!--", start)) {
|
|
458
|
+
this.#position += 4;
|
|
459
|
+
return token("cdo");
|
|
460
|
+
}
|
|
461
|
+
if (source.startsWith("-->", start)) {
|
|
462
|
+
this.#position += 3;
|
|
463
|
+
return token("cdc");
|
|
464
|
+
}
|
|
465
|
+
this.#position++;
|
|
466
|
+
switch (char) {
|
|
467
|
+
case ":":
|
|
468
|
+
return token("colon");
|
|
469
|
+
case ";":
|
|
470
|
+
return token("semicolon");
|
|
471
|
+
case ",":
|
|
472
|
+
return token("comma");
|
|
473
|
+
case "(":
|
|
474
|
+
case ")":
|
|
475
|
+
case "[":
|
|
476
|
+
case "]":
|
|
477
|
+
case "{":
|
|
478
|
+
case "}":
|
|
479
|
+
return token(char);
|
|
480
|
+
case "\\":
|
|
481
|
+
this.#fail("Invalid escape sequence", start);
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
return token("delim", char);
|
|
485
|
+
}
|
|
486
|
+
#string(quote) {
|
|
487
|
+
const source = this.source;
|
|
488
|
+
const start = this.#position;
|
|
489
|
+
this.#position++;
|
|
490
|
+
let closed = false;
|
|
491
|
+
for (; ; ) {
|
|
492
|
+
const char = source[this.#position];
|
|
493
|
+
if (char === void 0) break;
|
|
494
|
+
if (char === quote) {
|
|
495
|
+
this.#position++;
|
|
496
|
+
closed = true;
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
if (char === "\n" || char === "\r" || char === "\f") break;
|
|
500
|
+
if (char === "\\") {
|
|
501
|
+
this.#position++;
|
|
502
|
+
if (source[this.#position] === "\r") this.#position++;
|
|
503
|
+
if (source[this.#position] !== void 0) this.#position++;
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
this.#position++;
|
|
507
|
+
}
|
|
508
|
+
if (!closed) this.#fail("Missing closing quote", start);
|
|
509
|
+
return {
|
|
510
|
+
type: "string",
|
|
511
|
+
start,
|
|
512
|
+
end: this.#position,
|
|
513
|
+
value: source.slice(start, this.#position),
|
|
514
|
+
closed
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
/** An unquoted url, after its `(`. */
|
|
518
|
+
#url(start, name) {
|
|
519
|
+
const source = this.source;
|
|
520
|
+
while (isWhitespace(source[this.#position])) this.#position++;
|
|
521
|
+
const valueStart = this.#position;
|
|
522
|
+
let valueEnd = valueStart;
|
|
523
|
+
for (; ; ) {
|
|
524
|
+
const char = source[this.#position];
|
|
525
|
+
if (char === void 0) {
|
|
526
|
+
this.#fail("Unterminated url", start);
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
if (char === ")") {
|
|
530
|
+
this.#position++;
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
if (isWhitespace(char)) {
|
|
534
|
+
this.#position++;
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
if (char === "\\" && this.#validEscape(this.#position)) {
|
|
538
|
+
this.#position++;
|
|
539
|
+
this.#escape();
|
|
540
|
+
valueEnd = this.#position;
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
if (char === "(") {
|
|
544
|
+
this.#position = start + name.length + 1;
|
|
545
|
+
return {
|
|
546
|
+
type: "function",
|
|
547
|
+
start,
|
|
548
|
+
end: this.#position,
|
|
549
|
+
value: name
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
if (char === '"' || char === "'")
|
|
553
|
+
this.#fail("Unexpected character in url", this.#position);
|
|
554
|
+
this.#position++;
|
|
555
|
+
valueEnd = this.#position;
|
|
556
|
+
}
|
|
557
|
+
return {
|
|
558
|
+
type: "url",
|
|
559
|
+
start,
|
|
560
|
+
end: this.#position,
|
|
561
|
+
value: source.slice(valueStart, valueEnd),
|
|
562
|
+
unit: name
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
// shared/syntax/src/css/parser.ts
|
|
568
|
+
var SELECTOR_PSEUDOS = /* @__PURE__ */ new Set([
|
|
569
|
+
"is",
|
|
570
|
+
"where",
|
|
571
|
+
"not",
|
|
572
|
+
"matches",
|
|
573
|
+
"any",
|
|
574
|
+
"-webkit-any",
|
|
575
|
+
"-moz-any",
|
|
576
|
+
"has",
|
|
577
|
+
"host",
|
|
578
|
+
"host-context",
|
|
579
|
+
"slotted",
|
|
580
|
+
"current",
|
|
581
|
+
"past",
|
|
582
|
+
"future",
|
|
583
|
+
"cue",
|
|
584
|
+
"cue-region",
|
|
585
|
+
"global",
|
|
586
|
+
"local",
|
|
587
|
+
"deep"
|
|
588
|
+
]);
|
|
589
|
+
var NTH_PSEUDOS = /* @__PURE__ */ new Set([
|
|
590
|
+
"nth-child",
|
|
591
|
+
"nth-last-child",
|
|
592
|
+
"nth-of-type",
|
|
593
|
+
"nth-last-of-type",
|
|
594
|
+
"nth-col",
|
|
595
|
+
"nth-last-col"
|
|
596
|
+
]);
|
|
597
|
+
var KEYFRAMES = /^(?:-[a-z]+-)?keyframes$/;
|
|
598
|
+
var Parser = class _Parser {
|
|
599
|
+
static {
|
|
600
|
+
__name(this, "Parser");
|
|
601
|
+
}
|
|
602
|
+
#source;
|
|
603
|
+
#tokenizer;
|
|
604
|
+
#tokens;
|
|
605
|
+
#index = 0;
|
|
606
|
+
/** In a declaration value, where `name (` is a function like `name(`. */
|
|
607
|
+
#declarationValue = false;
|
|
608
|
+
/** Errors skipped while recovering, null when errors throw. */
|
|
609
|
+
#errors;
|
|
610
|
+
/** Parses a stylesheet, or only values from `start` to `end`, recovering from errors when asked. */
|
|
611
|
+
constructor(source, start, end, recover = false) {
|
|
612
|
+
this.#source = source;
|
|
613
|
+
const bom = source.startsWith("\uFEFF");
|
|
614
|
+
this.#tokenizer = new Tokenizer(
|
|
615
|
+
end === void 0 ? source : source.slice(0, end),
|
|
616
|
+
start ?? (bom ? 1 : 0),
|
|
617
|
+
recover
|
|
618
|
+
);
|
|
619
|
+
this.#tokens = this.#tokenizer.tokenize();
|
|
620
|
+
this.#errors = this.#tokenizer.errors;
|
|
621
|
+
}
|
|
622
|
+
/** An item, or when recovering from an error in it, null after skipping it. */
|
|
623
|
+
#recovering(top, keyframes) {
|
|
624
|
+
if (!this.#errors) return this.#item(top, keyframes);
|
|
625
|
+
const start = this.#index;
|
|
626
|
+
try {
|
|
627
|
+
return this.#item(top, keyframes);
|
|
628
|
+
} catch (error) {
|
|
629
|
+
if (!(error instanceof ParseError)) throw error;
|
|
630
|
+
this.#errors.push(error);
|
|
631
|
+
this.#index = start;
|
|
632
|
+
const from = this.#peek().start;
|
|
633
|
+
this.#skipItem();
|
|
634
|
+
return {
|
|
635
|
+
type: "Empty",
|
|
636
|
+
start: from,
|
|
637
|
+
end: Math.max(from, this.#peek(-1).end)
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
/** Skips to the end of an item: after its `;` or block, or before the `}` of its block. */
|
|
642
|
+
#skipItem() {
|
|
643
|
+
let depth = 0;
|
|
644
|
+
for (; ; ) {
|
|
645
|
+
const { type } = this.#peek();
|
|
646
|
+
if (type === "eof" || depth === 0 && type === "}") return;
|
|
647
|
+
this.#index++;
|
|
648
|
+
if (type === "(" || type === "[" || type === "{" || type === "function")
|
|
649
|
+
depth++;
|
|
650
|
+
else if (type === ")" || type === "]" || type === "}") {
|
|
651
|
+
depth = Math.max(0, depth - 1);
|
|
652
|
+
if (depth === 0 && type === "}") return;
|
|
653
|
+
} else if (depth === 0 && type === "semicolon") return;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
parse() {
|
|
657
|
+
const children = [];
|
|
658
|
+
for (; ; ) {
|
|
659
|
+
this.#skip(true);
|
|
660
|
+
const token = this.#peek();
|
|
661
|
+
if (token.type === "eof") break;
|
|
662
|
+
if (token.type === "}") {
|
|
663
|
+
if (!this.#errors) this.#error("Unexpected `}`", token.start);
|
|
664
|
+
this.#errors.push(
|
|
665
|
+
new ParseError("Unexpected `}`", token.start)
|
|
666
|
+
);
|
|
667
|
+
this.#index++;
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
const item = this.#recovering(true, false);
|
|
671
|
+
if (item) children.push(item);
|
|
672
|
+
}
|
|
673
|
+
return {
|
|
674
|
+
type: "Stylesheet",
|
|
675
|
+
start: 0,
|
|
676
|
+
end: this.#source.length,
|
|
677
|
+
bom: this.#source.startsWith("\uFEFF"),
|
|
678
|
+
children,
|
|
679
|
+
comments: this.#tokenizer.comments,
|
|
680
|
+
errors: (this.#errors ?? []).map(({ message, position }) => ({
|
|
681
|
+
message,
|
|
682
|
+
position
|
|
683
|
+
}))
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
#error(message, position) {
|
|
687
|
+
throw new ParseError(message, position);
|
|
688
|
+
}
|
|
689
|
+
#peek(offset = 0) {
|
|
690
|
+
return this.#tokens[this.#index + offset] ?? this.#tokens.at(-1);
|
|
691
|
+
}
|
|
692
|
+
#next() {
|
|
693
|
+
const token = this.#peek();
|
|
694
|
+
if (token.type !== "eof") this.#index++;
|
|
695
|
+
return token;
|
|
696
|
+
}
|
|
697
|
+
/** Skips whitespace, and at the top level `<!--` and `-->`. */
|
|
698
|
+
#skip(top = false) {
|
|
699
|
+
for (; ; ) {
|
|
700
|
+
const { type } = this.#peek();
|
|
701
|
+
if (type === "whitespace" || top && (type === "cdo" || type === "cdc"))
|
|
702
|
+
this.#index++;
|
|
703
|
+
else return;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/** The next token that is not whitespace, from an index. */
|
|
707
|
+
#ahead(index) {
|
|
708
|
+
let at = index;
|
|
709
|
+
while (this.#tokens[at]?.type === "whitespace") at++;
|
|
710
|
+
return this.#tokens[at] ?? this.#tokens.at(-1);
|
|
711
|
+
}
|
|
712
|
+
/** A statement of a stylesheet or an item of a block. */
|
|
713
|
+
#item(top, keyframes) {
|
|
714
|
+
const token = this.#peek();
|
|
715
|
+
if (token.type === "semicolon") {
|
|
716
|
+
this.#next();
|
|
717
|
+
return { type: "Empty", start: token.start, end: token.end };
|
|
718
|
+
}
|
|
719
|
+
if (token.type === "at") return this.#atRule();
|
|
720
|
+
if (!top && !keyframes && this.#isDeclaration())
|
|
721
|
+
return this.#declaration();
|
|
722
|
+
return this.#rule(top, keyframes);
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Whether the item starting here is a declaration: a name and a colon,
|
|
726
|
+
* not followed by a block before the end of the declaration, like
|
|
727
|
+
* `a:hover { }` is a rule.
|
|
728
|
+
*/
|
|
729
|
+
#isDeclaration() {
|
|
730
|
+
const token = this.#peek();
|
|
731
|
+
if (token.type !== "ident") {
|
|
732
|
+
if (token.type === "delim" && /^[*_]$/.test(token.value) && this.#peek(1).type === "ident" && this.#ahead(this.#index + 2).type === "colon")
|
|
733
|
+
return !this.#blockAhead(this.#index + 2);
|
|
734
|
+
return false;
|
|
735
|
+
}
|
|
736
|
+
if (this.#ahead(this.#index + 1).type !== "colon") return false;
|
|
737
|
+
if (token.value.startsWith("--")) return true;
|
|
738
|
+
return !this.#blockAhead(this.#index + 1);
|
|
739
|
+
}
|
|
740
|
+
/** Whether a `{` comes at depth 0 before the next `;` or `}`. */
|
|
741
|
+
#blockAhead(from) {
|
|
742
|
+
let depth = 0;
|
|
743
|
+
for (let at = from; at < this.#tokens.length; at++) {
|
|
744
|
+
const { type } = this.#tokens[at];
|
|
745
|
+
if (type === "(" || type === "[" || type === "function") depth++;
|
|
746
|
+
else if (type === ")" || type === "]") depth--;
|
|
747
|
+
else if (depth <= 0) {
|
|
748
|
+
if (type === "{") return true;
|
|
749
|
+
if (type === "semicolon" || type === "}") return false;
|
|
750
|
+
}
|
|
751
|
+
if (type === "eof") return false;
|
|
752
|
+
}
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
755
|
+
#rule(top, keyframes) {
|
|
756
|
+
const start = this.#peek().start;
|
|
757
|
+
const prelude = [];
|
|
758
|
+
for (; ; ) {
|
|
759
|
+
const token = this.#peek();
|
|
760
|
+
if (token.type === "{") break;
|
|
761
|
+
if (token.type === "eof" || token.type === "}" || token.type === "semicolon")
|
|
762
|
+
this.#error("expected a block", token.start);
|
|
763
|
+
if (token.type === "whitespace") {
|
|
764
|
+
this.#next();
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
prelude.push(this.#value());
|
|
768
|
+
}
|
|
769
|
+
const end = prelude.at(-1)?.end ?? start;
|
|
770
|
+
const selectors = keyframes ? null : new SelectorParser(this.#source, start, end, !top).list(!top);
|
|
771
|
+
const block = this.#block(false);
|
|
772
|
+
return {
|
|
773
|
+
type: "Rule",
|
|
774
|
+
start,
|
|
775
|
+
end: block.end,
|
|
776
|
+
selectors,
|
|
777
|
+
prelude,
|
|
778
|
+
block
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
#block(keyframes) {
|
|
782
|
+
const open = this.#next();
|
|
783
|
+
const children = [];
|
|
784
|
+
for (; ; ) {
|
|
785
|
+
this.#skip();
|
|
786
|
+
const token = this.#peek();
|
|
787
|
+
if (token.type === "}") break;
|
|
788
|
+
if (token.type === "eof") this.#error("expected `}`", token.start);
|
|
789
|
+
const item = this.#recovering(false, keyframes);
|
|
790
|
+
if (item) children.push(item);
|
|
791
|
+
}
|
|
792
|
+
const close = this.#next();
|
|
793
|
+
return {
|
|
794
|
+
type: "Block",
|
|
795
|
+
start: open.start,
|
|
796
|
+
end: close.end,
|
|
797
|
+
children
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
#declaration() {
|
|
801
|
+
const first = this.#next();
|
|
802
|
+
let property = first.value;
|
|
803
|
+
let propertyEnd = first.end;
|
|
804
|
+
if (first.type === "delim") {
|
|
805
|
+
const name = this.#next();
|
|
806
|
+
property = first.value + name.value;
|
|
807
|
+
propertyEnd = name.end;
|
|
808
|
+
}
|
|
809
|
+
this.#skip();
|
|
810
|
+
const colon = this.#next();
|
|
811
|
+
const value = [];
|
|
812
|
+
for (; ; ) {
|
|
813
|
+
const token = this.#peek();
|
|
814
|
+
if (token.type === "semicolon" || token.type === "}" || token.type === "eof")
|
|
815
|
+
break;
|
|
816
|
+
if (token.type === "whitespace") {
|
|
817
|
+
this.#next();
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
if (token.type === ")" || token.type === "]")
|
|
821
|
+
this.#error(`Unexpected \`${token.type}\``, token.start);
|
|
822
|
+
if (token.type === "{" && !property.startsWith("--"))
|
|
823
|
+
this.#error("Unexpected `{`", token.start);
|
|
824
|
+
this.#declarationValue = true;
|
|
825
|
+
try {
|
|
826
|
+
value.push(this.#value());
|
|
827
|
+
} finally {
|
|
828
|
+
this.#declarationValue = false;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
const important = _Parser.#important(value);
|
|
832
|
+
let end = important?.end ?? value.at(-1)?.end ?? colon.end;
|
|
833
|
+
let semicolon = false;
|
|
834
|
+
if (this.#peek().type === "semicolon") {
|
|
835
|
+
end = this.#next().end;
|
|
836
|
+
semicolon = true;
|
|
837
|
+
}
|
|
838
|
+
if (!property.startsWith("--") && value.length === 0 && !important)
|
|
839
|
+
this.#error("Expected a value", colon.end);
|
|
840
|
+
return {
|
|
841
|
+
type: "Declaration",
|
|
842
|
+
start: first.start,
|
|
843
|
+
end,
|
|
844
|
+
property,
|
|
845
|
+
propertyEnd,
|
|
846
|
+
colon: colon.start,
|
|
847
|
+
value,
|
|
848
|
+
important,
|
|
849
|
+
semicolon
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
/** Takes a trailing `!important` off values. */
|
|
853
|
+
static #important(value) {
|
|
854
|
+
const keyword = value.at(-1);
|
|
855
|
+
const bang = value.at(-2);
|
|
856
|
+
if (keyword?.type !== "Identifier" || bang?.type !== "Delimiter" || bang.value !== "!" || keyword.value.toLowerCase() !== "important")
|
|
857
|
+
return null;
|
|
858
|
+
value.splice(-2);
|
|
859
|
+
return {
|
|
860
|
+
type: "Important",
|
|
861
|
+
start: bang.start,
|
|
862
|
+
end: keyword.end,
|
|
863
|
+
keyword: keyword.value
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
#atRule() {
|
|
867
|
+
const at = this.#next();
|
|
868
|
+
const name = at.value;
|
|
869
|
+
const keyword = name.toLowerCase();
|
|
870
|
+
this.#skip();
|
|
871
|
+
const prelude = [];
|
|
872
|
+
for (; ; ) {
|
|
873
|
+
const token2 = this.#peek();
|
|
874
|
+
if (token2.type === "{" || token2.type === "semicolon" || token2.type === "}" || token2.type === "eof")
|
|
875
|
+
break;
|
|
876
|
+
if (token2.type === "whitespace") {
|
|
877
|
+
this.#next();
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
if (token2.type === ")" || token2.type === "]")
|
|
881
|
+
this.#error(`Unexpected \`${token2.type}\``, token2.start);
|
|
882
|
+
prelude.push(this.#value());
|
|
883
|
+
}
|
|
884
|
+
const preludeStart = prelude[0]?.start ?? at.end;
|
|
885
|
+
const preludeEnd = prelude.at(-1)?.end ?? at.end;
|
|
886
|
+
let block = null;
|
|
887
|
+
let semicolon = false;
|
|
888
|
+
let end = preludeEnd;
|
|
889
|
+
const token = this.#peek();
|
|
890
|
+
if (token.type === "{") {
|
|
891
|
+
block = this.#block(KEYFRAMES.test(keyword));
|
|
892
|
+
end = block.end;
|
|
893
|
+
} else if (token.type === "semicolon") {
|
|
894
|
+
end = this.#next().end;
|
|
895
|
+
semicolon = true;
|
|
896
|
+
}
|
|
897
|
+
return {
|
|
898
|
+
type: "AtRule",
|
|
899
|
+
start: at.start,
|
|
900
|
+
end,
|
|
901
|
+
name,
|
|
902
|
+
keyword,
|
|
903
|
+
prelude,
|
|
904
|
+
preludeStart,
|
|
905
|
+
preludeEnd,
|
|
906
|
+
block,
|
|
907
|
+
semicolon
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
/** A component value, with nested values of functions and blocks. */
|
|
911
|
+
#value() {
|
|
912
|
+
const token = this.#next();
|
|
913
|
+
const { start, end, value } = token;
|
|
914
|
+
switch (token.type) {
|
|
915
|
+
case "ident":
|
|
916
|
+
if (this.#declarationValue && !/^(?:and|or|not)$/i.test(value) && this.#ahead(this.#index).type === "(") {
|
|
917
|
+
this.#skip();
|
|
918
|
+
const block = this.#value();
|
|
919
|
+
return {
|
|
920
|
+
type: "Function",
|
|
921
|
+
start,
|
|
922
|
+
end: block.end,
|
|
923
|
+
name: value,
|
|
924
|
+
open: block.start,
|
|
925
|
+
children: block.children,
|
|
926
|
+
closed: block.closed
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
return { type: "Identifier", start, end, value };
|
|
930
|
+
case "number":
|
|
931
|
+
return { type: "Number", start, end, value };
|
|
932
|
+
case "percentage":
|
|
933
|
+
return { type: "Percentage", start, end, value };
|
|
934
|
+
case "dimension":
|
|
935
|
+
return {
|
|
936
|
+
type: "Dimension",
|
|
937
|
+
start,
|
|
938
|
+
end,
|
|
939
|
+
value,
|
|
940
|
+
unit: token.unit ?? ""
|
|
941
|
+
};
|
|
942
|
+
case "string":
|
|
943
|
+
return {
|
|
944
|
+
type: "String",
|
|
945
|
+
start,
|
|
946
|
+
end,
|
|
947
|
+
raw: value,
|
|
948
|
+
closed: token.closed ?? true
|
|
949
|
+
};
|
|
950
|
+
case "hash":
|
|
951
|
+
return { type: "Hash", start, end, value };
|
|
952
|
+
case "unicode-range":
|
|
953
|
+
return { type: "UnicodeRange", start, end, value };
|
|
954
|
+
case "at":
|
|
955
|
+
return { type: "AtKeyword", start, end, value };
|
|
956
|
+
case "comma":
|
|
957
|
+
return { type: "Comma", start, end };
|
|
958
|
+
case "colon":
|
|
959
|
+
return { type: "Colon", start, end };
|
|
960
|
+
case "semicolon":
|
|
961
|
+
return { type: "Semicolon", start, end };
|
|
962
|
+
case "url": {
|
|
963
|
+
const inner = this.#source.indexOf(
|
|
964
|
+
value,
|
|
965
|
+
start + (token.unit?.length ?? 3) + 1
|
|
966
|
+
);
|
|
967
|
+
return {
|
|
968
|
+
type: "Url",
|
|
969
|
+
start,
|
|
970
|
+
end,
|
|
971
|
+
name: token.unit ?? "url",
|
|
972
|
+
value: value ? {
|
|
973
|
+
type: "UrlRaw",
|
|
974
|
+
start: inner,
|
|
975
|
+
end: inner + value.length,
|
|
976
|
+
value
|
|
977
|
+
} : null,
|
|
978
|
+
modifiers: []
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
case "function": {
|
|
982
|
+
const children = this.#values(")");
|
|
983
|
+
const close = this.#peek();
|
|
984
|
+
const closed = close.type === ")";
|
|
985
|
+
if (!closed) this.#error("expected `)`", close.start);
|
|
986
|
+
this.#next();
|
|
987
|
+
if (/^url$/i.test(value) && (children.length === 0 || children[0]?.type === "String")) {
|
|
988
|
+
const [address, ...modifiers] = children;
|
|
989
|
+
return {
|
|
990
|
+
type: "Url",
|
|
991
|
+
start,
|
|
992
|
+
end: close.end,
|
|
993
|
+
name: value,
|
|
994
|
+
value: address?.type === "String" ? address : null,
|
|
995
|
+
modifiers
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
return {
|
|
999
|
+
type: "Function",
|
|
1000
|
+
start,
|
|
1001
|
+
end: close.end,
|
|
1002
|
+
name: value,
|
|
1003
|
+
open: end - 1,
|
|
1004
|
+
children,
|
|
1005
|
+
closed
|
|
1006
|
+
};
|
|
1007
|
+
}
|
|
1008
|
+
case "(":
|
|
1009
|
+
case "[":
|
|
1010
|
+
case "{": {
|
|
1011
|
+
const closing = token.type === "(" ? ")" : token.type === "[" ? "]" : "}";
|
|
1012
|
+
const children = this.#values(closing);
|
|
1013
|
+
const close = this.#peek();
|
|
1014
|
+
const closed = close.type === closing;
|
|
1015
|
+
if (!closed)
|
|
1016
|
+
this.#error(`expected \`${closing}\``, close.start);
|
|
1017
|
+
this.#next();
|
|
1018
|
+
return {
|
|
1019
|
+
type: "SimpleBlock",
|
|
1020
|
+
start,
|
|
1021
|
+
end: close.end,
|
|
1022
|
+
open: token.type,
|
|
1023
|
+
children,
|
|
1024
|
+
closed
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
case "cdo":
|
|
1028
|
+
return { type: "Delimiter", start, end, value: "<!--" };
|
|
1029
|
+
case "cdc":
|
|
1030
|
+
return { type: "Delimiter", start, end, value: "-->" };
|
|
1031
|
+
default:
|
|
1032
|
+
if (token.type === "eof")
|
|
1033
|
+
this.#error("Unexpected end of file", start);
|
|
1034
|
+
if (token.type === ")" || token.type === "]" || token.type === "}")
|
|
1035
|
+
this.#error(`Unexpected \`${token.type}\``, start);
|
|
1036
|
+
return { type: "Delimiter", start, end, value };
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
/** Values up to a closing bracket, which stays next. */
|
|
1040
|
+
#values(closing) {
|
|
1041
|
+
const values = [];
|
|
1042
|
+
for (; ; ) {
|
|
1043
|
+
const token = this.#peek();
|
|
1044
|
+
if (token.type === closing || token.type === "eof") return values;
|
|
1045
|
+
if (token.type === "whitespace") {
|
|
1046
|
+
this.#next();
|
|
1047
|
+
continue;
|
|
1048
|
+
}
|
|
1049
|
+
values.push(this.#value());
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
/** Values of a range of the source, like the argument of a pseudo class. */
|
|
1053
|
+
static values(source, start, end) {
|
|
1054
|
+
const parser = new _Parser(source, start, end);
|
|
1055
|
+
const values = [];
|
|
1056
|
+
for (; ; ) {
|
|
1057
|
+
const token = parser.#peek();
|
|
1058
|
+
if (token.type === "eof") return values;
|
|
1059
|
+
if (token.type === "whitespace") {
|
|
1060
|
+
parser.#next();
|
|
1061
|
+
continue;
|
|
1062
|
+
}
|
|
1063
|
+
values.push(parser.#value());
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
var isNameStart2 = /* @__PURE__ */ __name((char) => char !== void 0 && (/^[A-Za-z_]$/.test(char) || char.charCodeAt(0) >= 128), "isNameStart");
|
|
1068
|
+
var isName2 = /* @__PURE__ */ __name((char) => char !== void 0 && (isNameStart2(char) || /^[0-9-]$/.test(char)), "isName");
|
|
1069
|
+
var SelectorParser = class _SelectorParser {
|
|
1070
|
+
static {
|
|
1071
|
+
__name(this, "SelectorParser");
|
|
1072
|
+
}
|
|
1073
|
+
#source;
|
|
1074
|
+
#end;
|
|
1075
|
+
#nested;
|
|
1076
|
+
#position;
|
|
1077
|
+
constructor(source, start, end, nested) {
|
|
1078
|
+
this.#source = source;
|
|
1079
|
+
this.#position = start;
|
|
1080
|
+
this.#end = end;
|
|
1081
|
+
this.#nested = nested;
|
|
1082
|
+
}
|
|
1083
|
+
#error(message, position = this.#position) {
|
|
1084
|
+
throw new ParseError(message, position);
|
|
1085
|
+
}
|
|
1086
|
+
#char(offset = 0) {
|
|
1087
|
+
const at = this.#position + offset;
|
|
1088
|
+
return at < this.#end ? this.#source[at] : void 0;
|
|
1089
|
+
}
|
|
1090
|
+
/** Skips whitespace and comments, whether there were any. */
|
|
1091
|
+
#trivia() {
|
|
1092
|
+
const start = this.#position;
|
|
1093
|
+
for (; ; ) {
|
|
1094
|
+
if (isWhitespace(this.#char())) this.#position++;
|
|
1095
|
+
else if (this.#char() === "/" && this.#char(1) === "*") {
|
|
1096
|
+
const close = this.#source.indexOf("*/", this.#position + 2);
|
|
1097
|
+
this.#position = close < 0 ? this.#end : close + 2;
|
|
1098
|
+
} else return this.#position > start;
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
list(relative) {
|
|
1102
|
+
const start = this.#position;
|
|
1103
|
+
const children = [];
|
|
1104
|
+
for (; ; ) {
|
|
1105
|
+
this.#trivia();
|
|
1106
|
+
children.push(this.#selector(relative));
|
|
1107
|
+
this.#trivia();
|
|
1108
|
+
if (this.#char() === ",") {
|
|
1109
|
+
this.#position++;
|
|
1110
|
+
continue;
|
|
1111
|
+
}
|
|
1112
|
+
break;
|
|
1113
|
+
}
|
|
1114
|
+
if (this.#position < this.#end && this.#char() !== ")")
|
|
1115
|
+
this.#error(`Unexpected \`${this.#char()}\` in selector`);
|
|
1116
|
+
return {
|
|
1117
|
+
type: "SelectorList",
|
|
1118
|
+
start,
|
|
1119
|
+
end: children.at(-1)?.end ?? start,
|
|
1120
|
+
children
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
#combinator() {
|
|
1124
|
+
const start = this.#position;
|
|
1125
|
+
const char = this.#char();
|
|
1126
|
+
if (char === ">" || char === "+" || char === "~") {
|
|
1127
|
+
this.#position++;
|
|
1128
|
+
return {
|
|
1129
|
+
type: "Combinator",
|
|
1130
|
+
start,
|
|
1131
|
+
end: this.#position,
|
|
1132
|
+
value: char
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
if (char === "|" && this.#char(1) === "|") {
|
|
1136
|
+
this.#position += 2;
|
|
1137
|
+
return {
|
|
1138
|
+
type: "Combinator",
|
|
1139
|
+
start,
|
|
1140
|
+
end: this.#position,
|
|
1141
|
+
value: "||"
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
return null;
|
|
1145
|
+
}
|
|
1146
|
+
#selector(relative) {
|
|
1147
|
+
const start = this.#position;
|
|
1148
|
+
const children = [];
|
|
1149
|
+
let combinator = relative || this.#nested ? this.#combinator() : null;
|
|
1150
|
+
if (combinator) this.#trivia();
|
|
1151
|
+
for (; ; ) {
|
|
1152
|
+
const compound = this.#compound(combinator);
|
|
1153
|
+
if (!compound) {
|
|
1154
|
+
if (combinator || children.length === 0)
|
|
1155
|
+
this.#error("Expected a selector");
|
|
1156
|
+
break;
|
|
1157
|
+
}
|
|
1158
|
+
children.push(compound);
|
|
1159
|
+
const before = this.#position;
|
|
1160
|
+
const space = this.#trivia();
|
|
1161
|
+
combinator = this.#combinator();
|
|
1162
|
+
if (combinator) this.#trivia();
|
|
1163
|
+
else if (space) {
|
|
1164
|
+
const next = this.#char();
|
|
1165
|
+
if (next === void 0 || next === "," || next === ")") {
|
|
1166
|
+
this.#position = before;
|
|
1167
|
+
break;
|
|
1168
|
+
}
|
|
1169
|
+
combinator = {
|
|
1170
|
+
type: "Combinator",
|
|
1171
|
+
start: before,
|
|
1172
|
+
end: this.#position,
|
|
1173
|
+
value: " "
|
|
1174
|
+
};
|
|
1175
|
+
} else break;
|
|
1176
|
+
}
|
|
1177
|
+
const last = children.at(-1);
|
|
1178
|
+
return { type: "Selector", start, end: last.end, children };
|
|
1179
|
+
}
|
|
1180
|
+
#name() {
|
|
1181
|
+
const source = this.#source;
|
|
1182
|
+
const start = this.#position;
|
|
1183
|
+
if (this.#char() === "-") this.#position++;
|
|
1184
|
+
if (this.#char() === "-") this.#position++;
|
|
1185
|
+
for (; ; ) {
|
|
1186
|
+
const char = this.#char();
|
|
1187
|
+
if (isName2(char)) this.#position++;
|
|
1188
|
+
else if (char === "\\" && this.#position + 1 < this.#end) {
|
|
1189
|
+
this.#position++;
|
|
1190
|
+
if (/[0-9a-fA-F]/.test(this.#char() ?? "")) {
|
|
1191
|
+
let count = 0;
|
|
1192
|
+
while (count < 6 && /[0-9a-fA-F]/.test(this.#char() ?? "")) {
|
|
1193
|
+
this.#position++;
|
|
1194
|
+
count++;
|
|
1195
|
+
}
|
|
1196
|
+
if (isWhitespace(this.#char())) this.#position++;
|
|
1197
|
+
} else this.#position++;
|
|
1198
|
+
} else break;
|
|
1199
|
+
}
|
|
1200
|
+
return source.slice(start, this.#position);
|
|
1201
|
+
}
|
|
1202
|
+
#isNameStart() {
|
|
1203
|
+
const char = this.#char();
|
|
1204
|
+
if (char === "-")
|
|
1205
|
+
return isNameStart2(this.#char(1)) || this.#char(1) === "-" || this.#char(1) === "\\";
|
|
1206
|
+
return isNameStart2(char) || char === "\\";
|
|
1207
|
+
}
|
|
1208
|
+
#compound(combinator) {
|
|
1209
|
+
const start = this.#position;
|
|
1210
|
+
const children = [];
|
|
1211
|
+
let element = null;
|
|
1212
|
+
while (this.#char() === "&") {
|
|
1213
|
+
const at = this.#position;
|
|
1214
|
+
this.#position++;
|
|
1215
|
+
children.push({
|
|
1216
|
+
type: "NestingSelector",
|
|
1217
|
+
start: at,
|
|
1218
|
+
end: this.#position,
|
|
1219
|
+
suffix: ""
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
const typeStart = this.#position;
|
|
1223
|
+
let namespace = null;
|
|
1224
|
+
let name = null;
|
|
1225
|
+
if (this.#char() === "*" || this.#isNameStart() || this.#char() === "|" && this.#char(1) !== "|") {
|
|
1226
|
+
let first = null;
|
|
1227
|
+
if (this.#char() === "*") {
|
|
1228
|
+
this.#position++;
|
|
1229
|
+
first = "*";
|
|
1230
|
+
} else if (this.#char() !== "|") first = this.#name();
|
|
1231
|
+
const before = this.#position;
|
|
1232
|
+
this.#trivia();
|
|
1233
|
+
if (this.#char() === "|" && this.#char(1) !== "|" && this.#char(1) !== "=") {
|
|
1234
|
+
this.#position++;
|
|
1235
|
+
this.#trivia();
|
|
1236
|
+
namespace = first ?? "";
|
|
1237
|
+
if (this.#char() === "*") {
|
|
1238
|
+
this.#position++;
|
|
1239
|
+
name = "*";
|
|
1240
|
+
} else if (this.#isNameStart()) name = this.#name();
|
|
1241
|
+
else this.#error("Expected a name after the namespace");
|
|
1242
|
+
} else {
|
|
1243
|
+
this.#position = before;
|
|
1244
|
+
name = first;
|
|
1245
|
+
}
|
|
1246
|
+
if (name === "*")
|
|
1247
|
+
element = {
|
|
1248
|
+
type: "UniversalSelector",
|
|
1249
|
+
start: typeStart,
|
|
1250
|
+
end: this.#position,
|
|
1251
|
+
namespace
|
|
1252
|
+
};
|
|
1253
|
+
else if (name !== null)
|
|
1254
|
+
element = {
|
|
1255
|
+
type: "TypeSelector",
|
|
1256
|
+
start: typeStart,
|
|
1257
|
+
end: this.#position,
|
|
1258
|
+
namespace,
|
|
1259
|
+
name
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
for (; ; ) {
|
|
1263
|
+
const at = this.#position;
|
|
1264
|
+
const char = this.#char();
|
|
1265
|
+
if (char === "#") {
|
|
1266
|
+
this.#position++;
|
|
1267
|
+
if (!isName2(this.#char()) && this.#char() !== "\\")
|
|
1268
|
+
this.#error("Expected an identifier");
|
|
1269
|
+
const name2 = this.#name();
|
|
1270
|
+
children.push({
|
|
1271
|
+
type: "IdSelector",
|
|
1272
|
+
start: at,
|
|
1273
|
+
end: this.#position,
|
|
1274
|
+
name: name2
|
|
1275
|
+
});
|
|
1276
|
+
} else if (char === ".") {
|
|
1277
|
+
this.#position++;
|
|
1278
|
+
if (!this.#isNameStart()) this.#error("Expected an identifier");
|
|
1279
|
+
const name2 = this.#name();
|
|
1280
|
+
children.push({
|
|
1281
|
+
type: "ClassSelector",
|
|
1282
|
+
start: at,
|
|
1283
|
+
end: this.#position,
|
|
1284
|
+
name: name2
|
|
1285
|
+
});
|
|
1286
|
+
} else if (char === "[") children.push(this.#attribute());
|
|
1287
|
+
else if (char === ":") children.push(this.#pseudo());
|
|
1288
|
+
else if (char === "&") {
|
|
1289
|
+
this.#position++;
|
|
1290
|
+
children.push({
|
|
1291
|
+
type: "NestingSelector",
|
|
1292
|
+
start: at,
|
|
1293
|
+
end: this.#position,
|
|
1294
|
+
suffix: ""
|
|
1295
|
+
});
|
|
1296
|
+
} else break;
|
|
1297
|
+
}
|
|
1298
|
+
if (!element && children.length === 0) return null;
|
|
1299
|
+
return {
|
|
1300
|
+
type: "CompoundSelector",
|
|
1301
|
+
start: combinator?.start ?? start,
|
|
1302
|
+
end: this.#position,
|
|
1303
|
+
combinator,
|
|
1304
|
+
element,
|
|
1305
|
+
children
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
#attribute() {
|
|
1309
|
+
const start = this.#position;
|
|
1310
|
+
this.#position++;
|
|
1311
|
+
this.#trivia();
|
|
1312
|
+
let namespace = null;
|
|
1313
|
+
let name;
|
|
1314
|
+
if (this.#char() === "*") {
|
|
1315
|
+
this.#position++;
|
|
1316
|
+
this.#trivia();
|
|
1317
|
+
if (this.#char() !== "|") this.#error("Expected `|`");
|
|
1318
|
+
this.#position++;
|
|
1319
|
+
this.#trivia();
|
|
1320
|
+
namespace = "*";
|
|
1321
|
+
name = this.#name();
|
|
1322
|
+
} else if (this.#char() === "|") {
|
|
1323
|
+
this.#position++;
|
|
1324
|
+
this.#trivia();
|
|
1325
|
+
namespace = "";
|
|
1326
|
+
name = this.#name();
|
|
1327
|
+
} else {
|
|
1328
|
+
if (!this.#isNameStart()) this.#error("Expected an attribute name");
|
|
1329
|
+
name = this.#name();
|
|
1330
|
+
const before = this.#position;
|
|
1331
|
+
this.#trivia();
|
|
1332
|
+
if (this.#char() === "|" && this.#char(1) !== "=") {
|
|
1333
|
+
this.#position++;
|
|
1334
|
+
this.#trivia();
|
|
1335
|
+
namespace = name;
|
|
1336
|
+
name = this.#name();
|
|
1337
|
+
} else this.#position = before;
|
|
1338
|
+
}
|
|
1339
|
+
if (!name) this.#error("Expected an attribute name");
|
|
1340
|
+
this.#trivia();
|
|
1341
|
+
let matcher = null;
|
|
1342
|
+
let value = null;
|
|
1343
|
+
let modifier = null;
|
|
1344
|
+
const char = this.#char();
|
|
1345
|
+
if (char === "=") {
|
|
1346
|
+
matcher = "=";
|
|
1347
|
+
this.#position++;
|
|
1348
|
+
} else if (char && "~|^$*".includes(char) && this.#char(1) === "=") {
|
|
1349
|
+
matcher = `${char}=`;
|
|
1350
|
+
this.#position += 2;
|
|
1351
|
+
}
|
|
1352
|
+
if (matcher) {
|
|
1353
|
+
this.#trivia();
|
|
1354
|
+
const at = this.#position;
|
|
1355
|
+
const quote = this.#char();
|
|
1356
|
+
if (quote === '"' || quote === "'") {
|
|
1357
|
+
this.#position++;
|
|
1358
|
+
while (this.#char() !== void 0 && this.#char() !== quote) {
|
|
1359
|
+
if (this.#char() === "\\") this.#position++;
|
|
1360
|
+
this.#position++;
|
|
1361
|
+
}
|
|
1362
|
+
if (this.#char() !== quote)
|
|
1363
|
+
this.#error("Missing closing quote", at);
|
|
1364
|
+
this.#position++;
|
|
1365
|
+
value = {
|
|
1366
|
+
type: "String",
|
|
1367
|
+
start: at,
|
|
1368
|
+
end: this.#position,
|
|
1369
|
+
raw: this.#source.slice(at, this.#position),
|
|
1370
|
+
closed: true
|
|
1371
|
+
};
|
|
1372
|
+
} else if (this.#isNameStart() || /[0-9]/.test(quote ?? "")) {
|
|
1373
|
+
while (isName2(this.#char()) || this.#char() === "\\") {
|
|
1374
|
+
if (this.#char() === "\\") this.#position++;
|
|
1375
|
+
this.#position++;
|
|
1376
|
+
}
|
|
1377
|
+
value = {
|
|
1378
|
+
type: "Identifier",
|
|
1379
|
+
start: at,
|
|
1380
|
+
end: this.#position,
|
|
1381
|
+
value: this.#source.slice(at, this.#position)
|
|
1382
|
+
};
|
|
1383
|
+
} else this.#error("Expected an attribute value");
|
|
1384
|
+
this.#trivia();
|
|
1385
|
+
if (/^[iIsS]$/.test(this.#char() ?? "") && !isName2(this.#char(1))) {
|
|
1386
|
+
modifier = this.#char();
|
|
1387
|
+
this.#position++;
|
|
1388
|
+
this.#trivia();
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
if (this.#char() !== "]") this.#error("Expected `]`");
|
|
1392
|
+
this.#position++;
|
|
1393
|
+
return {
|
|
1394
|
+
type: "AttributeSelector",
|
|
1395
|
+
start,
|
|
1396
|
+
end: this.#position,
|
|
1397
|
+
namespace,
|
|
1398
|
+
name,
|
|
1399
|
+
matcher,
|
|
1400
|
+
value,
|
|
1401
|
+
modifier
|
|
1402
|
+
};
|
|
1403
|
+
}
|
|
1404
|
+
#pseudo() {
|
|
1405
|
+
const start = this.#position;
|
|
1406
|
+
this.#position++;
|
|
1407
|
+
const element = this.#char() === ":";
|
|
1408
|
+
if (element) this.#position++;
|
|
1409
|
+
this.#trivia();
|
|
1410
|
+
if (!this.#isNameStart()) this.#error("Expected a pseudo name");
|
|
1411
|
+
const name = this.#name();
|
|
1412
|
+
let argument = null;
|
|
1413
|
+
const before = this.#position;
|
|
1414
|
+
this.#trivia();
|
|
1415
|
+
if (this.#char() !== "(" || /\s/.test(
|
|
1416
|
+
this.#source.slice(before, this.#position).replace(/\/\*[\s\S]*?\*\//g, "")
|
|
1417
|
+
))
|
|
1418
|
+
this.#position = before;
|
|
1419
|
+
if (this.#char() === "(") {
|
|
1420
|
+
this.#position++;
|
|
1421
|
+
const open = this.#position;
|
|
1422
|
+
const close = this.#closing();
|
|
1423
|
+
const lower = name.toLowerCase();
|
|
1424
|
+
this.#trivia();
|
|
1425
|
+
if (SELECTOR_PSEUDOS.has(lower) || element && /^(?:slotted|cue|-webkit-any)$/.test(lower)) {
|
|
1426
|
+
if (this.#position >= close) {
|
|
1427
|
+
if (lower !== "host" && lower !== "where" && lower !== "is")
|
|
1428
|
+
this.#error("Expected a selector");
|
|
1429
|
+
argument = {
|
|
1430
|
+
type: "SelectorList",
|
|
1431
|
+
start: close,
|
|
1432
|
+
end: close,
|
|
1433
|
+
children: []
|
|
1434
|
+
};
|
|
1435
|
+
} else {
|
|
1436
|
+
argument = new _SelectorParser(
|
|
1437
|
+
this.#source,
|
|
1438
|
+
this.#position,
|
|
1439
|
+
close,
|
|
1440
|
+
false
|
|
1441
|
+
).list(lower === "has");
|
|
1442
|
+
}
|
|
1443
|
+
} else if (!element && NTH_PSEUDOS.has(lower))
|
|
1444
|
+
argument = this.#nth(close);
|
|
1445
|
+
else
|
|
1446
|
+
argument = {
|
|
1447
|
+
type: "PseudoValues",
|
|
1448
|
+
start: open,
|
|
1449
|
+
end: close,
|
|
1450
|
+
children: Parser.values(
|
|
1451
|
+
this.#source,
|
|
1452
|
+
this.#position,
|
|
1453
|
+
close
|
|
1454
|
+
)
|
|
1455
|
+
};
|
|
1456
|
+
this.#position = close + 1;
|
|
1457
|
+
}
|
|
1458
|
+
return element ? {
|
|
1459
|
+
type: "PseudoElementSelector",
|
|
1460
|
+
start,
|
|
1461
|
+
end: this.#position,
|
|
1462
|
+
name,
|
|
1463
|
+
argument
|
|
1464
|
+
} : {
|
|
1465
|
+
type: "PseudoClassSelector",
|
|
1466
|
+
start,
|
|
1467
|
+
end: this.#position,
|
|
1468
|
+
name,
|
|
1469
|
+
argument
|
|
1470
|
+
};
|
|
1471
|
+
}
|
|
1472
|
+
/** The position of the `)` closing the parentheses we are in. */
|
|
1473
|
+
#closing() {
|
|
1474
|
+
const source = this.#source;
|
|
1475
|
+
let depth = 0;
|
|
1476
|
+
for (let at = this.#position; at < this.#end; at++) {
|
|
1477
|
+
const char = source[at];
|
|
1478
|
+
if (char === "\\") at++;
|
|
1479
|
+
else if (char === '"' || char === "'") {
|
|
1480
|
+
at++;
|
|
1481
|
+
while (at < this.#end && source[at] !== char) {
|
|
1482
|
+
if (source[at] === "\\") at++;
|
|
1483
|
+
at++;
|
|
1484
|
+
}
|
|
1485
|
+
} else if (char === "/" && source[at + 1] === "*") {
|
|
1486
|
+
const close = source.indexOf("*/", at + 2);
|
|
1487
|
+
at = close < 0 ? this.#end : close + 1;
|
|
1488
|
+
} else if (char === "(") depth++;
|
|
1489
|
+
else if (char === ")") {
|
|
1490
|
+
if (depth === 0) return at;
|
|
1491
|
+
depth--;
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
this.#error("Expected `)`");
|
|
1495
|
+
}
|
|
1496
|
+
#nth(close) {
|
|
1497
|
+
const source = this.#source;
|
|
1498
|
+
const start = this.#position;
|
|
1499
|
+
let end = close;
|
|
1500
|
+
let selectors = null;
|
|
1501
|
+
const of = /\s+of\s+/i.exec(source.slice(start, close));
|
|
1502
|
+
if (of) {
|
|
1503
|
+
end = start + of.index;
|
|
1504
|
+
const parser = new _SelectorParser(
|
|
1505
|
+
source,
|
|
1506
|
+
start + of.index + of[0].length,
|
|
1507
|
+
close,
|
|
1508
|
+
false
|
|
1509
|
+
);
|
|
1510
|
+
selectors = parser.list(false);
|
|
1511
|
+
}
|
|
1512
|
+
let value = source.slice(start, end).replace(/\/\*[\s\S]*?\*\//g, "").trim();
|
|
1513
|
+
end = start + source.slice(start, end).trimEnd().length;
|
|
1514
|
+
const compact = value.replace(/\s+/g, "").toLowerCase();
|
|
1515
|
+
let a = null;
|
|
1516
|
+
let b = null;
|
|
1517
|
+
if (compact === "even") [a, b] = [2, 0];
|
|
1518
|
+
else if (compact === "odd") [a, b] = [2, 1];
|
|
1519
|
+
else {
|
|
1520
|
+
const match = /^([+-]?\d*)n([+-]\d+)?$|^([+-]?\d+)$/.exec(compact);
|
|
1521
|
+
if (match) {
|
|
1522
|
+
if (match[3] !== void 0) [a, b] = [0, Number(match[3])];
|
|
1523
|
+
else {
|
|
1524
|
+
const coefficient = match[1] ?? "";
|
|
1525
|
+
a = coefficient === "" || coefficient === "+" ? 1 : coefficient === "-" ? -1 : Number(coefficient);
|
|
1526
|
+
b = match[2] ? Number(match[2]) : 0;
|
|
1527
|
+
}
|
|
1528
|
+
} else if (!value) this.#error("Expected An+B");
|
|
1529
|
+
}
|
|
1530
|
+
value = source.slice(start, end);
|
|
1531
|
+
return {
|
|
1532
|
+
type: "Nth",
|
|
1533
|
+
start,
|
|
1534
|
+
end: selectors?.end ?? end,
|
|
1535
|
+
value,
|
|
1536
|
+
a,
|
|
1537
|
+
b,
|
|
1538
|
+
selectors
|
|
1539
|
+
};
|
|
1540
|
+
}
|
|
1541
|
+
};
|
|
1542
|
+
|
|
274
1543
|
// shared/syntax/src/html/tags.ts
|
|
275
1544
|
var tagNames = /* @__PURE__ */ new Set([
|
|
276
1545
|
"a",
|
|
@@ -513,7 +1782,7 @@ var VOID = /* @__PURE__ */ new Set([
|
|
|
513
1782
|
"wbr"
|
|
514
1783
|
]);
|
|
515
1784
|
var RAW = /* @__PURE__ */ new Set(["script", "style", "pre", "textarea", "xmp", "plaintext"]);
|
|
516
|
-
var
|
|
1785
|
+
var isWhitespace2 = /* @__PURE__ */ __name((char) => char === " " || char === " " || char === "\n" || char === "\r", "isWhitespace");
|
|
517
1786
|
var isTagNameChar = /* @__PURE__ */ __name((char) => char !== void 0 && /^[A-Za-z0-9_:.-]$/.test(char), "isTagNameChar");
|
|
518
1787
|
var isAttributeNameChar = /* @__PURE__ */ __name((char) => char !== void 0 && char.charCodeAt(0) < 127 && char.charCodeAt(0) > 31 && !/[ \t\n"'<>/=]/.test(char), "isAttributeNameChar");
|
|
519
1788
|
function tagOf(name) {
|
|
@@ -522,7 +1791,7 @@ function tagOf(name) {
|
|
|
522
1791
|
return tagNames.has(lower) ? lower : null;
|
|
523
1792
|
}
|
|
524
1793
|
__name(tagOf, "tagOf");
|
|
525
|
-
var
|
|
1794
|
+
var Parser2 = class {
|
|
526
1795
|
static {
|
|
527
1796
|
__name(this, "Parser");
|
|
528
1797
|
}
|
|
@@ -561,7 +1830,7 @@ var Parser = class {
|
|
|
561
1830
|
#trivia() {
|
|
562
1831
|
const source = this.#source;
|
|
563
1832
|
for (; ; ) {
|
|
564
|
-
while (
|
|
1833
|
+
while (isWhitespace2(source[this.#position])) this.#position++;
|
|
565
1834
|
if (!source.startsWith("<!--", this.#position)) return;
|
|
566
1835
|
const start = this.#position;
|
|
567
1836
|
const close = source.indexOf("-->", start + 4);
|
|
@@ -750,7 +2019,7 @@ var Parser = class {
|
|
|
750
2019
|
const source = this.#source;
|
|
751
2020
|
let position = start + 2;
|
|
752
2021
|
while (isTagNameChar(source[position])) position++;
|
|
753
|
-
while (
|
|
2022
|
+
while (isWhitespace2(source[position])) position++;
|
|
754
2023
|
if (source[position] !== ">")
|
|
755
2024
|
this.#error("Expected a `>` to end the closing tag.", position);
|
|
756
2025
|
return position + 1;
|
|
@@ -767,7 +2036,7 @@ var Parser = class {
|
|
|
767
2036
|
const source = this.#source;
|
|
768
2037
|
const attributes = [];
|
|
769
2038
|
for (; ; ) {
|
|
770
|
-
while (
|
|
2039
|
+
while (isWhitespace2(source[this.#position])) this.#position++;
|
|
771
2040
|
const char = source[this.#position];
|
|
772
2041
|
if (char === void 0 || char === ">" || char === "/" || char === "?" && source[this.#position + 1] === ">")
|
|
773
2042
|
return attributes;
|
|
@@ -789,10 +2058,10 @@ var Parser = class {
|
|
|
789
2058
|
value: null
|
|
790
2059
|
};
|
|
791
2060
|
let after = this.#position;
|
|
792
|
-
while (
|
|
2061
|
+
while (isWhitespace2(source[after])) after++;
|
|
793
2062
|
if (source[after] === "=") {
|
|
794
2063
|
this.#position = after + 1;
|
|
795
|
-
while (
|
|
2064
|
+
while (isWhitespace2(source[this.#position])) this.#position++;
|
|
796
2065
|
attribute.value = this.#value();
|
|
797
2066
|
attribute.end = attribute.value.end;
|
|
798
2067
|
}
|
|
@@ -818,7 +2087,7 @@ var Parser = class {
|
|
|
818
2087
|
}
|
|
819
2088
|
while (this.#position < source.length) {
|
|
820
2089
|
const char = source[this.#position];
|
|
821
|
-
if (
|
|
2090
|
+
if (isWhitespace2(char) || char === ">") break;
|
|
822
2091
|
if (/[?'"=<`]/.test(char) || char.charCodeAt(0) > 127)
|
|
823
2092
|
this.#error("Unexpected character in unquoted attribute value");
|
|
824
2093
|
this.#position++;
|
|
@@ -841,9 +2110,9 @@ var Parser = class {
|
|
|
841
2110
|
const start = this.#position;
|
|
842
2111
|
this.#position += 2;
|
|
843
2112
|
const word = /* @__PURE__ */ __name(() => {
|
|
844
|
-
while (
|
|
2113
|
+
while (isWhitespace2(source[this.#position])) this.#position++;
|
|
845
2114
|
const from = this.#position;
|
|
846
|
-
while (this.#position < source.length && !
|
|
2115
|
+
while (this.#position < source.length && !isWhitespace2(source[this.#position]) && !/[>"']/.test(source[this.#position]))
|
|
847
2116
|
this.#position++;
|
|
848
2117
|
return from < this.#position ? source.slice(from, this.#position) : null;
|
|
849
2118
|
}, "word");
|
|
@@ -852,7 +2121,7 @@ var Parser = class {
|
|
|
852
2121
|
const literal = word();
|
|
853
2122
|
const identifiers = [];
|
|
854
2123
|
for (let index = 0; index < 2; index++) {
|
|
855
|
-
while (
|
|
2124
|
+
while (isWhitespace2(source[this.#position])) this.#position++;
|
|
856
2125
|
const quote = source[this.#position];
|
|
857
2126
|
if (quote !== '"' && quote !== "'") break;
|
|
858
2127
|
const close = source.indexOf(quote, this.#position + 1);
|
|
@@ -860,7 +2129,7 @@ var Parser = class {
|
|
|
860
2129
|
identifiers.push(source.slice(this.#position, close + 1));
|
|
861
2130
|
this.#position = close + 1;
|
|
862
2131
|
}
|
|
863
|
-
while (
|
|
2132
|
+
while (isWhitespace2(source[this.#position])) this.#position++;
|
|
864
2133
|
this.#expect(">");
|
|
865
2134
|
return {
|
|
866
2135
|
type: "Doctype",
|
|
@@ -4297,7 +5566,7 @@ var ExpressionParser = class _ExpressionParser extends TypeParser {
|
|
|
4297
5566
|
};
|
|
4298
5567
|
|
|
4299
5568
|
// shared/syntax/src/parser/statements.ts
|
|
4300
|
-
var
|
|
5569
|
+
var Parser3 = class extends ExpressionParser {
|
|
4301
5570
|
static {
|
|
4302
5571
|
__name(this, "Parser");
|
|
4303
5572
|
}
|
|
@@ -5201,8 +6470,25 @@ function isHtml(file) {
|
|
|
5201
6470
|
return /\.html?$/i.test(file);
|
|
5202
6471
|
}
|
|
5203
6472
|
__name(isHtml, "isHtml");
|
|
6473
|
+
function isCss(file) {
|
|
6474
|
+
return /\.css$/i.test(file);
|
|
6475
|
+
}
|
|
6476
|
+
__name(isCss, "isCss");
|
|
6477
|
+
function parseCss(source, options = {}) {
|
|
6478
|
+
return new Parser(
|
|
6479
|
+
source,
|
|
6480
|
+
void 0,
|
|
6481
|
+
void 0,
|
|
6482
|
+
options.recover ?? false
|
|
6483
|
+
).parse();
|
|
6484
|
+
}
|
|
6485
|
+
__name(parseCss, "parseCss");
|
|
6486
|
+
function parseCssValues(source, start = 0, end = source.length) {
|
|
6487
|
+
return Parser.values(source, start, end);
|
|
6488
|
+
}
|
|
6489
|
+
__name(parseCssValues, "parseCssValues");
|
|
5204
6490
|
function parseHtml(source) {
|
|
5205
|
-
return new
|
|
6491
|
+
return new Parser2(source).parse();
|
|
5206
6492
|
}
|
|
5207
6493
|
__name(parseHtml, "parseHtml");
|
|
5208
6494
|
function parseMarkdown(source, options = {}) {
|
|
@@ -5210,13 +6496,13 @@ function parseMarkdown(source, options = {}) {
|
|
|
5210
6496
|
}
|
|
5211
6497
|
__name(parseMarkdown, "parseMarkdown");
|
|
5212
6498
|
function parse(source, options) {
|
|
5213
|
-
const parser = new
|
|
6499
|
+
const parser = new Parser3(source, options);
|
|
5214
6500
|
const program = parser.parseProgram();
|
|
5215
6501
|
return { program, comments: parser.comments };
|
|
5216
6502
|
}
|
|
5217
6503
|
__name(parse, "parse");
|
|
5218
6504
|
function parseExpression(source) {
|
|
5219
|
-
const parser = new
|
|
6505
|
+
const parser = new Parser3(source, { typescript: false, jsx: false });
|
|
5220
6506
|
const expression = parser.parseDocument();
|
|
5221
6507
|
const statement = {
|
|
5222
6508
|
type: "ExpressionStatement",
|
|
@@ -5238,10 +6524,13 @@ export {
|
|
|
5238
6524
|
Lines,
|
|
5239
6525
|
ParseError,
|
|
5240
6526
|
Walker,
|
|
6527
|
+
isCss,
|
|
5241
6528
|
isHtml,
|
|
5242
6529
|
isMarkdown,
|
|
5243
6530
|
language,
|
|
5244
6531
|
parse,
|
|
6532
|
+
parseCss,
|
|
6533
|
+
parseCssValues,
|
|
5245
6534
|
parseExpression,
|
|
5246
6535
|
parseHtml,
|
|
5247
6536
|
parseMarkdown,
|