@tbela99/css-parser 0.1.0 → 0.3.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 +267 -2
- package/dist/config.json.js +611 -4
- package/dist/index-umd-web.js +2898 -1223
- package/dist/index.cjs +2898 -1223
- package/dist/lib/ast/expand.js +11 -11
- package/dist/lib/ast/features/calc.js +33 -224
- package/dist/lib/ast/features/index.js +3 -3
- package/dist/lib/ast/features/inlinecssvariables.js +46 -31
- package/dist/lib/ast/features/shorthand.js +7 -7
- package/dist/lib/ast/features/utils/math.js +95 -0
- package/dist/lib/ast/math/expression.js +185 -0
- package/dist/lib/ast/math/math.js +95 -0
- package/dist/lib/ast/minify.js +34 -29
- package/dist/lib/ast/types.js +108 -78
- package/dist/lib/ast/walk.js +42 -9
- package/dist/lib/fs/resolve.js +4 -3
- package/dist/lib/iterable/set.js +48 -0
- package/dist/lib/iterable/weakmap.js +53 -0
- package/dist/lib/iterable/weakset.js +48 -0
- package/dist/lib/parser/declaration/list.js +7 -3
- package/dist/lib/parser/declaration/map.js +86 -7
- package/dist/lib/parser/declaration/set.js +43 -23
- package/dist/lib/parser/parse.js +561 -387
- package/dist/lib/parser/tokenize.js +42 -13
- package/dist/lib/parser/utils/declaration.js +67 -0
- package/dist/lib/parser/utils/syntax.js +32 -2
- package/dist/lib/parser/utils/type.js +7 -2
- package/dist/lib/renderer/render.js +163 -47
- package/dist/lib/renderer/utils/calccolor.js +238 -0
- package/dist/lib/renderer/utils/color.js +36 -164
- package/dist/lib/renderer/utils/hex.js +124 -0
- package/dist/lib/renderer/utils/hsl.js +49 -0
- package/dist/lib/renderer/utils/hsv.js +15 -0
- package/dist/lib/renderer/utils/hwb.js +50 -0
- package/dist/lib/renderer/utils/rgb.js +66 -0
- package/dist/node/index.js +8 -12
- package/dist/web/index.js +8 -12
- package/package.json +9 -7
- package/dist/index.d.ts +0 -1056
- /package/dist/lib/ast/{utiles → utils}/minifyfeature.js +0 -0
package/README.md
CHANGED
|
@@ -13,11 +13,13 @@ $ npm install @tbela99/css-parser
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
15
15
|
- 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)
|
|
16
|
+
- efficient minification, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
|
|
17
17
|
- automatically generate nested css rules
|
|
18
18
|
- generate sourcemap
|
|
19
19
|
- compute css shorthands. see the list below
|
|
20
20
|
- compute calc() expression
|
|
21
|
+
- inline css variables
|
|
22
|
+
- relative css colors using rgb(), hsl() and hwb()
|
|
21
23
|
- nested css expansion
|
|
22
24
|
- remove duplicate properties
|
|
23
25
|
- flatten @import rules
|
|
@@ -64,6 +66,8 @@ Include ParseOptions and RenderOptions
|
|
|
64
66
|
- inlineCssVariables: boolean, optional. replace css variables with their current value.
|
|
65
67
|
- computeCalcExpression: boolean, optional. evaluate calc() expression
|
|
66
68
|
- inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the :root {} rule.
|
|
69
|
+
- visitor: VisitorNodeMap, optional. node visitor used to transform the ast.
|
|
70
|
+
- signal: AbortSignal, optional. abort parsing.
|
|
67
71
|
|
|
68
72
|
#### RenderOptions
|
|
69
73
|
|
|
@@ -100,7 +104,7 @@ const {ast, errors, stats} = await parse(css);
|
|
|
100
104
|
### Usage
|
|
101
105
|
|
|
102
106
|
```javascript
|
|
103
|
-
|
|
107
|
+
render(ast, RenderOptions = {});
|
|
104
108
|
```
|
|
105
109
|
|
|
106
110
|
### Example
|
|
@@ -342,6 +346,37 @@ result
|
|
|
342
346
|
|
|
343
347
|
```
|
|
344
348
|
|
|
349
|
+
### Example 5
|
|
350
|
+
|
|
351
|
+
### CSS variable inlining and relative color
|
|
352
|
+
|
|
353
|
+
```javascript
|
|
354
|
+
|
|
355
|
+
import {parse, render} from '@tbela99/css-parser';
|
|
356
|
+
|
|
357
|
+
const css = `
|
|
358
|
+
|
|
359
|
+
:root {
|
|
360
|
+
--color: green;
|
|
361
|
+
}
|
|
362
|
+
._19_u :focus {
|
|
363
|
+
color: hsl(from var(--color) calc(h * 2) s l);
|
|
364
|
+
|
|
365
|
+
}
|
|
366
|
+
`
|
|
367
|
+
|
|
368
|
+
const prettyPrint = await parse(css, {inlineCssVariables: true}).then(result => render(result.ast, {minify: false}).code);
|
|
369
|
+
|
|
370
|
+
```
|
|
371
|
+
result
|
|
372
|
+
|
|
373
|
+
```css
|
|
374
|
+
._19_u :focus {
|
|
375
|
+
color: navy
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
```
|
|
379
|
+
|
|
345
380
|
## AST
|
|
346
381
|
|
|
347
382
|
### Comment
|
|
@@ -394,28 +429,258 @@ result
|
|
|
394
429
|
- [x] decode and replace utf-8 escape sequence
|
|
395
430
|
|
|
396
431
|
## Computed shorthands properties
|
|
432
|
+
|
|
433
|
+
- ~all~
|
|
434
|
+
- [x] animation
|
|
397
435
|
- [x] background
|
|
398
436
|
- [x] border
|
|
437
|
+
- [ ] border-block-end
|
|
438
|
+
- [ ] border-block-start
|
|
399
439
|
- [x] border-bottom
|
|
400
440
|
- [x] border-color
|
|
441
|
+
- [ ] border-image
|
|
442
|
+
- [ ] border-inline-end
|
|
443
|
+
- [ ] border-inline-start
|
|
401
444
|
- [x] border-left
|
|
402
445
|
- [x] border-radius
|
|
403
446
|
- [x] border-right
|
|
404
447
|
- [x] border-style
|
|
405
448
|
- [x] border-top
|
|
406
449
|
- [x] border-width
|
|
450
|
+
- [x] column-rule
|
|
451
|
+
- [x] columns
|
|
452
|
+
- [x] container
|
|
453
|
+
- [ ] contain-intrinsic-size
|
|
454
|
+
- [x] flex
|
|
455
|
+
- [x] flex-flow
|
|
407
456
|
- [x] font
|
|
457
|
+
- [ ] font-synthesis
|
|
458
|
+
- [ ] font-variant
|
|
459
|
+
- [x] gap
|
|
460
|
+
- [ ] grid
|
|
461
|
+
- [ ] grid-area
|
|
462
|
+
- [ ] grid-column
|
|
463
|
+
- [ ] grid-row
|
|
464
|
+
- [ ] grid-template
|
|
408
465
|
- [x] inset
|
|
466
|
+
- [x] list-style
|
|
409
467
|
- [x] margin
|
|
468
|
+
- [ ] mask
|
|
469
|
+
- [ ] offset
|
|
410
470
|
- [x] outline
|
|
411
471
|
- [x] overflow
|
|
412
472
|
- [x] padding
|
|
473
|
+
- [ ] place-content
|
|
474
|
+
- [ ] place-items
|
|
475
|
+
- [ ] place-self
|
|
476
|
+
- [ ] scroll-margin
|
|
477
|
+
- [ ] scroll-padding
|
|
478
|
+
- [ ] scroll-timeline
|
|
413
479
|
- [x] text-decoration
|
|
480
|
+
- [x] text-emphasis
|
|
481
|
+
- [x] transition
|
|
414
482
|
|
|
415
483
|
## Performance
|
|
416
484
|
|
|
417
485
|
- [x] flatten @import
|
|
418
486
|
|
|
487
|
+
## Node Transformation
|
|
488
|
+
|
|
489
|
+
Ast can be transformed using node visitors
|
|
490
|
+
|
|
491
|
+
### Exemple 1: Declaration
|
|
492
|
+
|
|
493
|
+
```typescript
|
|
494
|
+
|
|
495
|
+
import {AstDeclaration, ParserOptions} from "../src/@types";
|
|
496
|
+
|
|
497
|
+
const options: ParserOptions = {
|
|
498
|
+
|
|
499
|
+
visitor: {
|
|
500
|
+
|
|
501
|
+
Declaration: (node: AstDeclaration) => {
|
|
502
|
+
|
|
503
|
+
if (node.nam == '-webkit-transform') {
|
|
504
|
+
|
|
505
|
+
node.nam = 'transform'
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const css = `
|
|
512
|
+
|
|
513
|
+
.foo {
|
|
514
|
+
-webkit-transform: scale(calc(100 * 2/ 15));
|
|
515
|
+
}
|
|
516
|
+
`;
|
|
517
|
+
|
|
518
|
+
console.debug(await transform(css, options));
|
|
519
|
+
|
|
520
|
+
// .foo{transform:scale(calc(40/3))}
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
### Exemple 2: Declaration
|
|
524
|
+
|
|
525
|
+
```typescript
|
|
526
|
+
|
|
527
|
+
import {AstDeclaration, LengthToken, ParserOptions} from "../src/@types";
|
|
528
|
+
import {EnumToken, NodeType} from "../src/lib";
|
|
529
|
+
import {transform} from "../src/node";
|
|
530
|
+
|
|
531
|
+
const options: ParserOptions = {
|
|
532
|
+
|
|
533
|
+
visitor: {
|
|
534
|
+
|
|
535
|
+
Declaration: {
|
|
536
|
+
|
|
537
|
+
// called only for height declaration
|
|
538
|
+
height: (node: AstDeclaration): AstDeclaration[] => {
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
return [
|
|
542
|
+
node,
|
|
543
|
+
{
|
|
544
|
+
|
|
545
|
+
typ: NodeType.DeclarationNodeType,
|
|
546
|
+
nam: 'width',
|
|
547
|
+
val: [
|
|
548
|
+
<LengthToken>{
|
|
549
|
+
typ: EnumToken.Length,
|
|
550
|
+
val: '3',
|
|
551
|
+
unit: 'px'
|
|
552
|
+
}
|
|
553
|
+
]
|
|
554
|
+
}
|
|
555
|
+
];
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
const css = `
|
|
562
|
+
|
|
563
|
+
.foo {
|
|
564
|
+
height: calc(100px * 2/ 15);
|
|
565
|
+
}
|
|
566
|
+
`;
|
|
567
|
+
|
|
568
|
+
console.debug(await transform(css, options));
|
|
569
|
+
|
|
570
|
+
// .foo{height:calc(40px/3);width:3px}
|
|
571
|
+
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### Exemple 3: At-Rule
|
|
575
|
+
|
|
576
|
+
```typescript
|
|
577
|
+
|
|
578
|
+
import {AstAtRule, ParserOptions} from "../src/@types";
|
|
579
|
+
import {transform} from "../src/node";
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
const options: ParserOptions = {
|
|
583
|
+
|
|
584
|
+
visitor: {
|
|
585
|
+
|
|
586
|
+
AtRule: (node: AstAtRule): AstAtRule => {
|
|
587
|
+
|
|
588
|
+
if (node.nam == 'media') {
|
|
589
|
+
|
|
590
|
+
return {...node, val: 'all'}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
const css = `
|
|
597
|
+
|
|
598
|
+
@media screen {
|
|
599
|
+
|
|
600
|
+
.foo {
|
|
601
|
+
|
|
602
|
+
height: calc(100px * 2/ 15);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
`;
|
|
606
|
+
|
|
607
|
+
console.debug(await transform(css, options));
|
|
608
|
+
|
|
609
|
+
// .foo{height:calc(40px/3)}
|
|
610
|
+
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
### Exemple 4: At-Rule
|
|
614
|
+
|
|
615
|
+
```typescript
|
|
616
|
+
|
|
617
|
+
import {AstAtRule, ParserOptions} from "../src/@types";
|
|
618
|
+
import {transform} from "../src/node";
|
|
619
|
+
|
|
620
|
+
const options: ParserOptions = {
|
|
621
|
+
|
|
622
|
+
visitor: {
|
|
623
|
+
|
|
624
|
+
AtRule: {
|
|
625
|
+
|
|
626
|
+
media: (node: AstAtRule): AstAtRule => {
|
|
627
|
+
|
|
628
|
+
return {...node, val: 'all'}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
const css = `
|
|
635
|
+
|
|
636
|
+
@media screen {
|
|
637
|
+
|
|
638
|
+
.foo {
|
|
639
|
+
|
|
640
|
+
height: calc(100px * 2/ 15);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
`;
|
|
644
|
+
|
|
645
|
+
console.debug(await transform(css, options));
|
|
646
|
+
|
|
647
|
+
// .foo{height:calc(40px/3)}
|
|
648
|
+
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
### Exemple 5: Rule
|
|
652
|
+
|
|
653
|
+
```typescript
|
|
654
|
+
|
|
655
|
+
import {AstAtRule, ParserOptions} from "../src/@types";
|
|
656
|
+
import {transform} from "../src/node";
|
|
657
|
+
|
|
658
|
+
const options: ParserOptions = {
|
|
659
|
+
|
|
660
|
+
visitor: {
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
Rule (node: AstRule): AstRule {
|
|
664
|
+
|
|
665
|
+
return {...node, sel: '.foo,.bar,.fubar'};
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
const css = `
|
|
671
|
+
|
|
672
|
+
.foo {
|
|
673
|
+
|
|
674
|
+
height: calc(100px * 2/ 15);
|
|
675
|
+
}
|
|
676
|
+
`;
|
|
677
|
+
|
|
678
|
+
console.debug(await transform(css, options));
|
|
679
|
+
|
|
680
|
+
// .foo,.bar,.fubar{height:calc(40px/3)}
|
|
681
|
+
|
|
682
|
+
```
|
|
683
|
+
|
|
419
684
|
---
|
|
420
685
|
|
|
421
686
|
Thanks to [Jetbrains](https://jetbrains.com) for sponsoring this project with a free license
|