@tbela99/css-parser 1.2.0 → 1.3.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 (55) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +2 -2
  3. package/dist/index-umd-web.js +483 -849
  4. package/dist/index.cjs +483 -849
  5. package/dist/index.d.ts +107 -11
  6. package/dist/lib/ast/features/calc.js +1 -25
  7. package/dist/lib/ast/features/inlinecssvariables.js +0 -19
  8. package/dist/lib/ast/features/transform.js +2 -2
  9. package/dist/lib/ast/math/expression.js +26 -149
  10. package/dist/lib/ast/math/math.js +8 -8
  11. package/dist/lib/ast/transform/compute.js +4 -37
  12. package/dist/lib/ast/transform/matrix.js +33 -34
  13. package/dist/lib/ast/transform/minify.js +32 -51
  14. package/dist/lib/ast/transform/perspective.js +1 -1
  15. package/dist/lib/ast/transform/rotate.js +13 -13
  16. package/dist/lib/ast/transform/scale.js +8 -8
  17. package/dist/lib/ast/transform/skew.js +4 -4
  18. package/dist/lib/ast/transform/translate.js +8 -8
  19. package/dist/lib/ast/transform/utils.js +31 -39
  20. package/dist/lib/ast/types.js +91 -2
  21. package/dist/lib/parser/declaration/set.js +2 -2
  22. package/dist/lib/parser/parse.js +34 -12
  23. package/dist/lib/parser/tokenize.js +28 -40
  24. package/dist/lib/parser/utils/type.js +1 -1
  25. package/dist/lib/renderer/render.js +36 -27
  26. package/dist/lib/syntax/color/cmyk.js +2 -2
  27. package/dist/lib/syntax/color/color-mix.js +11 -12
  28. package/dist/lib/syntax/color/color.js +7 -7
  29. package/dist/lib/syntax/color/hsl.js +4 -4
  30. package/dist/lib/syntax/color/hwb.js +27 -8
  31. package/dist/lib/syntax/color/lab.js +4 -4
  32. package/dist/lib/syntax/color/lch.js +4 -4
  33. package/dist/lib/syntax/color/oklab.js +4 -4
  34. package/dist/lib/syntax/color/oklch.js +4 -4
  35. package/dist/lib/syntax/color/relativecolor.js +1 -1
  36. package/dist/lib/syntax/color/rgb.js +4 -4
  37. package/dist/lib/syntax/color/utils/components.js +15 -3
  38. package/dist/lib/syntax/color/utils/distance.js +11 -1
  39. package/dist/lib/syntax/syntax.js +8 -17
  40. package/dist/lib/syntax/utils.js +1 -1
  41. package/dist/lib/validation/at-rules/document.js +1 -1
  42. package/dist/lib/validation/at-rules/import.js +4 -4
  43. package/dist/lib/validation/at-rules/keyframes.js +0 -11
  44. package/dist/lib/validation/at-rules/supports.js +6 -6
  45. package/dist/lib/validation/config.js +0 -4
  46. package/dist/lib/validation/parser/parse.js +0 -8
  47. package/dist/lib/validation/selector.js +0 -9
  48. package/dist/lib/validation/syntax.js +14 -134
  49. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -11
  50. package/dist/lib/validation/syntaxes/family-name.js +0 -32
  51. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -11
  52. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -26
  53. package/dist/lib/validation/syntaxes/url.js +0 -33
  54. package/dist/lib/validation/utils/list.js +0 -8
  55. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -142,33 +142,117 @@ declare enum EnumToken {
142
142
  TimingFunction = 17,
143
143
  TimelineFunction = 16
144
144
  }
