@tbela99/css-parser 1.1.1-alpha4 → 1.2.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 (101) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +4195 -3375
  4. package/dist/index.cjs +5468 -4648
  5. package/dist/index.d.ts +42 -31
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +37 -35
  8. package/dist/lib/ast/features/inlinecssvariables.js +25 -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 +184 -159
  13. package/dist/lib/ast/math/math.js +22 -20
  14. package/dist/lib/ast/minify.js +249 -200
  15. package/dist/lib/ast/transform/compute.js +48 -38
  16. package/dist/lib/ast/transform/matrix.js +6 -5
  17. package/dist/lib/ast/transform/minify.js +31 -34
  18. package/dist/lib/ast/transform/utils.js +76 -16
  19. package/dist/lib/ast/types.js +32 -1
  20. package/dist/lib/fs/resolve.js +1 -14
  21. package/dist/lib/parser/declaration/list.js +1 -1
  22. package/dist/lib/parser/declaration/map.js +1 -1
  23. package/dist/lib/parser/declaration/set.js +1 -1
  24. package/dist/lib/parser/parse.js +19 -95
  25. package/dist/lib/parser/tokenize.js +1 -13
  26. package/dist/lib/parser/utils/declaration.js +1 -1
  27. package/dist/lib/parser/utils/type.js +1 -1
  28. package/dist/lib/renderer/render.js +44 -168
  29. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  30. package/dist/lib/syntax/color/cmyk.js +104 -0
  31. package/dist/lib/{renderer → syntax}/color/color-mix.js +20 -21
  32. package/dist/lib/syntax/color/color.js +581 -0
  33. package/dist/lib/syntax/color/hex.js +179 -0
  34. package/dist/lib/syntax/color/hsl.js +201 -0
  35. package/dist/lib/syntax/color/hwb.js +185 -0
  36. package/dist/lib/syntax/color/lab.js +262 -0
  37. package/dist/lib/syntax/color/lch.js +194 -0
  38. package/dist/lib/syntax/color/oklab.js +237 -0
  39. package/dist/lib/syntax/color/oklch.js +166 -0
  40. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  41. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  42. package/dist/lib/{renderer → syntax}/color/relativecolor.js +53 -40
  43. package/dist/lib/syntax/color/rgb.js +140 -0
  44. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  45. package/dist/lib/{renderer → syntax}/color/utils/components.js +7 -7
  46. package/dist/lib/{renderer → syntax}/color/utils/constants.js +7 -34
  47. package/dist/lib/syntax/color/utils/distance.js +30 -0
  48. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  49. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  50. package/dist/lib/syntax/syntax.js +77 -67
  51. package/dist/lib/syntax/utils.js +70 -0
  52. package/dist/lib/validation/at-rules/container.js +1 -1
  53. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  54. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  55. package/dist/lib/validation/at-rules/document.js +1 -1
  56. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  57. package/dist/lib/validation/at-rules/import.js +1 -1
  58. package/dist/lib/validation/at-rules/keyframes.js +14 -13
  59. package/dist/lib/validation/at-rules/layer.js +1 -1
  60. package/dist/lib/validation/at-rules/media.js +1 -1
  61. package/dist/lib/validation/at-rules/namespace.js +1 -1
  62. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  63. package/dist/lib/validation/at-rules/page.js +1 -1
  64. package/dist/lib/validation/at-rules/supports.js +1 -1
  65. package/dist/lib/validation/at-rules/when.js +1 -1
  66. package/dist/lib/validation/atrule.js +2 -2
  67. package/dist/lib/validation/config.js +4 -3
  68. package/dist/lib/validation/config.json.js +1 -1
  69. package/dist/lib/validation/parser/parse.js +12 -7
  70. package/dist/lib/validation/selector.js +9 -8
  71. package/dist/lib/validation/syntax.js +171 -132
  72. package/dist/lib/validation/syntaxes/complex-selector-list.js +13 -19
  73. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  74. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  75. package/dist/lib/validation/syntaxes/family-name.js +36 -39
  76. package/dist/lib/validation/syntaxes/keyframe-selector.js +14 -22
  77. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  78. package/dist/lib/validation/syntaxes/relative-selector-list.js +27 -25
  79. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  80. package/dist/lib/validation/syntaxes/url.js +35 -33
  81. package/dist/lib/validation/utils/list.js +10 -9
  82. package/dist/lib/validation/utils/whitespace.js +1 -1
  83. package/dist/node/index.js +4 -2
  84. package/dist/web/index.js +4 -2
  85. package/package.json +4 -4
  86. package/.editorconfig +0 -484
  87. package/dist/lib/ast/transform/convert.js +0 -33
  88. package/dist/lib/ast/utils/utils.js +0 -104
  89. package/dist/lib/renderer/color/color.js +0 -654
  90. package/dist/lib/renderer/color/hex.js +0 -105
  91. package/dist/lib/renderer/color/hsl.js +0 -125
  92. package/dist/lib/renderer/color/hwb.js +0 -103
  93. package/dist/lib/renderer/color/lab.js +0 -148
  94. package/dist/lib/renderer/color/lch.js +0 -90
  95. package/dist/lib/renderer/color/oklab.js +0 -131
  96. package/dist/lib/renderer/color/oklch.js +0 -75
  97. package/dist/lib/renderer/color/rgb.js +0 -50
  98. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  99. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  100. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  101. /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,19 +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
