@tbela99/css-parser 0.9.1 → 1.0.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 (58) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +15 -8
  3. package/dist/index-umd-web.js +1815 -498
  4. package/dist/index.cjs +1816 -499
  5. package/dist/index.d.ts +46 -14
  6. package/dist/lib/ast/features/calc.js +7 -10
  7. package/dist/lib/ast/features/index.js +1 -0
  8. package/dist/lib/ast/features/inlinecssvariables.js +0 -5
  9. package/dist/lib/ast/features/prefix.js +2 -7
  10. package/dist/lib/ast/features/shorthand.js +6 -9
  11. package/dist/lib/ast/features/transform.js +60 -0
  12. package/dist/lib/ast/math/expression.js +14 -10
  13. package/dist/lib/ast/math/math.js +14 -2
  14. package/dist/lib/ast/minify.js +46 -5
  15. package/dist/lib/ast/transform/compute.js +336 -0
  16. package/dist/lib/ast/transform/convert.js +33 -0
  17. package/dist/lib/ast/transform/matrix.js +111 -0
  18. package/dist/lib/ast/transform/minify.js +296 -0
  19. package/dist/lib/ast/transform/perspective.js +10 -0
  20. package/dist/lib/ast/transform/rotate.js +40 -0
  21. package/dist/lib/ast/transform/scale.js +32 -0
  22. package/dist/lib/ast/transform/skew.js +23 -0
  23. package/dist/lib/ast/transform/translate.js +32 -0
  24. package/dist/lib/ast/transform/utils.js +198 -0
  25. package/dist/lib/ast/types.js +1 -0
  26. package/dist/lib/ast/walk.js +23 -17
  27. package/dist/lib/parser/parse.js +109 -88
  28. package/dist/lib/parser/utils/declaration.js +1 -1
  29. package/dist/lib/renderer/color/{colormix.js → color-mix.js} +6 -0
  30. package/dist/lib/renderer/color/color.js +94 -18
  31. package/dist/lib/renderer/color/hex.js +17 -7
  32. package/dist/lib/renderer/color/hsl.js +7 -2
  33. package/dist/lib/renderer/color/lab.js +8 -0
  34. package/dist/lib/renderer/color/lch.js +8 -0
  35. package/dist/lib/renderer/color/oklab.js +8 -0
  36. package/dist/lib/renderer/color/oklch.js +8 -0
  37. package/dist/lib/renderer/color/relativecolor.js +10 -21
  38. package/dist/lib/renderer/color/rgb.js +10 -7
  39. package/dist/lib/renderer/color/srgb.js +30 -6
  40. package/dist/lib/renderer/color/utils/components.js +13 -2
  41. package/dist/lib/renderer/render.js +67 -30
  42. package/dist/lib/syntax/syntax.js +74 -56
  43. package/dist/lib/validation/at-rules/container.js +6 -6
  44. package/dist/lib/validation/at-rules/document.js +1 -1
  45. package/dist/lib/validation/at-rules/keyframes.js +1 -1
  46. package/dist/lib/validation/at-rules/media.js +0 -1
  47. package/dist/lib/validation/atrule.js +0 -4
  48. package/dist/lib/validation/selector.js +5 -2
  49. package/dist/lib/validation/syntaxes/keyframe-block-list.js +2 -2
  50. package/dist/lib/validation/syntaxes/keyframe-selector.js +11 -90
  51. package/dist/lib/validation/syntaxes/relative-selector.js +15 -14
  52. package/dist/lib/validation/utils/list.js +18 -1
  53. package/dist/node/load.js +1 -1
  54. package/package.json +12 -12
  55. package/dist/lib/renderer/color/prophotoRgb.js +0 -56
  56. package/dist/lib/validation/declaration.js +0 -94
  57. package/dist/lib/validation/syntax.js +0 -1509
  58. package/dist/lib/validation/syntaxes/image.js +0 -29
