@tbela99/css-parser 1.4.10 → 1.5.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/README.md +11 -4
  3. package/dist/index-umd-web.js +2118 -1701
  4. package/dist/index.cjs +2130 -1696
  5. package/dist/index.d.ts +781 -269
  6. package/dist/lib/ast/clone.js +13 -8
  7. package/dist/lib/ast/expand.js +45 -9
  8. package/dist/lib/ast/features/calc.js +2 -1
  9. package/dist/lib/ast/features/if.js +24 -4
  10. package/dist/lib/ast/features/prefix.js +13 -31
  11. package/dist/lib/ast/features/shorthand.js +1 -1
  12. package/dist/lib/ast/features/transform.js +5 -2
  13. package/dist/lib/ast/find.js +7 -5
  14. package/dist/lib/ast/minify.js +80 -35
  15. package/dist/lib/ast/node.js +50 -0
  16. package/dist/lib/ast/types.js +1 -1
  17. package/dist/lib/ast/walk.js +27 -7
  18. package/dist/lib/fs/resolve.js +64 -32
  19. package/dist/lib/parser/declaration/map.js +14 -5
  20. package/dist/lib/parser/linesmap.js +5 -13
  21. package/dist/lib/parser/parse.js +489 -719
  22. package/dist/lib/parser/source.js +16 -2
  23. package/dist/lib/parser/tokenize.js +382 -278
  24. package/dist/lib/parser/utils/at-rule-container.js +2 -1
  25. package/dist/lib/parser/utils/selector.js +88 -10
  26. package/dist/lib/renderer/render.js +174 -99
  27. package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
  28. package/dist/lib/renderer/sourcemap/sourcemap.js +190 -34
  29. package/dist/lib/syntax/syntax.js +14 -151
  30. package/dist/lib/validation/match.js +81 -1
  31. package/dist/node.js +41 -26
  32. package/dist/utils/sync.d.ts +16 -0
  33. package/dist/utils/sync.js +52 -0
  34. package/dist/web.js +26 -28
  35. package/llms.txt +12 -5
  36. package/package.json +1 -1
  37. package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
@@ -1,6 +1,6 @@
1
1
  import { EnumToken, ColorType } from '../ast/types.js';
2
2
  import { LOC, wildCardFuncs, whenElseFunc, transformFunctions, mathFuncs, colorsFunc, timingFunc, supportFunc, timelineFunc, imageFunc, gridTemplateFunc, urlFunc, containerFunc, pseudoElements } from '../syntax/constants.js';
3
- import { isDigit, isPseudo, isIdent, isWhiteSpace, isNumber, isHexColor, isHash, isPercentage, parseDimension, isNewLine } from '../syntax/syntax.js';
3
+ import { isDigit, isWhiteSpace, isIdent, isHexColor, isHash, isNumber, isPercentage, parseDimension, isNewLine, isIdentStart, isIdentCodepoint, isNonPrintable } from '../syntax/syntax.js';
4
4
  import { SourceFile } from './source.js';
5
5
  import { equalsIgnoreCase } from './utils/text.js';
6
6
 
@@ -136,17 +136,17 @@ var TokenMap;
136
136
  TokenMap[TokenMap["GREATERTHAN"] = 62] = "GREATERTHAN";
137
137
  })(TokenMap || (TokenMap = {}));
138
138
  function consumeString(parseInfo) {
139
- const quote = next(parseInfo);
140
- let value;
141
- let buffer = quote;
139
+ const quote = next(parseInfo).charCodeAt(0);
140
+ let charCode;
141
+ let decodeSegments = false;
142
142
  const result = [];
143
- while ((value = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1))) {
144
- if (value == "\\") {
145
- if ("\\" == parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 2)) {
146
- buffer += next(parseInfo, 2);
143
+ while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
144
+ if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
145
+ if (charCode == parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1)) {
146
+ next(parseInfo, 2);
147
147
  continue;
148
148
  }
149
- const sequence = peek(parseInfo, 6);
149
+ const sequence = peek(parseInfo, 7);
150
150
  let escapeSequence = "";
151
151
  let codepoint;
152
152
  let i;
@@ -165,50 +165,66 @@ function consumeString(parseInfo) {
165
165
  break;
166
166
  }
167
167
  if (escapeSequence.trimEnd().length > 0) {
168
- const codepoint = parseInt(escapeSequence, 16);
169
- if (codepoint == 0 ||
170
- // leading surrogate
171
- (0xd800 <= codepoint && codepoint <= 0xdbff) ||
172
- // trailing surrogate
173
- (0xdc00 <= codepoint && codepoint <= 0xdfff)) {
174
- buffer += String.fromCodePoint(0xfffd);
175
- }
176
- else {
177
- buffer += String.fromCodePoint(codepoint);
178
- }
179
- next(parseInfo, escapeSequence.length +
168
+ // const codepoint = parseInt(escapeSequence, 16);
169
+ // TODO set decode flag ON
170
+ // if (
171
+ // codepoint == 0 ||
172
+ // // leading surrogate
173
+ // (0xd800 <= codepoint && codepoint <= 0xdbff) ||
174
+ // // trailing surrogate
175
+ // (0xdc00 <= codepoint && codepoint <= 0xdfff)
176
+ // ) {
177
+ // buffer += String.fromCodePoint(0xfffd);
178
+ // } else {
179
+ // buffer += String.fromCodePoint(codepoint);
180
+ // }
181
+ const length = escapeSequence.length +
180
182
  1 +
181
- (isWhiteSpace(parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1)?.charCodeAt(0))
183
+ (isWhiteSpace(parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset)?.charCodeAt(0))
182
184
  ? 1
183
- : 0));
185
+ : 0);
186
+ decodeSegments = true;
187
+ next(parseInfo, length);
184
188
  continue;
