@tbela99/css-parser 0.4.0 → 0.5.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.
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) [![NPM Downloads](https://img.shields.io/npm/dm/%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser)
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/dy/%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser)
2
2
 
3
3
  # css-parser
4
4
 
@@ -14,8 +14,8 @@ $ npm install @tbela99/css-parser
14
14
 
15
15
  - no dependency
16
16
  - fault-tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
17
- - efficient minification without unsafe transforms, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
18
- - minify colors.
17
+ - fast and efficient minification without unsafe transforms, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
18
+ - minify colors.
19
19
  - support css color level 4 & 5: color(), lab(), lch(), oklab(), oklch(), color-mix() and relative color
20
20
  - generate nested css rules
21
21
  - convert nested css rules to legacy syntax
@@ -26,6 +26,10 @@ $ npm install @tbela99/css-parser
26
26
  - remove duplicate properties
27
27
  - flatten @import rules
28
28
 
29
+ ## Playground
30
+
31
+ Try it [online](https://tbela99.github.io/css-parser/playground/)
32
+
29
33
  ## Exports
30
34
 
31
35
  There are several ways to import the library into your application.
@@ -137,7 +141,6 @@ Include ParseOptions and RenderOptions
137
141
  - expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules to false.
138
142
  - removeDuplicateDeclarations: boolean, optional. remove duplicate declarations.
139
143
  - computeShorthand: boolean, optional. compute shorthand properties.
140
- - inlineCssVariables: boolean, optional. replace css variables with their current value.
141
144
  - computeCalcExpression: boolean, optional. evaluate calc() expression
142
145
  - inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the :root {} or html {} rule.
143
146
  - removeEmpty: boolean, optional. remove empty rule lists from the ast.
@@ -161,10 +164,11 @@ Include ParseOptions and RenderOptions
161
164
  > Minify Options
162
165
 
163
166
  - minify: boolean, optional. default to _true_. minify css output.
167
+ - withParents: boolean, optional. render this node and its parents.
164
168
  - expandNestingRules: boolean, optional. expand nesting rules.
165
169
  - preserveLicense: boolean, force preserving comments starting with '/\*!' when minify is enabled.
166
170
  - removeComments: boolean, remove comments in generated css.
167
- - colorConvert: boolean, convert colors to hex.
171
+ - convertColor: boolean, convert colors to hex.
168
172
 
169
173
  > Sourcemap Options
170
174
 
@@ -206,13 +210,30 @@ render(ast, RenderOptions = {});
206
210
  Rendering ast
207
211
 
208
212
  ```javascript
213
+ import {parse, render} from '@tbela99/css-parser';
214
+
215
+ const css = `
216
+ @media screen and (min-width: 40em) {
217
+ .featurette-heading {
218
+ font-size: 50px;
219
+ }
220
+ .a {
221
+ color: red;
222
+ width: 3px;
223
+ }
224
+ }
225
+ `;
209
226
 
210
- import {render} from '@tbela99/css-parser';
227
+ const result = await parse(css, options);
211
228
 
212
- // minified
213
- const {code, stats} = render(ast, {minify: true});
229
+ // print declaration without parents
230
+ console.error(render(result.ast.chi[0].chi[1].chi[1], {withParents: false}));
231
+ // -> width:3px
232
+
233
+ // print declaration with parents
234
+ console.debug(render(result.ast.chi[0].chi[1].chi[1], {withParents: true}));
235
+ // -> @media screen and (min-width:40em){.a{width:3px}}
214
236
 
215
- console.log(code);
216
237
  ```
217
238
 
218
239
  ### Merge similar rules
@@ -434,9 +455,34 @@ result
434
455
  color: navy
435
456
  }
436
457
 
458
+ ```
459
+ ### CSS variable inlining and relative color
460
+
461
+ ```javascript
462
+
463
+ import {parse, render} from '@tbela99/css-parser';
464
+
465
+ const css = `
466
+
467
+ html { --bluegreen: oklab(54.3% -22.5% -5%); }
468
+ .overlay {
469
+ background: oklab(from var(--bluegreen) calc(1.0 - l) calc(a * 0.8) b);
470
+ }
471
+ `
472
+
473
+ const prettyPrint = await parse(css, {inlineCssVariables: true}).then(result => render(result.ast, {minify: false}).code);
474
+
475
+ ```
476
+ result
477
+
478
+ ```css
479
+ .overlay {
480
+ background: #0c6464
481
+ }
482
+
437
483
  ```
438
484
 
439
- ## Node Walker
485
+ # Node Walker
440
486
 
441
487
  ```javascript
442
488
  import {walk} from '@tbela99/css-parser';
@@ -500,7 +546,7 @@ for (const {node, parent, root} of walk(ast)) {
500
546
 
501
547
  ## Computed shorthands properties
502
548
 
503
- - ~all~
549
+ - [ ] ~all~
504
550
  - [x] animation
505
551
  - [x] background
506
552
  - [x] border
@@ -560,6 +606,8 @@ Ast can be transformed using node visitors
560
606
 
561
607
  ### Exemple 1: Declaration
562
608
 
609
+ the visitor is called for any declaration encountered
610
+
563
611
  ```typescript
564
612
 
565
613
  import {AstDeclaration, ParserOptions} from "../src/@types";
@@ -592,6 +640,8 @@ console.debug(await transform(css, options));
592
640
 
593
641
  ### Exemple 2: Declaration
594
642
 
643
+ the visitor is called only on 'height' declarations
644
+
595
645
  ```typescript
596
646
 
597
647
  import {AstDeclaration, LengthToken, ParserOptions} from "../src/@types";
@@ -646,6 +696,8 @@ console.debug(await transform(css, options));
646
696
 
647
697
  ### Exemple 3: At-Rule
648
698
 
699
+ the visitor is called on any at-rule
700
+
649
701
  ```typescript
650
702
 
651
703
  import {AstAtRule, ParserOptions} from "../src/@types";
@@ -685,6 +737,8 @@ console.debug(await transform(css, options));
685
737
 
686
738
  ### Exemple 4: At-Rule
687
739
 
740
+ the visitor is called only for at-rule media
741
+
688
742
  ```typescript
689
743
 
690
744
  import {AstAtRule, ParserOptions} from "../src/@types";
@@ -723,6 +777,8 @@ console.debug(await transform(css, options));
723
777
 
724
778
  ### Exemple 5: Rule
725
779
 
780
+ the visitor is called on any Rule
781
+
726
782
  ```typescript
727
783
 
728
784
  import {AstAtRule, ParserOptions} from "../src/@types";
@@ -755,9 +811,10 @@ console.debug(await transform(css, options));
755
811
  ```
756
812
  ### Exemple 6: Rule
757
813
 
758
- Adding declarations
814
+ Adding declarations to any rule
759
815
 
760
816
  ```typescript
817
+
761
818
  import {transform} from "../src/node";
762
819
  import {AstRule, ParserOptions} from "../src/@types";
763
820
  import {parseDeclarations} from "../src/lib";
@@ -353,18 +353,18 @@ var map = {
353
353
  "initial"
354
354
  ],
355
355
  "default": [
356
- "0",
357
- "0 1",
358
- "0 auto",
359
- "0 1 auto"
360
356
  ],
357
+ mapping: {
358
+ "0 1 auto": "initial",
359
+ "0 0 auto": "none",
360
+ "1 1 auto": "auto"
361
+ },
361
362
  properties: {
362
363
  "flex-grow": {
363
364
  required: true,
364
365
  keywords: [
365
366
  ],
366
367
  "default": [
367
- "0"
368
368
  ],
369
369
  types: [
370
370
  "Number"
@@ -374,7 +374,6 @@ var map = {
374
374
  keywords: [
375
375
  ],
376
376
  "default": [
377
- "1"
378
377
  ],
379
378
  types: [
380
379
  "Number"
@@ -390,7 +389,6 @@ var map = {
390
389
  "auto"
391
390
  ],
392
391
  "default": [
393
- "auto"
394
392
  ],
395
393
  types: [
396
394
  "Length",
@@ -1050,8 +1048,8 @@ var map = {
1050
1048
  "Number"
1051
1049
  ],
1052
1050
  "default": [
1053
- "normal",
1054
- "400"
1051
+ "400",
1052
+ "normal"
1055
1053
  ],
1056
1054
  keywords: [
1057
1055
  "normal",
@@ -1406,6 +1404,8 @@ var map = {
1406
1404
  left: "0",
1407
1405
  top: "0",
1408
1406
  center: "50%",
1407
+ "center center": "50%",
1408
+ "50% 50%": "50%",
1409
1409
  bottom: "100%",
1410
1410
  right: "100%"
1411
1411
  },