@tbela99/css-parser 0.9.1 → 1.1.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 (99) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/LICENSE.md +1 -1
  3. package/README.md +29 -17
  4. package/dist/index-umd-web.js +7461 -4360
  5. package/dist/index.cjs +8608 -5507
  6. package/dist/index.d.ts +203 -61
  7. package/dist/lib/ast/expand.js +2 -1
  8. package/dist/lib/ast/features/calc.js +19 -11
  9. package/dist/lib/ast/features/index.js +1 -0
  10. package/dist/lib/ast/features/inlinecssvariables.js +47 -29
  11. package/dist/lib/ast/features/prefix.js +117 -91
  12. package/dist/lib/ast/features/shorthand.js +34 -14
  13. package/dist/lib/ast/features/transform.js +67 -0
  14. package/dist/lib/ast/features/type.js +7 -0
  15. package/dist/lib/ast/math/expression.js +20 -10
  16. package/dist/lib/ast/math/math.js +20 -2
  17. package/dist/lib/ast/minify.js +209 -80
  18. package/dist/lib/ast/transform/compute.js +337 -0
  19. package/dist/lib/ast/transform/convert.js +33 -0
  20. package/dist/lib/ast/transform/matrix.js +112 -0
  21. package/dist/lib/ast/transform/minify.js +296 -0
  22. package/dist/lib/ast/transform/perspective.js +10 -0
  23. package/dist/lib/ast/transform/rotate.js +40 -0
  24. package/dist/lib/ast/transform/scale.js +32 -0
  25. package/dist/lib/ast/transform/skew.js +23 -0
  26. package/dist/lib/ast/transform/translate.js +32 -0
  27. package/dist/lib/ast/transform/utils.js +198 -0
  28. package/dist/lib/ast/types.js +18 -15
  29. package/dist/lib/ast/walk.js +54 -22
  30. package/dist/lib/fs/resolve.js +10 -0
  31. package/dist/lib/parser/declaration/list.js +48 -45
  32. package/dist/lib/parser/declaration/map.js +1 -0
  33. package/dist/lib/parser/declaration/set.js +2 -1
  34. package/dist/lib/parser/parse.js +449 -340
  35. package/dist/lib/parser/tokenize.js +147 -72
  36. package/dist/lib/parser/utils/declaration.js +5 -4
  37. package/dist/lib/parser/utils/type.js +2 -1
  38. package/dist/lib/renderer/color/a98rgb.js +2 -1
  39. package/dist/lib/renderer/color/{colormix.js → color-mix.js} +16 -7
  40. package/dist/lib/renderer/color/color.js +264 -170
  41. package/dist/lib/renderer/color/hex.js +19 -8
  42. package/dist/lib/renderer/color/hsl.js +9 -3
  43. package/dist/lib/renderer/color/hwb.js +2 -1
  44. package/dist/lib/renderer/color/lab.js +10 -1
  45. package/dist/lib/renderer/color/lch.js +10 -1
  46. package/dist/lib/renderer/color/oklab.js +10 -1
  47. package/dist/lib/renderer/color/oklch.js +10 -1
  48. package/dist/lib/renderer/color/p3.js +2 -1
  49. package/dist/lib/renderer/color/rec2020.js +2 -1
  50. package/dist/lib/renderer/color/relativecolor.js +27 -32
  51. package/dist/lib/renderer/color/rgb.js +14 -10
  52. package/dist/lib/renderer/color/srgb.js +48 -23
  53. package/dist/lib/renderer/color/utils/components.js +18 -6
  54. package/dist/lib/renderer/color/utils/constants.js +47 -3
  55. package/dist/lib/renderer/color/xyz.js +2 -1
  56. package/dist/lib/renderer/color/xyzd50.js +2 -1
  57. package/dist/lib/renderer/render.js +108 -43
  58. package/dist/lib/syntax/syntax.js +267 -136
  59. package/dist/lib/validation/at-rules/container.js +81 -103
  60. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  61. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  62. package/dist/lib/validation/at-rules/document.js +22 -27
  63. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  64. package/dist/lib/validation/at-rules/import.js +30 -81
  65. package/dist/lib/validation/at-rules/keyframes.js +19 -23
  66. package/dist/lib/validation/at-rules/layer.js +5 -5
  67. package/dist/lib/validation/at-rules/media.js +42 -53
  68. package/dist/lib/validation/at-rules/namespace.js +19 -23
  69. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  70. package/dist/lib/validation/at-rules/page.js +8 -7
  71. package/dist/lib/validation/at-rules/supports.js +73 -82
  72. package/dist/lib/validation/at-rules/when.js +32 -36
  73. package/dist/lib/validation/atrule.js +15 -18
  74. package/dist/lib/validation/config.js +24 -1
  75. package/dist/lib/validation/config.json.js +563 -63
  76. package/dist/lib/validation/parser/parse.js +196 -185
  77. package/dist/lib/validation/parser/types.js +1 -1
  78. package/dist/lib/validation/selector.js +8 -5
  79. package/dist/lib/validation/syntax.js +724 -1405
  80. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  81. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  82. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  83. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  84. package/dist/lib/validation/syntaxes/keyframe-block-list.js +6 -5
  85. package/dist/lib/validation/syntaxes/keyframe-selector.js +23 -105
  86. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  87. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  88. package/dist/lib/validation/syntaxes/relative-selector.js +17 -15
  89. package/dist/lib/validation/syntaxes/url.js +18 -22
  90. package/dist/lib/validation/utils/list.js +20 -2
  91. package/dist/lib/validation/utils/whitespace.js +2 -1
  92. package/dist/node/index.js +4 -2
  93. package/dist/node/load.js +6 -1
  94. package/dist/web/index.js +4 -2
  95. package/dist/web/load.js +5 -0
  96. package/package.json +16 -15
  97. package/dist/lib/renderer/color/prophotoRgb.js +0 -56
  98. package/dist/lib/validation/declaration.js +0 -94
  99. package/dist/lib/validation/syntaxes/image.js +0 -29
