@tbela99/css-parser 0.3.0 → 0.4.1

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 (60) hide show
  1. package/{LICENSE → LICENSE.md} +1 -1
  2. package/README.md +191 -80
  3. package/dist/config.json.js +20 -1
  4. package/dist/index-umd-web.js +3210 -1352
  5. package/dist/index.cjs +3211 -1353
  6. package/dist/index.d.ts +910 -0
  7. package/dist/lib/ast/expand.js +1 -1
  8. package/dist/lib/ast/features/calc.js +1 -2
  9. package/dist/lib/ast/features/inlinecssvariables.js +1 -1
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/math/expression.js +38 -3
  12. package/dist/lib/ast/math/math.js +2 -2
  13. package/dist/lib/ast/minify.js +0 -1
  14. package/dist/lib/ast/types.js +1 -0
  15. package/dist/lib/ast/utils/minifyfeature.js +4 -3
  16. package/dist/lib/parser/declaration/list.js +1 -1
  17. package/dist/lib/parser/declaration/map.js +129 -26
  18. package/dist/lib/parser/declaration/set.js +1 -1
  19. package/dist/lib/parser/parse.js +325 -303
  20. package/dist/lib/parser/tokenize.js +220 -223
  21. package/dist/lib/parser/utils/declaration.js +1 -1
  22. package/dist/lib/parser/utils/syntax.js +159 -23
  23. package/dist/lib/parser/utils/type.js +2 -2
  24. package/dist/lib/renderer/color/a98rgb.js +64 -0
  25. package/dist/lib/renderer/color/color.js +521 -0
  26. package/dist/lib/renderer/color/colormix.js +337 -0
  27. package/dist/lib/renderer/color/hex.js +92 -0
  28. package/dist/lib/renderer/color/hsl.js +118 -0
  29. package/dist/lib/renderer/color/hsv.js +20 -0
  30. package/dist/lib/renderer/color/hwb.js +101 -0
  31. package/dist/lib/renderer/color/lab.js +136 -0
  32. package/dist/lib/renderer/color/lch.js +79 -0
  33. package/dist/lib/renderer/color/oklab.js +121 -0
  34. package/dist/lib/renderer/color/oklch.js +65 -0
  35. package/dist/lib/renderer/color/p3.js +57 -0
  36. package/dist/lib/renderer/color/prophotorgb.js +56 -0
  37. package/dist/lib/renderer/color/rec2020.js +70 -0
  38. package/dist/lib/renderer/color/relativecolor.js +152 -0
  39. package/dist/lib/renderer/color/rgb.js +44 -0
  40. package/dist/lib/renderer/color/srgb.js +261 -0
  41. package/dist/lib/renderer/color/utils/components.js +20 -0
  42. package/dist/lib/renderer/color/utils/constants.js +191 -0
  43. package/dist/lib/renderer/color/utils/matrix.js +35 -0
  44. package/dist/lib/renderer/color/xyz.js +64 -0
  45. package/dist/lib/renderer/color/xyzd50.js +33 -0
  46. package/dist/lib/renderer/render.js +61 -32
  47. package/dist/node/index.js +1 -1
  48. package/dist/node/load.js +1 -1
  49. package/dist/web/index.js +1 -1
  50. package/package.json +15 -14
  51. package/dist/lib/ast/features/utils/math.js +0 -95
  52. package/dist/lib/iterable/set.js +0 -48
  53. package/dist/lib/iterable/weakmap.js +0 -53
  54. package/dist/lib/renderer/utils/calccolor.js +0 -238
  55. package/dist/lib/renderer/utils/color.js +0 -371
  56. package/dist/lib/renderer/utils/hex.js +0 -124
  57. package/dist/lib/renderer/utils/hsl.js +0 -49
  58. package/dist/lib/renderer/utils/hsv.js +0 -15
  59. package/dist/lib/renderer/utils/hwb.js +0 -50
  60. package/dist/lib/renderer/utils/rgb.js +0 -66
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Thierry Bela
3
+ Copyright (c) 2024 Thierry Bela
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![npm](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Ftbela99%2Fcss-parser%2Fmaster%2Fpackage.json&query=version&logo=npm&label=npm&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser) [![cov](https://tbela99.github.io/css-parser/badges/coverage.svg)](https://github.com/tbela99/css-parser/actions)
1
+ [![npm](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Ftbela99%2Fcss-parser%2Fmaster%2Fpackage.json&query=version&logo=npm&label=npm&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser) [![cov](https://tbela99.github.io/css-parser/badges/coverage.svg)](https://github.com/tbela99/css-parser/actions) [![NPM Downloads](https://img.shields.io/npm/dm/%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser)
2
2
 
3
3
  # css-parser
4
4
 
@@ -12,18 +12,98 @@ $ npm install @tbela99/css-parser
12
12
 
13
13
  ## Features
14
14
 
15
+ - no dependency
15
16
  - fault-tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
16
- - efficient minification, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
17
- - automatically generate nested css rules
17
+ - efficient minification without unsafe transforms, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
18
+ - minify colors.
19
+ - support css color level 4 & 5: color(), lab(), lch(), oklab(), oklch(), color-mix() and relative color
20
+ - generate nested css rules
21
+ - convert nested css rules to legacy syntax
18
22
  - generate sourcemap
19
- - compute css shorthands. see the list below
20
- - compute calc() expression
23
+ - compute css shorthands. see supported properties list below
24
+ - evaluate calc()
21
25
  - inline css variables
22
- - relative css colors using rgb(), hsl() and hwb()
23
- - nested css expansion
24
26
  - remove duplicate properties
25
27
  - flatten @import rules
26
- - works the same way in node and web browser
28
+
29
+ ## Exports
30
+
31
+ There are several ways to import the library into your application.
32
+
33
+ ### Node exports
34
+
35
+ import as a module
36
+
37
+ ```javascript
38
+
39
+ import {transform} from '@tbela99/css-parser';
40
+
41
+ // ...
42
+ ```
43
+ ### Deno exports
44
+
45
+ import as a module
46
+
47
+ ```javascript
48
+
49
+ import {transform} from 'npm:@tbela99/css-parser';
50
+
51
+ // ...
52
+ ```
53
+ import as a CommonJS module
54
+
55
+ ```javascript
56
+
57
+ const {transform} = require('@tbela99/css-parser/cjs');
58
+
59
+ // ...
60
+ ```
61
+
62
+ ### Web export
63
+
64
+ Programmatic import
65
+
66
+ ```javascript
67
+
68
+ import {transform} from '@tbela99/css-parser/web';
69
+
70
+ // ...
71
+ ```
72
+
73
+ Javascript module from cdn
74
+
75
+ ```javascript
76
+
77
+ <script type="module">
78
+
79
+ import {transform} from 'https://esm.sh/@tbela99/css-parser@0.4.0/web';
80
+
81
+
82
+ const css = `
83
+ .s {
84
+
85
+ background: color-mix(in hsl, color(display-p3 0 1 0) 80%, yellow);
86
+ }
87
+ `;
88
+
89
+ console.debug(await transform(css).then(r => r.code));
90
+
91
+ </script>
92
+ ```
93
+
94
+ Javascript module
95
+
96
+ ```javascript
97
+
98
+ <script src="dist/web/index.js" type="module"></script>
99
+ ```
100
+
101
+ Single Javascript file
102
+
103
+ ```javascript
104
+
105
+ <script src="dist/index-umd-web.js"></script>
106
+ ```
27
107
 
28
108
  ## Transform
29
109
 
@@ -51,35 +131,49 @@ Include ParseOptions and RenderOptions
51
131
 
52
132
  #### ParseOptions
53
133
 
134
+ > Minify Options
54
135
  - minify: boolean, optional. default to _true_. optimize ast.
55
- - src: string, optional. original css file location to be used with sourcemap.
56
- - sourcemap: boolean, optional. preserve node location data.
57
136
  - nestingRules: boolean, optional. automatically generated nested rules.
58
137
  - expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules to false.
59
- - removeCharset: boolean, optional. remove @charset.
60
- - removeEmpty: boolean, optional. remove empty rule lists from the ast.
61
- - resolveUrls: boolean, optional. resolve css 'url()' according to the parameters 'src' and 'cwd'
62
- - resolveImport: boolean, optional. replace @import rule by the content of its referenced stylesheet.
63
- - cwd: string, optional. the current working directory. when specified url() are resolved using this value
64
138
  - removeDuplicateDeclarations: boolean, optional. remove duplicate declarations.
65
139
  - computeShorthand: boolean, optional. compute shorthand properties.
66
140
  - inlineCssVariables: boolean, optional. replace css variables with their current value.
67
141
  - computeCalcExpression: boolean, optional. evaluate calc() expression
68
- - inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the :root {} rule.
142
+ - inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the :root {} or html {} rule.
143
+ - removeEmpty: boolean, optional. remove empty rule lists from the ast.
144
+
145
+ > Sourcemap Options
146
+
147
+ - src: string, optional. original css file location to be used with sourcemap.
148
+ - sourcemap: boolean, optional. preserve node location data.
149
+
150
+ > Misc Options
151
+
152
+ - resolveUrls: boolean, optional. resolve css 'url()' according to the parameters 'src' and 'cwd'
153
+ - resolveImport: boolean, optional. replace @import rule by the content of its referenced stylesheet.
154
+ - removeCharset: boolean, optional. remove @charset.
155
+ - cwd: string, optional. the current working directory. when specified url() are resolved using this value
69
156
  - visitor: VisitorNodeMap, optional. node visitor used to transform the ast.
70
157
  - signal: AbortSignal, optional. abort parsing.
71
158
 
72
159
  #### RenderOptions
73
160
 
161
+ > Minify Options
162
+
74
163
  - minify: boolean, optional. default to _true_. minify css output.
75
164
  - expandNestingRules: boolean, optional. expand nesting rules.
76
- - sourcemap: boolean, optional. generate sourcemap
77
165
  - preserveLicense: boolean, force preserving comments starting with '/\*!' when minify is enabled.
78
- - sourcemap: boolean, optional. generate sourcemap.
79
- - indent: string, optional. css indention string. uses space character by default.
80
- - newLine: string, optional. new line character.
81
166
  - removeComments: boolean, remove comments in generated css.
82
167
  - colorConvert: boolean, convert colors to hex.
168
+
169
+ > Sourcemap Options
170
+
171
+ - sourcemap: boolean, optional. generate sourcemap
172
+
173
+ > Misc Options
174
+
175
+ - indent: string, optional. css indention string. uses space character by default.
176
+ - newLine: string, optional. new line character.
83
177
  - output: string, optional. file where to store css. url() are resolved according to the specified value. no file is created though.
84
178
  - cwd: string, optional. value used as current working directory. when output is not provided, urls are resolved according to this value.
85
179
 
@@ -107,9 +201,12 @@ const {ast, errors, stats} = await parse(css);
107
201
  render(ast, RenderOptions = {});
108
202
  ```
109
203
 
110
- ### Example
204
+ ### Examples
205
+
206
+ Rendering ast
111
207
 
112
208
  ```javascript
209
+
113
210
  import {render} from '@tbela99/css-parser';
114
211
 
115
212
  // minified
@@ -118,67 +215,39 @@ const {code, stats} = render(ast, {minify: true});
118
215
  console.log(code);
119
216
  ```
120
217
 
121
- ## Node Walker
122
-
123
- ```javascript
124
- import {walk} from '@tbela99/css-parser';
125
-
126
- for (const {node, parent, root} of walk(ast)) {
127
-
128
- // do somehting
129
- }
130
- ```
131
-
132
- ## Exports
218
+ ### Merge similar rules
133
219
 
134
- There are several ways to import the library into your application.
135
-
136
- ### Node exports
137
-
138
- import as a module
139
-
140
- ```javascript
141
-
142
- import {transform} from '@tbela99/css-parser';
220
+ CSS
143
221
 
144
- // ...
145
- ```
146
- import as a CommonJS module
222
+ ```css
147
223
 
148
- ```javascript
224
+ .clear {
225
+ width: 0;
226
+ height: 0;
227
+ color: transparent;
228
+ }
149
229
 
150
- const {transform} = require('@tbela99/css-parser/cjs');
230
+ .clearfix:before {
151
231
 
152
- // ...
232
+ height: 0;
233
+ width: 0;
234
+ }
153
235
  ```
154
236
 
155
- ### Web export
156
-
157
- Programmatic import
158
-
159
237
  ```javascript
160
238
 
161
- import {transform} from '@tbela99/css-parser/web';
162
-
163
- // ...
164
- ```
165
-
166
- Javascript module
239
+ import {transform} from '@tbela99/css-parser';
167
240
 
168
- ```javascript
241
+ const result = await transform(css);
169
242
 
170
- <script src="dist/web/index.js" type="module"></script>
171
243
  ```
172
244
 
173
- Single JavaScript file
174
-
175
- ```javascript
245
+ Result
176
246
 
177
- <script src="dist/index-umd-web.js"></script>
247
+ ```css
248
+ .clear,.clearfix:before{height:0;width:0}.clear{color:#0000}
178
249
  ```
179
250
 
180
- ## Example 1
181
-
182
251
  ### Automatic CSS Nesting
183
252
 
184
253
  CSS
@@ -226,8 +295,6 @@ table.colortable {
226
295
  }
227
296
  ```
228
297
 
229
- ## Example 2
230
-
231
298
  ### Nested CSS Expansion
232
299
 
233
300
  CSS
@@ -250,14 +317,12 @@ table.colortable {
250
317
  }
251
318
  }
252
319
  ```
253
-
254
320
  Javascript
321
+
255
322
  ```javascript
256
323
  import {parse, render} from '@tbela99/css-parser';
257
324
 
258
-
259
325
  const options = {minify: true};
260
-
261
326
  const {code} = await parse(css, options).then(result => render(result.ast, {minify: false, expandNestingRules: true}));
262
327
  //
263
328
  console.debug(code);
@@ -283,8 +348,6 @@ table.colortable th {
283
348
  }
284
349
  ```
285
350
 
286
- ### Example 3
287
-
288
351
  ### Calc() resolution
289
352
 
290
353
  ```javascript
@@ -313,8 +376,6 @@ result
313
376
  }
314
377
  ```
315
378
 
316
- ### Example 4
317
-
318
379
  ### CSS variable inlining
319
380
 
320
381
  ```javascript
@@ -346,8 +407,6 @@ result
346
407
 
347
408
  ```
348
409
 
349
- ### Example 5
350
-
351
410
  ### CSS variable inlining and relative color
352
411
 
353
412
  ```javascript
@@ -377,6 +436,17 @@ result
377
436
 
378
437
  ```
379
438
 
439
+ ## Node Walker
440
+
441
+ ```javascript
442
+ import {walk} from '@tbela99/css-parser';
443
+
444
+ for (const {node, parent, root} of walk(ast)) {
445
+
446
+ // do something
447
+ }
448
+ ```
449
+
380
450
  ## AST
381
451
 
382
452
  ### Comment
@@ -525,7 +595,7 @@ console.debug(await transform(css, options));
525
595
  ```typescript
526
596
 
527
597
  import {AstDeclaration, LengthToken, ParserOptions} from "../src/@types";
528
- import {EnumToken, NodeType} from "../src/lib";
598
+ import {EnumToken, EnumToken} from "../src/lib";
529
599
  import {transform} from "../src/node";
530
600
 
531
601
  const options: ParserOptions = {
@@ -542,7 +612,7 @@ const options: ParserOptions = {
542
612
  node,
543
613
  {
544
614
 
545
- typ: NodeType.DeclarationNodeType,
615
+ typ: EnumToken.DeclarationNodeType,
546
616
  nam: 'width',
547
617
  val: [
548
618
  <LengthToken>{
@@ -563,11 +633,14 @@ const css = `
563
633
  .foo {
564
634
  height: calc(100px * 2/ 15);
565
635
  }
636
+ .selector {
637
+ color: lch(from peru calc(l * 0.8) calc(c * 0.7) calc(h + 180))
638
+ }
566
639
  `;
567
640
 
568
641
  console.debug(await transform(css, options));
569
642
 
570
- // .foo{height:calc(40px/3);width:3px}
643
+ // .foo{height:calc(40px/3);width:3px}.selector{color:#0880b0}
571
644
 
572
645
  ```
573
646
 
@@ -679,6 +752,44 @@ console.debug(await transform(css, options));
679
752
 
680
753
  // .foo,.bar,.fubar{height:calc(40px/3)}
681
754
 
755
+ ```
756
+ ### Exemple 6: Rule
757
+
758
+ Adding declarations
759
+
760
+ ```typescript
761
+ import {transform} from "../src/node";
762
+ import {AstRule, ParserOptions} from "../src/@types";
763
+ import {parseDeclarations} from "../src/lib";
764
+
765
+ const options: ParserOptions = {
766
+
767
+ removeEmpty: false,
768
+ visitor: {
769
+
770
+ Rule: async (node: AstRule): Promise<AstRule | null> => {
771
+
772
+ if (node.sel == '.foo') {
773
+
774
+ node.chi.push(...await parseDeclarations('width: 3px'));
775
+ return node;
776
+ }
777
+
778
+ return null;
779
+ }
780
+ }
781
+ };
782
+
783
+ const css = `
784
+
785
+ .foo {
786
+ }
787
+ `;
788
+
789
+ console.debug(await transform(css, options));
790
+
791
+ // .foo{width:3px}
792
+
682
793
  ```
683
794
 
684
795
  ---
@@ -924,6 +924,8 @@ var map = {
924
924
  "overflow-x": {
925
925
  "default": [
926
926
  ],
927
+ types: [
928
+ ],
927
929
  keywords: [
928
930
  "auto",
929
931
  "visible",
@@ -935,6 +937,8 @@ var map = {
935
937
  "overflow-y": {
936
938
  "default": [
937
939
  ],
940
+ types: [
941
+ ],
938
942
  keywords: [
939
943
  "auto",
940
944
  "visible",
@@ -1267,13 +1271,27 @@ var map = {
1267
1271
  },
1268
1272
  background: {
1269
1273
  shorthand: "background",
1270
- pattern: "background-repeat background-color background-image background-attachment background-clip background-origin background-position background-size",
1274
+ pattern: "background-attachment background-origin background-clip background-color background-image background-repeat background-position background-size",
1271
1275
  keywords: [
1272
1276
  "none"
1273
1277
  ],
1274
1278
  "default": [
1279
+ "0 0",
1280
+ "none",
1281
+ "auto",
1282
+ "repeat",
1283
+ "transparent",
1284
+ "#0000",
1285
+ "scroll",
1286
+ "padding-box",
1287
+ "border-box"
1275
1288
  ],
1276
1289
  multiple: true,
1290
+ set: {
1291
+ "background-origin": [
1292
+ "background-clip"
1293
+ ]
1294
+ },
1277
1295
  separator: {
1278
1296
  typ: "Comma"
1279
1297
  },
@@ -1307,6 +1325,7 @@ var map = {
1307
1325
  "Color"
1308
1326
  ],
1309
1327
  "default": [
1328
+ "#0000",
1310
1329
  "transparent"
1311
1330
  ],
1312
1331
  multiple: true,