185
189
  }
186
- buffer += next(parseInfo, 2);
190
+ next(parseInfo, 2);
187
191
  continue;
188
192
  }
189
- if (value == quote) {
190
- buffer += value;
191
- result.push(yieldResult(buffer, parseInfo,
192
- /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType));
193
+ if (charCode == quote) {
193
194
  next(parseInfo);
194
- buffer = "";
195
+ result.push(yieldResult(parseInfo,
196
+ /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType, decodeSegments ? { decodeSegments } : null));
195
197
  return result;
196
198
  }
197
- if (isNewLine(value.charCodeAt(0))) {
198
- result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.BadStringTokenType));
199
+ if (isNewLine(charCode)) {
200
+ next(parseInfo);
201
+ result.push(yieldResult(parseInfo, EnumToken.BadStringTokenType));
199
202
  return result;
200
203
  }
201
- buffer += value;
202
204
  next(parseInfo);
203
205
  }
204
206
  // EOF - 'Unclosed-string' fixed
205
- result.push(yieldResult(buffer + quote, parseInfo, EnumToken.StringTokenType));
207
+ result.push(yieldResult(parseInfo, EnumToken.StringTokenType));
206
208
  return result;
207
209
  }
208
- function yieldResult(val, parseInfo, hint) {
210
+ function yieldResult(parseInfo, hint, options) {
211
+ let val = parseInfo.stream.slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
209
212
  let token = null;
210
213
  let dimension;
211
- // console.debug(`Yield result: ${val}, ${hint}`);
214
+ if (options?.decodeSegments) {
215
+ val = val.replace(/\\([0-9a-fA-F]{1,6})(?:\s)?/g, (_, sequence) => {
216
+ const codepoint = parseInt(sequence, 16);
217
+ if (codepoint == 0 ||
218
+ // leading surrogate
219
+ (0xd800 <= codepoint && codepoint <= 0xdbff) ||
220
+ // trailing surrogate
221
+ (0xdc00 <= codepoint && codepoint <= 0xdfff) ||
222
+ codepoint > 0x10ffff) {
223
+ return "\uFFFD";
224
+ }
225
+ return String.fromCodePoint(codepoint);
226
+ });
227
+ }
212
228
  if (hint != null) {
213
229
  let searchArray = null;
214
230
  switch (hint) {
@@ -260,7 +276,12 @@ function yieldResult(val, parseInfo, hint) {
260
276
  else {
261
277
  let slice = val.slice(1);
262
278
  const chr = val.charAt(0);
263
- if (chr == "@" && isIdent(slice)) {
279
+ if (chr == "!" && equalsIgnoreCase("!important", val)) {
280
+ token = {
281
+ typ: EnumToken.ImportantTokenType,
282
+ };
283
+ }
284
+ else if (chr == "@" && isIdent(slice)) {
264
285
  token = {
265
286
  typ: EnumToken.AtRuleTokenType,
266
287
  nam: slice,
@@ -335,12 +356,12 @@ function yieldResult(val, parseInfo, hint) {
335
356
  end: parseInfo.currentPosition,
336
357
  };
337
358
  parseInfo.position = parseInfo.currentPosition;
338
- return { token, bytesIn: parseInfo.currentPosition + 1 };
359
+ return { token, bytesIn: parseInfo.currentPosition };
339
360
  }
340
361
  function match(parseInfo, input) {
341
362
  let position = parseInfo.currentPosition - parseInfo.offset;
342
363
  for (let i = 0; i < input.length; i++) {
343
- if (parseInfo.stream[position + i + 1] != input.charAt(i)) {
364
+ if (parseInfo.stream[position + i] != input.charAt(i)) {
344
365
  return false;
345
366
  }
346
367
  }
@@ -348,14 +369,14 @@ function match(parseInfo, input) {
348
369
  }
349
370
  function peek(parseInfo, count = 1) {
350
371
  if (count == 1) {
351
- return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1);
372
+ return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset);
352
373
  }
353
374
  const position = parseInfo.currentPosition - parseInfo.offset;
354
- return parseInfo.stream.slice(position + 1, position + count + 1);
375
+ return parseInfo.stream.slice(position, position + count);
355
376
  }
356
377
  function next(parseInfo, count = 1) {
357
378
  let position = parseInfo.currentPosition - parseInfo.offset;
358
- let char = count == 1 ? parseInfo.stream.charAt(position + 1) : parseInfo.stream.slice(position + 1, position + 1 + count);
379
+ let char = count == 1 ? parseInfo.stream.charAt(position) : parseInfo.stream.slice(position, position + count);
359
380
  let i = 0;
360
381
  let codepoint;
361
382
  for (; i < char.length; i++) {
@@ -377,195 +398,292 @@ function next(parseInfo, count = 1) {
377
398
  parseInfo.currentPosition += char.length;
378
399
  return char;
379
400
  }
401
+ function isIdentToken(parseInfo, start, end) {
402
+ let j = parseInfo.currentPosition - parseInfo.offset;
403
+ let i = parseInfo.position - parseInfo.offset;
404
+ if (start != null) {
405
+ if (end == null) {
406
+ if (start < 0) {
407
+ j += start;
408
+ }
409
+ else {
410
+ i += start;
411
+ }
412
+ }
413
+ else {
414
+ if (end < 0) {
415
+ j += end;
416
+ }
417
+ else {
418
+ j = parseInfo.position + end;
419
+ }
420
+ }
421
+ }
422
+ j--;
423
+ let codepoint = parseInfo.stream.charCodeAt(i);
424
+ // -
425
+ if (codepoint == 0x2d) {
426
+ let nextCodepoint;
427
+ if ((nextCodepoint = parseInfo.stream.charCodeAt(i + 1)) != nextCodepoint) {
428
+ return false;
429
+ }
430
+ if (isDigit(nextCodepoint)) {
431
+ return false;
432
+ }
433
+ codepoint = nextCodepoint;
434
+ i++;
435
+ }
436
+ if (codepoint !== 0x2d && !isIdentStart(codepoint)) {
437
+ return false;
438
+ }
439
+ if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
440
+ codepoint = parseInfo.stream.charCodeAt(i + 1);
441
+ // if (!isIdentCodepoint(codepoint)) {
442
+ // return false;
443
+ // }
444
+ i += String.fromCodePoint(codepoint).length;
445
+ // if (i < j) {
446
+ // codepoint = name.charCodeAt(i) as number;
447
+ // if (!isIdentCodepoint(codepoint)) {
448
+ // return false;
449
+ // }
450
+ // }
451
+ }
452
+ while (i < j) {
453
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
454
+ codepoint = parseInfo.stream.charCodeAt(i);
455
+ if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
456
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
457
+ codepoint = parseInfo.stream.charCodeAt(i);
458
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
459
+ continue;
460
+ }
461
+ if (codepoint !== 0x2d && !isIdentCodepoint(codepoint)) {
462
+ return false;
463
+ }
464
+ }
465
+ return true;
466
+ }
467
+ function isPseudo(parseInfo) {
468
+ let position = parseInfo.currentPosition - parseInfo.offset;
469
+ let endPosition = parseInfo.currentPosition - parseInfo.offset;
470
+ return (parseInfo.stream.charAt(position) == ":" &&
471
+ parseInfo.stream.charAt(endPosition - 1) == "(" &&
472
+ (parseInfo.stream.charAt(position + 1) == ":"
473
+ ? isIdentToken(parseInfo, 2, -1)
474
+ : isIdentToken(parseInfo, 1, -1))) ||
475
+ parseInfo.stream.charAt(position + 1) == ":"
476
+ ? isIdentToken(parseInfo, 2)
477
+ : isIdentToken(parseInfo, 1);
478
+ }
479
+ function startsWith(parseInfo, input) {
480
+ let i = 0;
481
+ let j = input.length;
482
+ while (i < j) {
483
+ if (parseInfo.stream.charAt(parseInfo.position - parseInfo.offset + i) != input.charAt(i)) {
484
+ return false;
485
+ }
486
+ i++;
487
+ }
488
+ return true;
489
+ }
490
+ function isURLToken(parseInfo) {
491
+ let i = parseInfo.position - parseInfo.offset;
492
+ let c;
493
+ while (++i < parseInfo.currentPosition) {
494
+ c = parseInfo.stream.charCodeAt(i);
495
+ // single quote or double quote or start parenthesis or close parenthesis
496
+ if (isNonPrintable(c) || c == 0x27 || c == 0x22 || c == 0x28 || c == 0x29) {
497
+ return false;
498
+ }
499
+ // valid escape
500
+ if (c == 92 /* TokenMap.REVERSE_SOLIDUS */) {
501
+ i++;
502
+ if (i >= parseInfo.currentPosition) {
503
+ return false;
504
+ }
505
+ c = parseInfo.stream.charCodeAt(i);
506
+ // c is not '\n' or '\r' or '\f'
507
+ if (c == 0x6e || c == 0x72 || c == 0x66) {
508
+ return false;
509
+ }
510
+ continue;
511
+ }
512
+ // is white space
513
+ if (c == 0x20 || c == 0x09) {
514
+ break;
515
+ }
516
+ }
517
+ return i == parseInfo.currentPosition;
518
+ }
380
519
  /**
381
- * Tokenize css string
520
+ * Tokenize CSS string
382
521
  * @param parseInfo
383
522
  * @param yieldEOFToken
384
523
  */
385
524
  function tokenize(parseInfo, yieldEOFToken = true) {
386
525
  if (typeof parseInfo == "string") {
387
526
  parseInfo = {
388
- buffer: "",
389
527
  stream: parseInfo,
390
528
  source: new SourceFile(parseInfo, [], ""),
391
529
  offset: 0,
392
530
  time: 0,
393
531
  position: 0,
394
- currentPosition: -1,
532
+ currentPosition: 0,
395
533
  };
396
534
  }
397
- let value;
398
- let buffer = parseInfo.buffer;
399
535
  let charCode;
400
536
  let nextCharCode;
401
537
  const startTime = performance.now();
402
538
  const result = [];
403
539
  // allow 10 characters buffer for the streaming parser to avoid incomplete tokens
404
- const endPosition = parseInfo.stream.length - 10;
405
- parseInfo.buffer = "";
406
- while ((value = peek(parseInfo))) {
407
- charCode = value.charCodeAt(0);
408
- // nextCharCode = nextValue.charCodeAt(0);
409
- // console.debug({value, buffer});
540
+ const endPosition = parseInfo.stream.length - 1;
541
+ // NaN is not equal to NaN
542
+ while ((charCode = peek(parseInfo).charCodeAt(0)) == charCode) {
410
543
  switch (charCode) {
411
544
  case 61 /* TokenMap.EQUALS */:
412
- if (buffer.length > 0) {
413
- result.push(yieldResult(buffer, parseInfo));
414
- buffer = "";
545
+ if (parseInfo.position < parseInfo.currentPosition) {
546
+ result.push(yieldResult(parseInfo));
415
547
  }
416
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.DelimTokenType));
548
+ next(parseInfo);
549
+ result.push(yieldResult(parseInfo, EnumToken.DelimTokenType));
417
550
  break;
418
551
  // '+' or '-'
419
552
  case 43 /* TokenMap.PLUS */:
420
553
  case 45 /* TokenMap.MINUS */:
421
- next(parseInfo);
422
- if (charCode === 43 /* TokenMap.PLUS */ && !isNumber(peek(parseInfo))) {
423
- if (buffer.length > 0) {
424
- result.push(yieldResult(buffer, parseInfo));
425
- buffer = "";
554
+ nextCharCode = peek(parseInfo).charCodeAt(0);
555
+ // not a number
556
+ if (charCode === 43 /* TokenMap.PLUS */ && !(nextCharCode >= 0x30 && nextCharCode <= 0x39)) {
557
+ if (parseInfo.position < parseInfo.currentPosition) {
558
+ result.push(yieldResult(parseInfo));
426
559
  }
427
- result.push(yieldResult(value, parseInfo, SymbolsMapTokens[value]));
560
+ next(parseInfo);
561
+ result.push(yieldResult(parseInfo, SymbolsMapTokens[parseInfo.stream
562
+ .slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset)
563
+ .toLowerCase()]));
428
564
  break;
429
565
  }
430
- buffer += value;
566
+ next(parseInfo);
431
567
  break;
432
568
  // '{'
433
569
  case 123 /* TokenMap.LEFT_BRACE */:
434
- if (buffer.length > 0) {
435
- result.push(yieldResult(buffer, parseInfo));
436
- buffer = "";
570
+ if (parseInfo.position < parseInfo.currentPosition) {
571
+ result.push(yieldResult(parseInfo));
437
572
  }
438
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.BlockStartTokenType));
573
+ next(parseInfo);
574
+ result.push(yieldResult(parseInfo, EnumToken.BlockStartTokenType));
439
575
  break;
440
576
  // '}'
441
577
  case 125 /* TokenMap.RIGHT_BRACE */:
442
- if (buffer.length > 0) {
443
- result.push(yieldResult(buffer, parseInfo));
444
- buffer = "";
578
+ if (parseInfo.position < parseInfo.currentPosition) {
579
+ result.push(yieldResult(parseInfo));
445
580
  }
446
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.BlockEndTokenType));
581
+ next(parseInfo);
582
+ result.push(yieldResult(parseInfo, EnumToken.BlockEndTokenType));
447
583
  break;
448
584
  // '('
449
585
  case 40 /* TokenMap.LEFT_PARENTHESIS */:
450
- if (buffer.length > 0) {
451
- if (buffer[0] === ":" && isPseudo(buffer)) {
586
+ if (parseInfo.position < parseInfo.currentPosition) {
587
+ if (parseInfo.stream[parseInfo.position - parseInfo.offset] === ":" && isPseudo(parseInfo)) {
452
588
  next(parseInfo);
453
- result.push(yieldResult(buffer, parseInfo, EnumToken.PseudoClassFunctionTokenDefType));
454
- buffer = "";
589
+ result.push(yieldResult(parseInfo, EnumToken.PseudoClassFunctionTokenDefType));
455
590
  break;
456
591
  }
457
- else if (isIdent(buffer)) {
458
- const hint = buffer.startsWith("--")
592
+ else if (isIdentToken(parseInfo)) {
593
+ const hint = startsWith(parseInfo, "--")
459
594
  ? EnumToken.CustomFunctionTokenDefType
460
- : (SymbolsMapTokens[buffer.toLowerCase() + "("] ?? EnumToken.FunctionTokenDefType);
461
- result.push(yieldResult(buffer, parseInfo, hint));
595
+ : (SymbolsMapTokens[parseInfo.stream
596
+ .slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset)
597
+ .toLowerCase() + "("] ?? EnumToken.FunctionTokenDefType);
598
+ result.push(yieldResult(parseInfo, hint));
462
599
  next(parseInfo);
463
- buffer = "";
600
+ // consume '('
601
+ parseInfo.position = parseInfo.currentPosition;
464
602
  if (hint === EnumToken.UrlFunctionTokenDefType) {
465
- buffer = "";
466
- value = peek(parseInfo);
467
603
  // consume an <url>
468
- while (isWhiteSpace((charCode = value.charCodeAt(0)))) {
469
- buffer += next(parseInfo);
470
- value = peek(parseInfo);
471
- charCode = value.charCodeAt(0);
472
- if (value === "/" && match(parseInfo, "/*")) {
473
- if (buffer.length > 0) {
474
- result.push(yieldResult(buffer, parseInfo));
475
- buffer = "";
476
- }
477
- buffer += next(parseInfo, 2);
478
- while ((value = next(parseInfo))) {
479
- if (value == "*") {
480
- buffer += value;
481
- if (match(parseInfo, "/")) {
482
- result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.CommentTokenType));
483
- buffer = "";
484
- break;
485
- }
486
- }
487
- else {
488
- buffer += value;
489
- }
490
- }
491
- if (buffer.length > 0) {
492
- result.push(yieldResult(buffer, parseInfo, EnumToken.BadCommentTokenType));
493
- buffer = "";
494
- }
495
- value = peek(parseInfo);
496
- charCode = value.charCodeAt(0);
497
- }
604
+ while (isWhiteSpace(peek(parseInfo).charCodeAt(0))) {
605
+ next(parseInfo);
498
606
  }
499
- if (buffer.length > 0) {
500
- result.push(yieldResult(buffer, parseInfo, EnumToken.WhitespaceTokenType));
501
- buffer = "";
607
+ charCode = peek(parseInfo).charCodeAt(0);
608
+ let values = null;
609
+ if (charCode == 34 /* TokenMap.DOUBLE_QUOTE */ || charCode == 39 /* TokenMap.SINGLE_QUOTE */) {
610
+ values = consumeString(parseInfo);
502
611
  }
503
- if (value === ")" || value === '"' || value === "'") {
504
- break;
612
+ else {
613
+ do {
614
+ next(parseInfo);
615
+ // value = peek(parseInfo);
616
+ charCode = peek(parseInfo).charCodeAt(0);
617
+ } while (
618
+ // !(value === "/" && match(parseInfo, "/*") &&
619
+ charCode !== 41 /* TokenMap.RIGHT_PARENTHESIS */ &&
620
+ parseInfo.currentPosition < endPosition);
505
621
  }
506
- do {
507
- buffer += next(parseInfo);
508
- value = peek(parseInfo);
509
- charCode = value.charCodeAt(0);
510
- } while (value !== ")" &&
511
- !isWhiteSpace(charCode) &&
512
- !(value === "/" && match(parseInfo, "/*")));
513
- if (buffer.length > 0) {
514
- result.push(yieldResult(buffer, parseInfo, peek(parseInfo) === ""
622
+ if (values != null) {
623
+ // NaN is not equal to NaN
624
+ if ((charCode = peek(parseInfo).charCodeAt(0)) != charCode) {
625
+ for (let i = 0; i < values.length; i++) {
626
+ values[i].token.typ = EnumToken.BadUrlTokenType;
627
+ }
628
+ }
629
+ result.push(...values);
630
+ }
631
+ else if (parseInfo.position < parseInfo.currentPosition) {
632
+ result.push(yieldResult(parseInfo,
633
+ // parseInfo.position < parseInfo.currentPosition
634
+ (charCode = peek(parseInfo).charCodeAt(0)) != charCode || !isURLToken(parseInfo)
515
635
  ? EnumToken.BadUrlTokenType
516
636
  : EnumToken.UrlTokenTokenType));
517
- buffer = "";
518
637
  }
519
638
  }
520
- // console.debug({value: peek(parseInfo)});
521
639
  break;
522
640
  }
523
641
  }
524
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.StartParensTokenType));
525
- buffer = "";
642
+ next(parseInfo);
643
+ result.push(yieldResult(parseInfo, EnumToken.StartParensTokenType));
526
644
  break;
527
645
  // ')'
528
646
  case 41 /* TokenMap.RIGHT_PARENTHESIS */:
529
- if (buffer.length > 0) {
530
- result.push(yieldResult(buffer, parseInfo));
531
- buffer = "";
647
+ if (parseInfo.position < parseInfo.currentPosition) {
648
+ result.push(yieldResult(parseInfo));
532
649
  }
533
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.EndParensTokenType));
650
+ next(parseInfo);
651
+ result.push(yieldResult(parseInfo, EnumToken.EndParensTokenType));
534
652
  break;
535
653
  // '['
536
654
  case 91 /* TokenMap.LEFT_BRACKETS */:
537
- if (buffer.length > 0) {
538
- result.push(yieldResult(buffer, parseInfo));
539
- buffer = "";
655
+ if (parseInfo.position < parseInfo.currentPosition) {
656
+ result.push(yieldResult(parseInfo));
540
657
  }
541
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.AttrStartTokenType));
658
+ next(parseInfo);
659
+ result.push(yieldResult(parseInfo, EnumToken.AttrStartTokenType));
542
660
  break;
543
661
  // ']'
544
662
  case 93 /* TokenMap.RIGHT_BRACKETS */:
545
- if (buffer.length > 0) {
546
- result.push(yieldResult(buffer, parseInfo));
547
- buffer = "";
663
+ if (parseInfo.position < parseInfo.currentPosition) {
664
+ result.push(yieldResult(parseInfo));
548
665
  }
549
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.AttrEndTokenType));
666
+ next(parseInfo);
667
+ result.push(yieldResult(parseInfo, EnumToken.AttrEndTokenType));
550
668
  break;
551
669
  case 59 /* TokenMap.SEMICOLON */:
552
- if (buffer.length > 0) {
553
- result.push(yieldResult(buffer, parseInfo));
554
- buffer = "";
670
+ if (parseInfo.position < parseInfo.currentPosition) {
671
+ result.push(yieldResult(parseInfo));
555
672
  }
556
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.SemiColonTokenType));
673
+ next(parseInfo);
674
+ result.push(yieldResult(parseInfo, EnumToken.SemiColonTokenType));
557
675
  break;
558
676
  case 58 /* TokenMap.COLON */:
559
- if (buffer.length > 0) {
560
- result.push(yieldResult(buffer, parseInfo));
561
- buffer = "";
677
+ if (parseInfo.position < parseInfo.currentPosition) {
678
+ result.push(yieldResult(parseInfo));
562
679
  }
563
680
  next(parseInfo);
564
681
  if (peek(parseInfo).charCodeAt(0) == 58 /* TokenMap.COLON */) {
565
- result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.DoubleColonTokenType));
682
+ next(parseInfo);
683
+ result.push(yieldResult(parseInfo, EnumToken.DoubleColonTokenType));
566
684
  break;
567
685
  }
568
- result.push(yieldResult(value, parseInfo, EnumToken.ColonTokenType));
686
+ result.push(yieldResult(parseInfo, EnumToken.ColonTokenType));
569
687
  break;
570
688
  // \n \r \f \v \t space
571
689
  case 0x9:
@@ -576,241 +694,229 @@ function tokenize(parseInfo, yieldEOFToken = true) {
576
694
  case 0xd:
577
695
  case 0x2028:
578
696
  case 0x2029:
579
- if (buffer.length > 0) {
580
- result.push(yieldResult(buffer, parseInfo));
581
- buffer = "";
697
+ if (parseInfo.position < parseInfo.currentPosition) {
698
+ result.push(yieldResult(parseInfo));
582
699
  }
583
- buffer += next(parseInfo);
584
- nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1).charCodeAt(0);
700
+ next(parseInfo);
701
+ nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset).charCodeAt(0);
585
702
  while (nextCharCode == 0x20 ||
586
703
  (nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
587
704
  nextCharCode == 0x2028 ||
588
705
  nextCharCode == 0x2029) {
589
- value += next(parseInfo);
590
- nextCharCode = parseInfo.stream
591
- .charAt(parseInfo.currentPosition - parseInfo.offset + 1)
592
- .charCodeAt(0);
706
+ next(parseInfo);
707
+ nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset).charCodeAt(0);
593
708
  }
594
- result.push(yieldResult(value, parseInfo, EnumToken.WhitespaceTokenType));
595
- buffer = "";
709
+ result.push(yieldResult(parseInfo, EnumToken.WhitespaceTokenType));
596
710
  break;
597
711
  case 44 /* TokenMap.COMMA */:
598
- if (buffer.length > 0) {
599
- result.push(yieldResult(buffer, parseInfo));
600
- buffer = "";
712
+ if (parseInfo.position < parseInfo.currentPosition) {
713
+ result.push(yieldResult(parseInfo));
601
714
  }
602
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.CommaTokenType));
715
+ next(parseInfo);
716
+ result.push(yieldResult(parseInfo, EnumToken.CommaTokenType));
603
717
  break;
604
718
  case 36 /* TokenMap.DOLLAR */:
605
- if (buffer.length > 0) {
606
- result.push(yieldResult(buffer, parseInfo));
607
- buffer = "";
719
+ if (parseInfo.position < parseInfo.currentPosition) {
720
+ result.push(yieldResult(parseInfo));
608
721
  }
609
722
  if (match(parseInfo, "$=")) {
610
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.EndMatchTokenType));
723
+ next(parseInfo, 2);
724
+ result.push(yieldResult(parseInfo, EnumToken.EndMatchTokenType));
611
725
  break;
612
726
  }
613
- buffer += next(parseInfo);
727
+ next(parseInfo);
614
728
  break;
615
729
  case 126 /* TokenMap.TILDA */:
616
- if (buffer.length > 0) {
617
- result.push(yieldResult(buffer, parseInfo));
618
- buffer = "";
730
+ if (parseInfo.position < parseInfo.currentPosition) {
731
+ result.push(yieldResult(parseInfo));
619
732
  }
620
733
  if (match(parseInfo, "~=")) {
621
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.IncludeMatchTokenType));
734
+ next(parseInfo, 2);
735
+ result.push(yieldResult(parseInfo, EnumToken.IncludeMatchTokenType));
622
736
  break;
623
737
  }
624
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Tilda));
625
- buffer = "";
738
+ next(parseInfo);
739
+ result.push(yieldResult(parseInfo, EnumToken.Tilda));
626
740
  break;
627
741
  // case '^':
628
742
  case 94 /* TokenMap.CARET */:
629
- if (buffer.length > 0) {
630
- result.push(yieldResult(buffer, parseInfo));
631
- buffer = "";
743
+ if (parseInfo.position < parseInfo.currentPosition) {
744
+ result.push(yieldResult(parseInfo));
632
745
  }
633
746
  if (match(parseInfo, "^=")) {
634
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.StartMatchTokenType));
747
+ next(parseInfo, 2);
748
+ result.push(yieldResult(parseInfo, EnumToken.StartMatchTokenType));
635
749
  break;
636
750
  }
637
- buffer += next(parseInfo);
751
+ next(parseInfo);
638
752
  break;
639
753
  case 42 /* TokenMap.STAR */:
640
- if (buffer.length > 0) {
641
- result.push(yieldResult(buffer, parseInfo));
642
- buffer = "";
754
+ if (parseInfo.position < parseInfo.currentPosition) {
755
+ result.push(yieldResult(parseInfo));
643
756
  }
644
757
  if (match(parseInfo, "*=")) {
645
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.ContainMatchTokenType));
758
+ next(parseInfo, 2);
759
+ result.push(yieldResult(parseInfo, EnumToken.ContainMatchTokenType));
646
760
  break;
647
761
  }
648
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Star));
649
- buffer = "";
762
+ next(parseInfo);
763
+ result.push(yieldResult(parseInfo, EnumToken.Star));
650
764
  break;