@@ -1,10 +1,11 @@
1
- import { ValidationLevel, EnumToken } from '../../ast/types.js';
1
+ import { SyntaxValidationResult, EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
- import '../../renderer/color/utils/constants.js';
6
- import '../../renderer/sourcemap/lib/encode.js';
5
+ import '../../parser/tokenize.js';
7
6
  import '../../parser/utils/config.js';
7
+ import { generalEnclosedFunc } from '../../renderer/color/utils/constants.js';
8
+ import '../../renderer/sourcemap/lib/encode.js';
8
9
  import { consumeWhitespace } from '../utils/whitespace.js';
9
10
  import { splitTokenList } from '../utils/list.js';
10
11
  import { validateMediaFeature, validateMediaCondition } from './media.js';
@@ -16,8 +17,8 @@ function validateAtRuleWhen(atRule, options, root) {
16
17
  if (slice.length == 0) {
17
18
  // @ts-ignore
18
19
  return {
19
- valid: ValidationLevel.Valid,
20
- matches: [],
20
+ valid: SyntaxValidationResult.Valid,
21
+ context: [],
21
22
  node: atRule,
22
23
  syntax: '@when',
23
24
  error: '',
@@ -25,14 +26,14 @@ function validateAtRuleWhen(atRule, options, root) {
25
26
  };
26
27
  }
27
28
  const result = validateAtRuleWhenQueryList(atRule.tokens, atRule);
28
- if (result.valid == ValidationLevel.Drop) {
29
+ if (result.valid == SyntaxValidationResult.Drop) {
29
30
  return result;
30
31
  }
31
32
  if (!('chi' in atRule)) {
32
33
  // @ts-ignore
33
34
  return {
34
- valid: ValidationLevel.Drop,
35
- matches: [],
35
+ valid: SyntaxValidationResult.Drop,
36
+ context: [],
36
37
  node: atRule,
37
38
  syntax: '@when',
38
39
  error: 'expected at-rule body',
@@ -40,12 +41,11 @@ function validateAtRuleWhen(atRule, options, root) {
40
41
  };
41
42
  }
42
43
  return {
43
- valid: ValidationLevel.Valid,
44
- matches: [],
44
+ valid: SyntaxValidationResult.Valid,
45
+ context: [],
45
46
  node: atRule,
46
47
  syntax: '@when',
47
- error: '',
48
- tokens: result.tokens
48
+ error: ''
49
49
  };
50
50
  }
51
51
  // media() = media( [ <mf-plain> | <mf-boolean> | <mf-range> ] )
@@ -61,10 +61,10 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
61
61
  continue;
62
62
  }
63
63
  while (split.length > 0) {
64
- if (split[0].typ != EnumToken.FunctionTokenType || !['media', 'supports', 'font-tech', 'font-format'].includes(split[0].val)) {
64
+ if (split[0].typ != EnumToken.FunctionTokenType || !generalEnclosedFunc.includes(split[0].val)) {
65
65
  result = {
66
- valid: ValidationLevel.Drop,
67
- matches: [],
66
+ valid: SyntaxValidationResult.Drop,
67
+ context: [],
68
68
  node: split[0] ?? atRule,
69
69
  syntax: '@when',
70
70
  error: 'unexpected token',
@@ -78,26 +78,24 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
78
78
  // result = valida
79
79
  if (chi.length != 1 || !(validateMediaFeature(chi[0]) || validateMediaCondition(split[0], atRule))) {
80
80
  result = {
81
- valid: ValidationLevel.Drop,
82
- matches: [],
81
+ valid: SyntaxValidationResult.Drop,
82
+ context: [],
83
83
  node: split[0] ?? atRule,
84
84
  syntax: 'media( [ <mf-plain> | <mf-boolean> | <mf-range> ] )',
85
- error: 'unexpected token',
86
- tokens: []
85
+ error: 'unexpected token'
87
86
  };
88
87
  break;
89
88
  }
90
89
  }
91
- else if (['supports', 'font-tech', 'font-format'].includes(split[0].val)) {
90
+ else if (generalEnclosedFunc.includes(split[0].val)) {
92
91
  // result = valida
93
92
  if (!validateSupportCondition(atRule, split[0])) {
94
93
  result = {
95
- valid: ValidationLevel.Drop,
96
- matches: [],
94
+ valid: SyntaxValidationResult.Drop,
95
+ context: [],
97
96
  node: split[0] ?? atRule,
98
97
  syntax: 'media( [ <mf-plain> | <mf-boolean> | <mf-range> ] )',
99
- error: 'unexpected token',
100
- tokens: []
98
+ error: 'unexpected token'
101
99
  };
102
100
  break;
103
101
  }
@@ -112,8 +110,8 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
112
110
  }
113
111
  if (![EnumToken.MediaFeatureAndTokenType, EnumToken.MediaFeatureOrTokenType].includes(split[0].typ)) {
114
112
  result = {
115
- valid: ValidationLevel.Drop,
116
- matches: [],
113
+ valid: SyntaxValidationResult.Drop,
114
+ context: [],
117
115
  node: split[0] ?? atRule,
118
116
  syntax: '@when',
119
117
  error: 'expecting and/or media-condition',
@@ -128,8 +126,8 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
128
126
  consumeWhitespace(split);
129
127
  if (split.length == 0) {
130
128
  result = {
131
- valid: ValidationLevel.Drop,
132
- matches: [],
129
+ valid: SyntaxValidationResult.Drop,
130
+ context: [],
133
131
  node: split[0] ?? atRule,
134
132
  syntax: '@when',
135
133
  error: 'expecting media-condition',
@@ -147,13 +145,12 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
147
145
  }
148
146
  if (matched.length == 0) {
149
147
  return {
150
- valid: ValidationLevel.Drop,
151
- matches: [],
148
+ valid: SyntaxValidationResult.Drop,
149
+ context: [],
152
150
  // @ts-ignore
153
151
  node: result?.node ?? atRule,
154
152
  syntax: '@when',
155
- error: 'invalid at-rule body',
156
- tokens: []
153
+ error: 'invalid at-rule body'
157
154
  };
158
155
  }
159
156
  tokenList.length = 0;
@@ -166,12 +163,11 @@ function validateAtRuleWhenQueryList(tokenList, atRule) {
166
163
  tokenList.push(...match);
167
164
  }
168
165
  return {
169
- valid: ValidationLevel.Valid,
170
- matches: [],
166
+ valid: SyntaxValidationResult.Valid,
167
+ context: [],
171
168
  node: atRule,
172
169
  syntax: '@when',
173
- error: '',
174
- tokens: tokenList
170
+ error: ''
175
171
  };
176
172
  }
177
173
 
@@ -1,10 +1,11 @@
1
- import { ValidationLevel, EnumToken } from '../ast/types.js';
1
+ import { SyntaxValidationResult, EnumToken } from '../ast/types.js';
2
2
  import '../ast/minify.js';
3
3
  import '../ast/walk.js';
4
4
  import '../parser/parse.js';
5
+ import '../parser/tokenize.js';
6
+ import '../parser/utils/config.js';
5
7
  import '../renderer/color/utils/constants.js';
6
8
  import '../renderer/sourcemap/lib/encode.js';
7
- import '../parser/utils/config.js';
8
9
  import { getSyntaxConfig, getParsedSyntax } from './config.js';
9
10
  import { validateAtRuleMedia } from './at-rules/media.js';
10
11
  import { validateAtRuleCounterStyle } from './at-rules/counter-style.js';
@@ -16,7 +17,6 @@ import { validateAtRuleLayer } from './at-rules/layer.js';
16
17
  import { validateAtRuleFontFeatureValues } from './at-rules/font-feature-values.js';
17
18
  import { validateAtRuleNamespace } from './at-rules/namespace.js';
18
19
  import { validateAtRuleDocument } from './at-rules/document.js';
19
- import { validateAtRuleKeyframes } from './at-rules/keyframes.js';
20
20
  import { validateAtRuleWhen } from './at-rules/when.js';
21
21
  import { validateAtRuleElse } from './at-rules/else.js';
22
22
  import { validateAtRuleContainer } from './at-rules/container.js';
@@ -26,18 +26,15 @@ function validateAtRule(atRule, options, root) {
26
26
  if (atRule.nam == 'charset') {
27
27
  const valid = atRule.val.match(/^"[a-zA-Z][a-zA-Z0-9_-]+"$/i) != null;
28
28
  return {
29
- valid: valid ? ValidationLevel.Valid : ValidationLevel.Drop,
29
+ valid: valid ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
30
30
  node: atRule,
31
31
  syntax: null,
32
32
  error: ''
33
33
  };
34
34
  }
35
- if (atRule.nam == 'keyframes') {
36
- return validateAtRuleKeyframes(atRule);
37
- }
38
35
  if (['font-face', 'view-transition', 'starting-style'].includes(atRule.nam)) {
39
36
  return {
40
- valid: ValidationLevel.Valid,
37
+ valid: SyntaxValidationResult.Valid,
41
38
  node: atRule,
42
39
  syntax: '@' + atRule.nam,
43
40
  error: ''
@@ -82,7 +79,7 @@ function validateAtRule(atRule, options, root) {
82
79
  if (['position-try', 'property', 'font-palette-values'].includes(atRule.nam)) {
83
80
  if (!('tokens' in atRule)) {
84
81
  return {
85
- valid: ValidationLevel.Drop,
82
+ valid: SyntaxValidationResult.Drop,
86
83
  node: atRule,
87
84
  syntax: '@' + atRule.nam,
88
85
  error: 'expected prelude'
@@ -90,7 +87,7 @@ function validateAtRule(atRule, options, root) {
90
87
  }
91
88
  if (!('chi' in atRule)) {
92
89
  return {
93
- valid: ValidationLevel.Drop,
90
+ valid: SyntaxValidationResult.Drop,
94
91
  node: atRule,
95
92
  syntax: '@' + atRule.nam,
96
93
  error: 'expected body'
@@ -99,7 +96,7 @@ function validateAtRule(atRule, options, root) {
99
96
  const chi = atRule.tokens.filter((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ != EnumToken.CommentTokenType);
100
97
  if (chi.length != 1) {
101
98
  return {
102
- valid: ValidationLevel.Drop,
99
+ valid: SyntaxValidationResult.Drop,
103
100
  node: atRule,
104
101
  syntax: '@' + atRule.nam,
105
102
  error: 'expected ' + (atRule.nam == 'property' ? 'custom-property-name' : 'dashed-ident')
@@ -108,7 +105,7 @@ function validateAtRule(atRule, options, root) {
108
105
  if (chi[0].typ != EnumToken.DashedIdenTokenType) {
109
106
  // @ts-ignore
110
107
  return {
111
- valid: ValidationLevel.Drop,
108
+ valid: SyntaxValidationResult.Drop,
112
109
  node: atRule,
113
110
  syntax: '@' + atRule.nam,
114
111
  error: 'expected ' + (atRule.nam == 'property' ? 'custom-property-name' : 'dashed-ident')
@@ -116,7 +113,7 @@ function validateAtRule(atRule, options, root) {
116
113
  }
117
114
  // @ts-ignore
118
115
  return {
119
- valid: ValidationLevel.Valid,
116
+ valid: SyntaxValidationResult.Valid,
120
117
  node: atRule,
121
118
  syntax: '@' + atRule.nam,
122
119
  error: ''
@@ -130,7 +127,7 @@ function validateAtRule(atRule, options, root) {
130
127
  if (!(root == null || (root.typ == EnumToken.AtRuleNodeType && root.nam == 'page'))) {
131
128
  // @ts-ignore
132
129
  return {
133
- valid: ValidationLevel.Drop,
130
+ valid: SyntaxValidationResult.Drop,
134
131
  node: atRule,
135
132
  syntax: '@page',
136
133
  error: 'not allowed here'
@@ -150,14 +147,14 @@ function validateAtRule(atRule, options, root) {
150
147
  if (!(name in config.atRules)) {
151
148
  if (options.lenient) {
152
149
  return {
153
- valid: ValidationLevel.Lenient,
150
+ valid: SyntaxValidationResult.Lenient,
154
151
  node: atRule,
155
152
  syntax: null,
156
153
  error: ''
157
154
  };
158
155
  }
159
156
  return {
160
- valid: ValidationLevel.Drop,
157
+ valid: SyntaxValidationResult.Drop,
161
158
  node: atRule,
162
159
  syntax: null,
163
160
  error: 'unknown at-rule'
@@ -166,7 +163,7 @@ function validateAtRule(atRule, options, root) {
166
163
  const syntax = getParsedSyntax("atRules" /* ValidationSyntaxGroupEnum.AtRules */, name)?.[0];
167
164
  if ('chi' in syntax && !('chi' in atRule)) {
168
165
  return {
169
- valid: ValidationLevel.Drop,
166
+ valid: SyntaxValidationResult.Drop,
170
167
  node: atRule,
171
168
  syntax,
172
169
  error: 'missing at-rule body'
@@ -177,7 +174,7 @@ function validateAtRule(atRule, options, root) {
177
174
  // return validateSyntax(syntax.prelude as ValidationToken[], atRule.tokens as Token[], root, options);
178
175
  // }
179
176
  return {
180
- valid: ValidationLevel.Valid,
177
+ valid: SyntaxValidationResult.Valid,
181
178
  node: null,
182
179
  syntax,
183
180
  error: ''
@@ -8,6 +8,29 @@ function getSyntaxConfig() {
8
8
  // @ts-ignore
9
9
  return config;
10
10
  }
11
+ function getSyntax(group, key) {
12
+ // @ts-ignore
13
+ let obj = config[group];
14
+ const keys = Array.isArray(key) ? key : [key];
15
+ for (let i = 0; i < keys.length; i++) {
16
+ key = keys[i];
17
+ if (!(key in obj)) {
18
+ if ((i == 0 && key.charAt(0) == '@') || key.charAt(0) == '-') {
19
+ const matches = key.match(/^(@?)(-[a-zA-Z]+)-(.*?)$/);
20
+ if (matches != null) {
21
+ key = matches[1] + matches[3];
22
+ }
23
+ }
24
+ if (!(key in obj)) {
25
+ return null;
26
+ }
27
+ }
28
+ // @ts-ignore
29
+ obj = obj[key];
30
+ }
31
+ // @ts-ignore
32
+ return obj?.syntax ?? null;
33
+ }
11
34
  function getParsedSyntax(group, key) {
12
35
  // @ts-ignore
13
36
  let obj = config[group];
@@ -39,4 +62,4 @@ function getParsedSyntax(group, key) {
39
62
  return parsedSyntaxes.get(index);
40
63
  }
41
64
 
42
- export { getParsedSyntax, getSyntaxConfig };
65
+ export { getParsedSyntax, getSyntax, getSyntaxConfig };