145
+ /**
146
+ * color types enum
147
+ */
145
148
  declare enum ColorType {
149
+ /**
150
+ * system colors
151
+ */
146
152
  SYS = 0,
153
+ /**
154
+ * deprecated system colors
155
+ */
147
156
  DPSYS = 1,
157
+ /**
158
+ * colors as literals
159
+ */
148
160
  LIT = 2,
161
+ /**
162
+ * colors as hex values
163
+ */
149
164
  HEX = 3,
165
+ /**
166
+ * colors as rgb values
167
+ */
150
168
  RGBA = 4,
169
+ /**
170
+ * colors as hsl values
171
+ */
151
172
  HSLA = 5,
173
+ /**
174
+ * colors as hwb values
175
+ */
152
176
  HWB = 6,
177
+ /**
178
+ * colors as cmyk values
179
+ */
153
180
  CMYK = 7,
181
+ /**
182
+ * colors as oklab values
183
+ * */
154
184
  OKLAB = 8,
185
+ /**
186
+ * colors as oklch values
187
+ * */
155
188
  OKLCH = 9,
189
+ /**
190
+ * colors as lab values
191
+ */
156
192
  LAB = 10,
193
+ /**
194
+ * colors as lch values
195
+ */
157
196
  LCH = 11,
197
+ /**
198
+ * colors using color() function
199
+ */
158
200
  COLOR = 12,
201
+ /**
202
+ * color using srgb values
203
+ */
159
204
  SRGB = 13,
205
+ /**
206
+ * color using prophoto-rgb values
207
+ */
160
208
  PROPHOTO_RGB = 14,
209
+ /**
210
+ * color using a98-rgb values
211
+ */
161
212
  A98_RGB = 15,
213
+ /**
214
+ * color using rec2020 values
215
+ */
162
216
  REC2020 = 16,
217
+ /**
218
+ * color using display-p3 values
219
+ */
163
220
  DISPLAY_P3 = 17,
221
+ /**
222
+ * color using srgb-linear values
223
+ */
164
224
  SRGB_LINEAR = 18,
225
+ /**
226
+ * color using xyz-d50 values
227
+ */
165
228
  XYZ_D50 = 19,
229
+ /**
230
+ * color using xyz-d65 values
231
+ */
166
232
  XYZ_D65 = 20,
233
+ /**
234
+ * light-dark() color function
235
+ */
167
236
  LIGHT_DARK = 21,
237
+ /**
238
+ * color-mix() color function
239
+ */
168
240
  COLOR_MIX = 22,
241
+ /**
242
+ * alias for rgba
243
+ */
169
244
  RGB = 4,
245
+ /**
246
+ * alias for hsl
247
+ */
170
248
  HSL = 5,
249
+ /**
250
+ * alias for xyz-d65
251
+ */
171
252
  XYZ = 20,
253
+ /**
254
+ * alias for cmyk
255
+ */
172
256
  DEVICE_CMYK = 7
173
257
  }
174
258
 
@@ -232,7 +316,18 @@ declare function renderToken(token: Token, options?: RenderOptions, cache?: {
232
316
  [key: string]: any;
233
317
  }, reducer?: (acc: string, curr: Token) => string, errors?: ErrorDescription[]): string;
234
318
 
319
+ /**
320
+ * Calculate the distance between two okLab colors.
321
+ * @param okLab1
322
+ * @param okLab2
323
+ */
235
324
  declare function okLabDistance(okLab1: [number, number, number], okLab2: [number, number, number]): number;
325
+ /**
326
+ * Check if two colors are close.
327
+ * @param color1
328
+ * @param color2
329
+ * @param threshold
330
+ */
236
331
  declare function isOkLabClose(color1: ColorToken, color2: ColorToken, threshold?: number): boolean;
237
332
 