- // @ts-ignore
351
- s[0] = node.optimized.optimized[0];
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(',')})`;
352
367
  }
353
- return s.join('');
354
- }).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
+ }
355
377
  // @ts-ignore
356
378
  let sel = wrap ? node.optimized.optimized.join('') + `:is(${rule})` : rule;
357
- if (rule.includes('&')) {
358
- // @ts-ignore
359
- rule = replaceCompound(rule, node.optimized.optimized[0]);
360
- }
379
+ // if (rule.includes('&')) {
380
+ //
381
+ // // @ts-ignore
382
+ // rule = replaceCompound(rule, node.optimized.optimized[0]);
383
+ // }
361
384
  if (sel.length < node.sel.length) {
362
385
  node.sel = sel;
363
386
  }
@@ -391,12 +414,12 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
391
414
  // @ts-ignore
392
415
  ast.chi.splice(nodeIndex, 1);
393
416
  // @ts-ignore
394
- if (!hasDeclaration(node)) {
395
- // @ts-ignore
396
- // minifyRule(node, <MinifyOptions>options, ast, context);
397
- // } else {
398
- doMinify(node, options, recursive, errors, nestingContent, context);
399
- }
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
+ // }
400
423
  i--;
401
424
  previous = node;
402
425
  nodeIndex = i;
@@ -454,17 +477,21 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
454
477
  }
455
478
  }
456
479
  }
457
- else {
458
- if ('chi' in previous) {
459
- // @ts-ignore
460
- if (!hasDeclaration(previous)) {
461
- // @ts-ignore
462
- // minifyRule(previous, <MinifyOptions>options, ast, context);
463
- // } else {
464
- doMinify(previous, options, recursive, errors, nestingContent, context);
465
- }
466
- }
467
- }
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
+ // }
468
495
  }
469
496
  // else if ('chi' in node) {
470
497
  //
@@ -514,10 +541,14 @@ function hasDeclaration(node) {
514
541
  }
515
542
  return true;
516
543
  }
517
- function reduceSelector(selector) {
518
- if (selector.length == 0) {
519
- return null;
520
- }
544
+ /**
545
+ * optimize selector
546
+ * @param selector
547
+ */
548
+ function optimizeSelector(selector) {
549
+ // if (selector.length == 0) {
550
+ // return null;
551
+ // }
521
552
  selector = selector.reduce((acc, curr) => {
522
553
  // @ts-ignore
523
554
  if (curr.length > 0 && curr.at(-1).startsWith(':is(')) {
@@ -566,10 +597,10 @@ function reduceSelector(selector) {
566
597
  }
567
598
  selector.forEach((selector) => selector.splice(0, optimized.length));
568
599
  // combinator
569
- if (combinators.includes(optimized.at(-1))) {
570
- const combinator = optimized.pop();
571
- selector.forEach((selector) => selector.unshift(combinator));
572
- }
600
+ // if (combinators.includes(<string>optimized.at(-1))) {
601
+ // const combinator: string = <string>optimized.pop();
602
+ // selector.forEach((selector: string[]) => selector.unshift(combinator));
603
+ // }
573
604
  let reducible = optimized.length == 1;
574
605
  if (optimized[0] == '&') {
575
606
  if (optimized[1] == ' ') {
@@ -624,14 +655,17 @@ function splitRule(buffer) {
624
655
  for (let i = 0; i < buffer.length; i++) {
625
656
  let chr = buffer.charAt(i);
626
657
  if (isWhiteSpace(chr.charCodeAt(0))) {
627
- let k = i;
628
- while (k + 1 < buffer.length) {
629
- if (isWhiteSpace(buffer[k + 1].charCodeAt(0))) {
630
- k++;
631
- continue;
632
- }
633
- break;
634
- }
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
+ // }
635
669
  if (str !== '') {
636
670
  // @ts-ignore
637
671
  result.at(-1).push(str);
@@ -642,7 +676,7 @@ function splitRule(buffer) {
642
676
  // @ts-ignore
643
677
  result.at(-1).push(' ');
644
678
  }
645
- i = k;
679
+ // i = k;
646
680
  continue;
647
681
  }
648
682
  if (chr == ',') {
@@ -693,21 +727,22 @@ function splitRule(buffer) {
693
727
  str += buffer.charAt(++i);
694
728
  continue;
695
729
  }
696
- if (chr == '"' || chr == "'") {
697
- let k = i;
698
- while (++k < buffer.length) {
699
- chr = buffer.charAt(k);
700
- str += chr;
701
- if (chr == '//') {
702
- str += buffer.charAt(++k);
703
- continue;
704
- }
705
- if (chr == buffer.charAt(i)) {
706
- break;
707
- }
708
- }
709
- continue;
710
- }
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
+ // }
711
746
  if (chr == '(' || chr == '[') {
712
747
  const open = chr;
713
748
  const close = chr == '(' ? ')' : ']';
@@ -740,6 +775,79 @@ function splitRule(buffer) {
740
775
  }
741
776
  return result;
742
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
+ }
743
851
  function matchSelectors(selector1, selector2, parentType, errors) {
744
852
  let match = [[]];
745
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));
@@ -771,47 +879,52 @@ function matchSelectors(selector1, selector2, parentType, errors) {
771
879
  if (!matching) {
772
880
  break;
773
881
  }
774
- if (token == ',') {
775
- match.push([]);
776
- }
777
- else {
778
- if (token.endsWith('(')) {
779
- matchFunction++;
780
- }
781
- if (token.endsWith('[')) {
782
- inAttr++;
783
- }
784
- else if (token == ')') {
785
- matchFunction--;
786
- }
787
- else if (token == ']') {
788
- inAttr--;
789
- }
790
- match.at(-1).push(token);
882
+ // if (token == ',') {
883
+ //
884
+ // match.push([]);
885
+ // } else {
886
+ if (token.endsWith('(')) {
887
+ matchFunction++;
791
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
+ // }
792
901
  }
793
902
  // invalid function
794
903
  if (matchFunction != 0 || inAttr != 0) {
795
904
  return null;
796
905
  }
797
- if (parentType != EnumToken.RuleNodeType) {
798
- for (const part of match) {
799
- if (part.length > 0 && combinators.includes(part[0].charAt(0))) {
800
- return null;
801
- }
802
- }
803
- }
804
- if (match.length > 1) {
805
- errors?.push({
806
- action: 'ignore',
807
- message: `minify: unsupported multilevel matching\n${JSON.stringify({
808
- match,
809
- selector1,
810
- selector2
811
- }, null, 1)}`
812
- });
813
- return null;
814
- }
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
+ // }
815
928
  for (const part of match) {
816
929
  while (part.length > 0) {
817
930
  const token = part.at(-1);
@@ -828,77 +941,11 @@ function matchSelectors(selector1, selector2, parentType, errors) {
828
941
  if (eq([['&']], match)) {
829
942
  return null;
830
943
  }
831
- function reduce(acc, curr) {
832
- if (acc === null) {
833
- return null;
834
- }
835
- let hasCompoundSelector = true;
836
- curr = curr.slice(match[0].length);
837
- while (curr.length > 0) {
838
- if (curr[0] == ' ') {
839
- hasCompoundSelector = false;
840
- curr.unshift('&');
841
- continue;
842
- }
843
- break;
844
- }
845
- // invalid function match
846
- if (curr.length > 0 && curr[0].endsWith('(') && curr.at(-1) != ')') {
847
- return null;
848
- }
849
- if (curr.length == 1 && combinators.includes(curr[0].charAt(0))) {
850
- return null;
851
- }
852
- if (hasCompoundSelector && curr.length > 0) {
853
- hasCompoundSelector = !['&'].concat(combinators).includes(curr[0].charAt(0));
854
- }
855
- if (curr[0] == ':is(') {
856
- let inFunction = 0;
857
- let canReduce = true;
858
- const isCompound = curr.reduce((acc, token, index) => {
859
- if (index == 0) {
860
- inFunction++;
861
- canReduce = curr[1] == '&';
862
- }
863
- else if (token.endsWith('(')) {
864
- if (inFunction == 0) {
865
- canReduce = false;
866
- }
867
- inFunction++;
868
- }
869
- else if (token == ')') {
870
- inFunction--;
871
- }
872
- else if (token == ',') {
873
- if (!canReduce) {
874
- canReduce = curr[index + 1] == '&';
875
- }
876
- acc.push([]);
877
- }
878
- else
879
- acc.at(-1)?.push(token);
880
- return acc;
881
- }, [[]]);
882
- if (inFunction > 0) {
883
- canReduce = false;
884
- }
885
- if (canReduce) {
886
- curr = isCompound.reduce((acc, curr) => {
887
- if (acc.length > 0) {
888
- acc.push(',');
889
- }
890
- acc.push(...curr);
891
- return acc;
892
- }, []);
893
- }
894
- }
895
- acc.push(match.length == 0 ? ['&'] : (hasCompoundSelector && curr[0] != '&' && (curr.length == 0 || !combinators.includes(curr[0].charAt(0))) ? ['&'].concat(curr) : curr));
896
- return acc;
897
- }
944
+ const reducer = reduceSelector.bind({ match });
898
945
  // @ts-ignore
899
- selector1 = selector1.reduce(reduce, []);
946
+ selector1 = selector1.reduce(reducer, []);
900
947
  // @ts-ignore
901
- selector2 = selector2.reduce(reduce, []);
948
+ selector2 = selector2.reduce(reducer, []);
902
949
  return selector1 == null || selector2 == null ? null : {
903
950
  eq: eq(selector1, selector2),
904
951
  match,
@@ -983,9 +1030,10 @@ function diff(n1, n2, reducer, options = {}) {
983
1030
  }
984
1031
  let i = node1.chi.length;
985
1032
  let j = node2.chi.length;
986
- if (i == 0 || j == 0) {
987
- return null;
988
- }
1033
+ // if (i == 0 || j == 0) {
1034
+ //
1035
+ // return null;
1036
+ // }
989
1037
  const raw1 = node1.raw;
990
1038
  const raw2 = node2.raw;
991
1039
  if (raw1 != null && raw2 != null) {
@@ -1056,9 +1104,10 @@ function diff(n1, n2, reducer, options = {}) {
1056
1104
  continue;
1057
1105
  }
1058
1106
  j = node2.chi.length;
1059
- if (j == 0) {
1060
- break;
1061
- }
1107
+ // if (j == 0) {
1108
+ //
1109
+ // break;
1110
+ // }
1062
1111
  while (j--) {
1063
1112
  if (node2.chi[j].typ == EnumToken.CommentNodeType) {
1064
1113
  continue;
@@ -1100,7 +1149,7 @@ function reduceRuleSelector(node) {
1100
1149
  if (node.raw == null) {
1101
1150
  Object.defineProperty(node, 'raw', { ...definedPropertySettings, value: splitRule(node.sel) });
1102
1151
  }
1103
- let optimized = reduceSelector(node.raw.reduce((acc, curr) => {
1152
+ let optimized = optimizeSelector(node.raw.reduce((acc, curr) => {
1104
1153
  acc.push(curr.slice());
1105
1154
  return acc;
1106
1155
  }, []));