@tbela99/css-parser 1.1.1 → 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 (107) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +5503 -5037
  4. package/dist/index.cjs +5510 -5044
  5. package/dist/index.d.ts +148 -41
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +14 -36
  8. package/dist/lib/ast/features/inlinecssvariables.js +6 -17
  9. package/dist/lib/ast/features/prefix.js +22 -19
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/features/transform.js +17 -2
  12. package/dist/lib/ast/math/expression.js +74 -172
  13. package/dist/lib/ast/math/math.js +24 -22
  14. package/dist/lib/ast/minify.js +249 -199
  15. package/dist/lib/ast/transform/compute.js +18 -41
  16. package/dist/lib/ast/transform/matrix.js +36 -36
  17. package/dist/lib/ast/transform/minify.js +37 -59
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +13 -13
  20. package/dist/lib/ast/transform/scale.js +8 -8
  21. package/dist/lib/ast/transform/skew.js +4 -4
  22. package/dist/lib/ast/transform/translate.js +8 -8
  23. package/dist/lib/ast/transform/utils.js +80 -28
  24. package/dist/lib/ast/types.js +122 -2
  25. package/dist/lib/fs/resolve.js +1 -14
  26. package/dist/lib/parser/declaration/list.js +1 -1
  27. package/dist/lib/parser/declaration/map.js +1 -1
  28. package/dist/lib/parser/declaration/set.js +3 -3
  29. package/dist/lib/parser/parse.js +53 -107
  30. package/dist/lib/parser/tokenize.js +29 -53
  31. package/dist/lib/parser/utils/declaration.js +1 -1
  32. package/dist/lib/parser/utils/type.js +2 -2
  33. package/dist/lib/renderer/render.js +79 -194
  34. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  35. package/dist/lib/syntax/color/cmyk.js +104 -0
  36. package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
  37. package/dist/lib/syntax/color/color.js +581 -0
  38. package/dist/lib/syntax/color/hex.js +179 -0
  39. package/dist/lib/syntax/color/hsl.js +201 -0
  40. package/dist/lib/syntax/color/hwb.js +204 -0
  41. package/dist/lib/syntax/color/lab.js +262 -0
  42. package/dist/lib/syntax/color/lch.js +194 -0
  43. package/dist/lib/syntax/color/oklab.js +237 -0
  44. package/dist/lib/syntax/color/oklch.js +166 -0
  45. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  46. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  47. package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
  48. package/dist/lib/syntax/color/rgb.js +140 -0
  49. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  50. package/dist/lib/syntax/color/utils/components.js +46 -0
  51. package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
  52. package/dist/lib/syntax/color/utils/distance.js +40 -0
  53. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  54. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  55. package/dist/lib/syntax/syntax.js +78 -77
  56. package/dist/lib/syntax/utils.js +70 -0
  57. package/dist/lib/validation/at-rules/container.js +1 -1
  58. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  59. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  60. package/dist/lib/validation/at-rules/document.js +2 -2
  61. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  62. package/dist/lib/validation/at-rules/import.js +5 -5
  63. package/dist/lib/validation/at-rules/keyframes.js +3 -13
  64. package/dist/lib/validation/at-rules/layer.js +1 -1
  65. package/dist/lib/validation/at-rules/media.js +1 -1
  66. package/dist/lib/validation/at-rules/namespace.js +1 -1
  67. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  68. package/dist/lib/validation/at-rules/page.js +1 -1
  69. package/dist/lib/validation/at-rules/supports.js +7 -7
  70. package/dist/lib/validation/at-rules/when.js +1 -1
  71. package/dist/lib/validation/atrule.js +2 -2
  72. package/dist/lib/validation/config.js +0 -3
  73. package/dist/lib/validation/config.json.js +1 -1
  74. package/dist/lib/validation/parser/parse.js +8 -11
  75. package/dist/lib/validation/selector.js +1 -9
  76. package/dist/lib/validation/syntax.js +67 -137
  77. package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
  78. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  79. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  80. package/dist/lib/validation/syntaxes/family-name.js +5 -40
  81. package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
  82. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  83. package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
  84. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  85. package/dist/lib/validation/syntaxes/url.js +3 -34
  86. package/dist/lib/validation/utils/list.js +2 -9
  87. package/dist/lib/validation/utils/whitespace.js +1 -1
  88. package/dist/node/index.js +4 -2
  89. package/dist/web/index.js +4 -2
  90. package/package.json +4 -4
  91. package/.editorconfig +0 -484
  92. package/dist/lib/ast/transform/convert.js +0 -33
  93. package/dist/lib/ast/utils/utils.js +0 -104
  94. package/dist/lib/renderer/color/color.js +0 -654
  95. package/dist/lib/renderer/color/hex.js +0 -105
  96. package/dist/lib/renderer/color/hsl.js +0 -125
  97. package/dist/lib/renderer/color/hwb.js +0 -103
  98. package/dist/lib/renderer/color/lab.js +0 -148
  99. package/dist/lib/renderer/color/lch.js +0 -90
  100. package/dist/lib/renderer/color/oklab.js +0 -131
  101. package/dist/lib/renderer/color/oklch.js +0 -75
  102. package/dist/lib/renderer/color/rgb.js +0 -50
  103. package/dist/lib/renderer/color/utils/components.js +0 -34
  104. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  105. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  106. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  107. /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
