@tbela99/css-parser 1.6.3 → 1.6.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 (46) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +112 -8
  3. package/dist/{lib/data → data}/properties.json.js +296 -29
  4. package/dist/{lib/data → data}/validation.json.js +6 -0
  5. package/dist/index-umd-web.js +962 -744
  6. package/dist/index.cjs +961 -744
  7. package/dist/index.d.ts +24 -6
  8. package/dist/lib/ast/expand.js +1 -1
  9. package/dist/lib/ast/features/calc.js +16 -4
  10. package/dist/lib/ast/features/if.js +2 -2
  11. package/dist/lib/ast/features/inlinecssvariables.js +3 -3
  12. package/dist/lib/ast/features/prefix.js +2 -2
  13. package/dist/lib/ast/features/shorthand.js +8 -6
  14. package/dist/lib/ast/features/transform.js +2 -2
  15. package/dist/lib/ast/math/expression.js +24 -1
  16. package/dist/lib/ast/minify.js +3 -2
  17. package/dist/lib/ast/transform/utils.js +2 -2
  18. package/dist/lib/ast/walk.js +108 -90
  19. package/dist/lib/parser/declaration/list.js +29 -26
  20. package/dist/lib/parser/declaration/map.js +186 -54
  21. package/dist/lib/parser/parse.js +11 -9
  22. package/dist/lib/parser/source.js +2 -2
  23. package/dist/lib/parser/tokenize.js +99 -124
  24. package/dist/lib/parser/utils/config.js +1 -1
  25. package/dist/lib/parser/utils/declaration.js +0 -18
  26. package/dist/lib/parser/utils/hash.js +10 -5
  27. package/dist/lib/parser/utils/selector.js +1 -1
  28. package/dist/lib/{renderer → printer}/render.js +29 -53
  29. package/dist/lib/syntax/color/a98rgb.js +1 -1
  30. package/dist/lib/syntax/color/alpha.js +19 -20
  31. package/dist/lib/syntax/color/color-mix.js +15 -14
  32. package/dist/lib/syntax/color/hex.js +4 -1
  33. package/dist/lib/syntax/color/lch.js +0 -1
  34. package/dist/lib/syntax/color/oklch.js +0 -1
  35. package/dist/lib/syntax/color/relative-color.js +11 -11
  36. package/dist/lib/syntax/color/utils/components.js +0 -20
  37. package/dist/lib/syntax/color/xyzd50.js +0 -17
  38. package/dist/lib/syntax/constants.js +6 -1
  39. package/dist/lib/syntax/syntax.js +2 -2
  40. package/dist/lib/validation/config.js +5 -33
  41. package/dist/lib/validation/match.js +52 -181
  42. package/dist/node.js +3 -3
  43. package/dist/web.js +4 -3
  44. package/package.json +6 -5
  45. /package/dist/lib/{renderer → printer}/sourcemap/lib/codec.js +0 -0
  46. /package/dist/lib/{renderer → printer}/sourcemap/sourcemap.js +0 -0
@@ -2,15 +2,16 @@ import { eq } from '../utils/eq.js';
2
2
  import { getConfig } from '../utils/config.js';
3
3
  import { matchType } from '../utils/type.js';
4
4
  import { EnumToken } from '../../ast/types.js';
5
- import { renderValue } from '../../renderer/render.js';
5
+ import { renderValue } from '../../printer/render.js';
6
6
  import { parseString } from '../parse.js';
7
7
  import { PropertySet } from './set.js';
8
8
  import { PROPERTYNAME } from '../../syntax/constants.js';
9
+ import { cloneNode } from '../../ast/clone.js';
9
10
 
10
11
  const propertiesConfig = getConfig();
