Skip to content

Commit 46b8379

Browse files
authored
Merge pull request #32 from tbela99/next
- [x] render node with parents - [x] fix relative color from xyz - [x] fix bug when inlineCss is true bug no css variable exists - [x] compute more shorthands - [x] (web) fetch imported css files from external domains using cors - [x] create a playground page
2 parents a6cfc92 + f6d3990 commit 46b8379

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1673
-1314
lines changed

.github/workflows/node.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
node-version: [ 18.x, 20.x, 21.x ]
20+
node-version: [ 18.x, 20.x, 22.x ]
2121
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2222

2323
steps:

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
/.github
1818
/.gitattributes
1919
/Writerside
20-
/validator
20+
/validator
21+
/sw.js

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## V0.5.0
4+
- [x] render node with parents
5+
- [x] fix relative color from xyz
6+
- [x] fix bug when inlineCss is true bug no css variable exists
7+
- [x] compute more shorthands
8+
- [x] (web) fetch imported css files from external domains using cors
9+
10+
## V0.4.1
11+
12+
no code change
13+
314
## V0.4.0
415

516
Parsing

README.md

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
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

3135
There 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 = {});
206210
Rendering 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
442488
import {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

565613
import {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

597647
import {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

651703
import {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

690744
import {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

728784
import {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+
761818
import {transform} from "../src/node";
762819
import {AstRule, ParserOptions} from "../src/@types";
763820
import {parseDeclarations} from "../src/lib";

dist/config.json.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,18 @@ var map = {
353353
"initial"
354354
],
355355
"default": [
356-
"0",
357-
"0 1",
358-
"0 auto",
359-
"0 1 auto"
360356
],
357+
mapping: {
358+
"0 1 auto": "initial",
359+
"0 0 auto": "none",
360+
"1 1 auto": "auto"
361+
},
361362
properties: {
362363
"flex-grow": {
363364
required: true,
364365
keywords: [
365366
],
366367
"default": [
367-
"0"
368368
],
369369
types: [
370370
"Number"
@@ -374,7 +374,6 @@ var map = {
374374
keywords: [
375375
],
376376
"default": [
377-
"1"
378377
],
379378
types: [
380379
"Number"
@@ -390,7 +389,6 @@ var map = {
390389
"auto"
391390
],
392391
"default": [
393-
"auto"
394392
],
395393
types: [
396394
"Length",
@@ -1050,8 +1048,8 @@ var map = {
10501048
"Number"
10511049
],
10521050
"default": [
1053-
"normal",
1054-
"400"
1051+
"400",
1052+
"normal"
10551053
],
10561054
keywords: [
10571055
"normal",
@@ -1406,6 +1404,8 @@ var map = {
14061404
left: "0",
14071405
top: "0",
14081406
center: "50%",
1407+
"center center": "50%",
1408+
"50% 50%": "50%",
14091409
bottom: "100%",
14101410
right: "100%"
14111411
},

0 commit comments

Comments
 (0)