@tbela99/css-parser 0.7.1 → 0.9.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 (90) hide show
  1. package/.editorconfig +484 -0
  2. package/README.md +140 -84
  3. package/dist/index-umd-web.js +8461 -51655
  4. package/dist/index.cjs +8437 -51636
  5. package/dist/index.d.ts +220 -68
  6. package/dist/lib/ast/expand.js +46 -9
  7. package/dist/lib/ast/features/calc.js +76 -12
  8. package/dist/lib/ast/features/inlinecssvariables.js +6 -1
  9. package/dist/lib/ast/features/prefix.js +17 -9
  10. package/dist/lib/ast/features/shorthand.js +1 -0
  11. package/dist/lib/ast/math/expression.js +299 -11
  12. package/dist/lib/ast/math/math.js +7 -1
  13. package/dist/lib/ast/minify.js +30 -16
  14. package/dist/lib/ast/types.js +59 -49
  15. package/dist/lib/ast/walk.js +92 -18
  16. package/dist/lib/parser/declaration/list.js +1 -0
  17. package/dist/lib/parser/declaration/map.js +60 -52
  18. package/dist/lib/parser/declaration/set.js +1 -12
  19. package/dist/lib/parser/parse.js +371 -119
  20. package/dist/lib/parser/tokenize.js +31 -6
  21. package/dist/lib/parser/utils/declaration.js +2 -2
  22. package/dist/lib/parser/utils/type.js +6 -6
  23. package/dist/lib/renderer/color/a98rgb.js +1 -0
  24. package/dist/lib/renderer/color/color.js +1 -0
  25. package/dist/lib/renderer/color/colormix.js +1 -0
  26. package/dist/lib/renderer/color/hex.js +2 -1
  27. package/dist/lib/renderer/color/hsl.js +2 -1
  28. package/dist/lib/renderer/color/hwb.js +3 -2
  29. package/dist/lib/renderer/color/lab.js +2 -1
  30. package/dist/lib/renderer/color/lch.js +2 -1
  31. package/dist/lib/renderer/color/oklab.js +3 -2
  32. package/dist/lib/renderer/color/oklch.js +2 -1
  33. package/dist/lib/renderer/color/p3.js +2 -1
  34. package/dist/lib/renderer/color/prophotoRgb.js +56 -0
  35. package/dist/lib/renderer/color/prophotorgb.js +1 -1
  36. package/dist/lib/renderer/color/rec2020.js +1 -0
  37. package/dist/lib/renderer/color/relativecolor.js +52 -28
  38. package/dist/lib/renderer/color/rgb.js +2 -1
  39. package/dist/lib/renderer/color/srgb.js +3 -2
  40. package/dist/lib/renderer/color/utils/components.js +1 -0
  41. package/dist/lib/renderer/color/utils/constants.js +2 -1
  42. package/dist/lib/renderer/color/xyz.js +2 -1
  43. package/dist/lib/renderer/color/xyzd50.js +1 -0
  44. package/dist/lib/renderer/render.js +62 -12
  45. package/dist/lib/syntax/syntax.js +362 -4
  46. package/dist/lib/validation/at-rules/container.js +353 -0
  47. package/dist/lib/validation/at-rules/counter-style.js +78 -0
  48. package/dist/lib/validation/at-rules/custom-media.js +52 -0
  49. package/dist/lib/validation/at-rules/document.js +114 -0
  50. package/dist/lib/validation/at-rules/else.js +5 -0
  51. package/dist/lib/validation/at-rules/font-feature-values.js +52 -0
  52. package/dist/lib/validation/at-rules/import.js +199 -0
  53. package/dist/lib/validation/at-rules/keyframes.js +70 -0
  54. package/dist/lib/validation/at-rules/layer.js +30 -0
  55. package/dist/lib/validation/at-rules/media.js +254 -0
  56. package/dist/lib/validation/at-rules/namespace.js +85 -0
  57. package/dist/lib/validation/at-rules/page-margin-box.js +56 -0
  58. package/dist/lib/validation/at-rules/page.js +88 -0
  59. package/dist/lib/validation/at-rules/supports.js +262 -0
  60. package/dist/lib/validation/at-rules/when.js +178 -0
  61. package/dist/lib/validation/atrule.js +187 -0
  62. package/dist/lib/validation/config.js +35 -2
  63. package/dist/lib/validation/config.json.js +1683 -50905
  64. package/dist/lib/validation/declaration.js +102 -0
  65. package/dist/lib/validation/parser/parse.js +1137 -7
  66. package/dist/lib/validation/parser/types.js +28 -12
  67. package/dist/lib/validation/selector.js +26 -444
  68. package/dist/lib/validation/syntax.js +1475 -0
  69. package/dist/lib/validation/syntaxes/complex-selector-list.js +45 -0
  70. package/dist/lib/validation/syntaxes/complex-selector.js +53 -0
  71. package/dist/lib/validation/syntaxes/compound-selector.js +226 -0
  72. package/dist/lib/validation/syntaxes/family-name.js +91 -0
  73. package/dist/lib/validation/syntaxes/image.js +29 -0
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +27 -0
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +137 -0
  76. package/dist/lib/validation/syntaxes/layer-name.js +67 -0
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +57 -0
  78. package/dist/lib/validation/syntaxes/relative-selector.js +36 -0
  79. package/dist/lib/validation/syntaxes/selector-list.js +5 -0
  80. package/dist/lib/validation/syntaxes/selector.js +5 -0
  81. package/dist/lib/validation/syntaxes/url.js +75 -0
  82. package/dist/lib/validation/utils/list.js +24 -0
  83. package/dist/lib/validation/utils/whitespace.js +22 -0
  84. package/dist/node/index.js +5 -5
  85. package/dist/web/index.js +5 -1
  86. package/dist/web/load.js +1 -0
  87. package/package.json +16 -14
  88. package/dist/lib/ast/utils/minifyfeature.js +0 -9
  89. package/dist/lib/iterable/weakset.js +0 -58
  90. package/dist/lib/parser/utils/syntax.js +0 -450