11
12
  class PropertyMap {
12
- config;
13
13
  declarations;
14
+ config;
14
15
  requiredCount;
15
16
  pattern;
16
17
  constructor(config) {
@@ -24,7 +25,7 @@ class PropertyMap {
24
25
  }
25
26
  add(declaration) {
26
27
  if (declaration.nam == this.config.shorthand) {
27
- this.declarations = new Map();
28
+ this.declarations.clear();
28
29
  this.declarations.set(declaration.nam, declaration);
29
30
  this.matchTypes(declaration);
30
31
  }
@@ -40,23 +41,17 @@ class PropertyMap {
40
41
  if (this.declarations.has(this.config.shorthand)) {
41
42
  const tokens = {};
42
43
  const values = [];
43
- // @ts-ignore
44
- this.declarations
45
- .get(this.config.shorthand)
46
- // @ts-ignore
47
- .val.slice()
48
- .reduce((acc, curr) => {
44
+ const val = [[]];
45
+ for (const curr of this.declarations.get(this.config.shorthand).val) { // @ts-ignore
49
46
  // @ts-ignore
50
47
  if (separator != null && separator.typ == curr.typ && separator.val == curr.val) {
51
- acc.push([]);
52
- return acc;
48
+ val.push([]);
49
+ continue;
53
50
  }
54
51
  // @ts-ignore
55
- acc.at(-1).push(curr);
56
- return acc;
57
- }, [[]])
58
- // @ts-ignore
59
- .reduce((acc, list, current) => {
52
+ val.at(-1).push(curr);
53
+ }
54
+ for (let current = 0; current < val.length; current++) {
60
55
  values.push(...this.pattern.reduce((acc, property) => {
61
56
  // let current: number = 0;
62
57
  const props = this.config.properties[property];
@@ -71,9 +66,7 @@ class PropertyMap {
71
66
  // @ts-ignore
72
67
  acc[i][PROPERTYNAME] == property ||
73
68
  matchType(acc[i], props)) {
74
- if ("prefix" in props &&
75
- props.previous != null &&
76
- !(props.previous in tokens)) {
69
+ if ("prefix" in props && props.previous != null && !(props.previous in tokens)) {
77
70
  return acc;
78
71
  }
79
72
  if (!(property in tokens)) {
@@ -138,9 +131,8 @@ class PropertyMap {
138
131
  }
139
132
  }
140
133
  return acc;
141
- }, list));
142
- return values;
143
- }, []);
134
+ }, val[current]));
135
+ }
144
136
  if (values.length == 0) {
145
137
  this.declarations = Object.entries(tokens).reduce((acc, curr) => {
146
138
  acc.set(curr[0], {
@@ -184,18 +176,73 @@ class PropertyMap {
184
176
  return this;
185
177
  }
186
178
  [Symbol.iterator]() {
179
+ const propertiesMapping = { ...this.config.properties };
180
+ const patterns = this.config.pattern.split(" ");
181
+ let hasMapping = false;
187
182
  let iterable;
188
183
  let requiredCount = 0;
189
184
  let property;
190
185
  let isShorthand = true;
191
- for (property of Object.keys(this.config.properties)) {
192
- if (this.config.properties[property].required) {
193
- if (!this.declarations.has(property)) {
186
+ let declarations = this.declarations;
187
+ for (const key of declarations.keys()) {
188
+ if (declarations.has(key) && declarations.get(key) instanceof PropertyMap) {
189
+ hasMapping = true;
190
+ break;
191
+ }
192
+ }
193
+ if (hasMapping) {
194
+ const mapped = {};
195
+ let key;
196
+ for (const value of declarations.values()) {
197
+ key = value.nam;
198
+ if (value instanceof PropertyMap) {
199
+ for (const [k, v] of value.declarations) {
200
+ if (k == value.config.shorthand) {
201
+ for (const [key, val] of Object.entries(value.config.properties)) {
202
+ mapped[key] = cloneNode(v);
203
+ for (let i = 0; i < v.val.length; i++) {
204
+ // @ts-ignore
205
+ if (key == v.val[i][PROPERTYNAME]) {
206
+ if (mapped[key].val.length > 0) {
207
+ mapped[key].val.push({
208
+ typ: EnumToken.WhitespaceTokenType,
209
+ });
210
+ }
211
+ mapped[key].val.push(v.val[i]);
212
+ }
213
+ }
214
+ if (mapped[key].val.length == 0) {
215
+ mapped[key].val.push(...parseString(value.config.properties[key].default[0]));
216
+ }
217
+ }
218
+ }
219
+ else {
220
+ mapped[k] = v;
221
+ }
222
+ }
223
+ }
224
+ else {
225
+ mapped[key] = value;
226
+ }
227
+ }
228
+ // console.error(mapped);
229
+ if (patterns.length === Object.keys(mapped).length) {
230
+ declarations = new Map();
231
+ for (const key of patterns) {
232
+ if (mapped[key] != null) {
233
+ declarations.set(key, mapped[key]);
234
+ }
235
+ }
236
+ }
237
+ }
238
+ for (property of Object.keys(propertiesMapping)) {
239
+ if (propertiesMapping[property].required) {
240
+ if (!declarations.has(property)) {
194
241
  isShorthand = false;
195
242
  break;
196
243
  }
197
244
  else {
198
- const val = (this.declarations.get(property));
245
+ const val = declarations.get(property);
199
246
  if (val instanceof PropertySet && !val.isShortHand()) {
200
247
  isShorthand = false;
201
248
  break;
@@ -207,10 +254,10 @@ class PropertyMap {
207
254
  }
208
255
  }
209
256
  if (requiredCount === 0) {
210
- requiredCount = this.declarations.size;
257
+ requiredCount = declarations.size;
211
258
  }
212
259
  if (!isShorthand || requiredCount < this.requiredCount) {
213
- if (isShorthand && this.declarations.has(this.config.shorthand)) {
260
+ if (isShorthand && declarations.has(this.config.shorthand)) {
214
261
  const cache = new Map();
215
262
  const removeDefaults = (declaration) => {
216
263
  let i;
@@ -285,8 +332,8 @@ class PropertyMap {
285
332
  }
286
333
  return declaration;
287
334
  };
288
- const values = [...this.declarations.values()].reduce((acc, curr) => {
289
- if (curr instanceof PropertySet) {
335
+ const values = [...declarations.values()].reduce((acc, curr) => {
336
+ if (curr instanceof PropertySet || curr instanceof PropertyMap) {
290
337
  for (const declaration of curr) {
291
338
  acc.push(declaration);
292
339
  }
@@ -326,13 +373,13 @@ class PropertyMap {
326
373
  }
327
374
  return (filtered.length > 0 ? filtered : values)[Symbol.iterator]();
328
375
  }
329
- for (const declaration of this.declarations.values()) {
330
- if (declaration instanceof PropertySet) {
376
+ for (const declaration of declarations.values()) {
377
+ if (declaration instanceof PropertySet || declaration instanceof PropertyMap) {
331
378
  continue;
332
379
  }
333
380
  const config = declaration.nam == this.config.shorthand
334
381
  ? this.config
335
- : (this.config.properties[declaration.nam] ?? this.config);
382
+ : (propertiesMapping[declaration.nam] ?? this.config);
336
383
  if (!("mapping" in config)) {
337
384
  continue;
338
385
  }
@@ -349,7 +396,7 @@ class PropertyMap {
349
396
  }
350
397
  }
351
398
  // @ts-ignore
352
- iterable = this.declarations.values();
399
+ iterable = declarations.values();
353
400
  }
354
401
  else {
355
402
  let count = 0;
@@ -363,19 +410,19 @@ class PropertyMap {
363
410
  : null;
364
411
  const tokens = {};
365
412
  // @ts-ignore
366
- Object.entries(this.config.properties).reduce((acc, curr) => {
367
- if (!this.declarations.has(curr[0])) {
413
+ Object.entries(propertiesMapping).reduce((acc, curr) => {
414
+ if (!declarations.has(curr[0])) {
368
415
  if (curr[1].required) {
369
416
  acc.push(curr[0]);
370
417
  }
371
418
  return acc;
372
419
  }
373
420
  let current = 0;
374
- const props = this.config.properties[curr[0]];
375
- const properties = (this.declarations.get(curr[0]));
376
- for (const declaration of [
377
- properties instanceof PropertySet ? [...properties][0] : properties,
378
- ]) {
421
+ const props = propertiesMapping[curr[0]];
422
+ const properties = (declarations.get(curr[0]));
423
+ for (const declaration of properties instanceof PropertySet || properties instanceof PropertyMap
424
+ ? [...properties].flat()
425
+ : [properties]) {
379
426
  // @ts-ignore
380
427
  for (const val of declaration.val) {
381
428
  // @ts-ignore
@@ -422,9 +469,45 @@ class PropertyMap {
422
469
  }
423
470
  return acc;
424
471
  }, []);
472
+ // grid-template
473
+ if (this.config.shorthand == "grid-template" && patterns.length == Object.keys(tokens).length) {
474
+ const k = tokens[patterns[0]][0].length;
475
+ const l = tokens[patterns[1]][0].length;
476
+ const j = k < l ? k : l;
477
+ const result = [];
478
+ let i;
479
+ for (i = 0; i < j; i++) {
480
+ if (i < k) {
481
+ result.push(tokens[patterns[0]][0][i]);
482
+ }
483
+ if (i < l) {
484
+ result.push(tokens[patterns[1]][0][i]);
485
+ }
486
+ }
487
+ if (j < k) {
488
+ result.push(...tokens[patterns[0]][0].slice(j));
489
+ }
490
+ if (j < l) {
491
+ result.push(...tokens[patterns[1]][0].slice(j));
492
+ }
493
+ const separator = this.config.properties[patterns[2]]?.prefix;
494
+ result.push(
495
+ // @ts-ignore
496
+ { ...separator, typ: EnumToken[separator.typ] }, ...tokens[patterns[2]][0].reduce((acc, curr) => {
497
+ if (acc.length > 0) {
498
+ acc.push({ typ: EnumToken.WhitespaceTokenType });
499
+ }
500
+ acc.push(curr);
501
+ return acc;
502
+ }, []));
503
+ const declaration = cloneNode(declarations.get(patterns[0]));
504
+ declaration.nam = this.config.shorthand;
505
+ declaration.val = result;
506
+ return [declaration][Symbol.iterator]();
507
+ }
425
508
  count++;
426
509
  if (!isShorthand ||
427
- Object.entries(this.config.properties).some((entry) => {
510
+ Object.entries(propertiesMapping).some((entry) => {
428
511
  // missing required property
429
512
  return entry[1].required && !(entry[0] in tokens);
430
513
  }) ||
@@ -433,27 +516,74 @@ class PropertyMap {
433
516
  // @ts-expect-error
434
517
  v.filter((t) => t.typ != EnumToken.CommentTokenType).length === count)) {
435
518
  // @ts-ignore
436
- iterable = this.declarations.values();
519
+ iterable = declarations.values();
520
+ }
521
+ else if (hasMapping) {
522
+ if (patterns.length == Object.keys(tokens).length) {
523
+ const declaration = cloneNode(declarations.get(patterns[0]));
524
+ declaration.nam = this.config.shorthand;
525
+ for (let i = 0; i < patterns.length; i++) {
526
+ if (declaration.val.length > 0) {
527
+ declaration.val.push(
528
+ // @ts-ignore
529
+ this.config.separator != null
530
+ ? {
531
+ ...this.config.separator,
532
+ typ: EnumToken[this.config.separator.typ],
533
+ }
534
+ : { typ: EnumToken.WhitespaceTokenType });
535
+ }
536
+ declaration.val.push(...tokens[patterns[i]].reduce((acc, curr) => {
537
+ if (acc.length > 0) {
538
+ acc.push({ typ: EnumToken.WhitespaceTokenType });
539
+ }
540
+ acc.push(...curr.reduce((acc, curr) => {
541
+ if (acc.length > 0) {
542
+ acc.push({ typ: EnumToken.WhitespaceTokenType });
543
+ }
544
+ acc.push(curr);
545
+ return acc;
546
+ }, []));
547
+ return acc;
548
+ }, []));
549
+ }
550
+ return [declaration][Symbol.iterator]();
551
+ }
552
+ iterable = declarations.values();
437
553
  }
438
554
  else {
439
555
  let values = Object.entries(tokens)
440
556
  .reduce((acc, curr) => {
441
- const props = this.config.properties[curr[0]];
557
+ const props = propertiesMapping[curr[0]];
442
558
  for (let i = 0; i < curr[1].length; i++) {
443
559
  if (acc.length == i) {
444
560
  acc.push([]);
445
561
  }
446
- let values = curr[1][i].reduce((acc, curr) => {
447
- if (acc.length > 0) {
448
- acc.push({ typ: EnumToken.WhitespaceTokenType });
449
- }
450
- acc.push(curr);
451
- return acc;
452
- }, []);
562
+ // if (acc[acc.length - 1].length > 0) {
563
+ // acc[acc.length - 1].push(
564
+ // // @ts-ignore
565
+ // this.config.separator != null ?
566
+ // {
567
+ // ...this.config.separator,
568
+ // typ: EnumToken[this.config.separator.typ as keyof typeof EnumToken],
569
+ // } : <Token>{ typ: EnumToken.WhitespaceTokenType },
570
+ // )
571
+ // }
572
+ let values = curr[1][i];
573
+ // .reduce(
574
+ // (acc, curr) => {
575
+ // // if (acc.length > 0) {
576
+ // // acc.push(<Token>{ typ: EnumToken.WhitespaceTokenType });
577
+ // // }
578
+ // acc.push(curr);
579
+ // return acc;
580
+ // },
581
+ // <Token[]>[],
582
+ // );
453
583
  if (props.default.includes(curr[1][i]
454
584
  .reduce((acc, curr) => acc + renderValue(curr) + " ", "")
455
585
  .trimEnd())) {
456
- if (!this.config.properties[curr[0]].required) {
586
+ if (!propertiesMapping[curr[0]].required) {
457
587
  continue;
458
588
  }
459
589
  }
@@ -467,7 +597,7 @@ class PropertyMap {
467
597
  EnumToken.IdenTokenType,
468
598
  ].includes(v.typ) ||
469
599
  (v.typ == EnumToken.IdenTokenType &&
470
- !this.config.properties[curr[0]].default.includes(v.val))) {
600
+ !propertiesMapping[curr[0]].default.includes(v.val))) {
471
601
  doFilterDefault = false;
472
602
  break;
473
603
  }
@@ -484,7 +614,7 @@ class PropertyMap {
484
614
  props.default.includes(val.val)));
485
615
  });
486
616
  if (filtered.length > 0 ||
487
- !(this.requiredCount == requiredCount && this.config.properties[curr[0]].required)) {
617
+ !(this.requiredCount == requiredCount && propertiesMapping[curr[0]].required)) {
488
618
  values = filtered;
489
619
  }
490
620
  if (values.length > 0) {
@@ -509,6 +639,7 @@ class PropertyMap {
509
639
  acc[i].push({ ...props.prefix, typ: EnumToken[props.prefix.typ] });
510
640
  }
511
641
  else if (acc[i].length > 0) {
642
+ // @ts-ignore
512
643
  acc[i].push({ typ: EnumToken.WhitespaceTokenType });
513
644
  }
514
645
  for (const v of values.reduce((acc, curr) => {
@@ -586,6 +717,7 @@ class PropertyMap {
586
717
  }
587
718
  }
588
719
  const iterators = [];
720
+ // @ts-ignore
589
721
  return {
590
722
  // @ts-ignore
591
723
  next() {
@@ -1,6 +1,6 @@
1
1
  import { isIdentColor, parseColor, isColor } from '../syntax/syntax.js';
2
2
  import { camelize, equalsIgnoreCase, dasherize } from './utils/text.js';
3
- import { renderValue } from '../renderer/render.js';
3
+ import { renderValue } from '../printer/render.js';
4
4
  import { EnumToken, EnumAstNodeStatus, ModuleCaseTransformEnum, ModuleScopeEnumOptions } from '../ast/types.js';
5
5
  import { minify } from '../ast/minify.js';
6
6
  import { expand } from '../ast/expand.js';
@@ -475,6 +475,7 @@ function doParseSync(tokenizer, options = {}) {
475
475
  if (options.expandNestingRules) {
476
476
  options.nestingRules = false;
477
477
  }
478
+ options.minifyAST ??= options.minify;
478
479
  const startTime = performance.now();
479
480
  const errors = [];
480
481
  const stack = [];
@@ -854,7 +855,7 @@ function doParseSync(tokenizer, options = {}) {
854
855
  }
855
856
  }
856
857
  }
857
- if (options.minify) {
858
+ if (options.minifyAST) {
858
859
  if (ast.chi.length > 0) {
859
860
  let passes = options.pass ?? 1;
860
861
  while (passes--) {
@@ -1394,6 +1395,7 @@ async function doParse(iter, options = {}) {
1394
1395
  lenient: true,
1395
1396
  ...options,
1396
1397
  };
1398
+ options.minifyAST ??= options.minify;
1397
1399
  if (typeof options.validation !== "boolean") {
1398
1400
  options.validation = !!options.validation;
1399
1401
  }
@@ -1848,7 +1850,7 @@ async function doParse(iter, options = {}) {
1848
1850
  }
1849
1851
  }
1850
1852
  }
1851
- if (options.minify) {
1853
+ if (options.minifyAST) {
1852
1854
  if (ast.chi.length > 0) {
1853
1855
  let passes = options.pass ?? 1;
1854
1856
  while (passes--) {
@@ -2760,7 +2762,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2760
2762
  atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
2761
2763
  // @ts-expect-error
2762
2764
  return Object.assign(atRule, {
2763
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2765
+ typ: EnumToken.AtRuleNodeType,
2764
2766
  val: renderTokens(trimArray(stream), options),
2765
2767
  });
2766
2768
  }
@@ -2768,12 +2770,12 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
2768
2770
  return null;
2769
2771
  }
2770
2772
  atRule[TOKENS] = stream;
2771
- atRule[STATE] = EnumAstNodeStatus.Validated;
2773
+ atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
2772
2774
  atRule[ERRORS] = [];
2773
2775
  atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
2774
2776
  // @ts-expect-error
2775
2777
  return Object.assign(atRule, {
2776
- typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
2778
+ typ: EnumToken.AtRuleNodeType,
2777
2779
  val: renderTokens(trimArray(stream), options),
2778
2780
  });
2779
2781
  }
@@ -3331,9 +3333,9 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
3331
3333
  let result = null;
3332
3334
  if (syntax == null) {
3333
3335
  // check matching '(' and ')'
3334
- // check commas , or ,,
3335
- // check colon :
3336
- // check or and and
3336
+ // check commas ',' or ',,'
3337
+ // check colon ':'
3338
+ // check 'or' and 'and'
3337
3339
  result = matchGenericSyntax(stream, options);
3338
3340
  if (result.errors.length > 0) {
3339
3341
  for (const error of result.errors) {
@@ -1,4 +1,4 @@
1
- import { SourceMap } from '../renderer/sourcemap/sourcemap.js';
1
+ import { SourceMap } from '../printer/sourcemap/sourcemap.js';
2
2
  import { LineMap } from './linesmap.js';
3
3
 
4
4
  /**
@@ -9,7 +9,6 @@ let sourceId = 0;
9
9
  * Source file helper class
10
10
  */
11
11
  class SourceFile {
12
- inputSourceMap = null;
13
12
  /**
14
13
  * Source file ID
15
14
  */
@@ -26,6 +25,7 @@ class SourceFile {
26
25
  * Source file content
27
26
  */
28
27
  content;
28
+ inputSourceMap = null;
29
29
  /**
30
30
  * Constructor
31
31
  * @param content