651
765
  case 38 /* TokenMap.AMPERSAND */:
652
- if (buffer.length > 0) {
653
- result.push(yieldResult(buffer, parseInfo));
654
- buffer = "";
766
+ if (parseInfo.position < parseInfo.currentPosition) {
767
+ result.push(yieldResult(parseInfo));
655
768
  }
656
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.NestingSelectorTokenType));
657
- buffer = "";
769
+ next(parseInfo);
770
+ result.push(yieldResult(parseInfo, EnumToken.NestingSelectorTokenType));
658
771
  break;
659
772
  case 124 /* TokenMap.PIPE */:
660
- if (buffer.length > 0) {
661
- result.push(yieldResult(buffer, parseInfo));
662
- buffer = "";
773
+ if (parseInfo.position < parseInfo.currentPosition) {
774
+ result.push(yieldResult(parseInfo));
663
775
  }
664
776
  // '||'
665
777
  if (match(parseInfo, "||")) {
666
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.ColumnCombinatorTokenType));
778
+ next(parseInfo, 2);
779
+ result.push(yieldResult(parseInfo, EnumToken.ColumnCombinatorTokenType));
667
780
  break;
668
781
  }
669
782
  else if (match(parseInfo, "|=")) {
670
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.DashMatchTokenType));
783
+ next(parseInfo, 2);
784
+ result.push(yieldResult(parseInfo, EnumToken.DashMatchTokenType));
671
785
  break;