@@ -0,0 +1,187 @@
1
+ import { ValidationLevel, EnumToken } from '../ast/types.js';
2
+ import '../ast/minify.js';
3
+ import '../ast/walk.js';
4
+ import '../parser/parse.js';
5
+ import '../renderer/color/utils/constants.js';
6
+ import '../renderer/sourcemap/lib/encode.js';
7
+ import '../parser/utils/config.js';
8
+ import { getSyntaxConfig, getParsedSyntax } from './config.js';
9
+ import { validateAtRuleMedia } from './at-rules/media.js';
10
+ import { validateAtRuleCounterStyle } from './at-rules/counter-style.js';
11
+ import { validateAtRulePage } from './at-rules/page.js';
12
+ import { validateAtRulePageMarginBox } from './at-rules/page-margin-box.js';
13
+ import { validateAtRuleSupports } from './at-rules/supports.js';
14
+ import { validateAtRuleImport } from './at-rules/import.js';
15
+ import { validateAtRuleLayer } from './at-rules/layer.js';
16
+ import { validateAtRuleFontFeatureValues } from './at-rules/font-feature-values.js';
17
+ import { validateAtRuleNamespace } from './at-rules/namespace.js';
18
+ import { validateAtRuleDocument } from './at-rules/document.js';
19
+ import { validateAtRuleKeyframes } from './at-rules/keyframes.js';
20
+ import { validateAtRuleWhen } from './at-rules/when.js';
21
+ import { validateAtRuleElse } from './at-rules/else.js';
22
+ import { validateAtRuleContainer } from './at-rules/container.js';
23
+ import { validateAtRuleCustomMedia } from './at-rules/custom-media.js';
24
+
25
+ function validateAtRule(atRule, options, root) {
26
+ if (atRule.nam == 'charset') {
27
+ const valid = atRule.val.match(/^"[a-zA-Z][a-zA-Z0-9_-]+"$/i) != null;
28
+ return {
29
+ valid: valid ? ValidationLevel.Valid : ValidationLevel.Drop,
30
+ node: atRule,
31
+ syntax: null,
32
+ error: ''
33
+ };
34
+ }
35
+ if (atRule.nam == 'keyframes') {
36
+ return validateAtRuleKeyframes(atRule);
37
+ }
38
+ if (['font-face', 'view-transition', 'starting-style'].includes(atRule.nam)) {
39
+ return {
40
+ valid: ValidationLevel.Valid,
41
+ node: atRule,
42
+ syntax: '@' + atRule.nam,
43
+ error: ''
44
+ };
45
+ }
46
+ if (atRule.nam == 'media') {
47
+ return validateAtRuleMedia(atRule);
48
+ }
49
+ if (atRule.nam == 'import') {
50
+ return validateAtRuleImport(atRule);
51
+ }
52
+ if (atRule.nam == 'supports') {
53
+ return validateAtRuleSupports(atRule);
54
+ }
55
+ if (atRule.nam == 'counter-style') {
56
+ return validateAtRuleCounterStyle(atRule);
57
+ }
58
+ if (atRule.nam == 'layer') {
59
+ return validateAtRuleLayer(atRule);
60
+ }
61
+ if (atRule.nam == 'font-feature-values') {
62
+ return validateAtRuleFontFeatureValues(atRule);
63
+ }
64
+ if (atRule.nam == 'namespace') {
65
+ return validateAtRuleNamespace(atRule);
66
+ }
67
+ if (atRule.nam == 'when') {
68
+ return validateAtRuleWhen(atRule);
69
+ }
70
+ if (atRule.nam == 'else') {
71
+ return validateAtRuleElse(atRule);
72
+ }
73
+ if (atRule.nam == 'container') {
74
+ return validateAtRuleContainer(atRule);
75
+ }
76
+ if (atRule.nam == 'document') {
77
+ return validateAtRuleDocument(atRule);
78
+ }
79
+ if (atRule.nam == 'custom-media') {
80
+ return validateAtRuleCustomMedia(atRule);
81
+ }
82
+ if (['position-try', 'property', 'font-palette-values'].includes(atRule.nam)) {
83
+ if (!('tokens' in atRule)) {
84
+ return {
85
+ valid: ValidationLevel.Drop,
86
+ node: atRule,
87
+ syntax: '@' + atRule.nam,
88
+ error: 'expected prelude'
89
+ };
90
+ }
91
+ if (!('chi' in atRule)) {
92
+ return {
93
+ valid: ValidationLevel.Drop,
94
+ node: atRule,
95
+ syntax: '@' + atRule.nam,
96
+ error: 'expected body'
97
+ };
98
+ }
99
+ const chi = atRule.tokens.filter((t) => t.typ != EnumToken.WhitespaceTokenType && t.typ != EnumToken.CommentTokenType);
100
+ if (chi.length != 1) {
101
+ return {
102
+ valid: ValidationLevel.Drop,
103
+ node: atRule,
104
+ syntax: '@' + atRule.nam,
105
+ error: 'expected ' + (atRule.nam == 'property' ? 'custom-property-name' : 'dashed-ident')
106
+ };
107
+ }
108
+ if (chi[0].typ != EnumToken.DashedIdenTokenType) {
109
+ // @ts-ignore
110
+ return {
111
+ valid: ValidationLevel.Drop,
112
+ node: atRule,
113
+ syntax: '@' + atRule.nam,
114
+ error: 'expected ' + (atRule.nam == 'property' ? 'custom-property-name' : 'dashed-ident')
115
+ };
116
+ }
117
+ // @ts-ignore
118
+ return {
119
+ valid: ValidationLevel.Valid,
120
+ node: atRule,
121
+ syntax: '@' + atRule.nam,
122
+ error: ''
123
+ };
124
+ }
125
+ // scope
126
+ if (atRule.nam == 'page') {
127
+ return validateAtRulePage(atRule);
128
+ }
129
+ if (['top-left-corner', 'top-left', 'top-center', 'top-right', 'top-right-corner', 'bottom-left-corner', 'bottom-left', 'bottom-center', 'bottom-right', 'bottom-right-corner', 'left-top', 'left-middle', 'left-bottom', 'right-top', 'right-middle', 'right-bottom'].includes(atRule.nam)) {
130
+ if (!(root == null || (root.typ == EnumToken.AtRuleNodeType && root.nam == 'page'))) {
131
+ // @ts-ignore
132
+ return {
133
+ valid: ValidationLevel.Drop,
134
+ node: atRule,
135
+ syntax: '@page',
136
+ error: 'not allowed here'
137
+ };
138
+ }
139
+ return validateAtRulePageMarginBox(atRule);
140
+ }
141
+ // handle keyframe as special case
142
+ // check if the node exists
143
+ const config = getSyntaxConfig();
144
+ let name = '@' + atRule.nam;
145
+ if (!(name in config.atRules)) {
146
+ if (name.charAt(1) == '-') {
147
+ name = name.replace(/@-[a-zA-Z]+-([a-zA-Z][a-zA-Z0-9_-]*)/, '@$1');
148
+ }
149
+ }
150
+ if (!(name in config.atRules)) {
151
+ if (options.lenient) {
152
+ return {
153
+ valid: ValidationLevel.Lenient,
154
+ node: atRule,
155
+ syntax: null,
156
+ error: ''
157
+ };
158
+ }
159
+ return {
160
+ valid: ValidationLevel.Drop,
161
+ node: atRule,
162
+ syntax: null,
163
+ error: 'unknown at-rule'
164
+ };
165
+ }
166
+ const syntax = getParsedSyntax("atRules" /* ValidationSyntaxGroupEnum.AtRules */, name)?.[0];
167
+ if ('chi' in syntax && !('chi' in atRule)) {
168
+ return {
169
+ valid: ValidationLevel.Drop,
170
+ node: atRule,
171
+ syntax,
172
+ error: 'missing at-rule body'
173
+ };
174
+ }
175
+ // if ('prelude' in syntax) {
176
+ //
177
+ // return validateSyntax(syntax.prelude as ValidationToken[], atRule.tokens as Token[], root, options);
178
+ // }
179
+ return {
180
+ valid: ValidationLevel.Valid,
181
+ node: null,
182
+ syntax,
183
+ error: ''
184
+ };
185
+ }
186
+
187
+ export { validateAtRule };
@@ -1,9 +1,42 @@
1
1
  import config from './config.json.js';
2
+ import './parser/types.js';
3
+ import { parseSyntax } from './parser/parse.js';
2
4
 
5
+ const parsedSyntaxes = new Map();
3
6
  Object.freeze(config);
4
- function getConfig() {
7
+ function getSyntaxConfig() {
5
8
  // @ts-ignore
6
9
  return config;
7
10
  }
11
+ function getParsedSyntax(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
+ const index = group + '.' + keys.join('.');
32
+ // @ts-ignore
33
+ if (!parsedSyntaxes.has(index)) {
34
+ // @ts-ignore
35
+ const syntax = parseSyntax(obj.syntax);
36
+ // @ts-ignore
37
+ parsedSyntaxes.set(index, syntax.chi);
38
+ }
39
+ return parsedSyntaxes.get(index);
40
+ }
8
41
 
9
- export { getConfig };
42
+ export { getParsedSyntax, getSyntaxConfig };