@tbela99/css-parser 1.4.3 → 1.4.5

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 (166) hide show
  1. package/CHANGELOG.md +196 -2
  2. package/README.md +68 -1120
  3. package/dist/config.json.js +43 -2
  4. package/dist/index-umd-web.js +18118 -15886
  5. package/dist/index.cjs +16838 -14611
  6. package/dist/index.d.ts +1804 -1684
  7. package/dist/lib/ast/clone.js +4 -3
  8. package/dist/lib/ast/expand.js +2 -2
  9. package/dist/lib/ast/features/calc.js +59 -54
  10. package/dist/lib/ast/features/if.js +64 -17
  11. package/dist/lib/ast/features/inlinecssvariables.js +2 -2
  12. package/dist/lib/ast/features/prefix.js +343 -6
  13. package/dist/lib/ast/features/transform.js +7 -4
  14. package/dist/lib/ast/find.js +5 -5
  15. package/dist/lib/ast/math/expression.js +12 -8
  16. package/dist/lib/ast/minify.js +28 -33
  17. package/dist/lib/ast/transform/minify.js +135 -131
  18. package/dist/lib/ast/transform/utils.js +3 -2
  19. package/dist/lib/ast/types.js +69 -8
  20. package/dist/lib/ast/walk.js +4 -5
  21. package/dist/lib/fs/resolve.js +71 -57
  22. package/dist/lib/parser/declaration/list.js +92 -3
  23. package/dist/lib/parser/declaration/map.js +5 -5
  24. package/dist/lib/parser/parse.js +530 -466
  25. package/dist/lib/parser/tokenize.js +110 -4
  26. package/dist/lib/parser/utils/angle.js +29 -0
  27. package/dist/lib/parser/utils/at-rule-container.js +2 -2
  28. package/dist/lib/parser/utils/at-rule-font-feature-values.js +2 -2
  29. package/dist/lib/parser/utils/at-rule-generic.js +1 -1
  30. package/dist/lib/parser/utils/at-rule-media.js +2 -2
  31. package/dist/lib/parser/utils/at-rule-support.js +73 -8
  32. package/dist/lib/parser/utils/at-rule-when-else.js +2 -2
  33. package/dist/lib/parser/utils/at-rule.js +2 -8
  34. package/dist/lib/parser/utils/declaration.js +179 -76
  35. package/dist/lib/parser/utils/selector.js +53 -11
  36. package/dist/lib/parser/utils/token.js +13 -11
  37. package/dist/lib/renderer/render.js +501 -59
  38. package/dist/lib/syntax/color/alpha.js +64 -0
  39. package/dist/lib/syntax/color/cmyk.js +2 -2
  40. package/dist/lib/syntax/color/color-mix.js +283 -220
  41. package/dist/lib/syntax/color/color.js +37 -28
  42. package/dist/lib/syntax/color/hex.js +2 -2
  43. package/dist/lib/syntax/color/hsl.js +3 -3
  44. package/dist/lib/syntax/color/hwb.js +7 -7
  45. package/dist/lib/syntax/color/lab.js +5 -5
  46. package/dist/lib/syntax/color/lch.js +4 -4
  47. package/dist/lib/syntax/color/oklab.js +3 -3
  48. package/dist/lib/syntax/color/oklch.js +3 -3
  49. package/dist/lib/syntax/color/relativecolor.js +97 -12
  50. package/dist/lib/syntax/color/srgb.js +5 -5
  51. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  52. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  53. package/dist/lib/syntax/color/utils/components.js +7 -3
  54. package/dist/lib/syntax/color/utils/distance.js +21 -4
  55. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  56. package/dist/lib/syntax/color/xyz.js +2 -2
  57. package/dist/lib/syntax/constants.js +29 -1
  58. package/dist/lib/syntax/syntax.js +307 -39
  59. package/dist/lib/validation/config.js +2 -2
  60. package/dist/lib/validation/config.json.js +32 -17
  61. package/dist/lib/validation/match.js +239 -104
  62. package/dist/lib/validation/parser/parse.js +0 -1
  63. package/dist/lib/validation/parser/typedef.js +6 -0
  64. package/dist/node.js +64 -22
  65. package/dist/web.js +65 -18
  66. package/package.json +26 -15
  67. package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  68. package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  69. package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  70. package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  71. package/.repl_history +0 -4
  72. package/badges/coverage.svg +0 -20
  73. package/deno.lock +0 -2861
  74. package/dist/lib/ast/clone.d.ts +0 -10
  75. package/dist/lib/ast/expand.d.ts +0 -14
  76. package/dist/lib/ast/features/calc.d.ts +0 -10
  77. package/dist/lib/ast/features/if.d.ts +0 -10
  78. package/dist/lib/ast/features/index.d.ts +0 -6
  79. package/dist/lib/ast/features/inlinecssvariables.d.ts +0 -15
  80. package/dist/lib/ast/features/prefix.d.ts +0 -8
  81. package/dist/lib/ast/features/shorthand.d.ts +0 -12
  82. package/dist/lib/ast/features/transform.d.ts +0 -10
  83. package/dist/lib/ast/features/type.d.ts +0 -15
  84. package/dist/lib/ast/find.d.ts +0 -165
  85. package/dist/lib/ast/math/expression.d.ts +0 -18
  86. package/dist/lib/ast/math/math.d.ts +0 -6
  87. package/dist/lib/ast/minify.d.ts +0 -19
  88. package/dist/lib/ast/transform/compute.d.ts +0 -8
  89. package/dist/lib/ast/transform/matrix.d.ts +0 -22
  90. package/dist/lib/ast/transform/minify.d.ts +0 -5
  91. package/dist/lib/ast/transform/perspective.d.ts +0 -3
  92. package/dist/lib/ast/transform/rotate.d.ts +0 -12
  93. package/dist/lib/ast/transform/scale.d.ts +0 -6
  94. package/dist/lib/ast/transform/skew.d.ts +0 -4
  95. package/dist/lib/ast/transform/translate.d.ts +0 -6
  96. package/dist/lib/ast/transform/utils.d.ts +0 -9
  97. package/dist/lib/ast/types.d.ts +0 -903
  98. package/dist/lib/ast/walk.d.ts +0 -162
  99. package/dist/lib/fs/resolve.d.ts +0 -20
  100. package/dist/lib/parser/declaration/list.d.ts +0 -16
  101. package/dist/lib/parser/declaration/map.d.ts +0 -15
  102. package/dist/lib/parser/declaration/set.d.ts +0 -9
  103. package/dist/lib/parser/node.d.ts +0 -7
  104. package/dist/lib/parser/parse.d.ts +0 -107
  105. package/dist/lib/parser/tokenize.d.ts +0 -57
  106. package/dist/lib/parser/utils/at-rule-container.d.ts +0 -5
  107. package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +0 -5
  108. package/dist/lib/parser/utils/at-rule-generic.d.ts +0 -5
  109. package/dist/lib/parser/utils/at-rule-import.d.ts +0 -5
  110. package/dist/lib/parser/utils/at-rule-media.d.ts +0 -5
  111. package/dist/lib/parser/utils/at-rule-page.d.ts +0 -5
  112. package/dist/lib/parser/utils/at-rule-page.js +0 -28
  113. package/dist/lib/parser/utils/at-rule-support.d.ts +0 -5
  114. package/dist/lib/parser/utils/at-rule-token.d.ts +0 -1
  115. package/dist/lib/parser/utils/at-rule-when-else.d.ts +0 -5
  116. package/dist/lib/parser/utils/at-rule.d.ts +0 -13
  117. package/dist/lib/parser/utils/cache.d.ts +0 -6
  118. package/dist/lib/parser/utils/config.d.ts +0 -2
  119. package/dist/lib/parser/utils/declaration-list.d.ts +0 -5
  120. package/dist/lib/parser/utils/declaration.d.ts +0 -18
  121. package/dist/lib/parser/utils/eq.d.ts +0 -1
  122. package/dist/lib/parser/utils/hash.d.ts +0 -21
  123. package/dist/lib/parser/utils/selector.d.ts +0 -5
  124. package/dist/lib/parser/utils/text.d.ts +0 -3
  125. package/dist/lib/parser/utils/token.d.ts +0 -14
  126. package/dist/lib/parser/utils/type.d.ts +0 -2
  127. package/dist/lib/renderer/render.d.ts +0 -28
  128. package/dist/lib/renderer/sourcemap/lib/encode.d.ts +0 -1
  129. package/dist/lib/renderer/sourcemap/sourcemap.d.ts +0 -26
  130. package/dist/lib/syntax/color/a98rgb.d.ts +0 -2
  131. package/dist/lib/syntax/color/cmyk.d.ts +0 -10
  132. package/dist/lib/syntax/color/color-mix.d.ts +0 -2
  133. package/dist/lib/syntax/color/color.d.ts +0 -42
  134. package/dist/lib/syntax/color/hex.d.ts +0 -16
  135. package/dist/lib/syntax/color/hsl.d.ts +0 -20
  136. package/dist/lib/syntax/color/hsv.d.ts +0 -2
  137. package/dist/lib/syntax/color/hwb.d.ts +0 -21
  138. package/dist/lib/syntax/color/lab.d.ts +0 -25
  139. package/dist/lib/syntax/color/lch.d.ts +0 -23
  140. package/dist/lib/syntax/color/oklab.d.ts +0 -22
  141. package/dist/lib/syntax/color/oklch.d.ts +0 -20
  142. package/dist/lib/syntax/color/p3.d.ts +0 -6
  143. package/dist/lib/syntax/color/prophotorgb.d.ts +0 -2
  144. package/dist/lib/syntax/color/rec2020.d.ts +0 -2
  145. package/dist/lib/syntax/color/relativecolor.d.ts +0 -13
  146. package/dist/lib/syntax/color/rgb.d.ts +0 -20
  147. package/dist/lib/syntax/color/srgb.d.ts +0 -23
  148. package/dist/lib/syntax/color/utils/components.d.ts +0 -2
  149. package/dist/lib/syntax/color/utils/distance.d.ts +0 -18
  150. package/dist/lib/syntax/color/utils/matrix.d.ts +0 -6
  151. package/dist/lib/syntax/color/xyz.d.ts +0 -5
  152. package/dist/lib/syntax/color/xyzd50.d.ts +0 -4
  153. package/dist/lib/syntax/constants.d.ts +0 -67
  154. package/dist/lib/syntax/syntax.d.ts +0 -38
  155. package/dist/lib/validation/config.d.ts +0 -14
  156. package/dist/lib/validation/json.d.ts +0 -2
  157. package/dist/lib/validation/match.d.ts +0 -38
  158. package/dist/lib/validation/parser/parse.d.ts +0 -8
  159. package/dist/lib/validation/parser/typedef.d.ts +0 -95
  160. package/dist/lib/validation/utils/list.d.ts +0 -4
  161. package/dist/lib/validation/utils/whitespace.d.ts +0 -2
  162. package/dist/node.d.ts +0 -207
  163. package/dist/web.d.ts +0 -169
  164. package/playground/index.html +0 -1328
  165. package/playground/sw.js +0 -55
  166. package/playground/tree.js +0 -176
