@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
package/CHANGELOG.md CHANGED
@@ -1,7 +1,357 @@
1
1
  # Changelog
2
2
 
3
+ ## v1.4.4
4
+
5
+ ### Validation changes
6
+
7
+ - [x] Added the `state` and `errors` properties to nodes to expose their validation status and any associated validation errors.
8
+ - [x] Nodes that fail validation are no longer discarded. Nodes are discarded only if they are invalid, unparsed, or malformed. Unknown nodes are discarded when lenient parsing is disabled.
9
+
10
+ ### Color
11
+
12
+ - [x] `alpha()`: Added support for minifying the CSS Color 5 `alpha()` color function.
13
+
14
+ ```ts
15
+
16
+ import {transform} from '@tbela99/css-parser';
17
+
18
+ const css = `
19
+
20
+ :root {
21
+ --mycolor: oklch(60% 0.25 315 / 0.8);
22
+ }
23
+ .s {
24
+
25
+ color:
26
+ alpha(from var(--mycolor) / calc(alpha * 0.5));
27
+
28
+ `;
29
+
30
+ const result = await transform(css, {
31
+ beautify: true,
32
+ inlineCssVariables: true,
33
+ });
34
+
35
+ console.debug(result.code);
36
+
37
+ ```
38
+
39
+ result:
40
+
41
+ ```css
42
+ .s {
43
+ color: #b538e366
44
+ }
45
+ ```
46
+
47
+ - [x] `color-mix()`: Implemented support for the updated `color-mix()` syntax defined in https://www.w3.org/TR/css-values-5/#color-mix
48
+
49
+
50
+ ```ts
51
+
52
+ import {transform} from '@tbela99/css-parser';
53
+
54
+ const css = `
55
+ .hsl { color: color-mix(firebrick, goldenrod); }
56
+ `;
57
+
58
+ const result = await transform(css, {
59
+ beautify: true,
60
+ convertColor: ColorType.OKLAB
61
+ });
62
+
63
+ console.debug(result.code);
64
+
65
+ ```
66
+
67
+ result:
68
+
69
+ ```css
70
+ .hsl {
71
+ color: oklab(.624172 .087878 .113592)
72
+ }
73
+ ```
74
+
75
+ ### Webkit gradient prefix removal
76
+
77
+ - [x] Convert -webkit-* gradient functions
78
+ - [x] -webkit-gradient() to:
79
+ - [x] linear-gradient()
80
+ - [ ] radial-gradient() - not supported ⚠️
81
+ - [x] -webkit-linear-gradient()
82
+ - [x] -webkit-repeating-linear-gradient()
83
+ - [x] -webkit-radial-gradient()
84
+ - [x] -webkit-repeating-radial-gradient()
85
+
86
+ ### CSS gradient minification
87
+
88
+ - [x] Minify gradient functions
89
+ - [x] linear-gradient()
90
+ - [x] radial-gradient()
91
+ - [x] conic-gradient()
92
+ - [x] repeating-linear-gradient()
93
+ - [x] repeating-radial-gradient()
94
+ - [x] repeating-conic-gradient()
95
+
96
+ ### Other changes
97
+
98
+ - [x] Minify transform-origin property
99
+ - [x] Changing oklab distance threshold to `0.00001` according to https://drafts.csswg.org/css-color-4/#comparing-color-values
100
+
101
+ ### Bug fixes
102
+ - [x] Fix path resolution on Windows
103
+ - [x] Do not minify supports() arguments
104
+
105
+ CSS incorrectly parsed:
106
+
107
+ ```css
108
+ body {
109
+ background-color: if(
110
+ supports(color: oklch(0.7 0.185 232)): oklch(0.7 0.185 232);
111
+ else: #00adf3;
112
+ );
113
+
114
+ &::after {
115
+ content: if(
116
+ supports(color: oklch(0.7 0.185 232)): "Your browser supports OKLCH";
117
+ else: "Your browser does not support OKLCH";
118
+ );
119
+ }
120
+ }
121
+ ```
122
+
123
+ result:
124
+
125
+ ```css
126
+ body {
127
+ background-color: if(supports(color:#00aefc):#00aefc;else:#00adf3);
128
+ &:after {
129
+ content: if(supports(color:#00aefc):"Your browser supports OKLCH";else:"Your browser does not support OKLCH")
130
+ }
131
+ }
132
+ ```
133
+
134
+ instead of:
135
+
136
+ ```css
137
+ body {
138
+ background-color: #00adf3;
139
+ @supports (color:oklch(.7 .185 232)) {
140
+ background-color: #00aefc
141
+ }
142
+ &:after {
143
+ content: "Your browser does not support OKLCH";
144
+ @supports (color:oklch(.7 .185 232)) {
145
+ content: "Your browser supports OKLCH"
146
+ }
147
+ }
148
+ }
149
+ ```
150
+
151
+ - [x] Incorrectly parse selector when removePrefix and css module settings are enabled
152
+
153
+ ```css
154
+ *,:before,:after {
155
+ box-sizing: border-box
156
+ }
157
+ ```
158
+ is parsed and rendered as:
159
+
160
+ ```css
161
+ ,,*:before:after {
162
+ box-sizing: border-box
163
+ }
164
+ ```
165
+
166
+ instead of:
167
+
168
+ ```css
169
+ *,:before,:after {
170
+ box-sizing: border-box
171
+ }
172
+ ```
173
+
174
+ ### Deprecation
175
+
176
+ #### Loading as commonjs module
177
+ - [x] Deprecate commonjs entry point. It will be removed in a future release.
178
+
179
+ #### Functions
180
+ - [x] Deprecate `parseFile()`. Use `parse({file, asStream, ...options})` instead.
181
+ - [x] Deprecate `transformFile()`. Use `transform({file, asStream, ...options})` instead
182
+
183
+ #### Enums
184
+ - [x] Deprecate using `ValidationLevel` to configure parsing validation settings. Use `boolean` values instead.
185
+ - [x] Deprecate invalid `EnumToken` node types. There are no longer used.
186
+ - [x] `EnumToken.InvalidRuleNodeType`
187
+ - [x] `EnumToken.InvalidAtRuleNodeType`
188
+ - [x] `EnumToken.InvalidDeclarationNodeType`
189
+
190
+ #### Interfaces
191
+ - [x] `AstInvalidRule`
192
+ - [x] `AstInvalidDeclaration`
193
+ - [x] `AstInvalidAtRule`
194
+
195
+ ## v1.4.3
196
+
197
+ ### CSS Modules
198
+
199
+ - [x] Add css module scope 'shortest' to produce short scoped names
200
+
201
+ ```typescript
202
+
203
+ result = await transform(css, {
204
+
205
+ beautify: true,
206
+ module: {scoped: ModuleScopeEnumOptions.Shortest
207
+ }
208
+ });
209
+
210
+ console.log(result.code);
211
+ ```
212
+
213
+ output
214
+
215
+ ```css
216
+ .a {
217
+ background: red;
218
+ color: #ff0
219
+ }
220
+ .b {
221
+ background: blue
222
+ }
223
+ ```
224
+
225
+ ### CSS if() function
226
+
227
+ - [x] support if() css function syntax https://drafts.csswg.org/css-values-5/#if-notation
228
+ - [x] convert css if() function into legacy syntax
229
+
230
+ ```typescript
231
+
232
+ const css = `
233
+ button {
234
+ background: linear-gradient(
235
+ if(media(min-width: 768px): to right; else: to bottom),
236
+ if(style(--dark-mode): #333; else: #fff),
237
+ if(style(--dark-mode): #000; else: #ccc)
238
+ );
239
+ }`;
240
+
241
+ result = await transform(css, {
242
+
243
+ beautify: true,
244
+ expandIfSyntax: true
245
+ }
246
+
247
+ });
248
+
249
+ console.log(result.code);
250
+ ```
251
+
252
+ output
253
+
254
+ ```css
255
+ button {
256
+ background: linear-gradient(to bottom,#fff,#ccc);
257
+ @media (min-width:768px) {
258
+ background: linear-gradient(to right,#fff,#ccc);
259
+ @container style(--dark-mode) {
260
+ background: linear-gradient(to right,#333,#000)
261
+ }
262
+ }
263
+ @container style(--dark-mode) {
264
+ background: linear-gradient(to bottom,#333,#000)
265
+ }
266
+ }
267
+ ```
268
+
269
+
270
+ ### CSS Color Module Level 5 (Draft)
271
+ - [x] support contrast-color() https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/contrast-color
272
+ - [x] custom color space in color() https://drafts.csswg.org/css-color-5/#custom-color
273
+ - [x] relative color syntax
274
+ - [x] color syntax
275
+
276
+ ### Media Queries
277
+ - [x] media queries validation
278
+ - [x] automatically generate range media query
279
+ - [x] add at-rule() and named-feature() to @support-condition syntax https://drafts.csswg.org/css-conditional-5/#changes-from-L4
280
+ - [x] support calc() in media features
281
+
282
+
283
+ ```typescript
284
+
285
+ const css = `
286
+
287
+ @media (width >= calc(1024px + 50px)) {
288
+ h1 {
289
+ color: Highlight;
290
+ }
291
+ }`;
292
+
293
+ result = await transform(css, {
294
+
295
+ beautify: true,
296
+ validation: true
297
+ }
298
+
299
+ });
300
+
301
+ console.log(result.code);
302
+ ```
303
+
304
+ output
305
+
306
+ ```css
307
+ @media (width>=1074px) {
308
+ h1 {
309
+ color: Highlight
310
+ }
311
+ }
312
+ ```
313
+
314
+ ### Ast
315
+
316
+ - [x] add ast search functions
317
+ - [x] find() search the ast tree and return the first match
318
+ - [x] findAll() search the ast tree and return all matches
319
+ - [x] findLast() search the ast tree and return the last match
320
+ - [x] findByValue() search the ast tree by checking each node's value and return the first match
321
+
322
+ ### Other Changes
323
+ - [x] fix parsing bugs
324
+ - [x] automatically rename standard declaration names : color-adjust => print-color-adjust
325
+ - [x] rewrite parsing and validation logic
326
+ - [x] selector
327
+ - [x] declaration
328
+ - [x] at-rules
329
+ - [x] @charset
330
+ - [x] @color-profile
331
+ - [x] @container
332
+ - [x] @counter-style
333
+ - [x] @custom-media
334
+ - [x] @document
335
+ - [x] @font-face
336
+ - [x] @font-feature-values
337
+ - [x] @font-palette-values
338
+ - [x] @import
339
+ - [x] @keyframes
340
+ - [x] @layer
341
+ - [x] @media
342
+ - [x] @namespace
343
+ - [x] @page
344
+ - [x] @position-try
345
+ - [x] @property
346
+ - [x] @scope
347
+ - [x] @starting-style
348
+ - [x] @supports
349
+ - [x] @view-transition
350
+ - [x] introduce rawToken type to capture unparseable tokens
351
+
3
352
  ## v1.4.2
4
353
 
354
+ - [x] add css-conditional-5 @support at-rule(<at-keyword-token>) function
5
355
  - [x] fix bug when using remove prefix #120
6
356
 
7
357
  ## v1.4.0
@@ -9,7 +359,7 @@
9
359
  ### CSS Module support
10
360
 
11
361
  - [x] scoped name generation
12
- - composes:
362
+ - [x] composes:
13
363
  - [x] compose from local selector
14
364
  - [x] compose from other files
15
365
  - [x] compose from global selector
@@ -24,17 +374,17 @@
24
374
  - [x] css at-rule value
25
375
  - [x] keyframe animations
26
376
  - [x] grid
27
- - naming
377
+ - [x] naming
28
378
  - [x] ignore
29
379
  - [x] camelCase
30
380
  - [x] camelCaseOnly
31
381
  - [x] dashCase
32
382
  - [x] dashCaseOnly
33
- - default mode
383
+ - [x] default mode
34
384
  - [x] global
35
385
  - [x] local
36
386
  - [x] pure(at least one class or id)
37
- - icss
387
+ - [x] icss
38
388
  - [x] :import
39
389
  - [x] :export
40
390