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 )
22
33# css-parser
44
@@ -14,8 +14,8 @@ $ npm install @tbela99/css-parser
1414
1515- no dependency
1616- 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.
1919- support css color level 4 & 5: color(), lab(), lch(), oklab(), oklch(), color-mix() and relative color
2020- generate nested css rules
2121- convert nested css rules to legacy syntax
@@ -26,6 +26,10 @@ $ npm install @tbela99/css-parser
2626- remove duplicate properties
2727- flatten @import rules
2828
29+ ## Playground
30+
31+ Try it [ online] ( https://tbela99.github.io/css-parser/playground/ )
32+
2933## Exports
3034
3135There are several ways to import the library into your application.
@@ -137,7 +141,6 @@ Include ParseOptions and RenderOptions
137141- expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules to false.
138142- removeDuplicateDeclarations: boolean, optional. remove duplicate declarations.
139143- computeShorthand: boolean, optional. compute shorthand properties.
140- - inlineCssVariables: boolean, optional. replace css variables with their current value.
141144- computeCalcExpression: boolean, optional. evaluate calc() expression
142145- inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the : root {} or html {} rule.
143146- removeEmpty: boolean, optional. remove empty rule lists from the ast.
@@ -161,10 +164,11 @@ Include ParseOptions and RenderOptions
161164> Minify Options
162165
163166- minify: boolean, optional. default to _ true_ . minify css output.
167+ - withParents: boolean, optional. render this node and its parents.
164168- expandNestingRules: boolean, optional. expand nesting rules.
165169- preserveLicense: boolean, force preserving comments starting with '/\* !' when minify is enabled.
166170- removeComments: boolean, remove comments in generated css.
167- - colorConvert : boolean, convert colors to hex.
171+ - convertColor : boolean, convert colors to hex.
168172
169173> Sourcemap Options
170174
@@ -206,13 +210,30 @@ render(ast, RenderOptions = {});
206210Rendering ast
207211
208212``` 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+ ` ;
209226
210- import { render } from ' @tbela99/ css-parser ' ;
227+ const result = await parse ( css, options) ;
211228
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}}
214236
215- console .log (code);
216237```
217238
218239### Merge similar rules
@@ -434,9 +455,34 @@ result
434455 color : navy
435456}
436457
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+
437483```
438484
439- ## Node Walker
485+ # Node Walker
440486
441487``` javascript
442488import {walk } from ' @tbela99/css-parser' ;
@@ -500,7 +546,7 @@ for (const {node, parent, root} of walk(ast)) {
500546
501547## Computed shorthands properties
502548
503- - ~ all~
549+ - [ ] ~ all~
504550- [x] animation
505551- [x] background
506552- [x] border
@@ -560,6 +606,8 @@ Ast can be transformed using node visitors
560606
561607### Exemple 1: Declaration
562608
609+ the visitor is called for any declaration encountered
610+
563611``` typescript
564612
565613import {AstDeclaration , ParserOptions } from " ../src/@types" ;
@@ -592,6 +640,8 @@ console.debug(await transform(css, options));
592640
593641### Exemple 2: Declaration
594642
643+ the visitor is called only on 'height' declarations
644+
595645``` typescript
596646
597647import {AstDeclaration , LengthToken , ParserOptions } from " ../src/@types" ;
@@ -646,6 +696,8 @@ console.debug(await transform(css, options));
646696
647697### Exemple 3: At-Rule
648698
699+ the visitor is called on any at-rule
700+
649701``` typescript
650702
651703import {AstAtRule , ParserOptions } from " ../src/@types" ;
@@ -685,6 +737,8 @@ console.debug(await transform(css, options));
685737
686738### Exemple 4: At-Rule
687739
740+ the visitor is called only for at-rule media
741+
688742``` typescript
689743
690744import {AstAtRule , ParserOptions } from " ../src/@types" ;
@@ -723,6 +777,8 @@ console.debug(await transform(css, options));
723777
724778### Exemple 5: Rule
725779
780+ the visitor is called on any Rule
781+
726782``` typescript
727783
728784import {AstAtRule , ParserOptions } from " ../src/@types" ;
@@ -755,9 +811,10 @@ console.debug(await transform(css, options));
755811```
756812### Exemple 6: Rule
757813
758- Adding declarations
814+ Adding declarations to any rule
759815
760816``` typescript
817+
761818import {transform } from " ../src/node" ;
762819import {AstRule , ParserOptions } from " ../src/@types" ;
763820import {parseDeclarations } from " ../src/lib" ;
0 commit comments