238
333
  /**
@@ -315,7 +410,7 @@ export declare interface NestingSelectorToken extends BaseToken {
315
410
  export declare interface NumberToken extends BaseToken {
316
411
 
317
412
  typ: EnumToken.NumberTokenType,
318
- val: string | FractionToken;
413
+ val: number | FractionToken;
319
414
  }
320
415
 
321
416
  export declare interface AtRuleToken extends BaseToken {
@@ -328,13 +423,13 @@ export declare interface AtRuleToken extends BaseToken {
328
423
  export declare interface PercentageToken extends BaseToken {
329
424
 
330
425
  typ: EnumToken.PercentageTokenType,
331
- val: string | FractionToken;
426
+ val: number | FractionToken;
332
427
  }
333
428
 
334
429
  export declare interface FlexToken extends BaseToken {
335
430
 
336
431
  typ: EnumToken.FlexTokenType,
337
- val: string | FractionToken;
432
+ val: number | FractionToken;
338
433
  }
339
434
 
340
435
  export declare interface FunctionToken extends BaseToken {
@@ -400,42 +495,42 @@ export declare interface UnclosedStringToken extends BaseToken {
400
495
  export declare interface DimensionToken extends BaseToken {
401
496
 
402
497
  typ: EnumToken.DimensionTokenType;
403
- val: string | FractionToken;
498
+ val: number | FractionToken;
404
499
  unit: string;
405
500
  }
406
501
 
407
502
  export declare interface LengthToken extends BaseToken {
408
503
 
409
504
  typ: EnumToken.LengthTokenType;
410
- val: string | FractionToken;
505
+ val: number | FractionToken;
411
506
  unit: string;
412
507
  }
413
508
 
414
509
  export declare interface AngleToken extends BaseToken {
415
510
 
416
511
  typ: EnumToken.AngleTokenType;
417
- val: string | FractionToken;
512
+ val: number | FractionToken;
418
513
  unit: string;
419
514
  }
420
515
 
421
516
  export declare interface TimeToken extends BaseToken {
422
517
 
423
518
  typ: EnumToken.TimeTokenType;
424
- val: string | FractionToken;
519
+ val: number | FractionToken;
425
520
  unit: 'ms' | 's';
426
521
  }
427
522
 
428
523
  export declare interface FrequencyToken extends BaseToken {
429
524
 
430
525
  typ: EnumToken.FrequencyTokenType;
431
- val: string | FractionToken;
526
+ val: number | FractionToken;
432
527
  unit: 'Hz' | 'Khz';
433
528
  }
434
529
 
435
530
  export declare interface ResolutionToken extends BaseToken {
436
531
 
437
532
  typ: EnumToken.ResolutionTokenType;
438
- val: string | FractionToken;
533
+ val: number | FractionToken;
439
534
  unit: 'dpi' | 'dpcm' | 'dppx' | 'x';
440
535
  }
441
536
 
@@ -942,7 +1037,7 @@ interface ValidationToken {
942
1037
  }
943
1038
 
944
1039
  /**
945
- * Converts a color token to another color space
1040
+ * Converts a color to another color space
946
1041
  * @param token
947
1042
  * @param to
948
1043
  */