672
786
  }
673
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Pipe));
674
- buffer = "";
787
+ next(parseInfo);
788
+ result.push(yieldResult(parseInfo, EnumToken.Pipe));
675
789
  break;
676
790
  case 33 /* TokenMap.EXCLAMATION */:
677
- if (buffer.length > 0) {
678
- result.push(yieldResult(buffer, parseInfo));
679
- buffer = "";
791
+ if (parseInfo.position < parseInfo.currentPosition) {
792
+ result.push(yieldResult(parseInfo));
680
793
  }
681
794
  if (match(parseInfo, "!important")) {
682
- result.push(yieldResult(next(parseInfo, 10), parseInfo, EnumToken.ImportantTokenType));
683
- buffer = "";
795
+ next(parseInfo, 10);
796
+ result.push(yieldResult(parseInfo, EnumToken.ImportantTokenType));
684
797
  break;
685
798
  }
686
- buffer += next(parseInfo);
799
+ next(parseInfo);
687
800
  break;
688
801
  case 47 /* TokenMap.SLASH */:
689
- if (buffer.length > 0) {
690
- result.push(yieldResult(buffer, parseInfo));
691
- buffer = "";
802
+ if (parseInfo.position < parseInfo.currentPosition) {
803
+ result.push(yieldResult(parseInfo));
692
804
  }
693
805
  if (!match(parseInfo, "/*")) {
694
- result.push(yieldResult(next(parseInfo), parseInfo, SymbolsMapTokens[value]));
806
+ next(parseInfo);
807
+ result.push(yieldResult(parseInfo, SymbolsMapTokens[parseInfo.stream.slice(parseInfo.position, parseInfo.currentPosition)]));
695
808
  break;
696
809
  }
697
- buffer += next(parseInfo, 2);
698
- while ((value = next(parseInfo))) {
699
- if (value == "*") {
700
- buffer += value;
810
+ next(parseInfo, 2);
811
+ while ((charCode = next(parseInfo).charCodeAt(0)) == charCode) {
812
+ if (charCode == 42 /* TokenMap.STAR */) {
701
813
  if (match(parseInfo, "/")) {
702
- result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.CommentTokenType));
703
- buffer = "";
814
+ next(parseInfo);
815
+ result.push(yieldResult(parseInfo, EnumToken.CommentTokenType));
704
816
  break;
705
817
  }
706
818
  }
707
- else {
708
- buffer += value;
709
- }
819
+ // else {
820
+ // buffer += value;
821
+ // }
710
822
  }
711
- if (buffer.length > 0) {
712
- result.push(yieldResult(buffer, parseInfo, EnumToken.BadCommentTokenType));
713
- buffer = "";
823
+ if (parseInfo.position < parseInfo.currentPosition) {
824
+ result.push(yieldResult(parseInfo, EnumToken.BadCommentTokenType));
714
825
  }
715
826
  break;
716
827
  case 62 /* TokenMap.GREATERTHAN */:
717
- if (buffer.length > 0) {
718
- result.push(yieldResult(buffer, parseInfo));
719
- buffer = "";
828
+ if (parseInfo.position < parseInfo.currentPosition) {
829
+ result.push(yieldResult(parseInfo));
720
830
  }
721
831
  if (match(parseInfo, ">=")) {
722
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.GteTokenType));
832
+ next(parseInfo, 2);
833
+ result.push(yieldResult(parseInfo, EnumToken.GteTokenType));
723
834
  break;
724
835
  }
725
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.GtTokenType));
726
- buffer = "";
836
+ next(parseInfo);
837
+ result.push(yieldResult(parseInfo, EnumToken.GtTokenType));
727
838
  break;
