@tbela99/css-parser 1.0.0 → 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 (86) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/README.md +16 -11
  3. package/dist/index-umd-web.js +3613 -1829
  4. package/dist/index.cjs +3611 -1827
  5. package/dist/index.d.ts +160 -50
  6. package/dist/lib/ast/expand.js +2 -1
  7. package/dist/lib/ast/features/calc.js +12 -1
  8. package/dist/lib/ast/features/inlinecssvariables.js +47 -24
  9. package/dist/lib/ast/features/prefix.js +117 -86
  10. package/dist/lib/ast/features/shorthand.js +29 -6
  11. package/dist/lib/ast/features/transform.js +10 -3
  12. package/dist/lib/ast/features/type.js +7 -0
  13. package/dist/lib/ast/math/expression.js +7 -1
  14. package/dist/lib/ast/math/math.js +6 -0
  15. package/dist/lib/ast/minify.js +165 -77
  16. package/dist/lib/ast/transform/compute.js +1 -0
  17. package/dist/lib/ast/transform/matrix.js +1 -0
  18. package/dist/lib/ast/types.js +17 -15
  19. package/dist/lib/ast/walk.js +33 -7
  20. package/dist/lib/fs/resolve.js +10 -0
  21. package/dist/lib/parser/declaration/list.js +48 -45
  22. package/dist/lib/parser/declaration/map.js +1 -0
  23. package/dist/lib/parser/declaration/set.js +2 -1
  24. package/dist/lib/parser/parse.js +364 -276
  25. package/dist/lib/parser/tokenize.js +147 -72
  26. package/dist/lib/parser/utils/declaration.js +4 -3
  27. package/dist/lib/parser/utils/type.js +2 -1
  28. package/dist/lib/renderer/color/a98rgb.js +2 -1
  29. package/dist/lib/renderer/color/color-mix.js +10 -7
  30. package/dist/lib/renderer/color/color.js +171 -153
  31. package/dist/lib/renderer/color/hex.js +2 -1
  32. package/dist/lib/renderer/color/hsl.js +2 -1
  33. package/dist/lib/renderer/color/hwb.js +2 -1
  34. package/dist/lib/renderer/color/lab.js +2 -1
  35. package/dist/lib/renderer/color/lch.js +2 -1
  36. package/dist/lib/renderer/color/oklab.js +2 -1
  37. package/dist/lib/renderer/color/oklch.js +2 -1
  38. package/dist/lib/renderer/color/p3.js +2 -1
  39. package/dist/lib/renderer/color/rec2020.js +2 -1
  40. package/dist/lib/renderer/color/relativecolor.js +17 -11
  41. package/dist/lib/renderer/color/rgb.js +4 -3
  42. package/dist/lib/renderer/color/srgb.js +18 -17
  43. package/dist/lib/renderer/color/utils/components.js +6 -5
  44. package/dist/lib/renderer/color/utils/constants.js +47 -3
  45. package/dist/lib/renderer/color/xyz.js +2 -1
  46. package/dist/lib/renderer/color/xyzd50.js +2 -1
  47. package/dist/lib/renderer/render.js +48 -20
  48. package/dist/lib/syntax/syntax.js +253 -140
  49. package/dist/lib/validation/at-rules/container.js +75 -97
  50. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  51. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  52. package/dist/lib/validation/at-rules/document.js +22 -27
  53. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  54. package/dist/lib/validation/at-rules/import.js +30 -81
  55. package/dist/lib/validation/at-rules/keyframes.js +18 -22
  56. package/dist/lib/validation/at-rules/layer.js +5 -5
  57. package/dist/lib/validation/at-rules/media.js +42 -52
  58. package/dist/lib/validation/at-rules/namespace.js +19 -23
  59. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  60. package/dist/lib/validation/at-rules/page.js +8 -7
  61. package/dist/lib/validation/at-rules/supports.js +73 -82
  62. package/dist/lib/validation/at-rules/when.js +32 -36
  63. package/dist/lib/validation/atrule.js +15 -14
  64. package/dist/lib/validation/config.js +24 -1
  65. package/dist/lib/validation/config.json.js +563 -63
  66. package/dist/lib/validation/parser/parse.js +196 -185
  67. package/dist/lib/validation/parser/types.js +1 -1
  68. package/dist/lib/validation/selector.js +3 -3
  69. package/dist/lib/validation/syntax.js +828 -0
  70. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  71. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  72. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  73. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
  76. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  78. package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
  79. package/dist/lib/validation/syntaxes/url.js +18 -22
  80. package/dist/lib/validation/utils/list.js +2 -1
  81. package/dist/lib/validation/utils/whitespace.js +2 -1
  82. package/dist/node/index.js +4 -2
  83. package/dist/node/load.js +5 -0
  84. package/dist/web/index.js +4 -2
  85. package/dist/web/load.js +5 -0
  86. package/package.json +12 -11
