@tbela99/css-parser 2.3.1 → 2.3.2

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 (115) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +56 -26
  3. package/architecture.png +0 -0
  4. package/dist/ast/arena-node.js +32 -3
  5. package/dist/ast/arena.js +88 -10
  6. package/dist/ast/cache.js +1 -1
  7. package/dist/ast/features/calc.js +1 -1
  8. package/dist/ast/features/if.js +1 -1
  9. package/dist/ast/features/index.js +1 -1
  10. package/dist/ast/features/inlinecssvariables.js +1 -1
  11. package/dist/ast/features/mixin.js +1 -1
  12. package/dist/ast/features/prefix.js +1 -1
  13. package/dist/ast/features/shorthand.js +1 -1
  14. package/dist/ast/features/transform.js +1 -1
  15. package/dist/ast/features/type.js +1 -1
  16. package/dist/ast/math/expression.js +1 -1
  17. package/dist/ast/math/math.js +1 -1
  18. package/dist/ast/minify.js +131 -36
  19. package/dist/ast/mixins/custom-function.js +1 -1
  20. package/dist/ast/mixins/utils.js +1 -1
  21. package/dist/ast/mixins/variables.js +1 -1
  22. package/dist/ast/strings.js +26 -1
  23. package/dist/ast/transform/compute.js +6 -2
  24. package/dist/ast/transform/matrix.js +1 -1
  25. package/dist/ast/transform/minify.js +1 -1
  26. package/dist/ast/transform/perspective.js +1 -1
  27. package/dist/ast/transform/rotate.js +1 -1
  28. package/dist/ast/transform/scale.js +1 -1
  29. package/dist/ast/transform/skew.js +1 -1
  30. package/dist/ast/transform/translate.js +1 -1
  31. package/dist/ast/transform/utils.js +1 -1
  32. package/dist/ast/types.js +22 -2
  33. package/dist/ast/walk.js +1 -1
  34. package/dist/data/properties.json.js +1 -1
  35. package/dist/data/validation.json.js +1 -1
  36. package/dist/fs/resolve.js +1 -1
  37. package/dist/index-umd-web.js +1084 -293
  38. package/dist/index.d.ts +597 -686
  39. package/dist/node.js +14 -8
  40. package/dist/parser/declaration/list.js +8 -5
  41. package/dist/parser/declaration/map.js +1 -1
  42. package/dist/parser/declaration/set.js +1 -1
  43. package/dist/parser/input.js +62 -4
  44. package/dist/parser/linesmap.js +1 -1
  45. package/dist/parser/parse.js +121 -13
  46. package/dist/parser/source.js +1 -1
  47. package/dist/parser/tokenize.js +161 -19
  48. package/dist/parser/utils/angle.js +1 -1
  49. package/dist/parser/utils/at-rule-container.js +1 -1
  50. package/dist/parser/utils/at-rule-font-feature-values.js +1 -1
  51. package/dist/parser/utils/at-rule-generic.js +1 -1
  52. package/dist/parser/utils/at-rule-import.js +1 -1
  53. package/dist/parser/utils/at-rule-media.js +42 -2
  54. package/dist/parser/utils/at-rule-support.js +1 -1
  55. package/dist/parser/utils/at-rule-when-else.js +1 -1
  56. package/dist/parser/utils/at-rule.js +1 -1
  57. package/dist/parser/utils/base64.js +1 -1
  58. package/dist/parser/utils/cache.js +1 -1
  59. package/dist/parser/utils/canonical.js +1 -1
  60. package/dist/parser/utils/codes.js +1 -1
  61. package/dist/parser/utils/config.js +1 -1
  62. package/dist/parser/utils/declaration.js +1 -1
  63. package/dist/parser/utils/eq.js +1 -1
  64. package/dist/parser/utils/hash.js +1 -1
  65. package/dist/parser/utils/selector.js +98 -6
  66. package/dist/parser/utils/text.js +1 -1
  67. package/dist/parser/utils/token.js +1 -1
  68. package/dist/parser/utils/type.js +1 -1
  69. package/dist/printer/lib/codec.js +1 -1
  70. package/dist/printer/render.js +5 -7
  71. package/dist/printer/result.js +3 -1
  72. package/dist/printer/sourcemap.js +1 -1
  73. package/dist/printer/writer.js +1 -1
  74. package/dist/syntax/color/a98rgb.js +1 -1
  75. package/dist/syntax/color/alpha.js +1 -1
  76. package/dist/syntax/color/cmyk.js +1 -1
  77. package/dist/syntax/color/color-mix.js +1 -1
  78. package/dist/syntax/color/color.js +1 -1
  79. package/dist/syntax/color/hex.js +1 -1
  80. package/dist/syntax/color/hsl.js +12 -4
  81. package/dist/syntax/color/hwb.js +9 -3
  82. package/dist/syntax/color/lab.js +1 -1
  83. package/dist/syntax/color/lch.js +1 -1
  84. package/dist/syntax/color/oklab.js +1 -1
  85. package/dist/syntax/color/oklch.js +2 -2
  86. package/dist/syntax/color/p3.js +1 -1
  87. package/dist/syntax/color/prophotorgb.js +1 -1
  88. package/dist/syntax/color/rec2020.js +1 -1
  89. package/dist/syntax/color/relative-color.js +1 -1
  90. package/dist/syntax/color/rgb.js +1 -1
  91. package/dist/syntax/color/srgb.js +3 -2
  92. package/dist/syntax/color/utils/color-type.js +1 -1
  93. package/dist/syntax/color/utils/colorspace.js +1 -1
  94. package/dist/syntax/color/utils/components.js +1 -1
  95. package/dist/syntax/color/utils/distance.js +1 -1
  96. package/dist/syntax/color/utils/make-color.js +1 -1
  97. package/dist/syntax/color/utils/matrix.js +1 -1
  98. package/dist/syntax/color/xyz.js +1 -1
  99. package/dist/syntax/color/xyzd50.js +1 -1
  100. package/dist/syntax/constants.js +2 -2
  101. package/dist/syntax/syntax.js +1 -1
  102. package/dist/types.js +1 -1
  103. package/dist/utils/sync.js +1 -1
  104. package/dist/validation/compiled/builtins.js +1 -1
  105. package/dist/validation/compiled/runtime.js +1 -1
  106. package/dist/validation/compiled/syntaxes.js +1 -1
  107. package/dist/validation/config.js +1 -1
  108. package/dist/validation/first.js +1 -1
  109. package/dist/validation/match.js +77 -4
  110. package/dist/validation/parser/parse.js +1 -1
  111. package/dist/validation/parser/typedef.js +4 -1
  112. package/dist/validation/utils/list.js +1 -1
  113. package/dist/version.js +2 -2
  114. package/dist/web.js +13 -6
  115. package/package.json +15 -6