@@ -1,6 +1,7 @@
1
1
  import { convertColor } from '../color.js';
2
2
  import { getOKLABComponents } from '../oklab.js';
3
3
  import { ColorType } from '../../../ast/types.js';
4
+ import { colorDistancePrecision } from '../../constants.js';
4
5
 
5
6
  /**
6
7
  * Calculate the distance between two okLab colors.
@@ -8,9 +9,20 @@ import { ColorType } from '../../../ast/types.js';
8
9
  * @param okLab2
9
10
  *
10
11
  * @private
12
+ * {@link https://drafts.csswg.org/css-color-4/#comparing-color-values}
11
13
  */
12
- function okLabDistance(okLab1, okLab2) {
13
- return Math.sqrt(Math.pow(okLab1[0] - okLab2[0], 2) + Math.pow(okLab1[1] - okLab2[1], 2) + Math.pow(okLab1[2] - okLab2[2], 2));
14
+ function okLabDistance(color1, color2) {
15
+ color1 = convertColor(color1, ColorType.OKLAB);
16
+ color2 = convertColor(color2, ColorType.OKLAB);
17
+ if (color1 == null || color2 == null) {
18
+ return null;
19
+ }
20
+ const okLab1 = getOKLABComponents(color1);
21
+ const okLab2 = getOKLABComponents(color2);
22
+ if (okLab1 == null || okLab2 == null) {
23
+ return null;
24
+ }
25
+ return Math.hypot(okLab1[0] - okLab2[0], okLab1[1] - okLab2[1], okLab1[2] - okLab2[2]);
14
26
  }
15
27
  /**
16
28
  * Check if two colors are close in okLab space.
@@ -20,7 +32,7 @@ function okLabDistance(okLab1, okLab2) {
20
32
  *
21
33
  * @private
22
34
  */
23
- function isOkLabClose(color1, color2, threshold = 0.01) {
35
+ function isOkLabClose(color1, color2, threshold = colorDistancePrecision) {
24
36
  color1 = convertColor(color1, ColorType.OKLAB);
25
37
  color2 = convertColor(color2, ColorType.OKLAB);
26
38
  if (color1 == null || color2 == null) {
@@ -31,7 +43,12 @@ function isOkLabClose(color1, color2, threshold = 0.01) {
31
43
  if (okLab1 == null || okLab2 == null) {
32
44
  return false;
33
45
  }
34
- return okLabDistance(okLab1, okLab2) < threshold;
46
+ for (let i = 0; i < 3; i++) {
47
+ if (Math.abs(okLab1[i] - okLab2[i]) > threshold) {
48
+ return false;
49
+ }
50
+ }
51
+ return true;
35
52
  }
36
53
 
37
54
  export { isOkLabClose, okLabDistance };
@@ -0,0 +1,56 @@
1
+ import { EnumToken, ColorType } from '../../../ast/types.js';
2
+ import { equalsIgnoreCase } from '../../../parser/utils/text.js';
3
+ import { minmax, getNumber } from '../color.js';
4
+
5
+ /**
6
+ * Create a color token
7
+ * @param kind
8
+ * @param components
9
+ * @param alpha
10
+ * @returns
11
+ */
12
+ function makeColor(kind, components, alpha) {
13
+ if (components.length < 3) {
14
+ return null;
15
+ }
16
+ const alphaValue = alpha == null
17
+ ? 1
18
+ : alpha.typ === EnumToken.IdenTokenType && equalsIgnoreCase(alpha.val, "none")
19
+ ? 0
20
+ : minmax(getNumber(alpha), 0, 1);
21
+ components.length = 3;
22
+ if (alphaValue !== 1) {
23
+ components.push({ typ: EnumToken.NumberTokenType, val: alphaValue });
24
+ }
25
+ if (kind === ColorType.LIT || kind === ColorType.HEX) {
26
+ const val = "#" +
27
+ components.reduce((acc, token, index) => {
28
+ let value = getNumber(token);
29
+ if (index === 3) {
30
+ value *= 255;
31
+ }
32
+ return acc + Math.round(value).toString(16).padStart(2, "0");
33
+ }, "");
34
+ return {
35
+ typ: EnumToken.ColorTokenType,
36
+ val,
37
+ kin: ColorType.HEX,
38
+ };
39
+ }
40
+ const hasAlpha = components.length === 4;
41
+ let i = components.length;
42
+ while (--i > 0) {
43
+ components.splice(i, 0, { typ: EnumToken.WhitespaceTokenType });
44
+ }
45
+ if (hasAlpha) {
46
+ components[components.length - 2] = { typ: EnumToken.LiteralTokenType, val: "/" };
47
+ }
48
+ return {
49
+ typ: EnumToken.ColorTokenType,
50
+ val: ColorType[kind].toLowerCase(),
51
+ chi: components,
52
+ kin: kind,
53
+ };
54
+ }
55
+
56
+ export { makeColor };
@@ -39,7 +39,7 @@ function srgb2xyz(r, g, b, alpha) {
39
39
  return rgb;
40
40
  }
41
41
  // xyz d50
42
- function srgb2xyz_d50(r, g, b, alpha) {
42
+ function srgb2xyz_d65(r, g, b, alpha) {
43
43
  // xyx d65
44
44
  // @ts-ignore
45
45
  let rgb = XYZ_D65_to_D50(...srgb2xyz(r, g, b));
@@ -49,4 +49,4 @@ function srgb2xyz_d50(r, g, b, alpha) {
49
49
  return rgb;
50
50
  }
51
51
 
52
- export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz, srgb2xyz_d50 };
52
+ export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz, srgb2xyz_d65 };
@@ -1,6 +1,8 @@
1
1
  import { EnumToken } from '../ast/types.js';
2
2
  import config from '../validation/config.json.js';
3
3
 
4
+ const regMatchLinearGradient = /^-((webkit)|o|moz)(-repeating)?-linear-gradient$/i;
5
+ const regMatchRadialGradient = /^-((webkit)|o|moz)(-repeating)?-radial-gradient$/i;
4
6
  const mFLT = new Set([EnumToken.LtTokenType, EnumToken.LteTokenType]);
5
7
  const mFGT = new Set([EnumToken.GtTokenType, EnumToken.GteTokenType]);
6
8
  const tokensMap = new Map([
@@ -27,8 +29,25 @@ const tokensfuncDefMap = new Map([
27
29
  [EnumToken.GeneralEnclosedFunctionTokenDefType, EnumToken.GeneralEnclosedFunctionTokenType],
28
30
  ]);
29
31
  const tokensfuncSet = new Set(tokensfuncDefMap.values());
32
+ /**
33
+ * Epsilon
34
+ */
35
+ const epsilon = 1e-5;
36
+ /**
37
+ * Color distance precision
38
+ */
39
+ const colorDistancePrecision = epsilon;
40
+ /**
41
+ * Color precision
42
+ */
30
43
  const colorPrecision = 6;
44
+ /**
45
+ * Angle precision
46
+ */
31
47
  const anglePrecision = 0.001;
48
+ /**
49
+ * Color range definitions
50
+ */
32
51
  const colorRange = {
33
52
  lab: {
34
53
  l: [0, 100],
@@ -148,18 +167,27 @@ const colorsFunc = [
148
167
  "lch",
149
168
  "light-dark",
150
169
  "contrast-color",
170
+ "alpha",
151
171
  ];
152
172
  const imageFunc = [
153
173
  "linear-gradient",
154
174
  "radial-gradient",
155
175
  "repeating-linear-gradient",
156
176
  "repeating-radial-gradient",
177
+ "repeating-conic-gradient",
157
178
  "conic-gradient",
158
179
  "image",
159
180
  "image-set",
160
181
  "element",
161
182
  "cross-fade",
162
183
  "paint",
184
+ "-o-linear-gradient",
185
+ "-moz-linear-gradient",
186
+ "-webkit-linear-gradient",
187
+ "-webkit-repeating-linear-gradient",
188
+ "-webkit-gradient",
189
+ "-webkit-radial-gradient",
190
+ "-webkit-repeating-radial-gradient",
163
191
  ];
164
192
  const transformFunctions = [
165
193
  "translate",
@@ -433,4 +461,4 @@ const trimTokenSpace = new Set([
433
461
  const definedPropertySettings = { configurable: true, enumerable: false, writable: true };
434
462
  const combinators = ["+", ">", "~", "||", "|"];
435
463
 
436
- export { COLORS_NAMES, D50, NAMES_COLORS, anglePrecision, colorFuncColorSpace, colorPrecision, colorRange, colorsFunc, combinators, containerFunc, definedPropertySettings, deprecatedSystemColors, e, funcLike, generalEnclosedFunc, gridTemplateFunc, imageFunc, k, mFGT, mFLT, mathFuncs, mediaTypes, nonStandardColors, pageMarginBoxType, supportFunc, systemColors, timelineFunc, timingFunc, tokensMap, tokensfuncDefMap, tokensfuncSet, transformFunctions, trimTokenSpace, urlFunc, urlTokenMatcher, whenElseFunc, wildCardFuncs };
464
+ export { COLORS_NAMES, D50, NAMES_COLORS, anglePrecision, colorDistancePrecision, colorFuncColorSpace, colorPrecision, colorRange, colorsFunc, combinators, containerFunc, definedPropertySettings, deprecatedSystemColors, e, epsilon, funcLike, generalEnclosedFunc, gridTemplateFunc, imageFunc, k, mFGT, mFLT, mathFuncs, mediaTypes, nonStandardColors, pageMarginBoxType, regMatchLinearGradient, regMatchRadialGradient, supportFunc, systemColors, timelineFunc, timingFunc, tokensMap, tokensfuncDefMap, tokensfuncSet, transformFunctions, trimTokenSpace, urlFunc, urlTokenMatcher, whenElseFunc, wildCardFuncs };
@@ -1,7 +1,15 @@
1
1
  import { EnumToken, ColorType } from '../ast/types.js';
2
2
  import { walkValues, WalkerOptionEnum } from '../ast/walk.js';
3
+ import { toDegrees } from '../parser/utils/angle.js';
3
4
  import { equalsIgnoreCase } from '../parser/utils/text.js';
4
- import { colorsFunc, systemColors, deprecatedSystemColors, nonStandardColors, COLORS_NAMES, mathFuncs } from './constants.js';
5
+ import { getParsedSyntax } from '../validation/config.js';
6
+ import { matchAllSyntaxes, createValidationContext, trimArray } from '../validation/match.js';
7
+ import { ValidationSyntaxGroupEnum } from '../validation/parser/typedef.js';
8
+ import { splitTokenList } from '../validation/utils/list.js';
9
+ import { getColorSpace } from './color/utils/colorspace.js';
10
+ import { getColorComponents } from './color/utils/components.js';
11
+ import { colorsFunc, systemColors, deprecatedSystemColors, nonStandardColors, COLORS_NAMES, colorFuncColorSpace, mathFuncs } from './constants.js';
12
+ import { isOkLabClose } from './color/utils/distance.js';
5
13
 
6
14
  // https://www.w3.org/TR/CSS21/syndata.html#syntax
7
15
  // https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
@@ -230,6 +238,167 @@ function isColorspace(token) {
230
238
  "hwb",
231
239
  ].includes(token.val.toLowerCase());
232
240
  }
241
+ function reduceColorStops(stops) {
242
+ const parts = splitTokenList(stops);
243
+ const n = parts.length == 1 ? 1 : parts.length - 1;
244
+ let j;
245
+ let i;
246
+ let k = -1;
247
+ let updated = false;
248
+ for (i = 0; i < parts.length; i++) {
249
+ k++;
250
+ if (parts[i].length != 3) {
251
+ continue;
252
+ }
253
+ if (i > 0 && isOkLabClose(parts[i - 1][0], parts[i][0])) {
254
+ if (parts[i - 1].length == 1) {
255
+ parts[i - 1].push({ typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: ((k - 1) * 100) / n });
256
+ }
257
+ parts[i - 1].push(...parts[i].slice(1));
258
+ parts.splice(i--, 1);
259
+ updated = true;
260
+ continue;
261
+ }
262
+ for (j = 0; j < parts[i].length; j++) {
263
+ if ((parts[i][j].typ == EnumToken.LengthTokenType && 0 == parts[i][j].val) ||
264
+ parts[i][j].typ == EnumToken.NumberTokenType ||
265
+ parts[i][j].typ == EnumToken.PercentageTokenType) {
266
+ if (parts[i][j].val === (k * 100) / n) {
267
+ parts[i].length = j;
268
+ trimArray(parts[i]);
269
+ updated = true;
270
+ break;
271
+ }
272
+ }
273
+ }
274
+ }
275
+ if (updated) {
276
+ stops.length = 0;
277
+ for (j = 0; j < parts.length; j++) {
278
+ if (stops.length > 0) {
279
+ stops.push({ typ: EnumToken.CommaTokenType });
280
+ }
281
+ stops.push(...parts[j]);
282
+ }
283
+ }
284
+ return stops;
285
+ }
286
+ /**
287
+ * Reduce background-position values.
288
+ * @param positions
289
+ * @param position
290
+ */
291
+ function reducegradientBackgroundPosition(positions, position) {
292
+ switch (position) {
293
+ case "50%":
294
+ case "50% 50%":
295
+ case "center":
296
+ case "center center":
297
+ positions.length = 0;
298
+ break;
299
+ case "0 50%":
300
+ case "0% 50%":
301
+ case "left":
302
+ case "left center":
303
+ case "center left":
304
+ positions.length = 2;
305
+ positions.push({ typ: EnumToken.PercentageTokenType, val: 0 });
306
+ break;
307
+ case "50% 0":
308
+ case "50% 0%":
309
+ case "top center":
310
+ case "center top":
311
+ positions.length = 2;
312
+ positions.push({ typ: EnumToken.IdenTokenType, val: "top" });
313
+ break;
314
+ case "bottom":
315
+ case "50% 100%":
316
+ case "bottom center":
317
+ case "center bottom":
318
+ positions.length = 2;
319
+ positions.push({ typ: EnumToken.IdenTokenType, val: "bottom" });
320
+ break;
321
+ case "left":
322
+ case "0 50%":
323
+ case "0% 50%":
324
+ case "left center":
325
+ case "center left":
326
+ positions.length = 2;
327
+ positions.push({ typ: EnumToken.PercentageTokenType, val: 0 });
328
+ break;
329
+ case "100% 50%":
330
+ case "right":
331
+ case "right center":
332
+ case "center right":
333
+ positions.length = 2;
334
+ positions.push({ typ: EnumToken.PercentageTokenType, val: 100 });
335
+ break;
336
+ case "bottom left":
337
+ case "left bottom":
338
+ positions.length = 2;
339
+ positions.push({ typ: EnumToken.PercentageTokenType, val: 0 }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 100 });
340
+ break;
341
+ case "bottom right":
342
+ case "right bottom":
343
+ positions.length = 2;
344
+ positions.push({ typ: EnumToken.PercentageTokenType, val: 100 }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 100 });
345
+ break;
346
+ case "top left":
347
+ case "left top":
348
+ positions.length = 2;
349
+ positions.push({ typ: EnumToken.PercentageTokenType, val: 0 }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 0 });
350
+ break;
351
+ case "top right":
352
+ case "right top":
353
+ positions.length = 2;
354
+ positions.push({ typ: EnumToken.PercentageTokenType, val: 100 }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 0 });
355
+ break;
356
+ }
357
+ }
358
+ function reduceConicColorStops(stops) {
359
+ const parts = splitTokenList(stops);
360
+ const n = parts.length == 1 ? 1 : parts.length - 1;
361
+ let j;
362
+ let i;
363
+ let k = -1;
364
+ let updated = false;
365
+ for (i = 0; i < parts.length; i++) {
366
+ k++;
367
+ if (parts[i].length != 3) {
368
+ continue;
369
+ }
370
+ if (i > 0 && isOkLabClose(parts[i - 1][0], parts[i][0])) {
371
+ if (parts[i - 1].length == 1) {
372
+ parts[i - 1].push({ typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.AngleTokenType, val: ((k - 1) * 100) / n, unit: "deg" });
373
+ }
374
+ parts[i - 1].push(...parts[i].slice(1));
375
+ parts.splice(i--, 1);
376
+ updated = true;
377
+ continue;
378
+ }
379
+ for (j = 0; j < parts[i].length; j++) {
380
+ if ((parts[i][j].typ == EnumToken.NumberTokenType && 0 == parts[i][j].val) ||
381
+ parts[i][j].typ == EnumToken.AngleTokenType) {
382
+ if (toDegrees(parts[i][j]).val === (k * 360) / n) {
383
+ parts[i].length = j;
384
+ trimArray(parts[i]);
385
+ updated = true;
386
+ break;
387
+ }
388
+ }
389
+ }
390
+ }
391
+ if (updated) {
392
+ stops.length = 0;
393
+ for (j = 0; j < parts.length; j++) {
394
+ if (stops.length > 0) {
395
+ stops.push({ typ: EnumToken.CommaTokenType });
396
+ }
397
+ stops.push(...parts[j]);
398
+ }
399
+ }
400
+ return stops;
401
+ }
233
402
  function isRectangularOrthogonalColorspace(token) {
234
403
  if (token.typ != EnumToken.IdenTokenType) {
235
404
  return false;
@@ -246,23 +415,13 @@ function isRectangularOrthogonalColorspace(token) {
246
415
  "xyz",
247
416
  "xyz-d50",
248
417
  "xyz-d65",
249
- ].includes(token.val.toLowerCase());
418
+ ].some((t) => equalsIgnoreCase(t, token.val));
250
419
  }
251
420
  function isPolarColorspace(token) {
252
421
  if (token.typ != EnumToken.IdenTokenType) {
253
422
  return false;
254
423
  }
255
- return ["hsl", "hwb", "lch", "oklch"].includes(token.val);
256
- }
257
- function isHueInterpolationMethod(token) {
258
- if (!Array.isArray(token)) {
259
- return token.typ == EnumToken.IdenTokenType && "hue" === token.val?.toLowerCase?.();
260
- }
261
- if (token.length != 2 || token[0].typ != EnumToken.IdenTokenType || token[1].typ != EnumToken.IdenTokenType) {
262
- return false;
263
- }
264
- return (["shorter", "longer", "increasing", "decreasing"].includes(token[0].val?.toLowerCase?.()) &&
265
- "hue" === token[1].val?.toLowerCase?.());
424
+ return ["hsl", "hwb", "lch", "oklch"].some((t) => equalsIgnoreCase(t, token.val));
266
425
  }
267
426
  function isIdentColor(token) {
268
427
  return (token.typ == EnumToken.ColorTokenType &&
@@ -327,6 +486,73 @@ function isColor(token, errors) {
327
486
  }
328
487
  // adding numbers and percentages is disallowed
329
488
  // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/lch#defining_relative_color_output_channel_components:~:text=Adding%20a%20%3Cpercentage%3E%20to%20a%20%3Cnumber%3E%2C%20for%20example%2C%20doesn%27t%20work
489
+ const components = getColorComponents(token);
490
+ if (components !== null) {
491
+ const colorSpace = getColorSpace(token)?.split?.("");
492
+ if (colorSpace != null) {
493
+ for (const value of components) {
494
+ if (value.typ === EnumToken.IdenTokenType) {
495
+ const val = value.val.toLowerCase();
496
+ if (
497
+ // @ts-expect-error
498
+ typeof Math[val.toUpperCase()] !== "number" &&
499
+ val != "in" &&
500
+ val != "hue" &&
501
+ val != "from" &&
502
+ val != "alpha" &&
503
+ val != "none" &&
504
+ val != "shorter" &&
505
+ val != "longer" &&
506
+ val != "increasing" &&
507
+ val != "decreasing" &&
508
+ !colorsFunc.includes(val) &&
509
+ !colorSpace.includes(val) &&
510
+ !colorFuncColorSpace.includes(val)) {
511
+ errors?.push({
512
+ action: "drop",
513
+ message: `Unexpected constant '${val}'`,
514
+ node: value,
515
+ location: value.loc,
516
+ });
517
+ return false;
518
+ }
519
+ }
520
+ else if (value.typ === EnumToken.MathFunctionTokenType &&
521
+ equalsIgnoreCase("calc", value.val)) {
522
+ let val;
523
+ for (const v of walkValues(value.chi)) {
524
+ if (v.value.typ === EnumToken.IdenTokenType) {
525
+ val = v.value.val.toLowerCase();
526
+ if (
527
+ // @ts-expect-error
528
+ typeof Math[val.toUpperCase()] !== "number" &&
529
+ val != "in" &&
530
+ val != "hue" &&
531
+ val != "from" &&
532
+ val != "alpha" &&
533
+ val != "none" &&
534
+ val != "shorter" &&
535
+ val != "longer" &&
536
+ val != "increasing" &&
537
+ val != "decreasing" &&
538
+ !colorsFunc.includes(val) &&
539
+ !colorSpace.includes(val) &&
540
+ !colorFuncColorSpace.includes(val)) {
541
+ // console.debug({ val });
542
+ errors?.push({
543
+ action: "drop",
544
+ message: `Unexpected constant '${val}'`,
545
+ node: v.value,
546
+ location: v.value.loc,
547
+ });
548
+ return false;
549
+ }
550
+ }
551
+ }
552
+ }
553
+ }
554
+ }
555
+ }
330
556
  // @ts-ignore
331
557
  for (const { value, parent } of walkValues(token.chi, token, (value) => value.typ === EnumToken.WildCardFunctionTokenType
332
558
  ? WalkerOptionEnum.Ignore | WalkerOptionEnum.IgnoreChildren
@@ -481,42 +707,84 @@ function isColor(token, errors) {
481
707
  }
482
708
  return acc;
483
709
  }, [[]]);
484
- if (children.length == 3) {
485
- if (children[0].length > 4 ||
486
- children[0][0].typ != EnumToken.IdenTokenType ||
487
- "in" !== children[0][0].val?.toLowerCase?.() ||
488
- !isColorspace(children[0][1]) ||
489
- (children[0].length >= 3 && !isHueInterpolationMethod(children[0].slice(2))) ||
490
- children[1].length > 2 ||
491
- !isColor(children[1][0]) ||
492
- (children[1].length == 2 && !isPercentageToken(children[1][1])) ||
493
- children[2].length > 2 ||
494
- (children[2].length == 2 && !isPercentageToken(children[2][1])) ||
495
- !isColor(children[2][0])) {
710
+ if (children.length === 0 || children[0].length === 0) {
711
+ return false;
712
+ }
713
+ let j = 0;
714
+ let k = 0;
715
+ if (children[j][0].typ === EnumToken.IdenTokenType &&
716
+ equalsIgnoreCase("in", children[j][k].val)) {
717
+ k++;
718
+ if (children[j][k]?.typ === EnumToken.IdenTokenType) {
719
+ if (!isRectangularOrthogonalColorspace(children[j][k])) {
720
+ if (isPolarColorspace(children[j][k++])) {
721
+ if (k == children[j].length) ;
722
+ else if (children[j][k].typ !== EnumToken.IdenTokenType) {
723
+ return false;
724
+ }
725
+ else if (equalsIgnoreCase("hue", children[j][k].val)) {
726
+ k++;
727
+ }
728
+ else {
729
+ switch (children[j][k].val) {
730
+ case "increasing":
731
+ case "decreasing":
732
+ case "longer":
733
+ case "shorter":
734
+ k++;
735
+ break;
736
+ default:
737
+ return false;
738
+ }
739
+ if (children[j][k]?.typ !== EnumToken.IdenTokenType ||
740
+ !equalsIgnoreCase("hue", children[j][k].val)) {
741
+ return false;
742
+ }
743
+ k++;
744
+ }
745
+ }
746
+ else {
747
+ return false;
748
+ }
749
+ }
750
+ else {
751
+ k++;
752
+ }
753
+ }
754
+ else {
496
755
  return false;
497
756
  }
498
- if (children[1].length == 2) {
499
- if (!(children[1][1].typ == EnumToken.PercentageTokenType ||
500
- (children[1][1].typ == EnumToken.NumberTokenType &&
501
- children[1][1].val == 0))) {
502
- return false;
503
- }
757
+ if (k != children[j].length) {
758
+ return false;
759
+ }
760
+ j++;
761
+ }
762
+ while (j < children.length) {
763
+ if (children[j].length > 2) {
764
+ return false;
504
765
  }
505
- if (children[2].length == 2) {
506
- if (!(children[2][1].typ == EnumToken.PercentageTokenType ||
507
- (children[2][1].typ == EnumToken.NumberTokenType &&
508
- children[2][1].val == 0))) {
766
+ if (!isColor(children[j][0]) &&
767
+ !(children[j][0].typ == EnumToken.WildCardFunctionTokenType &&
768
+ equalsIgnoreCase("calc", children[j][0].val))) {
769
+ return false;
770
+ }
771
+ if (children[j][0].typ == EnumToken.WildCardFunctionTokenType) {
772
+ const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "calc()"), createValidationContext([children[j][0]]), {});
773
+ if (!result.success) {
509
774
  return false;
510
775
  }
511
776
  }
512
- return true;
777
+ if (children[j].length > 1 && !isPercentageToken(children[j][1])) {
778
+ return false;
779
+ }
780
+ j++;
513
781
  }
514
- return false;
782
+ return true;
515
783
  }
516
784
  else {
517
785
  const keywords = ["from", "none"];
518
786
  // @ts-ignore
519
- if (["rgb", "hsl", "hwb", "lab", "lch", "oklab", "oklch"].includes(token.val)) {
787
+ if (["rgb", "hsl", "hwb", "lab", "lch", "oklab", "oklch"].some((t) => equalsIgnoreCase(t, token.val))) {
520
788
  // @ts-ignore
521
789
  keywords.push("alpha", ...token.val.slice(-3).split(""));
522
790
  }
@@ -941,4 +1209,4 @@ function isValue(token) {
941
1209
  token.typ === EnumToken.TransformFunctionTokenType);
942
1210
  }
943
1211
 
944
- export { dimensionUnits, isAngle, isColor, isColorspace, isDigit, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isLetter, isNewLine, isNumber, isPercentage, isPercentageToken, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isValue, isWhiteSpace, parseColor, parseDimension, pseudoAliasMap, pseudoElements, renamedStandardProperties };
1212
+ export { dimensionUnits, isAngle, isColor, isColorspace, isDigit, isFlex, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isLetter, isNewLine, isNumber, isPercentage, isPercentageToken, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isValue, isWhiteSpace, parseColor, parseDimension, pseudoAliasMap, pseudoElements, reduceColorStops, reduceConicColorStops, reducegradientBackgroundPosition, renamedStandardProperties };
@@ -86,8 +86,8 @@ const getSyntaxRule = memoize((group, key) => {
86
86
  }
87
87
  }
88
88
  return {
89
- acceptAnyDeclarations: node.syntax.includes("<declaration-list>"),
90
- acceptAnyRules: node.syntax.includes("<group-rule-body>") ||
89
+ acceptAnyDeclaration: node.syntax.includes("<declaration-list>"),
90
+ acceptAnyRule: node.syntax.includes("<group-rule-body>") ||
91
91
  node.syntax.includes("<stylesheet>"),
92
92
  getPreludeRules: () => prelude,
93
93
  getBlockRules: () => (block == null || block.length === 0 ? null : block),