@@ -3,11 +3,10 @@ import '../parser/tokenize.js';
3
3
  import '../parser/utils/config.js';
4
4
  import { EnumToken } from './types.js';
5
5
  import { walkValues } from './walk.js';
6
- import { replaceCompound } from './expand.js';
6
+ import { doRender, renderToken } from '../renderer/render.js';
7
+ import '../syntax/color/utils/constants.js';
7
8
  import { isWhiteSpace, isIdent, isFunction, isIdentStart } from '../syntax/syntax.js';
8
9
  import { eq } from '../parser/utils/eq.js';
9
- import { doRender, renderToken } from '../renderer/render.js';
10
- import '../renderer/color/utils/constants.js';
11
10
  import * as index from './features/index.js';
12
11
  import { FeatureWalkMode } from './features/type.js';
13
12
 
@@ -124,22 +123,25 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
124
123
  }
125
124
  function reduce(acc, curr, index, array) {
126
125
  // trim :is()
127
- if (array.length == 1 && array[0][0] == ':is(' && array[0].at(-1) == ')') {
128
- curr = curr.slice(1, -1);
129
- }
126
+ // if (array.length == 1 && array[0][0] == ':is(' && array[0].at(-1) == ')') {
127
+ //
128
+ // curr = curr.slice(1, -1);
129
+ // }
130
130
  if (curr[0] == '&') {
131
131
  if (curr[1] == ' ' && !isIdent(curr[2]) && !isFunction(curr[2])) {
132
132
  curr.splice(0, 2);
133
133
  }
134
- else if (combinators.includes(curr[1])) {
135
- curr.shift();
136
- }
137
- }
138
- else { // @ts-ignore
139
- if (this.typ == EnumToken.RuleNodeType && (isIdent(curr[0]) || isFunction(curr[0]))) {
140
- curr.unshift('&', ' ');
141
- }
134
+ // else if (combinators.includes(curr[1])) {
135
+ //
136
+ // curr.shift();
137
+ // }
142
138
  }
139
+ // else { // @ts-ignore
140
+ // if (this.typ == EnumToken.RuleNodeType && (isIdent(curr[0]) || isFunction(curr[0]))) {
141
+ //
142
+ // curr.unshift('&', ' ');
143
+ // }
144
+ // }
143
145
  acc.push(curr.join(''));
144
146
  return acc;
145
147
  }
@@ -170,11 +172,12 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
170
172
  // @ts-ignore
171
173
  node = ast.chi[i];
172
174
  // @ts-ignore
173
- if (previous == node) {
174
- // @ts-ignore
175
- ast.chi.splice(i--, 1);
176
- continue;
177
- }
175
+ // if (previous == node) {
176
+ //
177
+ // // @ts-ignore
178
+ // ast.chi.splice(i--, 1);
179
+ // continue;
180
+ // }
178
181
  if (node.typ == EnumToken.AtRuleNodeType && node.nam == 'font-face') {
179
182
  continue;
180
183
  }
