@tbela99/css-parser 0.1.0 → 0.2.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
@@ -13,7 +13,7 @@ $ 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), 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
@@ -64,6 +64,8 @@ Include ParseOptions and RenderOptions
64
64
  - inlineCssVariables: boolean, optional. replace css variables with their current value.
65
65
  - computeCalcExpression: boolean, optional. evaluate calc() expression
66
66
  - inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the :root {} rule.
67
+ - visitor: VisitorNodeMap, optional. node visitor used to transform the ast.
68
+ - signal: AbortSignal, optional. abort parsing.
67
69
 
68
70
  #### RenderOptions
69
71
 
@@ -100,7 +102,7 @@ const {ast, errors, stats} = await parse(css);
100
102
  ### Usage
101
103
 
102
104
  ```javascript
103
- doRender(ast, RenderOptions = {});
105
+ render(ast, RenderOptions = {});
104
106
  ```
105
107
 
106
108
  ### Example
@@ -394,6 +396,7 @@ result
394
396
  - [x] decode and replace utf-8 escape sequence
395
397
 
396
398
  ## Computed shorthands properties
399
+ - [x] animation
397
400
  - [x] background
398
401
  - [x] border
399
402
  - [x] border-bottom
@@ -406,16 +409,216 @@ result
406
409
  - [x] border-width
407
410
  - [x] font
408
411
  - [x] inset
412
+ - [x] list-style
409
413
  - [x] margin
410
414
  - [x] outline
411
415
  - [x] overflow
412
416
  - [x] padding
413
417
  - [x] text-decoration
418
+ - [x] text-emphasis
419
+ - [x] transition
414
420
 
415
421
  ## Performance
416
422
 
417
423
  - [x] flatten @import
418
424
 
425
+ ## Node Transformation
426
+
427
+ Ast can be transformed using node visitors
428
+
429
+ ### Exemple 1: Declaration
430
+
431
+ ```typescript
432
+
433
+ import {AstDeclaration, ParserOptions} from "../src/@types";
434
+
435
+ const options: ParserOptions = {
436
+
437
+ visitor: {
438
+
439
+ Declaration: (node: AstDeclaration) => {
440
+
441
+ if (node.nam == '-webkit-transform') {
442
+
443
+ node.nam = 'transform'
444
+ }
445
+ }
446
+ }
447
+ }
448
+
449
+ const css = `
450
+
451
+ .foo {
452
+ -webkit-transform: scale(calc(100 * 2/ 15));
453
+ }
454
+ `;
455
+
456
+ console.debug(await transform(css, options));
457
+
458
+ // .foo{transform:scale(calc(40/3))}
459
+ ```
460
+
461
+ ### Exemple 2: Declaration
462
+
463
+ ```typescript
464
+
465
+ import {AstDeclaration, LengthToken, ParserOptions} from "../src/@types";
466
+ import {EnumToken, NodeType} from "../src/lib";
467
+ import {transform} from "../src/node";
468
+
469
+ const options: ParserOptions = {
470
+
471
+ visitor: {
472
+
473
+ Declaration: {
474
+
475
+ // called only for height declaration
476
+ height: (node: AstDeclaration): AstDeclaration[] => {
477
+
478
+
479
+ return [
480
+ node,
481
+ {
482
+
483
+ typ: NodeType.DeclarationNodeType,
484
+ nam: 'width',
485
+ val: [
486
+ <LengthToken>{
487
+ typ: EnumToken.Length,
488
+ val: '3',
489
+ unit: 'px'
490
+ }
491
+ ]
492
+ }
493
+ ];
494
+ }
495
+ }
496
+ }
497
+ };
498
+
499
+ const css = `
500
+
501
+ .foo {
502
+ height: calc(100px * 2/ 15);
503
+ }
504
+ `;
505
+
506
+ console.debug(await transform(css, options));
507
+
508
+ // .foo{height:calc(40px/3);width:3px}
509
+
510
+ ```
511
+
512
+ ### Exemple 3: At-Rule
513
+
514
+ ```typescript
515
+
516
+ import {AstAtRule, ParserOptions} from "../src/@types";
517
+ import {transform} from "../src/node";
518
+
519
+
520
+ const options: ParserOptions = {
521
+
522
+ visitor: {
523
+
524
+ AtRule: (node: AstAtRule): AstAtRule => {
525
+
526
+ if (node.nam == 'media') {
527
+
528
+ return {...node, val: 'all'}
529
+ }
530
+ }
531
+ }
532
+ };
533
+
534
+ const css = `
535
+
536
+ @media screen {
537
+
538
+ .foo {
539
+
540
+ height: calc(100px * 2/ 15);
541
+ }
542
+ }
543
+ `;
544
+
545
+ console.debug(await transform(css, options));
546
+
547
+ // .foo{height:calc(40px/3)}
548
+
549
+ ```
550
+
551
+ ### Exemple 4: At-Rule
552
+
553
+ ```typescript
554
+
555
+ import {AstAtRule, ParserOptions} from "../src/@types";
556
+ import {transform} from "../src/node";
557
+
558
+ const options: ParserOptions = {
559
+
560
+ visitor: {
561
+
562
+ AtRule: {
563
+
564
+ media: (node: AstAtRule): AstAtRule => {
565
+
566
+ return {...node, val: 'all'}
567
+ }
568
+ }
569
+ }
570
+ };
571
+
572
+ const css = `
573
+
574
+ @media screen {
575
+
576
+ .foo {
577
+
578
+ height: calc(100px * 2/ 15);
579
+ }
580
+ }
581
+ `;
582
+
583
+ console.debug(await transform(css, options));
584
+
585
+ // .foo{height:calc(40px/3)}
586
+
587
+ ```
588
+
589
+ ### Exemple 5: Rule
590
+
591
+ ```typescript
592
+
593
+ import {AstAtRule, ParserOptions} from "../src/@types";
594
+ import {transform} from "../src/node";
595
+
596
+ const options: ParserOptions = {
597
+
598
+ visitor: {
599
+
600
+
601
+ Rule (node: AstRule): AstRule {
602
+
603
+ return {...node, sel: '.foo,.bar,.fubar'};
604
+ }
605
+ }
606
+ };
607
+
608
+ const css = `
609
+
610
+ .foo {
611
+
612
+ height: calc(100px * 2/ 15);
613
+ }
614
+ `;
615
+
616
+ console.debug(await transform(css, options));
617
+
618
+ // .foo,.bar,.fubar{height:calc(40px/3)}
619
+
620
+ ```
621
+
419
622
  ---