@@ -2,14 +2,14 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken, ColorType } from '../ast/types.js';
9
9
  import { validationUnits, pseudoElements, containerFunc, urlFunc, gridTemplateFunc, imageFunc, timelineFunc, supportFunc, timingFunc, colorsFunc, mathFuncs, transformFunctions, whenElseFunc, wildCardFuncs } from '../syntax/constants.js';
10
10
  import { isNewLine, isWhiteSpace, isLetter, isIdentStart, isIdentCodepoint, isNonPrintable, isDigit, flexUnits, dimensionUnits, resolutionUnits, angleUnits, timeUnits, frequencyUnits } from '../syntax/syntax.js';
11
11
  import { SourceFile } from './source.js';
12
- import { isCssInput, toCodeUnits } from './input.js';
12
+ import { getStreamSize, isCssInput, toCodeUnits } from './input.js';
13
13
  import { hashRangeLower, lowerAscii } from './utils/codes.js';
14
14
  import { canonicalStart, canonicalEnd, canonicalCodes, registerCanonical } from './utils/canonical.js';
15
15
 
@@ -667,17 +667,52 @@ class Tokenizer {
667
667
  hint = null;
668
668
  state = null;
669
669
  parseInfo;
670
+ /**
671
+ * true when the whole css is in {@link ParseInfo.codes}: the input is not a stream, or the stream is read to its end
672
+ */
673
+ streamDone = true;
674
+ /**
675
+ * the last call of {@link next} reached the end of the code units read from the stream: the token may continue in
676
+ * the next chunk. no token is returned, the stream must be read ({@link read}) before {@link next} is called again
677
+ */
678
+ waiting = false;
679
+ /**
680
+ * reader of the stream
681
+ */
682
+ reader = null;
683
+ /**
684
+ * decoder of the chunks of the stream: a character can be split between two chunks
685
+ */
686
+ decoder = null;
687
+ /**
688
+ * the code units read from the stream: {@link ParseInfo.codes} is a view of its first {@link ParseInfo.length}
689
+ * code units. the buffer doubles when it is full, the code units are copied once per doubling
690
+ */
691
+ buffer = new Uint8Array(0);
692
+ /**
693
+ * the size of the css of the stream, in bytes, when it is known (see {@link setStreamSize}). 0: not known
694
+ */
695
+ expectedLength = 0;
670
696
  /**
671
697
  * @param parseInfo the parse info, or the css: a string, an ArrayBuffer or a view of an ArrayBuffer
672
698
  * @param input a stream of css
673
699
  */
674
700
  constructor(parseInfo, input = null) {
675
701
  this.input = input;
702
+ if (input != null) {
703
+ this.streamDone = false;
704
+ this.expectedLength = getStreamSize(input);
705
+ // the size of the stream is known: the code units are allocated once. the size in bytes is not less than
706
+ // the number of utf-16 code units
707
+ if (this.expectedLength > 0) {
708
+ this.buffer = new Uint8Array(this.expectedLength);
709
+ }
710
+ }
676
711
  if (isCssInput(parseInfo)) {
677
712
  this.parseInfo = createParseInfo(parseInfo, new SourceFile("", [], ""));
678
713
  }
679
714
  else {
680
- // the parse info of a stream is loaded by tokenizeStream()
715
+ // the code units of a stream are appended chunk by chunk, see read()
681
716
  if (parseInfo.codes == null && input == null) {
682
717
  Tokenizer.load(parseInfo, parseInfo.source.content);
683
718
  }
@@ -1575,9 +1610,37 @@ class Tokenizer {
1575
1610
  return this.typ === EnumToken.EOF;
1576
1611
  }
1577
1612
  /**
1578
- * Tokenize CSS string
1613
+ * read the next token. when the input is a stream that is not read to its end, a token that ends near the end of
1614
+ * the code units read so far may continue in the next chunk (an identifier, a number, a string, a comment, ...): the
1615
+ * token is not returned, {@link waiting} is true and the stream must be read ({@link read}) before the next call
1616
+ */
1617
+ next() {
1618
+ if (this.streamDone) {
1619
+ return this.nextToken();
1620
+ }
1621
+ const parseInfo = this.parseInfo;
1622
+ const position = parseInfo.position;
1623
+ const currentPosition = parseInfo.currentPosition;
1624
+ const state = this.state;
1625
+ const lineStarts = parseInfo.source.lineStarts.lineStarts;
1626
+ const lineCount = lineStarts.length;
1627
+ this.nextToken();
1628
+ // the tokenizer reads at most a few code units after the end of a token: the token is complete when the
1629
+ // stream has more code units after it
1630
+ if (parseInfo.currentPosition + STREAM_LOOKAHEAD > parseInfo.length) {
1631
+ parseInfo.position = position;
1632
+ parseInfo.currentPosition = currentPosition;
1633
+ this.state = state;
1634
+ lineStarts.length = lineCount;
1635
+ this.typ = null;
1636
+ this.waiting = true;
1637
+ }
1638
+ return this;
1639
+ }
1640
+ /**
1641
+ * read the next token from the code units of the input
1579
1642
  */
1580
- next( /* parseInfo: ParseInfo | string, yieldEOFToken: boolean = true */) {
1643
+ nextToken() {
1581
1644
  const parseInfo = this.parseInfo;
1582
1645
  this.source = parseInfo.source;
1583
1646
  let charCode;
@@ -2004,26 +2067,105 @@ class Tokenizer {
2004
2067
  return this.makeToken(parseInfo, EnumToken.EOFTokenType);
2005
2068
  }
2006
2069
  /**
2007
- * tokenize readable stream
2070
+ * start reading the stream: the first chunk is read. the next chunks are read when the tokenizer reaches the end
2071
+ * of the code units read so far, see {@link next} and {@link read}
2008
2072
  */
2009
2073
  async tokenizeStream() {
2010
- const decoder = new TextDecoder("utf-8");
2011
- const reader = this.input.getReader();
2012
- let parseInfo = this.parseInfo;
2013
- while (true) {
2014
- const { done, value } = await reader.read();
2015
- const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
2016
- if (!done) {
2017
- // inlined: parseInfo.source.append(stream as string)
2018
- parseInfo.source.content += stream;
2019
- }
2020
- else {
2074
+ if (!this.streamDone && this.reader == null) {
2075
+ await this.read();
2076
+ }
2077
+ return this;
2078
+ }
2079
+ /**
2080
+ * read the next chunk of the stream and append it to the css: the text of the source file and the code units.
2081
+ * the code units stay a view of the bytes while the css is ascii, they are utf-16 code units otherwise
2082
+ */
2083
+ async read() {
2084
+ if (this.streamDone) {
2085
+ this.waiting = false;
2086
+ return;
2087
+ }
2088
+ if (this.reader == null) {
2089
+ this.reader = this.input.getReader();
2090
+ this.decoder = new TextDecoder("utf-8");
2091
+ }
2092
+ // the chunks are appended together: at least 64 KB, or half of the css read so far. the text of the css is copied
2093
+ // when it is read after a chunk is appended (a string is not appended in place): the batches grow with the css,
2094
+ // the text is copied a few times instead of once per chunk
2095
+ const batch = Math.max(STREAM_BATCH, this.parseInfo.length >> 1);
2096
+ const texts = [];
2097
+ let length = 0;
2098
+ while (length < batch) {
2099
+ const { done, value } = await this.reader.read();
2100
+ if (done) {
2101
+ // the end of a character split between the last chunks
2102
+ texts.push(this.decoder.decode());
2103
+ this.streamDone = true;
2104
+ this.reader.releaseLock();
2021
2105
  break;
2022
2106
  }
2107
+ const text = typeof value === "string" ? value : this.decoder.decode(value, { stream: true });
2108
+ texts.push(text);
2109
+ length += text.length;
2110
+ }
2111
+ const text = texts.length === 1 ? texts[0] : texts.join("");
2112
+ if (text.length > 0) {
2113
+ this.append(text);
2023
2114
  }
2024
- Tokenizer.load(parseInfo, parseInfo.source.content);
2025
- return this; // .next();
2115
+ this.waiting = false;
2116
+ }
2117
+ /**
2118
+ * append the text of a chunk to the css
2119
+ * @param text
2120
+ */
2121
+ append(text) {
2122
+ const parseInfo = this.parseInfo;
2123
+ const length = parseInfo.length;
2124
+ const required = length + text.length;
2125
+ parseInfo.source.content += text;
2126
+ if (required > this.buffer.length) {
2127
+ let capacity = this.buffer.length > 0 ? this.buffer.length * 2 : 1 << 16;
2128
+ while (capacity < required) {
2129
+ capacity *= 2;
2130
+ }
2131
+ const buffer = this.buffer instanceof Uint8Array ? new Uint8Array(capacity) : new Uint16Array(capacity);
2132
+ buffer.set(this.buffer.subarray(0, length));
2133
+ this.buffer = buffer;
2134
+ }
2135
+ let ascii = false;
2136
+ if (this.buffer instanceof Uint8Array) {
2137
+ // an ascii text has a byte for each character
2138
+ const { read, written } = streamEncoder.encodeInto(text, this.buffer.subarray(length, required));
2139
+ ascii = read === text.length && written === text.length;
2140
+ if (!ascii) {
2141
+ // the css is not ascii: the code units are utf-16 code units from now on
2142
+ const buffer = new Uint16Array(this.buffer.length);
2143
+ buffer.set(this.buffer.subarray(0, length));
2144
+ this.buffer = buffer;
2145
+ }
2146
+ }
2147
+ if (!ascii) {
2148
+ const buffer = this.buffer;
2149
+ for (let i = 0; i < text.length; i++) {
2150
+ buffer[length + i] = text.charCodeAt(i);
2151
+ }
2152
+ }
2153
+ parseInfo.length = required;
2154
+ parseInfo.codes = this.buffer.subarray(0, required);
2026
2155
  }
2027
2156
  }
2157
+ /**
2158
+ * number of code units the tokenizer may read after the end of a token (escape sequences, unicode ranges, `<!--`,
2159
+ * ...): a token read from a stream is complete when the stream has more code units after it, see {@link Tokenizer.next}
2160
+ */
2161
+ const STREAM_LOOKAHEAD = 32;
2162
+ /**
2163
+ * minimum number of code units appended to the css when a stream is read, see {@link Tokenizer.read}
2164
+ */
2165
+ const STREAM_BATCH = 1 << 16;
2166
+ /**
2167
+ * encoder of the chunks of a stream, see {@link Tokenizer.append}
2168
+ */
2169
+ const streamEncoder = new TextEncoder();
2028
2170
 
2029
2171
  export { TokenMap, TokenValueField, TokenValueKind, Tokenizer, createParseInfo, hintsEnum };
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { getSyntaxRule } from '../../validation/config.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
@@ -55,7 +55,47 @@ function parseMediaqueryList(arena, stream, options) {
55
55
  i++;
56
56
  }
57
57
  // [not | only] <media-type> [ and <media-condition-without-or> ]?
58
- if (i < stream.length) {
58
+ let modifier = null;
59
+ if (i < stream.length && arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
60
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
61
+ if (val === "not" || val === "only") {
62
+ modifier = val;
63
+ tokens.push(stream[i]);
64
+ i++;
65
+ while (i < stream.length &&
66
+ (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType ||
67
+ arena.dataTypes[stream[i]] === EnumToken.CommentTokenType)) {
68
+ tokens.push(stream[i]);
69
+ i++;
70
+ }
71
+ }
72
+ }
73
+ if (modifier != null && i === stream.length) {
74
+ success = false;
75
+ errors.push({
76
+ action: "drop",
77
+ message: `expecting '<media-type>'`,
78
+ node: stream[i - 1],
79
+ location: [
80
+ options.source.file,
81
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i - 1])),
82
+ ],
83
+ });
84
+ }
85
+ else if (modifier === "only" && arena.dataTypes[stream[i]] !== EnumToken.IdenTokenType) {
86
+ // only is followed by a media type
87
+ success = false;
88
+ errors.push({
89
+ action: "drop",
90
+ message: `expecting '<media-type>'`,
91
+ node: stream[i],
92
+ location: [
93
+ options.source.file,
94
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
95
+ ],
96
+ });
97
+ }
98
+ else if (i < stream.length) {
59
99
  if (arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
60
100
  const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
61
101
  if (mediaTypes.includes(val)) {
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import config from '../../data/properties.json.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken, ColorType, EnumAstNodeStatus } from '../../ast/types.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  // Alphabet: a-z, A-Z, 0-9, _, -
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
@@ -11,7 +11,7 @@ import { renderValue } from '../../printer/render.js';
11
11
  import { isFunctionDefType, tokensfuncDefMap, pseudoElements, combinators } from '../../syntax/constants.js';
12
12
  import { isHash, isIdent } from '../../syntax/syntax.js';
13
13
  import { getParsedSyntax, getSyntaxRule } from '../../validation/config.js';
14
- import { matchAllSyntaxes, createValidationContext, trimArray, selectorFunctionSyntax, matchSelectorSyntax } from '../../validation/match.js';
14
+ import { matchAllSyntaxes, createValidationContext, trimArray, selectorFunctionSyntax, matchSimpleSelector, matchSelectorSyntax } from '../../validation/match.js';
15
15
  import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
16
16
  import { splitTokenList } from '../../validation/utils/list.js';
17
17
  import { trimWhiteSpace } from '../parse.js';
@@ -183,6 +183,58 @@ function splitUnicodeRanges(arena, tokens) {
183
183
  }
184
184
  }
185
185
  }
186
+ /**
187
+ * true if the token is a keyframe selector: `from`, `to` or a percentage between 0 and 100
188
+ * @param arena
189
+ * @param token
190
+ */
191
+ function isKeyframeSelectorToken(arena, token) {
192
+ if (token === undefined) {
193
+ return false;
194
+ }
195
+ if (arena.dataTypes[token] === EnumToken.PercentageTokenType) {
196
+ return arena.numericValue[token] >= 0 && arena.numericValue[token] <= 100;
197
+ }
198
+ if (arena.dataTypes[token] !== EnumToken.IdenTokenType) {
199
+ return false;
200
+ }
201
+ const id = arena.stringValue[token];
202
+ const length = arena.stringLength(id);
203
+ // `from` and `to`, case-insensitive
204
+ return length === 4
205
+ ? (arena.stringCodeAt(id, 0) | 0x20) === 0x66 &&
206
+ (arena.stringCodeAt(id, 1) | 0x20) === 0x72 &&
207
+ (arena.stringCodeAt(id, 2) | 0x20) === 0x6f &&
208
+ (arena.stringCodeAt(id, 3) | 0x20) === 0x6d
209
+ : length === 2 && (arena.stringCodeAt(id, 0) | 0x20) === 0x74 && (arena.stringCodeAt(id, 1) | 0x20) === 0x6f;
210
+ }
211
+ /**
212
+ * true if the selector of a keyframe is a list of `from`, `to` and percentages between 0 and 100, separated by commas
213
+ * and whitespace: it matches the `keyframe-selectors` syntax
214
+ * @param arena
215
+ * @param tokens
216
+ */
217
+ function isSimpleKeyframeSelector(arena, tokens) {
218
+ // a keyframe selector is expected
219
+ let selector = true;
220
+ let i = 0;
221
+ for (; i < tokens.length; i++) {
222
+ const type = arena.dataTypes[tokens[i]];
223
+ if (type === EnumToken.WhitespaceTokenType) {
224
+ continue;
225
+ }
226
+ if (selector) {
227
+ if (!isKeyframeSelectorToken(arena, tokens[i])) {
228
+ return false;
229
+ }
230
+ }
231
+ else if (type !== EnumToken.CommaTokenType) {
232
+ return false;
233
+ }
234
+ selector = !selector;
235
+ }
236
+ return !selector;
237
+ }
186
238
  /**
187
239
  * parse selector
188
240
  * @param arena
@@ -195,7 +247,10 @@ function parseSelector(arena, tokens, context, options, errors) {
195
247
  const contextType = context == null ? null : arena.dataTypes[context];
196
248
  splitUnicodeRanges(arena, tokens);
197
249
  if (contextType === EnumToken.KeyframesAtRuleNodeType) {
198
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(arena, tokens), options);
250
+ // `from`, `to` and the percentages are valid: the syntax is not matched
251
+ const result = isSimpleKeyframeSelector(arena, tokens)
252
+ ? { success: true, errors: [] }
253
+ : matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(arena, tokens), options);
199
254
  const parts = splitTokenList(arena, tokens);
200
255
  for (const part of parts) {
201
256
  trimArray(arena, part);
@@ -243,8 +298,11 @@ function parseSelector(arena, tokens, context, options, errors) {
243
298
  allowed = false;
244
299
  if (contextType === EnumToken.AtRuleNodeType) {
245
300
  // computed once per at-rule name: the name is not concatenated for each selector
301
+ const previousAtRuleArena = atRuleArena;
246
302
  atRuleArena = arena;
247
303
  allowed = arena.stringDerived(arena.name[context], 3 /* StringDerived.AtRuleAcceptsRules */, atRuleAcceptsRules);
304
+ // the arena is not kept after the parse: the previous arena is restored (a nested call)
305
+ atRuleArena = previousAtRuleArena;
248
306
  }
249
307
  }
250
308
  while (parent != null && parent > 0) {
@@ -345,9 +403,11 @@ function parseSelector(arena, tokens, context, options, errors) {
345
403
  }
346
404
  tokens.length = write;
347
405
  }
348
- const result = matchSelectorSyntax(arena, tokens, errors, options, nested);
406
+ // a selector made of simple selectors is valid, it has no attribute selector and no function
407
+ const simple = matchSimpleSelector(arena, tokens);
408
+ const result = simple ? simpleSelectorMatch : matchSelectorSyntax(arena, tokens, errors, options, nested);
349
409
  trimArray(arena, tokens);
350
- if (result.success) {
410
+ if (result.success && !simple) {
351
411
  for (let i = 0; i < tokens.length; i++) {
352
412
  const token = tokens[i];
353
413
  switch (arena.dataTypes[token]) {
@@ -400,7 +460,7 @@ function parseSelector(arena, tokens, context, options, errors) {
400
460
  }
401
461
  }
402
462
  }
403
- const node = arena.createRule(EnumToken.RuleNodeType, renderSelector(arena, tokens), tokens, result.success && allowed
463
+ const node = arena.createRule(EnumToken.RuleNodeType, simple ? renderSimpleSelector(arena, tokens) : renderSelector(arena, tokens), tokens, result.success && allowed
404
464
  ? EnumAstNodeStatus.Validated
405
465
  : allowed
406
466
  ? EnumAstNodeStatus.Invalid
@@ -409,6 +469,13 @@ function parseSelector(arena, tokens, context, options, errors) {
409
469
  arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
410
470
  return node;
411
471
  }
472
+ /**
473
+ * result of the validation of a simple selector, see {@link matchSimpleSelector}
474
+ */
475
+ const simpleSelectorMatch = Object.freeze({
476
+ success: true,
477
+ errors: Object.freeze([]),
478
+ });
412
479
  // options of the rendering of the selector tokens
413
480
  const selectorRenderOptions = { minify: false };
414
481
  /**
@@ -673,6 +740,31 @@ function addSelectorCodes(count, selectors) {
673
740
  selectorOffsets[selectors * 2 + 1] = end;
674
741
  return selectors + 1;
675
742
  }
743
+ /**
744
+ * selector of a rule made of simple selectors (see {@link matchSimpleSelector}), without the leading and the trailing
745
+ * whitespace: the string of a single token is used as is, a compound or a complex selector is built without
746
+ * {@link renderSelector}. a selector list is rendered with {@link renderSelector}: the duplicate selectors are removed
747
+ * @param arena
748
+ * @param tokens
749
+ * @returns the interned string id of the selector
750
+ */
751
+ function renderSimpleSelector(arena, tokens) {
752
+ if (tokens.length === 1) {
753
+ const type = arena.dataTypes[tokens[0]];
754
+ // rendered as their string by appendSelectorToken()
755
+ if (type !== EnumToken.PseudoElementTokenType) {
756
+ return arena.stringValue[tokens[0]];
757
+ }
758
+ }
759
+ selectorBuilder.reset();
760
+ for (let index = 0; index < tokens.length; index++) {
761
+ if (arena.dataTypes[tokens[index]] === EnumToken.CommaTokenType) {
762
+ return renderSelector(arena, tokens);
763
+ }
764
+ appendSelectorToken(arena, tokens[index], selectorBuilder);
765
+ }
766
+ return arena.internBuilder(selectorBuilder);
767
+ }
676
768
  /**
677
769
  * selector of a rule: the distinct comma separated selectors, the whitespace around the combinators is removed.
678
770
  * a universal selector is removed before a compound selector. the selector is built from the code units of the
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.3.1
5
+ Version: 2.3.2
6
6
  License: MIT
7
7
  */
8
8
  // from https://github.com/Rich-Harris/vlq/tree/master