@@ -191,7 +194,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
191
194
  doMinify(node, options, true, errors, nestingContent, context);
192
195
  }
193
196
  }
194
- if (node.typ == EnumToken.KeyFrameRuleNodeType) {
197
+ else if (node.typ == EnumToken.KeyFrameRuleNodeType) {
195
198
  if (previous?.typ == EnumToken.KeyFrameRuleNodeType &&
196
199
  node.sel == previous.sel) {
197
200
  previous.chi.push(...node.chi);
@@ -216,7 +219,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
216
219
  }
217
220
  }
218
221
  }
219
- if (node.typ == EnumToken.AtRuleNodeType) {
222
+ else if (node.typ == EnumToken.AtRuleNodeType) {
220
223
  // @ts-ignore
221
224
  if (node.nam == 'media' && ['all', '', null].includes(node.val)) {
222
225
  // @ts-ignore
@@ -244,7 +247,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
244
247
  continue;
245
248
  }
246
249
  // @ts-ignore
247
- if (node.typ == EnumToken.RuleNodeType) {
250
+ else if (node.typ == EnumToken.RuleNodeType) {
248
251
  reduceRuleSelector(node);
249
252
  let wrapper;
250
253
  let match;
@@ -255,7 +258,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
255
258
  // @ts-ignore
256
259
  reduceRuleSelector(previous);
257
260
  // @ts-ignore
258
- match = matchSelectors(previous.raw, node.raw, ast.typ, errors);
261
+ match = matchSelectors(previous.raw, node.raw, ast.typ);
259
262
  // @ts-ignore
260
263
  if (match != null) {
261
264
  // @ts-ignore
@@ -277,7 +280,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
277
280
  }
278
281
  reduceRuleSelector(nextNode);
279
282
  // @ts-ignore
280
- match = matchSelectors(wrapper.raw, nextNode.raw, ast.typ, errors);
283
+ match = matchSelectors(wrapper.raw, nextNode.raw, ast.typ);
281
284
  // @ts-ignore
282
285
  if (match == null) {
283
286
  break;
@@ -326,12 +329,12 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
326
329
  curr.splice(0, 2);
327
330
  }
328
331
  else {
329
- if (ast.typ != EnumToken.RuleNodeType && combinators.includes(curr[1])) {
330
- wrap = false;
331
- }
332
- else {
333
- curr.splice(0, 1);
334
- }
332
+ // if (ast.typ != EnumToken.RuleNodeType && combinators.includes(curr[1])) {
333
+ //
334
+ // wrap = false;
335
+ // } else {
336
+ curr.splice(0, 1);
337
+ // }
335
338
  }
336
339
  }
337
340
  else if (combinators.includes(curr[0])) {
@@ -345,18 +348,39 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
345
348
  if (!wrap) {
346
349
  wrap = selector.some((s) => s[0] != '&');
347
350
  }
348
- let rule = selector.map(s => {
349
- if (s[0] == '&') {
350
- s.splice(0, 1, ...node.optimized.optimized);
351
+ let rule = null;
352
+ const optimized = node.optimized.optimized.slice();
353
+ if (optimized.length > 1) {
354
+ const check = optimized.at(-2);
355
+ if (!combinators.includes(check)) {
356
+ let last = optimized.pop();
357
+ wrap = false;
358
+ rule = optimized.join('') + `:is(${selector.map(s => {
359
+ if (s[0] == '&') {
360
+ s.splice(0, 1, last);
361
+ }
362
+ else {
363
+ s.unshift(last);
364
+ }
365
+ return s.join('');
366
+ }).join(',')})`;
351
367
  }
352
- return s.join('');
353
- }).join(',');
368
+ }
369
+ if (rule == null) {
370
+ rule = selector.map(s => {
371
+ if (s[0] == '&') {
372
+ s.splice(0, 1, ...node.optimized.optimized);
373
+ }
374
+ return s.join('');
375
+ }).join(',');
376
+ }
354
377
  // @ts-ignore
355
378
  let sel = wrap ? node.optimized.optimized.join('') + `:is(${rule})` : rule;
356
- if (rule.includes('&')) {
357
- // @ts-ignore
358
- rule = replaceCompound(rule, node.optimized.optimized[0]);
359
- }
379
+ // if (rule.includes('&')) {
380
+ //
381
+ // // @ts-ignore
382
+ // rule = replaceCompound(rule, node.optimized.optimized[0]);
383
+ // }
360
384
  if (sel.length < node.sel.length) {
361
385
  node.sel = sel;
362
386
  }
@@ -390,12 +414,12 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
390
414
  // @ts-ignore
391
415
  ast.chi.splice(nodeIndex, 1);
392
416
  // @ts-ignore
393
- if (!hasDeclaration(node)) {
394
- // @ts-ignore
395
- // minifyRule(node, <MinifyOptions>options, ast, context);
396
- // } else {
397
- doMinify(node, options, recursive, errors, nestingContent, context);
398
- }
417
+ // if (!hasDeclaration(node)) {
418
+ // // @ts-ignore
419
+ // // minifyRule(node, <MinifyOptions>options, ast, context);
420
+ // // } else {
421
+ // doMinify(node, options, recursive, errors, nestingContent, context);
422
+ // }
399
423
  i--;
400
424
  previous = node;
401
425
  nodeIndex = i;
@@ -453,17 +477,21 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
453
477
  }
454
478
  }
455
479
  }
456
- else {
457
- if ('chi' in previous) {
458
- // @ts-ignore
459
- if (!hasDeclaration(previous)) {
460
- // @ts-ignore
461
- // minifyRule(previous, <MinifyOptions>options, ast, context);
462
- // } else {
463
- doMinify(previous, options, recursive, errors, nestingContent, context);
464
- }
465
- }
466
- }
480
+ // else {
481
+ //
482
+ // if ('chi' in previous) {
483
+ //
484
+ // // @ts-ignore
485
+ // if (!hasDeclaration(previous)) {
486
+ //
487
+ // // @ts-ignore
488
+ // // minifyRule(previous, <MinifyOptions>options, ast, context);
489
+ // // } else {
490
+ //
491
+ // doMinify(previous, options, recursive, errors, nestingContent, context);
492
+ // }
493
+ // }
494
+ // }
467
495
  }
468
496
  // else if ('chi' in node) {
469
497
  //
@@ -513,10 +541,14 @@ function hasDeclaration(node) {
513
541
  }
514
542
  return true;
515
543
  }