@@ -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';
@@ -25,7 +26,7 @@ function validateAtRule(atRule, options, root) {
25
26
  if (atRule.nam == 'charset') {
26
27
  const valid = atRule.val.match(/^"[a-zA-Z][a-zA-Z0-9_-]+"$/i) != null;
27
28
  return {
28
- valid: valid ? ValidationLevel.Valid : ValidationLevel.Drop,
29
+ valid: valid ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
29
30
  node: atRule,
30
31
  syntax: null,
31
32
  error: ''
@@ -33,7 +34,7 @@ function validateAtRule(atRule, options, root) {
33
34
  }
34
35
  if (['font-face', 'view-transition', 'starting-style'].includes(atRule.nam)) {
35
36
  return {
36
- valid: ValidationLevel.Valid,
37
+ valid: SyntaxValidationResult.Valid,
37
38
  node: atRule,
38
39
  syntax: '@' + atRule.nam,
39
40
  error: ''
@@ -78,7 +79,7 @@ function validateAtRule(atRule, options, root) {
78
79
  if (['position-try', 'property', 'font-palette-values'].includes(atRule.nam)) {
79
80
  if (!('tokens' in atRule)) {
80
81
  return {
81
- valid: ValidationLevel.Drop,
82
+ valid: SyntaxValidationResult.Drop,
82
83
  node: atRule,
83
84
  syntax: '@' + atRule.nam,
84
85
  error: 'expected prelude'
@@ -86,7 +87,7 @@ function validateAtRule(atRule, options, root) {
86
87
  }
87
88
  if (!('chi' in atRule)) {
88
89
  return {
89
- valid: ValidationLevel.Drop,
90
+ valid: SyntaxValidationResult.Drop,
90
91
  node: atRule,
91
92
  syntax: '@' + atRule.nam,
92
93
  error: 'expected body'
@@ -95,7 +96,7 @@ function validateAtRule(atRule, options, root) {
95
96
  const chi = atRule.tokens.filter((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ != EnumToken.CommentTokenType);
96
97
  if (chi.length != 1) {
97
98
  return {
98
- valid: ValidationLevel.Drop,
99
+ valid: SyntaxValidationResult.Drop,
99
100
  node: atRule,
100
101
  syntax: '@' + atRule.nam,
101
102
  error: 'expected ' + (atRule.nam == 'property' ? 'custom-property-name' : 'dashed-ident')
@@ -104,7 +105,7 @@ function validateAtRule(atRule, options, root) {
104
105
  if (chi[0].typ != EnumToken.DashedIdenTokenType) {
105
106
  // @ts-ignore
106
107
  return {
107
- valid: ValidationLevel.Drop,
108
+ valid: SyntaxValidationResult.Drop,
108
109
  node: atRule,
109
110
  syntax: '@' + atRule.nam,
110
111
  error: 'expected ' + (atRule.nam == 'property' ? 'custom-property-name' : 'dashed-ident')
@@ -112,7 +113,7 @@ function validateAtRule(atRule, options, root) {
112
113
  }
113
114
  // @ts-ignore
114
115
  return {
115
- valid: ValidationLevel.Valid,
116
+ valid: SyntaxValidationResult.Valid,
116
117
  node: atRule,
117
118
  syntax: '@' + atRule.nam,
118
119
  error: ''
@@ -126,7 +127,7 @@ function validateAtRule(atRule, options, root) {
126
127
  if (!(root == null || (root.typ == EnumToken.AtRuleNodeType && root.nam == 'page'))) {
127
128
  // @ts-ignore
128
129
  return {
129
- valid: ValidationLevel.Drop,
130
+ valid: SyntaxValidationResult.Drop,
130
131
  node: atRule,
131
132
  syntax: '@page',
132
133
  error: 'not allowed here'
@@ -146,14 +147,14 @@ function validateAtRule(atRule, options, root) {
146
147
  if (!(name in config.atRules)) {
147
148
  if (options.lenient) {
148
149
  return {
149
- valid: ValidationLevel.Lenient,
150
+ valid: SyntaxValidationResult.Lenient,
150
151
  node: atRule,
151
152
  syntax: null,
152
153
  error: ''
153
154
  };
154
155
  }
155
156
  return {
156
- valid: ValidationLevel.Drop,
157
+ valid: SyntaxValidationResult.Drop,
157
158
  node: atRule,
158
159
  syntax: null,
159
160
  error: 'unknown at-rule'
@@ -162,7 +163,7 @@ function validateAtRule(atRule, options, root) {
162
163
  const syntax = getParsedSyntax("atRules" /* ValidationSyntaxGroupEnum.AtRules */, name)?.[0];
163
164
  if ('chi' in syntax && !('chi' in atRule)) {
164
165
  return {
165
- valid: ValidationLevel.Drop,
166
+ valid: SyntaxValidationResult.Drop,
166
167
  node: atRule,
167
168
  syntax,
168
169
  error: 'missing at-rule body'
@@ -173,7 +174,7 @@ function validateAtRule(atRule, options, root) {
173
174
  // return validateSyntax(syntax.prelude as ValidationToken[], atRule.tokens as Token[], root, options);
174
175
  // }
175
176
  return {
176
- valid: ValidationLevel.Valid,
177
+ valid: SyntaxValidationResult.Valid,
177
178
  node: null,
178
179
  syntax,
179
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 };