@@ -1159,7 +1254,8 @@ export declare interface ErrorDescription {
1159
1254
  // drop rule or declaration | fix rule or declaration
1160
1255
  action: 'drop' | 'ignore';
1161
1256
  message: string;
1162
- syntax?: string;
1257
+ syntax?: string | null;
1258
+ node?: Token | AstNode | null;
1163
1259
  location?: Location;
1164
1260
  error?: Error;
1165
1261
  rawTokens?: TokenizeResult[];
@@ -56,8 +56,6 @@ class ComputeCalcExpressionFeature {
56
56
  const slice = (node.typ == EnumToken.FunctionTokenType ? node.chi : (node.typ == EnumToken.DeclarationNodeType ? node.val : node.chi))?.slice();
57
57
  if (slice != null && node.typ == EnumToken.FunctionTokenType && mathFuncs.includes(node.val)) {
58
58
  // @ts-ignore
59
- const cp = (node.typ == EnumToken.FunctionTokenType && mathFuncs.includes(node.val) && node.val != 'calc' ? [node] : (node.typ == EnumToken.DeclarationNodeType ? node.val : node.chi)).slice();
60
- evaluate(cp);
61
59
  const key = 'chi' in node ? 'chi' : 'val';
62
60
  const str1 = renderToken({ ...node, [key]: slice });
63
61
  const str2 = renderToken(node); // values.reduce((acc: string, curr: Token): string => acc + renderToken(curr), '');
@@ -65,11 +63,6 @@ class ComputeCalcExpressionFeature {
65
63
  // @ts-ignore
66
64
  node[key] = slice;
67
65
  }
68
- // else {
69
- //
70
- // // @ts-ignore
71
- // node[key] = values;
72
- // }
73
66
  return WalkerOptionEnum.Ignore;
74
67
  }
75
68
  return null;
@@ -85,20 +78,9 @@ class ComputeCalcExpressionFeature {
85
78
  // @ts-ignore
86
79
  const children = parent.typ == EnumToken.DeclarationNodeType ? parent.val : parent.chi;
87
80
  if (values.length == 1 && values[0].typ != EnumToken.BinaryExpressionTokenType) {
88
- // if (parent.typ == EnumToken.BinaryExpressionTokenType) {
89
- //
90
- // if ((parent as BinaryExpressionToken).l == value) {
91
- //
92
- // (parent as BinaryExpressionToken).l = values[0];
93
- // } else {
94
- //
95
- // (parent as BinaryExpressionToken).r = values[0];
96
- // }
97
- // } else {
98
81
  for (let i = 0; i < children.length; i++) {
99
82
  if (children[i] == value) {
100
- // @ts-ignore
101
- children.splice(i, 1, !(parent.typ == EnumToken.FunctionTokenType && parent.val == 'calc') && typeof values[0].val != 'string' ? {
83
+ children.splice(i, 1, !(parent.typ == EnumToken.FunctionTokenType && parent.val == 'calc') && (typeof values[0].val != 'number' && !(values[0].typ == EnumToken.FunctionTokenType && mathFuncs.includes(values[0].val))) ? {
102
84
  typ: EnumToken.FunctionTokenType,
103
85
  val: 'calc',
104
86
  chi: values
@@ -106,21 +88,15 @@ class ComputeCalcExpressionFeature {
106
88
  break;
107
89
  }
108
90
  }
109
- // }
110
91
  }
111
92
  else {
112
93
  for (let i = 0; i < children.length; i++) {
113
94
  if (children[i] == value) {
114
- // if (parent.typ == EnumToken.FunctionTokenType && (parent as FunctionToken).val == 'calc') {
115
- //
116
- // children.splice(i, 1, ...values);
117
- // } else {
118
95
  children.splice(i, 1, {
119
96
  typ: EnumToken.FunctionTokenType,
120
97
  val: 'calc',
121
98
  chi: values
122
99
  });
123
- // }
124
100
  break;
125
101
  }
126
102
  }
@@ -106,25 +106,6 @@ class InlineCssVariablesFeature {
106
106
  replace(node, variableScope);
107
107
  }
108
108
  }
109
- // else {
110
- //
111
- // const info: VariableScopeInfo = <VariableScopeInfo>variableScope.get((<AstDeclaration>node).nam);
112
- //
113
- // info.globalScope = isRoot;
114
- //
115
- // if (!isRoot) {
116
- //
117
- // ++info.declarationCount;
118
- // }
119
- //
120
- // if (info.replaceable) {
121
- //
122
- // info.replaceable = isRoot && info.declarationCount == 1;
123
- // }
124
- //
125
- // info.parent.add(ast);
126
- // info.node = (<AstDeclaration>node);
127
- // }
128
109
  }
129
110
  else {
130
111
  replace(node, variableScope);
@@ -43,11 +43,11 @@ class TransformCssFeature {
43
43
  const children = node.val.reduce((acc, curr) => {
44
44
  if (curr.typ == EnumToken.FunctionTokenType && 'skew' == curr.val.toLowerCase()) {
45
45
  if (curr.chi.length == 3) {
46
- if (curr.chi[2].val == '0') {
46
+ if (curr.chi[2].val == 0) {
47
47
  curr.chi.length = 1;
48
48
  curr.val = 'skew';
49
49
  }
50
- else if (curr.chi[0].val == '0') {
50
+ else if (curr.chi[0].val == 0) {
51
51
  curr.chi = [curr.chi[2]];
52
52
  curr.val = 'skewY';
53
53
  }