420
623
 
421
624
  Thanks to [Jetbrains](https://jetbrains.com) for sponsoring this project with a free license
@@ -232,6 +232,317 @@ var properties = {
232
232
  }
233
233
  };
234
234
  var map = {
235
+ transition: {
236
+ shorthand: "transition",
237
+ multiple: true,
238
+ separator: ",",
239
+ pattern: "transition-property transition-duration transition-timing-function transition-delay transition-behavior",
240
+ keywords: [
241
+ "none",
242
+ "all"
243
+ ],
244
+ "default": [
245
+ "0s",
246
+ "0ms",
247
+ "all",
248
+ "ease",
249
+ "none",
250
+ "normal"
251
+ ],
252
+ properties: {
253
+ "transition-property": {
254
+ keywords: [
255
+ "none",
256
+ "all"
257
+ ],
258
+ "default": [
259
+ ],
260
+ types: [
261
+ "Iden"
262
+ ]
263
+ },
264
+ "transition-duration": {
265
+ keywords: [
266
+ ],
267
+ "default": [
268
+ "0s",
269
+ "0ms",
270
+ "normal"
271
+ ],
272
+ types: [
273
+ "Time"
274
+ ]
275
+ },
276
+ "transition-timing-function": {
277
+ keywords: [
278
+ "ease",
279
+ "ease-in",
280
+ "ease-out",
281
+ "ease-in-out",
282
+ "linear",
283
+ "step-start",
284
+ "step-end"
285
+ ],
286
+ "default": [
287
+ "ease"
288
+ ],
289
+ types: [
290
+ "TimingFunction"
291
+ ],
292
+ mapping: {
293
+ "cubic-bezier(.25,.1,.25,1)": "ease",
294
+ "cubic-bezier(0,0,1,1)": "linear",
295
+ "cubic-bezier(.42,0,1,1)": "ease-in",
296
+ "cubic-bezier(0,0,.58,1)": "ease-out",
297
+ "cubic-bezier(.42,0,.58,.42)": "ease-in-out"
298
+ }
299
+ },
300
+ "transition-delay": {
301
+ keywords: [
302
+ ],
303
+ "default": [
304
+ "0s"
305
+ ],
306
+ types: [
307
+ "Time"
308
+ ]
309
+ },
310
+ "transition-behavior": {
311
+ keywords: [
312
+ "normal",
313
+ "allow-discrete"
314
+ ],
315
+ "default": [
316
+ "normal"
317
+ ],
318
+ types: [
319
+ ]
320
+ }
321
+ }
322
+ },
323
+ "transition-property": {
324
+ shorthand: "transition"
325
+ },
326
+ "transition-duration": {
327
+ shorthand: "transition"
328
+ },
329
+ "transition-timing-function": {
330
+ shorthand: "transition"
331
+ },
332
+ "transition-delay": {
333
+ shorthand: "transition"
334
+ },
335
+ "transition-behavior": {
336
+ shorthand: "transition"
337
+ },
338
+ animation: {
339
+ shorthand: "animation",
340
+ pattern: "animation-name animation-duration animation-timing-function animation-delay animation-iteration-count animation-direction animation-fill-mode animation-play-state animation-timeline",
341
+ "default": [
342
+ "1",
343
+ "0s",
344
+ "0ms",
345
+ "none",
346
+ "ease",
347
+ "normal",
348
+ "running",
349
+ "auto"
350
+ ],
351
+ properties: {
352
+ "animation-name": {
353
+ keywords: [
354
+ "none"
355
+ ],
356
+ "default": [
357
+ "none"
358
+ ],
359
+ types: [
360
+ "Iden"
361
+ ]
362
+ },
363
+ "animation-duration": {
364
+ keywords: [
365
+ "auto"
366
+ ],
367
+ "default": [
368
+ "0s",
369
+ "0ms",
370
+ "auto"
371
+ ],
372
+ types: [
373
+ "Time"
374
+ ],
375
+ mapping: {
376
+ auto: "0s"
377
+ }
378
+ },
379
+ "animation-timing-function": {
380
+ keywords: [
381
+ "ease",
382
+ "ease-in",
383
+ "ease-out",
384
+ "ease-in-out",
385
+ "linear",
386
+ "step-start",
387
+ "step-end"
388
+ ],
389
+ "default": [
390
+ "ease"
391
+ ],
392
+ types: [
393
+ "TimingFunction"
394
+ ],
395
+ mapping: {
396
+ "cubic-bezier(.25,.1,.25,1)": "ease",
397
+ "cubic-bezier(0,0,1,1)": "linear",
398
+ "cubic-bezier(.42,0,1,1)": "ease-in",
399
+ "cubic-bezier(0,0,.58,1)": "ease-out",
400
+ "cubic-bezier(.42,0,.58,.42)": "ease-in-out"
401
+ }
402
+ },
403
+ "animation-delay": {
404
+ keywords: [
405
+ ],
406
+ "default": [
407
+ "0s",
408
+ "0ms"
409
+ ],
410
+ types: [
411
+ "Time"
412
+ ]
413
+ },
414
+ "animation-iteration-count": {
415
+ keywords: [
416
+ "infinite"
417
+ ],
418
+ "default": [
419
+ "1"
420
+ ],
421
+ types: [
422
+ "Number"
423
+ ]
424
+ },
425
+ "animation-direction": {
426
+ keywords: [
427
+ "normal",
428
+ "reverse",
429
+ "alternate",
430
+ "alternate-reverse"
431
+ ],
432
+ "default": [
433
+ "normal"
434
+ ],
435
+ types: [
436
+ ]
437
+ },
438
+ "animation-fill-mode": {
439
+ keywords: [
440
+ "none",
441
+ "forwards",
442
+ "backwards",
443
+ "both"
444
+ ],
445
+ "default": [
446
+ "none"
447
+ ],
448
+ types: [
449
+ ]
450
+ },
451
+ "animation-play-state": {
452
+ keywords: [
453
+ "running",
454
+ "paused"
455
+ ],
456
+ "default": [
457
+ "running"
458
+ ],
459
+ types: [
460
+ ]
461
+ },
462
+ "animation-timeline": {
463
+ keywords: [
464
+ "none",
465
+ "auto"
466
+ ],
467
+ "default": [
468
+ "auto"
469
+ ],
470
+ types: [
471
+ "DashedIden",
472
+ "TimelineFunction"
473
+ ]
474
+ }
475
+ }
476
+ },
477
+ "animation-name": {
478
+ shorthand: "animation"
479
+ },
480
+ "animation-duration": {
481
+ shorthand: "animation"
482
+ },
483
+ "animation-timing-function": {
484
+ shorthand: "animation"
485
+ },
486
+ "animation-delay": {
487
+ shorthand: "animation"
488
+ },
489
+ "animation-iteration-count": {
490
+ shorthand: "animation"
491
+ },
492
+ "animation-direction": {
493
+ shorthand: "animation"
494
+ },
495
+ "animation-fill-mode": {
496
+ shorthand: "animation"
497
+ },
498
+ "animation-play-state": {
499
+ shorthand: "animation"
500
+ },
501
+ "animation-timeline": {
502
+ shorthand: "animation"
503
+ },
504
+ "text-emphasis": {
505
+ shorthand: "text-emphasis",
506
+ pattern: "text-emphasis-color text-emphasis-style",
507
+ "default": [
508
+ "none",
509
+ "currentcolor"
510
+ ],
511
+ properties: {
512
+ "text-emphasis-style": {
513
+ keywords: [
514
+ "none",
515
+ "filled",
516
+ "open",
517
+ "dot",
518
+ "circle",
519
+ "double-circle",
520
+ "triangle",
521
+ "sesame"
522
+ ],
523
+ "default": [
524
+ "none"
525
+ ],
526
+ types: [
527
+ "String"
528
+ ]
529
+ },
530
+ "text-emphasis-color": {
531
+ "default": [
532
+ "currentcolor"
533
+ ],
534
+ types: [
535
+ "Color"
536
+ ]
537
+ }
538
+ }
539
+ },
540
+ "text-emphasis-style": {
541
+ shorthand: "text-emphasis"
542
+ },
543
+ "text-emphasis-color": {
544
+ shorthand: "text-emphasis"
545
+ },
235
546
  border: {
236
547
  shorthand: "border",
237
548
  pattern: "border-color border-style border-width",
@@ -297,6 +608,75 @@ var map = {
297
608
  "border-width": {
298
609
  shorthand: "border"
299
610
  },
611
+ "list-style": {
612
+ shorthand: "list-style",
613
+ pattern: "list-style-type list-style-position list-style-image",
614
+ keywords: [
615
+ "none",
616
+ "outside"
617
+ ],
618
+ "default": [
619
+ "none",
620
+ "outside"
621
+ ],
622
+ properties: {
623
+ "list-style-position": {
624
+ types: [
625
+ ],
626
+ "default": [
627
+ "outside"
628
+ ],
629
+ keywords: [
630
+ "inside",
631
+ "outside"
632
+ ]
633
+ },
634
+ "list-style-image": {
635
+ "default": [
636
+ "none"
637
+ ],
638
+ keywords: [
639
+ "node"
640
+ ],
641
+ types: [
642
+ "UrlFunc",
643
+ "ImageFunc"
644
+ ]
645
+ },
646
+ "list-style-type": {
647
+ types: [
648
+ "String",
649
+ "Iden",
650
+ "Symbols"
651
+ ],
652
+ "default": [
653
+ "disc"
654
+ ],
655
+ keywords: [
656
+ "disc",
657
+ "circle",
658
+ "square",
659
+ "decimal",
660
+ "decimal-leading-zero",
661
+ "lower-roman",
662
+ "upper-roman",
663
+ "lower-greek",
664
+ "lower-latin",
665
+ "upper-latin",
666
+ "none"
667
+ ]
668
+ }
669
+ }
670
+ },
671
+ "list-style-position": {
672
+ shorthand: "list-style"
673
+ },
674
+ "list-style-image": {
675
+ shorthand: "list-style"
676
+ },
677
+ "list-style-type": {
678
+ shorthand: "list-style"
679
+ },
300
680
  overflow: {
301
681
  shorthand: "overflow",
302
682
  pattern: "overflow-x overflow-y",
@@ -354,7 +734,8 @@ var map = {
354
734
  ],
355
735
  "default": [
356
736
  "0",
357
- "none"
737
+ "none",
738
+ "currentcolor"
358
739
  ],
359
740
  properties: {
360
741
  "outline-color": {
@@ -362,10 +743,10 @@ var map = {
362
743
  "Color"
363
744
  ],
364
745
  "default": [
365
- "currentColor"
746
+ "currentcolor"
366
747
  ],
367
748
  keywords: [
368
- "currentColor"
749
+ "currentcolor"
369
750
  ]
370
751
  },
371
752
  "outline-style": {
@@ -709,7 +1090,8 @@ var map = {
709
1090
  },
710
1091
  "background-image": {
711
1092
  types: [
712
- "UrlFunc"
1093
+ "UrlFunc",
1094
+ "ImageFunc"
713
1095
  ],
714
1096
  "default": [
715
1097
  "none"