728
839
  case 60 /* TokenMap.LOWERTHAN */:
729
- if (buffer.length > 0) {
730
- result.push(yieldResult(buffer, parseInfo));
731
- buffer = "";
840
+ if (parseInfo.position < parseInfo.currentPosition) {
841
+ result.push(yieldResult(parseInfo));
732
842
  }
733
843
  if (match(parseInfo, "<=")) {
734
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.LteTokenType));
844
+ next(parseInfo, 2);
845
+ result.push(yieldResult(parseInfo, EnumToken.LteTokenType));
735
846
  break;
736
847
  }
737
- buffer += next(parseInfo);
848
+ next(parseInfo);
738
849
  if (match(parseInfo, "!--")) {
739
- buffer += next(parseInfo, 3);
740
- while ((value = next(parseInfo))) {
741
- buffer += value;
742
- if (value == "-" && match(parseInfo, "->")) {
850
+ next(parseInfo, 3);
851
+ while ((charCode = next(parseInfo).charCodeAt(0)) == charCode) {
852
+ if (charCode == 45 /* TokenMap.MINUS */ && match(parseInfo, "->")) {
743
853
  break;
744
854
  }
745
855
  }
746
- if (value === "") {
747
- result.push(yieldResult(buffer, parseInfo, EnumToken.BadCdoTokenType));
856
+ if (parseInfo.currentPosition >= endPosition) {
857
+ result.push(yieldResult(parseInfo, EnumToken.BadCdoTokenType));
748
858
  }
749
859
  else {
750
- result.push(yieldResult(buffer + next(parseInfo, 2), parseInfo, EnumToken.CDOCOMMTokenType));
860
+ next(parseInfo, 2);
861
+ result.push(yieldResult(parseInfo, EnumToken.CDOCOMMTokenType));
751
862
  }
752
- buffer = "";
753
863
  }
754
864
  break;
755
865
  case 35 /* TokenMap.HASH */:
756
- if (buffer.length > 0) {
757
- result.push(yieldResult(buffer, parseInfo));
758
- buffer = "";
866
+ if (parseInfo.position < parseInfo.currentPosition) {
867
+ result.push(yieldResult(parseInfo));
759
868
  }
760
- buffer += next(parseInfo);
869
+ next(parseInfo);
761
870
  break;
762
871
  case 92 /* TokenMap.REVERSE_SOLIDUS */:
872
+ if (!yieldEOFToken && parseInfo.stream.length == parseInfo.currentPosition - parseInfo.offset + 1) {
873
+ break;
874
+ }
763
875
  next(parseInfo);
764
876
  // EOF
765
- if (!(peek(parseInfo))) {
877
+ if (!peek(parseInfo)) {
878
+ if (!yieldEOFToken) {
879
+ break;
880
+ }
766
881
  // end of stream ignore \\
767
- if (buffer.length > 0) {
768
- result.push(yieldResult(buffer, parseInfo));
769
- buffer = "";
882
+ if (parseInfo.position < parseInfo.currentPosition) {
883
+ result.push(yieldResult(parseInfo));
770
884
  }
771
885
  break;
772
886
  }
773
- buffer += value + next(parseInfo);
774
- // buffer +=
775
- // (parseInfo.offset == parseInfo.currentPosition
776
- // ? parseInfo.buffer.slice(-1)
777
- // : parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset - 1)) + value;
887
+ next(parseInfo);
778
888
  break;
779
889
  case 39 /* TokenMap.SINGLE_QUOTE */:
780
890
  case 34 /* TokenMap.DOUBLE_QUOTE */:
781
- if (buffer.length > 0) {
782
- result.push(yieldResult(buffer, parseInfo));
783
- buffer = "";
891
+ if (parseInfo.position < parseInfo.currentPosition) {
892
+ result.push(yieldResult(parseInfo));
784
893
  }
785
894
  result.push(...consumeString(parseInfo));
786
895
  break;
787
896
  case 46 /* TokenMap.DOT */:
788
897
  const codepoint = parseInfo.stream
789
- .charAt(parseInfo.currentPosition - parseInfo.offset + 2)
898
+ .charAt(parseInfo.currentPosition - parseInfo.offset + 1)
790
899
  .charCodeAt(0);
791
- if (!isDigit(codepoint) && buffer !== "") {
792
- result.push(yieldResult(buffer, parseInfo));
793
- buffer = next(parseInfo, 2);
900
+ if (!isDigit(codepoint) && parseInfo.position !== parseInfo.currentPosition) {
901
+ result.push(yieldResult(parseInfo));
902
+ next(parseInfo, 2);
794
903
  break;
795
904
  }
796
- buffer += next(parseInfo);
905
+ next(parseInfo);
797
906
  break;
798
907
  default:
799
- buffer += next(parseInfo);
908
+ next(parseInfo);
800
909
  break;
801
910
  }
802
- if (!yieldEOFToken && endPosition <= parseInfo.stream.length - parseInfo.currentPosition + parseInfo.offset) {
911
+ if (!yieldEOFToken && endPosition <= parseInfo.currentPosition - parseInfo.offset + 1) {
803
912
  break;
804
913
  }
805
914
  }
806
915
  if (yieldEOFToken) {
807
- if (buffer.length > 0) {
808
- result.push(yieldResult(buffer, parseInfo));
916
+ if (parseInfo.position < parseInfo.currentPosition) {
917
+ result.push(yieldResult(parseInfo));
809
918
  }
810
- result.push(yieldResult("", parseInfo, EnumToken.EOFTokenType));
811
- }
812
- else {
813
- parseInfo.buffer = buffer;
919
+ result.push(yieldResult(parseInfo, EnumToken.EOFTokenType));
814
920
  }
815
921
  parseInfo.time += performance.now() - startTime;
816
922
  return result;
@@ -823,19 +929,17 @@ function tokenize(parseInfo, yieldEOFToken = true) {
823
929
  async function* tokenizeStream(input, parseInfo) {
824
930
  const decoder = new TextDecoder("utf-8");
825
931
  const reader = input.getReader();
932
+ parseInfo.stream = "";
826
933
  while (true) {
827
934
  const { done, value } = await reader.read();
828
935
  const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
829
936
  if (!done) {
830
937
  parseInfo.source.append(stream);
831
- if (typeof parseInfo.stream != "string") {
832
- parseInfo.stream = stream;
833
- }
834
- else {
835
- parseInfo.stream = (parseInfo.stream.slice(parseInfo.currentPosition - parseInfo.offset + 1) +
836
- stream);
837
- }
838
- parseInfo.offset = parseInfo.currentPosition + 1;
938
+ parseInfo.stream = (parseInfo.stream.slice(parseInfo.position - parseInfo.offset) + stream);
939
+ parseInfo.offset = parseInfo.offset = parseInfo.position;
940
+ }
941
+ else {
942
+ parseInfo.stream = "";
839
943
  }
840
944
  yield* tokenize(parseInfo, done);
841
945
  if (done) {