@@ -19,6 +19,14 @@ const dimensionUnits = new Set([
19
19
  const fontFormat = ['collection', 'embedded-opentype', 'opentype', 'svg', 'truetype', 'woff', 'woff2'];
20
20
  const colorFontTech = ['color-colrv0', 'color-colrv1', 'color-svg', 'color-sbix', 'color-cbdt'];
21
21
  const fontFeaturesTech = ['features-opentype', 'features-aat', 'features-graphite', 'incremental-patch', 'incremental-range', 'incremental-auto', 'variations', 'palettes'];
22
+ const transformFunctions = [
23
+ 'translate', 'scale', 'rotate', 'skew', 'perspective',
24
+ 'translateX', 'translateY', 'translateZ',
25
+ 'scaleX', 'scaleY', 'scaleZ',
26
+ 'rotateX', 'rotateY', 'rotateZ',
27
+ 'skewX', 'skewY',
28
+ 'rotate3d', 'translate3d', 'scale3d', 'matrix', 'matrix3d'
29
+ ];
22
30
  // https://drafts.csswg.org/mediaqueries/#media-types
23
31
  const mediaTypes = ['all', 'print', 'screen',
24
32
  /* deprecated */
@@ -410,7 +418,9 @@ function isColor(token) {
410
418
  }
411
419
  let isLegacySyntax = false;
412
420
  if (token.typ == EnumToken.FunctionTokenType && token.chi.length > 0 && colorsFunc.includes(token.val)) {
421
+ // @ts-ignore
413
422
  if (token.val == 'light-dark') {
423
+ // @ts-ignore
414
424
  const children = token.chi.filter((t) => [EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.LiteralTokenType, EnumToken.ColorTokenType, EnumToken.FunctionTokenType, EnumToken.PercentageTokenType].includes(t.typ));
415
425
  if (children.length != 2) {
416
426
  return false;
@@ -419,7 +429,9 @@ function isColor(token) {
419
429
  return true;
420
430
  }
421
431
  }
432
+ // @ts-ignore
422
433
  if (token.val == 'color') {
434
+ // @ts-ignore
423
435
  const children = token.chi.filter((t) => [EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.LiteralTokenType, EnumToken.ColorTokenType, EnumToken.FunctionTokenType, EnumToken.PercentageTokenType].includes(t.typ));
424
436
  const isRelative = children[0].typ == EnumToken.IdenTokenType && children[0].val == 'from';
425
437
  if (children.length < 4 || children.length > 8) {
@@ -468,73 +480,79 @@ function isColor(token) {
468
480
  }
469
481
  return true;
470
482
  }
471
- else if (token.val == 'color-mix') {
472
- const children = token.chi.reduce((acc, t) => {
473
- if (t.typ == EnumToken.CommaTokenType) {
474
- acc.push([]);
475
- }
476
- else {
477
- if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
478
- acc[acc.length - 1].push(t);
483
+ else { // @ts-ignore
484
+ if (token.val == 'color-mix') {
485
+ // @ts-ignore
486
+ const children = token.chi.reduce((acc, t) => {
487
+ if (t.typ == EnumToken.CommaTokenType) {
488
+ acc.push([]);
479
489
  }
480
- }
481
- return acc;
482
- }, [[]]);
483
- if (children.length == 3) {
484
- if (children[0].length > 3 ||
485
- children[0][0].typ != EnumToken.IdenTokenType ||
486
- children[0][0].val != 'in' ||
487
- !isColorspace(children[0][1]) ||
488
- (children[0].length == 3 && !isHueInterpolationMethod(children[0][2])) ||
489
- children[1].length > 2 ||
490
- children[1][0].typ != EnumToken.ColorTokenType ||
491
- children[2].length > 2 ||
492
- children[2][0].typ != EnumToken.ColorTokenType) {
493
- return false;
494
- }
495
- if (children[1].length == 2) {
496
- if (!(children[1][1].typ == EnumToken.PercentageTokenType || (children[1][1].typ == EnumToken.NumberTokenType && children[1][1].val == '0'))) {
497
- return false;
490
+ else {
491
+ if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
492
+ acc[acc.length - 1].push(t);
493
+ }
498
494
  }
499
- }
500
- if (children[2].length == 2) {
501
- if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
495
+ return acc;
496
+ }, [[]]);
497
+ if (children.length == 3) {
498
+ if (children[0].length > 3 ||
499
+ children[0][0].typ != EnumToken.IdenTokenType ||
500
+ children[0][0].val != 'in' ||
501
+ !isColorspace(children[0][1]) ||
502
+ (children[0].length == 3 && !isHueInterpolationMethod(children[0][2])) ||
503
+ children[1].length > 2 ||
504
+ children[1][0].typ != EnumToken.ColorTokenType ||
505
+ children[2].length > 2 ||
506
+ children[2][0].typ != EnumToken.ColorTokenType) {
502
507
  return false;
503
508
  }
509
+ if (children[1].length == 2) {
510
+ if (!(children[1][1].typ == EnumToken.PercentageTokenType || (children[1][1].typ == EnumToken.NumberTokenType && children[1][1].val == '0'))) {
511
+ return false;
512
+ }
513
+ }
514
+ if (children[2].length == 2) {
515
+ if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
516
+ return false;
517
+ }
518
+ }
519
+ return true;
504
520
  }
505
- return true;
506
- }
507
- return false;
508
- }
509
- else {
510
- const keywords = ['from', 'none'];
511
- if (['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'].includes(token.val)) {
512
- keywords.push('alpha', ...token.val.slice(-3).split(''));
521
+ return false;
513
522
  }
514
- // @ts-ignore
515
- for (const v of token.chi) {
516
- if (v.typ == EnumToken.CommaTokenType) {
517
- isLegacySyntax = true;
523
+ else {
524
+ const keywords = ['from', 'none'];
525
+ // @ts-ignore
526
+ if (['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'].includes(token.val)) {
527
+ // @ts-ignore
528
+ keywords.push('alpha', ...token.val.slice(-3).split(''));
518
529
  }
519
- if (v.typ == EnumToken.IdenTokenType) {
520
- if (!(keywords.includes(v.val) || v.val.toLowerCase() in COLORS_NAMES)) {
521
- return false;
530
+ // @ts-ignore
531
+ for (const v of token.chi) {
532
+ if (v.typ == EnumToken.CommaTokenType) {
533
+ isLegacySyntax = true;
522
534
  }
523
- if (keywords.includes(v.val)) {
524
- if (isLegacySyntax) {
535
+ if (v.typ == EnumToken.IdenTokenType) {
536
+ if (!(keywords.includes(v.val) || v.val.toLowerCase() in COLORS_NAMES)) {
525
537
  return false;
526
538
  }
527
- if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
528
- return false;
539
+ if (keywords.includes(v.val)) {
540
+ if (isLegacySyntax) {
541
+ return false;
542
+ }
543
+ // @ts-ignore
544
+ if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
545
+ return false;
546
+ }
529
547
  }
548
+ continue;
549
+ }
550
+ if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
551
+ continue;
552
+ }
553
+ if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
554
+ return false;
530
555
  }
531
- continue;
532
- }
533
- if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
534
- continue;
535
- }
536
- if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
537
- return false;
538
556
  }
539
557
  }
540
558
  }
@@ -812,4 +830,4 @@ function isWhiteSpace(codepoint) {
812
830
  codepoint == 0xa || codepoint == 0xc || codepoint == 0xd;
813
831
  }
814
832
 
815
- export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseDimension, pseudoElements, webkitExtensions, webkitPseudoAliasMap };
833
+ export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseDimension, pseudoElements, transformFunctions, webkitExtensions, webkitPseudoAliasMap };
@@ -95,10 +95,10 @@ function validateAtRuleContainerQueryList(tokens, atRule) {
95
95
  break;
96
96
  }
97
97
  token = queries[0];
98
- if (token.typ == EnumToken.MediaFeatureNotTokenType) {
98
+ if (token?.typ == EnumToken.MediaFeatureNotTokenType) {
99
99
  token = token.val;
100
100
  }
101
- if (token.typ != EnumToken.ParensTokenType && (token.typ != EnumToken.FunctionTokenType || !['scroll-state', 'style'].includes(token.val))) {
101
+ if (token?.typ != EnumToken.ParensTokenType && (token?.typ != EnumToken.FunctionTokenType || !['scroll-state', 'style'].includes(token.val))) {
102
102
  return {
103
103
  valid: ValidationLevel.Drop,
104
104
  matches: [],
@@ -108,7 +108,7 @@ function validateAtRuleContainerQueryList(tokens, atRule) {
108
108
  tokens
109
109
  };
110
110
  }
111
- if (token.typ == EnumToken.ParensTokenType) {
111
+ if (token?.typ == EnumToken.ParensTokenType) {
112
112
  result = validateContainerSizeFeature(token.chi, atRule);
113
113
  }
114
114
  else if (token.val == 'scroll-state') {
@@ -126,7 +126,7 @@ function validateAtRuleContainerQueryList(tokens, atRule) {
126
126
  break;
127
127
  }
128
128
  token = queries[0];
129
- if (token.typ != EnumToken.MediaFeatureAndTokenType && token.typ != EnumToken.MediaFeatureOrTokenType) {
129
+ if (token?.typ != EnumToken.MediaFeatureAndTokenType && token?.typ != EnumToken.MediaFeatureOrTokenType) {
130
130
  return {
131
131
  valid: ValidationLevel.Drop,
132
132
  matches: [],
@@ -137,9 +137,9 @@ function validateAtRuleContainerQueryList(tokens, atRule) {
137
137
  };
138
138
  }
139
139
  if (tokenType == null) {
140
- tokenType = token.typ;
140
+ tokenType = token?.typ;
141
141
  }
142
- if (tokenType != token.typ) {
142
+ if (tokenType == null || tokenType != token?.typ) {
143
143
  return {
144
144
  valid: ValidationLevel.Drop,
145
145
  matches: [],
@@ -59,7 +59,7 @@ function validateAtRuleDocument(atRule, options, root) {
59
59
  }
60
60
  if (t[0].typ == EnumToken.UrlFunctionTokenType) {
61
61
  result = validateURL(t[0]);
62
- if (result.valid == ValidationLevel.Drop) {
62
+ if (result?.valid == ValidationLevel.Drop) {
63
63
  return result;
64
64
  }
65
65
  continue;
@@ -14,7 +14,7 @@ function validateAtRuleKeyframes(atRule, options, root) {
14
14
  valid: ValidationLevel.Drop,
15
15
  matches: [],
16
16
  node: atRule,
17
- syntax: '@document',
17
+ syntax: '@keyframes',
18
18
  error: 'expecting at-rule prelude',
19
19
  tokens: []
20
20
  };
@@ -241,7 +241,6 @@ function validateMediaCondition(token, atRule) {
241
241
  if (chi[0].typ == EnumToken.MediaQueryConditionTokenType) {
242
242
  return chi[0].l.typ == EnumToken.IdenTokenType;
243
243
  }
244
- console.error(chi[0].parent);
245
244
  return false;
246
245
  }
247
246
  function validateMediaFeature(token) {
@@ -16,7 +16,6 @@ import { validateAtRuleLayer } from './at-rules/layer.js';
16
16
  import { validateAtRuleFontFeatureValues } from './at-rules/font-feature-values.js';
17
17
  import { validateAtRuleNamespace } from './at-rules/namespace.js';
18
18
  import { validateAtRuleDocument } from './at-rules/document.js';
19
- import { validateAtRuleKeyframes } from './at-rules/keyframes.js';
20
19
  import { validateAtRuleWhen } from './at-rules/when.js';
21
20
  import { validateAtRuleElse } from './at-rules/else.js';
22
21
  import { validateAtRuleContainer } from './at-rules/container.js';
@@ -32,9 +31,6 @@ function validateAtRule(atRule, options, root) {
32
31
  error: ''
33
32
  };
34
33
  }
35
- if (atRule.nam == 'keyframes') {
36
- return validateAtRuleKeyframes(atRule);
37
- }
38
34
  if (['font-face', 'view-transition', 'starting-style'].includes(atRule.nam)) {
39
35
  return {
40
36
  valid: ValidationLevel.Valid,
@@ -19,7 +19,7 @@ function validateSelector(selector, options, root) {
19
19
  }
20
20
  // @ts-ignore
21
21
  if (root.typ == EnumToken.AtRuleNodeType && root.nam.match(/^(-[a-z]+-)?keyframes$/)) {
22
- return validateKeyframeBlockList(selector, root);
22
+ return validateKeyframeBlockList(selector);
23
23
  }
24
24
  let isNested = root.typ == EnumToken.RuleNodeType ? 1 : 0;
25
25
  let currentRoot = root.parent;
@@ -35,7 +35,10 @@ function validateSelector(selector, options, root) {
35
35
  }
36
36
  const nestedSelector = isNested > 0;
37
37
  // @ts-ignore
38
- return nestedSelector ? validateRelativeSelectorList(selector, root, { ...(options ?? {}), nestedSelector }) : validateSelectorList(selector, root, { ...(options ?? {}), nestedSelector });
38
+ return nestedSelector ? validateRelativeSelectorList(selector, root, {
39
+ ...(options ?? {}),
40
+ nestedSelector
41
+ }) : validateSelectorList(selector, root, { ...(options ?? {}), nestedSelector });
39
42
  }
40
43
 
41
44
  export { validateSelector };
@@ -13,7 +13,7 @@ function validateKeyframeBlockList(tokens, atRule, options) {
13
13
  let result = null;
14
14
  while (i + 1 < tokens.length) {
15
15
  if (tokens[++i].typ == EnumToken.CommaTokenType) {
16
- result = validateKeyframeSelector(tokens.slice(j, i), atRule);
16
+ result = validateKeyframeSelector(tokens.slice(j, i));
17
17
  if (result.valid == ValidationLevel.Drop) {
18
18
  return result;
19
19
  }
@@ -21,7 +21,7 @@ function validateKeyframeBlockList(tokens, atRule, options) {
21
21
  i = j;
22
22
  }
23
23
  }
24
- return validateKeyframeSelector(i == j ? tokens.slice(i) : tokens.slice(j, i + 1), atRule);
24
+ return validateKeyframeSelector(i == j ? tokens.slice(i) : tokens.slice(j, i + 1));
25
25
  }
26
26
 
27
27
  export { validateKeyframeBlockList };
@@ -1,4 +1,5 @@
1
1
  import { consumeWhitespace } from '../utils/whitespace.js';
2
+ import { splitTokenList } from '../utils/list.js';
2
3
  import { ValidationLevel, EnumToken } from '../../ast/types.js';
3
4
  import '../../ast/minify.js';
4
5
  import '../../ast/walk.js';
@@ -7,120 +8,40 @@ import '../../renderer/color/utils/constants.js';
7
8
  import '../../renderer/sourcemap/lib/encode.js';
8
9
  import '../../parser/utils/config.js';
9
10
 
10
- function validateKeyframeSelector(tokens, atRule, options) {
11
+ function validateKeyframeSelector(tokens, options) {
11
12
  consumeWhitespace(tokens);
12
13
  if (tokens.length == 0) {
13
14
  // @ts-ignore
14
15
  return {
15
16
  valid: ValidationLevel.Drop,
16
17
  matches: [],
17
- node: atRule,
18
+ node: null,
18
19
  syntax: null,
19
20
  error: 'expected keyframe selector',
20
21
  tokens
21
22
  };
22
23
  }
23
- if (tokens[0].typ == EnumToken.PercentageTokenType) {
24
- tokens.shift();
25
- consumeWhitespace(tokens);
26
- if (tokens.length == 0) {
27
- // @ts-ignore
24
+ for (const t of splitTokenList(tokens)) {
25
+ if (t.length != 1) {
28
26
  return {
29
- valid: ValidationLevel.Valid,
27
+ valid: ValidationLevel.Drop,
30
28
  matches: [],
31
- node: atRule,
29
+ node: t[0] ?? null,
32
30
  syntax: null,
33
- error: '',
31
+ error: 'unexpected token',
34
32
  tokens
35
33
  };
36
34
  }
37
- // @ts-ignore
38
- return {
39
- valid: ValidationLevel.Drop,
40
- matches: [],
41
- node: tokens[0],
42
- syntax: null,
43
- error: 'unexpected token',
44
- tokens
45
- };
46
- }
47
- if (tokens[0].typ != EnumToken.IdenTokenType) {
48
- // @ts-ignore
49
- return {
50
- valid: ValidationLevel.Drop,
51
- matches: [],
52
- node: tokens[0],
53
- // @ts-ignore
54
- syntax: null,
55
- error: 'expected keyframe selector',
56
- tokens
57
- };
58
- }
59
- if (['from', 'to'].includes(tokens[0].val)) {
60
- tokens.shift();
61
- consumeWhitespace(tokens);
62
- if (tokens.length > 0) {
63
- // @ts-ignore
35
+ if (t[0].typ != EnumToken.PercentageTokenType && !(t[0].typ == EnumToken.IdenTokenType && ['from', 'to', 'cover', 'contain', 'entry', 'exit', 'entry-crossing', 'exit-crossing'].includes(t[0].val))) {
64
36
  return {
65
37
  valid: ValidationLevel.Drop,
66
38
  matches: [],
67
- node: tokens[0],
39
+ node: t[0],
68
40
  syntax: null,
69
- error: 'unexpected token',
41
+ error: 'expected keyframe selector',
70
42
  tokens
71
43
  };
72
44
  }
73
- // @ts-ignore
74
- return {
75
- valid: ValidationLevel.Valid,
76
- matches: [],
77
- node: null,
78
- // @ts-ignore
79
- syntax: null,
80
- error: '',
81
- tokens
82
- };
83
- }
84
- if (!['cover', 'contain', 'entry', 'exit', 'entry-crossing', 'exit-crossing'].includes(tokens[0].val)) {
85
- // @ts-ignore
86
- return {
87
- valid: ValidationLevel.Drop,
88
- matches: [],
89
- node: tokens[0],
90
- // @ts-ignore
91
- syntax: null,
92
- error: 'unexpected token',
93
- tokens
94
- };
95
- }
96
- tokens.shift();
97
- consumeWhitespace(tokens);
98
- // @ts-ignore
99
- if (tokens.length == 0 || tokens[0].typ != EnumToken.PercentageTokenType) {
100
- // @ts-ignore
101
- return {
102
- valid: ValidationLevel.Drop,
103
- matches: [],
104
- node: tokens[0],
105
- // @ts-ignore
106
- syntax: null,
107
- error: 'expecting percentage token',
108
- tokens
109
- };
110
- }
111
- tokens.shift();
112
- consumeWhitespace(tokens);
113
- if (tokens.length > 0) {
114
- // @ts-ignore
115
- return {
116
- valid: ValidationLevel.Drop,
117
- matches: [],
118
- node: tokens[0],
119
- // @ts-ignore
120
- syntax: null,
121
- error: 'unexpected token',
122
- tokens
123
- };
124
45
  }
125
46
  // @ts-ignore
126
47
  return {
@@ -1,5 +1,5 @@
1
1
  import { consumeWhitespace } from '../utils/whitespace.js';
2
- import { ValidationLevel } from '../../ast/types.js';
2
+ import '../../ast/types.js';
3
3
  import '../../ast/minify.js';
4
4
  import '../../ast/walk.js';
5
5
  import '../../parser/parse.js';
@@ -12,19 +12,20 @@ import { combinatorsTokens } from './complex-selector.js';
12
12
  function validateRelativeSelector(tokens, root, options) {
13
13
  tokens = tokens.slice();
14
14
  consumeWhitespace(tokens);
15
- if (tokens.length == 0) {
16
- // @ts-ignore
17
- return {
18
- valid: ValidationLevel.Drop,
19
- matches: [],
20
- // @ts-ignore
21
- node: root,
22
- // @ts-ignore
23
- syntax: null,
24
- error: 'expected selector',
25
- tokens
26
- };
27
- }
15
+ // if (tokens.length == 0) {
16
+ //
17
+ // // @ts-ignore
18
+ // return {
19
+ // valid: ValidationLevel.Drop,
20
+ // matches: [],
21
+ // // @ts-ignore
22
+ // node: root,
23
+ // // @ts-ignore
24
+ // syntax: null,
25
+ // error: 'expected selector',
26
+ // tokens
27
+ // }
28
+ // }
28
29
  // , EnumToken.DescendantCombinatorTokenType
29
30
  if (combinatorsTokens.includes(tokens[0].typ)) {
30
31
  tokens.shift();
@@ -6,6 +6,23 @@ import '../../renderer/color/utils/constants.js';
6
6
  import '../../renderer/sourcemap/lib/encode.js';
7
7
  import '../../parser/utils/config.js';
8
8
 
9
+ function stripCommaToken(tokenList) {
10
+ let result = [];
11
+ let last = null;
12
+ for (let i = 0; i < tokenList.length; i++) {
13
+ if (tokenList[i].typ == EnumToken.CommaTokenType && last != null && last.typ == EnumToken.CommaTokenType) {
14
+ return null;
15
+ }
16
+ if (tokenList[i].typ != EnumToken.WhitespaceTokenType) {
17
+ last = tokenList[i];
18
+ }
19
+ if (tokenList[i].typ == EnumToken.CommentTokenType || tokenList[i].typ == EnumToken.CommaTokenType) {
20
+ continue;
21
+ }
22
+ result.push(tokenList[i]);
23
+ }
24
+ return result;
25
+ }
9
26
  function splitTokenList(tokenList, split = [EnumToken.CommaTokenType]) {
10
27
  return tokenList.reduce((acc, curr) => {
11
28
  if (curr.typ == EnumToken.CommentTokenType) {
@@ -21,4 +38,4 @@ function splitTokenList(tokenList, split = [EnumToken.CommaTokenType]) {
21
38
  }, [[]]);
22
39
  }
23
40
 
24
- export { splitTokenList };
41
+ export { splitTokenList, stripCommaToken };
package/dist/node/load.js CHANGED
@@ -7,7 +7,7 @@ function parseResponse(response) {
7
7
  }
8
8
  return response.text();
9
9
  }
10
- async function load(url, currentFile) {
10
+ async function load(url, currentFile = '.') {
11
11
  const resolved = resolve(url, currentFile);
12
12
  return matchUrl.test(resolved.absolute) ? fetch(resolved.absolute).then(parseResponse) : readFile(resolved.absolute, { encoding: 'utf-8' });
13
13
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
3
  "description": "CSS parser for node and the browser",
4
- "version": "v0.9.1",
4
+ "version": "v1.0.0",
5
5
  "exports": {
6
6
  ".": "./dist/node/index.js",
7
7
  "./node": "./dist/node/index.js",
@@ -18,8 +18,8 @@
18
18
  "build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
19
19
  "test": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
20
20
  "test:node": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
21
- "test:cov": "c8 -x dist/lib/validation/syntax.js -x 'dist/lib/validation/parser/*.js' --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
22
- "test:web-cov": "web-test-runner -x dist/lib/validation/syntax.js,dist/lib/validation/parser \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
21
+ "test:cov": "c8 -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js -x 'dist/lib/validation/parser/*.js' --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
22
+ "test:web-cov": "web-test-runner -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js,dist/lib/validation/parser \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
23
23
  "profile": "node --enable-source-maps --inspect-brk test/inspect.js",
24
24
  "syntax-update": "esno tools/validation.ts",
25
25
  "debug": "web-test-runner \"test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."
@@ -51,22 +51,22 @@
51
51
  "homepage": "https://github.com/tbela99/css-parser#readme",
52
52
  "devDependencies": {
53
53
  "@esm-bundle/chai": "^4.3.4-fix.0",
54
- "@rollup/plugin-commonjs": "^28.0.2",
54
+ "@rollup/plugin-commonjs": "^28.0.3",
55
55
  "@rollup/plugin-json": "^6.1.0",
56
- "@rollup/plugin-node-resolve": "^16.0.0",
56
+ "@rollup/plugin-node-resolve": "^16.0.1",
57
57
  "@rollup/plugin-typescript": "^12.1.2",
58
- "@types/chai": "^5.0.1",
58
+ "@types/chai": "^5.2.1",
59
59
  "@types/mocha": "^10.0.10",
60
- "@types/node": "^22.13.5",
61
- "@types/web": "^0.0.206",
62
- "@web/test-runner": "^0.20.0",
60
+ "@types/node": "^22.15.2",
61
+ "@types/web": "^0.0.226",
62
+ "@web/test-runner": "^0.20.1",
63
63
  "@web/test-runner-playwright": "^0.11.0",
64
64
  "c8": "^10.1.3",
65
65
  "esno": "^4.8.0",
66
66
  "mocha": "^11.1.0",
67
- "playwright": "^1.50.1",
68
- "rollup": "^4.34.8",
69
- "rollup-plugin-dts": "^6.1.1",
67
+ "playwright": "^1.52.0",
68
+ "rollup": "^4.40.0",
69
+ "rollup-plugin-dts": "^6.2.1",
70
70
  "tslib": "^2.8.1"
71
71
  }
72
72
  }
@@ -1,56 +0,0 @@
1
- import { srgb2xyz } from './xyz.js';
2
- import { XYZ_D65_to_D50, xyzd502srgb } from './xyzd50.js';
3
-
4
- function prophotorgb2srgbvalues(r, g, b, a = null) {
5
- // @ts-ignore
6
- return xyzd502srgb(...prophotorgb2xyz50(r, g, b, a));
7
- }
8
- function srgb2prophotorgbvalues(r, g, b, a) {
9
- // @ts-ignore
10
- return xyz50_to_prophotorgb(...XYZ_D65_to_D50(...srgb2xyz(r, g, b, a)));
11
- }
12
- function prophotorgb2lin_ProPhoto(r, g, b, a = null) {
13
- return [r, g, b].map(v => {
14
- let abs = Math.abs(v);
15
- if (abs >= 16 / 512) {
16
- return Math.sign(v) * Math.pow(abs, 1.8);
17
- }
18
- return v / 16;
19
- }).concat(a == null || a == 1 ? [] : [a]);
20
- }
21
- function prophotorgb2xyz50(r, g, b, a = null) {
22
- [r, g, b, a] = prophotorgb2lin_ProPhoto(r, g, b, a);
23
- const xyz = [
24
- 0.7977666449006423 * r +
25
- 0.1351812974005331 * g +
26
- 0.0313477341283922 * b,
27
- 0.2880748288194013 * r +
28
- 0.7118352342418731 * g +
29
- 0.0000899369387256 * b,
30
- 0.8251046025104602 * b
31
- ];
32
- return xyz.concat(a == null || a == 1 ? [] : [a]);
33
- }
34
- function xyz50_to_prophotorgb(x, y, z, a) {
35
- // @ts-ignore
36
- return gam_prophotorgb(...[
37
- x * 1.3457868816471585 -
38
- y * 0.2555720873797946 -
39
- 0.0511018649755453 * z,
40
- x * -0.5446307051249019 +
41
- y * 1.5082477428451466 +
42
- 0.0205274474364214 * z,
43
- 1.2119675456389452 * z
44
- ].concat(a == null || a == 1 ? [] : [a]));
45
- }
46
- function gam_prophotorgb(r, g, b, a) {
47
- return [r, g, b].map(v => {
48
- let abs = Math.abs(v);
49
- if (abs >= 1 / 512) {
50
- return Math.sign(v) * Math.pow(abs, 1 / 1.8);
51
- }
52
- return 16 * v;
53
- }).concat(a == null || a == 1 ? [] : [a]);
54
- }
55
-
56
- export { prophotorgb2srgbvalues, srgb2prophotorgbvalues };