516
- function reduceSelector(selector) {
517
- if (selector.length == 0) {
518
- return null;
519
- }
544
+ /**
545
+ * optimize selector
546
+ * @param selector
547
+ */
548
+ function optimizeSelector(selector) {
549
+ // if (selector.length == 0) {
550
+ // return null;
551
+ // }
520
552
  selector = selector.reduce((acc, curr) => {
521
553
  // @ts-ignore
522
554
  if (curr.length > 0 && curr.at(-1).startsWith(':is(')) {
@@ -565,10 +597,10 @@ function reduceSelector(selector) {
565
597
  }
566
598
  selector.forEach((selector) => selector.splice(0, optimized.length));
567
599
  // combinator
568
- if (combinators.includes(optimized.at(-1))) {
569
- const combinator = optimized.pop();
570
- selector.forEach((selector) => selector.unshift(combinator));
571
- }
600
+ // if (combinators.includes(<string>optimized.at(-1))) {
601
+ // const combinator: string = <string>optimized.pop();
602
+ // selector.forEach((selector: string[]) => selector.unshift(combinator));
603
+ // }
572
604
  let reducible = optimized.length == 1;
573
605
  if (optimized[0] == '&') {
574
606
  if (optimized[1] == ' ') {
@@ -623,14 +655,17 @@ function splitRule(buffer) {
623
655
  for (let i = 0; i < buffer.length; i++) {
624
656
  let chr = buffer.charAt(i);
625
657
  if (isWhiteSpace(chr.charCodeAt(0))) {
626
- let k = i;
627
- while (k + 1 < buffer.length) {
628
- if (isWhiteSpace(buffer[k + 1].charCodeAt(0))) {
629
- k++;
630
- continue;
631
- }
632
- break;
633
- }
658
+ // let k: number = i;
659
+ // while (k + 1 < buffer.length) {
660
+ //
661
+ // if (isWhiteSpace(buffer[k + 1].charCodeAt(0))) {
662
+ //
663
+ // k++;
664
+ // continue;
665
+ // }
666
+ //
667
+ // break;
668
+ // }
634
669
  if (str !== '') {
635
670
  // @ts-ignore
636
671
  result.at(-1).push(str);
@@ -641,7 +676,7 @@ function splitRule(buffer) {
641
676
  // @ts-ignore
642
677
  result.at(-1).push(' ');
643
678
  }
644
- i = k;
679
+ // i = k;
645
680
  continue;
646
681
  }
647
682
  if (chr == ',') {
@@ -692,21 +727,22 @@ function splitRule(buffer) {
692
727
  str += buffer.charAt(++i);
693
728
  continue;
694
729
  }
695
- if (chr == '"' || chr == "'") {
696
- let k = i;
697
- while (++k < buffer.length) {
698
- chr = buffer.charAt(k);
699
- str += chr;
700
- if (chr == '//') {
701
- str += buffer.charAt(++k);
702
- continue;
703
- }
704
- if (chr == buffer.charAt(i)) {
705
- break;
706
- }
707
- }
708
- continue;
709
- }
730
+ // if (chr == '"' || chr == "'") {
731
+ //
732
+ // let k = i;
733
+ // while (++k < buffer.length) {
734
+ // chr = buffer.charAt(k);
735
+ // str += chr;
736
+ // if (chr == '//') {
737
+ // str += buffer.charAt(++k);
738
+ // continue;
739
+ // }
740
+ // if (chr == buffer.charAt(i)) {
741
+ // break;
742
+ // }
743
+ // }
744
+ // continue;
745
+ // }
710
746
  if (chr == '(' || chr == '[') {
711
747
  const open = chr;
712
748
  const close = chr == '(' ? ')' : ']';
@@ -739,6 +775,79 @@ function splitRule(buffer) {
739
775
  }
740
776
  return result;
741
777
  }
778
+ function reduceSelector(acc, curr) {
779
+ // if (acc === null) {
780
+ //
781
+ // return null;
782
+ // }
783
+ let hasCompoundSelector = true;
784
+ // @ts-ignore
785
+ curr = curr.slice(this.match[0].length);
786
+ while (curr.length > 0) {
787
+ if (curr[0] == ' ') {
788
+ hasCompoundSelector = false;
789
+ curr.unshift('&');
790
+ continue;
791
+ }
792
+ break;
793
+ }
794
+ // invalid function match
795
+ // if (curr.length > 0 && curr[0].endsWith('(') && curr.at(-1) != ')') {
796
+ //
797
+ // return null;
798
+ // }
799
+ //
800
+ // if (curr.length == 1 && combinators.includes(curr[0].charAt(0))) {
801
+ //
802
+ // return null;
803
+ // }
804
+ if (hasCompoundSelector && curr.length > 0) {
805
+ hasCompoundSelector = !['&'].concat(combinators).includes(curr[0].charAt(0));
806
+ }
807
+ if (curr[0] == ':is(') {
808
+ let canReduce = true;
809
+ const isCompound = curr.reduce((acc, token, index) => {
810
+ if (index == 0) {
811
+ canReduce = curr[1] == '&';
812
+ }
813
+ // else if (token.endsWith('(')) {
814
+ //
815
+ // if (inFunction == 0) {
816
+ //
817
+ // canReduce = false;
818
+ // }
819
+ //
820
+ // inFunction++;
821
+ // }
822
+ else if (token == ')') ;
823
+ else if (token == ',') {
824
+ if (!canReduce) {
825
+ canReduce = curr[index + 1] == '&';
826
+ }
827
+ acc.push([]);
828
+ }
829
+ else
830
+ acc.at(-1)?.push(token);
831
+ return acc;
832
+ }, [[]]);
833
+ // if (inFunction > 0) {
834
+ //
835
+ // canReduce = false;
836
+ // }
837
+ if (canReduce) {
838
+ curr = isCompound.reduce((acc, curr) => {
839
+ if (acc.length > 0) {
840
+ acc.push(',');
841
+ }
842
+ acc.push(...curr);
843
+ return acc;
844
+ }, []);
845
+ }
846
+ }
847
+ // @ts-ignore
848
+ acc.push(this.match.length == 0 ? ['&'] : (hasCompoundSelector && curr[0] != '&' && (curr.length == 0 || !combinators.includes(curr[0].charAt(0))) ? ['&'].concat(curr) : curr));
849
+ return acc;
850
+ }
742
851
  function matchSelectors(selector1, selector2, parentType, errors) {
743
852
  let match = [[]];
744
853
  const j = Math.min(selector1.reduce((acc, curr) => Math.min(acc, curr.length), selector1.length > 0 ? selector1[0].length : 0), selector2.reduce((acc, curr) => Math.min(acc, curr.length), selector2.length > 0 ? selector2[0].length : 0));
@@ -770,47 +879,52 @@ function matchSelectors(selector1, selector2, parentType, errors) {
770
879
  if (!matching) {
771
880
  break;
772
881
  }
773
- if (token == ',') {
774
- match.push([]);
775
- }
776
- else {
777
- if (token.endsWith('(')) {
778
- matchFunction++;
779
- }
780
- if (token.endsWith('[')) {
781
- inAttr++;
782
- }
783
- else if (token == ')') {
784
- matchFunction--;
785
- }
786
- else if (token == ']') {
787
- inAttr--;
788
- }
789
- match.at(-1).push(token);
882
+ // if (token == ',') {
883
+ //
884
+ // match.push([]);
885
+ // } else {
886
+ if (token.endsWith('(')) {
887
+ matchFunction++;
790
888
  }
889
+ // if (token.endsWith('[')) {
890
+ //
891
+ // inAttr++;
892
+ // } else if (token == ')') {
893
+ //
894
+ // matchFunction--;
895
+ // } else if (token == ']') {
896
+ //
897
+ // inAttr--;
898
+ // }
899
+ match.at(-1).push(token);
900
+ // }
791
901
  }
792
902
  // invalid function
793
903
  if (matchFunction != 0 || inAttr != 0) {
794
904
  return null;
795
905
  }
796
- if (parentType != EnumToken.RuleNodeType) {
797
- for (const part of match) {
798
- if (part.length > 0 && combinators.includes(part[0].charAt(0))) {
799
- return null;
800
- }
801
- }
802
- }
803
- if (match.length > 1) {
804
- errors?.push({
805
- action: 'ignore',
806
- message: `minify: unsupported multilevel matching\n${JSON.stringify({
807
- match,
808
- selector1,
809
- selector2
810
- }, null, 1)}`
811
- });
812
- return null;
813
- }
906
+ // if (parentType != EnumToken.RuleNodeType) {
907
+ //
908
+ // for (const part of match) {
909
+ //
910
+ // if (part.length > 0 && combinators.includes(part[0].charAt(0))) {
911
+ //
912
+ // return null;
913
+ // }
914
+ // }
915
+ // }
916
+ // if (match.length > 1) {
917
+ //
918
+ // errors?.push({
919
+ // action: 'ignore',
920
+ // message: `minify: unsupported multilevel matching\n${JSON.stringify({
921
+ // match,
922
+ // selector1,
923
+ // selector2
924
+ // }, null, 1)}`
925
+ // });
926
+ // return null;
927
+ // }
814
928
  for (const part of match) {
815
929
  while (part.length > 0) {
816
930
  const token = part.at(-1);
@@ -827,77 +941,11 @@ function matchSelectors(selector1, selector2, parentType, errors) {
827
941
  if (eq([['&']], match)) {
828
942
  return null;
829
943
  }
830
- function reduce(acc, curr) {
831
- if (acc === null) {
832
- return null;
833
- }
834
- let hasCompoundSelector = true;
835
- curr = curr.slice(match[0].length);
836
- while (curr.length > 0) {
837
- if (curr[0] == ' ') {
838
- hasCompoundSelector = false;
839
- curr.unshift('&');
840
- continue;
841
- }
842
- break;
843
- }
844
- // invalid function match
845
- if (curr.length > 0 && curr[0].endsWith('(') && curr.at(-1) != ')') {
846
- return null;
847
- }
848
- if (curr.length == 1 && combinators.includes(curr[0].charAt(0))) {
849
- return null;
850
- }
851
- if (hasCompoundSelector && curr.length > 0) {
852
- hasCompoundSelector = !['&'].concat(combinators).includes(curr[0].charAt(0));
853
- }
854
- if (curr[0] == ':is(') {
855
- let inFunction = 0;
856
- let canReduce = true;
857
- const isCompound = curr.reduce((acc, token, index) => {
858
- if (index == 0) {
859
- inFunction++;
860
- canReduce = curr[1] == '&';
861
- }
862
- else if (token.endsWith('(')) {
863
- if (inFunction == 0) {
864
- canReduce = false;
865
- }
866
- inFunction++;
867
- }
868
- else if (token == ')') {
869
- inFunction--;
870
- }
871
- else if (token == ',') {
872
- if (!canReduce) {
873
- canReduce = curr[index + 1] == '&';
874
- }
875
- acc.push([]);
876
- }
877
- else
878
- acc.at(-1)?.push(token);
879
- return acc;
880
- }, [[]]);
881
- if (inFunction > 0) {
882
- canReduce = false;
883
- }
884
- if (canReduce) {
885
- curr = isCompound.reduce((acc, curr) => {
886
- if (acc.length > 0) {
887
- acc.push(',');
888
- }
889
- acc.push(...curr);
890
- return acc;
891
- }, []);
892
- }
893
- }
894
- acc.push(match.length == 0 ? ['&'] : (hasCompoundSelector && curr[0] != '&' && (curr.length == 0 || !combinators.includes(curr[0].charAt(0))) ? ['&'].concat(curr) : curr));
895
- return acc;
896
- }
944
+ const reducer = reduceSelector.bind({ match });
897
945
  // @ts-ignore
898
- selector1 = selector1.reduce(reduce, []);
946
+ selector1 = selector1.reduce(reducer, []);
899
947
  // @ts-ignore
900
- selector2 = selector2.reduce(reduce, []);
948
+ selector2 = selector2.reduce(reducer, []);
901
949
  return selector1 == null || selector2 == null ? null : {
902
950
  eq: eq(selector1, selector2),
903
951
  match,
@@ -982,9 +1030,10 @@ function diff(n1, n2, reducer, options = {}) {
982
1030
  }
983
1031
  let i = node1.chi.length;
984
1032
  let j = node2.chi.length;
985
- if (i == 0 || j == 0) {
986
- return null;
987
- }
1033
+ // if (i == 0 || j == 0) {
1034
+ //
1035
+ // return null;
1036
+ // }
988
1037
  const raw1 = node1.raw;
989
1038
  const raw2 = node2.raw;
990
1039
  if (raw1 != null && raw2 != null) {
@@ -1055,9 +1104,10 @@ function diff(n1, n2, reducer, options = {}) {
1055
1104
  continue;
1056
1105
  }
1057
1106
  j = node2.chi.length;
1058
- if (j == 0) {
1059
- break;
1060
- }
1107
+ // if (j == 0) {
1108
+ //
1109
+ // break;
1110
+ // }
1061
1111
  while (j--) {
1062
1112
  if (node2.chi[j].typ == EnumToken.CommentNodeType) {
1063
1113
  continue;
@@ -1099,7 +1149,7 @@ function reduceRuleSelector(node) {
1099
1149
  if (node.raw == null) {
1100
1150
  Object.defineProperty(node, 'raw', { ...definedPropertySettings, value: splitRule(node.sel) });
1101
1151
  }
1102
- let optimized = reduceSelector(node.raw.reduce((acc, curr) => {
1152
+ let optimized = optimizeSelector(node.raw.reduce((acc, curr) => {
1103
1153
  acc.push(curr.slice());
1104
1154
  return acc;
1105
1155
  }, []));