@tbela99/css-parser 1.3.3 → 1.4.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +64 -48
  3. package/dist/config.json.js +3 -0
  4. package/dist/index-umd-web.js +2266 -631
  5. package/dist/index.cjs +2271 -620
  6. package/dist/index.d.ts +522 -181
  7. package/dist/lib/ast/expand.js +5 -10
  8. package/dist/lib/ast/features/calc.js +3 -2
  9. package/dist/lib/ast/features/inlinecssvariables.js +5 -3
  10. package/dist/lib/ast/features/prefix.js +1 -1
  11. package/dist/lib/ast/features/shorthand.js +1 -0
  12. package/dist/lib/ast/features/transform.js +13 -19
  13. package/dist/lib/ast/features/type.js +1 -1
  14. package/dist/lib/ast/minify.js +6 -3
  15. package/dist/lib/ast/transform/compute.js +2 -4
  16. package/dist/lib/ast/transform/matrix.js +20 -20
  17. package/dist/lib/ast/transform/minify.js +105 -12
  18. package/dist/lib/ast/transform/rotate.js +11 -11
  19. package/dist/lib/ast/transform/scale.js +6 -6
  20. package/dist/lib/ast/transform/skew.js +4 -4
  21. package/dist/lib/ast/transform/translate.js +3 -3
  22. package/dist/lib/ast/transform/utils.js +30 -37
  23. package/dist/lib/ast/types.js +76 -5
  24. package/dist/lib/ast/walk.js +77 -58
  25. package/dist/lib/fs/resolve.js +69 -10
  26. package/dist/lib/parser/declaration/list.js +6 -1
  27. package/dist/lib/parser/parse.js +1169 -312
  28. package/dist/lib/parser/tokenize.js +33 -20
  29. package/dist/lib/parser/utils/declaration.js +54 -0
  30. package/dist/lib/parser/utils/hash.js +86 -0
  31. package/dist/lib/parser/utils/text.js +8 -0
  32. package/dist/lib/renderer/render.js +26 -7
  33. package/dist/lib/syntax/color/relativecolor.js +0 -3
  34. package/dist/lib/syntax/syntax.js +36 -18
  35. package/dist/lib/validation/at-rules/container.js +11 -0
  36. package/dist/lib/validation/at-rules/counter-style.js +11 -0
  37. package/dist/lib/validation/at-rules/font-feature-values.js +11 -0
  38. package/dist/lib/validation/at-rules/keyframes.js +11 -0
  39. package/dist/lib/validation/at-rules/layer.js +11 -0
  40. package/dist/lib/validation/at-rules/media.js +11 -0
  41. package/dist/lib/validation/at-rules/page-margin-box.js +11 -0
  42. package/dist/lib/validation/at-rules/page.js +11 -0
  43. package/dist/lib/validation/at-rules/supports.js +11 -0
  44. package/dist/lib/validation/at-rules/when.js +11 -0
  45. package/dist/lib/validation/config.js +0 -2
  46. package/dist/lib/validation/config.json.js +36 -4
  47. package/dist/lib/validation/parser/parse.js +53 -2
  48. package/dist/lib/validation/syntax.js +204 -36
  49. package/dist/lib/validation/syntaxes/compound-selector.js +1 -2
  50. package/dist/lib/validation/syntaxes/relative-selector-list.js +2 -5
  51. package/dist/node.js +60 -18
  52. package/dist/types.d.ts +17 -0
  53. package/dist/types.js +20 -0
  54. package/dist/web.js +43 -17
  55. package/package.json +20 -17
  56. package/dist/lib/validation/parser/types.js +0 -54
package/CHANGELOG.md CHANGED
@@ -1,5 +1,49 @@
1
1
  # Changelog
2
2
 
3
+ ## v1.4.0
4
+
5
+ ### CSS Module support
6
+
7
+ - [x] scoped name generation
8
+ - composes:
9
+ - [x] compose from local selector
10
+ - [x] compose from other files
11
+ - [x] compose from global selector
12
+ - [x] comma separated compose list
13
+ - [x] :local
14
+ - [x] :global
15
+ - [x] :local()
16
+ - [x] :global()
17
+ - [x] selector
18
+ - [x] dashed ident (custom properties)
19
+ - [x] css at-rule property
20
+ - [x] css at-rule value
21
+ - [x] keyframe animations
22
+ - [x] grid
23
+ - naming
24
+ - [x] ignore
25
+ - [x] camelCase
26
+ - [x] camelCaseOnly
27
+ - [x] dashCase
28
+ - [x] dashCaseOnly
29
+ - default mode
30
+ - [x] global
31
+ - [x] local
32
+ - [x] pure(at least one class or id)
33
+ - icss
34
+ - [x] :import
35
+ - [x] :export
36
+
37
+ ### Bug Fixes
38
+ - [x] fix \<integer\> syntax validation #115
39
+
40
+ ## v1.3.4
41
+
42
+ - [x] make streaming optional #109
43
+ - [x] patch at-rule syntax for @font-feature-value #110
44
+ - [x] support percentage in transform() and scale() #111
45
+ - [x] allow arrays in visitor definition #112
46
+
3
47
  ## v1.3.3
4
48
 
5
49
  - [x] relative color computation bug #105
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # css-parser
6
6
 
7
- CSS parser and minifier for node and the browser
7
+ CSS parser, minifier and validator for node and the browser
8
8
 
9
9
  ## Installation
10
10
 
@@ -24,21 +24,22 @@ $ deno add @tbela99/css-parser
24
24
 
25
25
  - no dependency
26
26
  - CSS validation based upon mdn-data
27
- - fault-tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
27
+ - CSS modules support
28
+ - fault-tolerant parser implementing the CSS syntax module 3 recommendations.
28
29
  - fast and efficient minification without unsafe transforms,
29
30
  see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
30
- - minify colors: color(), lab(), lch(), oklab(), oklch(), color-mix(), light-dark(), system colors and
31
+ - colors minification: color(), lab(), lch(), oklab(), oklch(), color-mix(), light-dark(), system colors and
31
32
  relative color
32
- - generate nested css rules
33
- - convert nested css rules to legacy syntax
34
- - convert colors to any supported color format
35
- - generate sourcemap
36
- - compute css shorthands. see supported properties list below
37
- - minify css transform functions
38
- - evaluate math functions: calc(), clamp(), min(), max(), etc.
39
- - inline css variables
40
- - remove duplicate properties
41
- - flatten @import rules
33
+ - color conversion to any supported color format
34
+ - automatic nested css rules generation
35
+ - nested css rules conversion to legacy syntax
36
+ - sourcemap generation
37
+ - css shorthands computation. see the supported properties list below
38
+ - css transform functions minification
39
+ - css math functions evaluation: calc(), clamp(), min(), max(), etc.
40
+ - css variables inlining
41
+ - duplicate properties removal
42
+ - @import rules flattening
42
43
  - experimental CSS prefix removal
43
44
 
44
45
  ## Online documentation
@@ -49,11 +50,11 @@ See the full documentation at the [CSS Parser](https://tbela99.github.io/css-par
49
50
 
50
51
  Try it [online](https://tbela99.github.io/css-parser/playground/)
51
52
 
52
- ## Exports
53
+ ## Import
53
54
 
54
55
  There are several ways to import the library into your application.
55
56
 
56
- ### Node exports
57
+ ### Node
57
58
 
58
59
  import as a module
59
60
 
@@ -64,7 +65,7 @@ import {transform} from '@tbela99/css-parser';
64
65
  // ...
65
66
  ```
66
67
 
67
- ### Deno exports
68
+ ### Deno
68
69
 
69
70
  import as a module
70
71
 
@@ -84,7 +85,7 @@ const {transform} = require('@tbela99/css-parser/cjs');
84
85
  // ...
85
86
  ```
86
87
 
87
- ### Web export
88
+ ### Web
88
89
 
89
90
  Programmatic import
90
91
 
@@ -101,7 +102,7 @@ Javascript module from cdn
101
102
 
102
103
  <script type="module">
103
104
 
104
- import {transform} from 'https://esm.sh/@tbela99/css-parser@1.3.3/web';
105
+ import {transform} from 'https://esm.sh/@tbela99/css-parser@1.3.4/web';
105
106
 
106
107
  const css = `
107
108
  .s {
@@ -126,7 +127,7 @@ Javascript umd module from cdn
126
127
 
127
128
  ```html
128
129
 
129
- <script src="https://unpkg.com/@tbela99/css-parser@1.3.2/dist/index-umd-web.js"></script>
130
+ <script src="https://unpkg.com/@tbela99/css-parser@1.3.4/dist/index-umd-web.js"></script>
130
131
  <script>
131
132
 
132
133
  (async () => {
@@ -162,19 +163,27 @@ Javascript umd module from cdn
162
163
  </script>
163
164
  ```
164
165
 
165
- ## Transform
166
-
167
- Parse and render css in a single pass.
166
+ ## Exported functions
168
167
 
169
- ### Usage
168
+ ```ts
170
169
 
171
- ```typescript
170
+ /* parse and render css */
171
+ transform(css: string | ReadableStream<string>, options?: TransformOptions = {}): Promise<TransformResult>
172
+ /* parse css */
173
+ parse(css: string | ReadableStream<string>, options?: ParseOptions = {}): Promise<ParseResult>;
174
+ /* render ast */
175
+ render(ast: AstNode, options?: RenderOptions = {}): RenderResult;
176
+ /* parse and render css file or url */
177
+ transformFile(filePathOrUrl: string, options?: TransformOptions = {}, asStream?: boolean): Promise<TransformResult>;
178
+ /* parse css file or url */
179
+ parseFile(filePathOrUrl: string, options?: ParseOptions = {}, asStream?: boolean): Promise<ParseResult>;
172
180
 
173
- transform(css: string | ReadableStream<string>, transformOptions: TransformOptions = {}): TransformResult
174
- parse(css: string | ReadableStream<string>, parseOptions: ParseOptions = {}): ParseResult;
175
- render(ast: AstNode, renderOptions: RenderOptions = {}): RenderResult;
176
181
  ```
177
182
 
183
+ ## Transform
184
+
185
+ Parse and render css in a single pass.
186
+
178
187
  ### Example
179
188
 
180
189
  ```javascript
@@ -208,7 +217,7 @@ Include ParseOptions and RenderOptions
208
217
  > Minify Options
209
218
 
210
219
  - minify: boolean, optional. default to _true_. optimize ast.
211
- - pass: number, optional. minification pass. default to 1
220
+ - pass: number, optional. minification passes. default to 1
212
221
  - nestingRules: boolean, optional. automatically generated nested rules.
213
222
  - expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules
214
223
  to false.
@@ -220,6 +229,10 @@ Include ParseOptions and RenderOptions
220
229
  in the :root {} or html {} rule.
221
230
  - removeEmpty: boolean, optional. remove empty rule lists from the ast.
222
231
 
232
+ > CSS modules Options
233
+
234
+ - module: boolean | ModuleCaseTransformEnum | ModuleScopeEnumOptions | ModuleOptions, optional. css modules options.
235
+
223
236
  > CSS Prefix Removal Options
224
237
 
225
238
  - removePrefix: boolean, optional. remove CSS prefixes.
@@ -764,6 +777,7 @@ result
764
777
  # Node Walker
765
778
 
766
779
  ```javascript
780
+
767
781
  import {walk} from '@tbela99/css-parser';
768
782
 
769
783
  for (const {node, parent, root} of walk(ast)) {
@@ -894,7 +908,7 @@ for (const {node, parent, root} of walk(ast)) {
894
908
 
895
909
  Ast can be transformed using node visitors
896
910
 
897
- ### Exemple 1: Declaration
911
+ ### Example 1: Declaration
898
912
 
899
913
  the visitor is called for any declaration encountered
900
914
 
@@ -923,20 +937,19 @@ const css = `
923
937
  }
924
938
  `;
925
939
 
926
- console.debug(await transform(css, options));
940
+ const result = await transform(css, options);
941
+ console.debug(result.code);
927
942
 
928
943
  // .foo{transform:scale(calc(40/3))}
929
944
  ```
930
945
 
931
- ### Exemple 2: Declaration
946
+ ### Example 2: Declaration
932
947
 
933
948
  the visitor is called only on 'height' declarations
934
949
 
935
950
  ```typescript
936
951
 
937
- import {AstDeclaration, LengthToken, ParserOptions} from "../src/@types";
938
- import {EnumToken} from "../src/lib";
939
- import {transform} from "../src/node";
952
+ import {AstDeclaration, EnumToken, LengthToken, ParserOptions, transform} from '@tbela99/css-parser';
940
953
 
941
954
  const options: ParserOptions = {
942
955
 
@@ -978,13 +991,14 @@ color: lch(from peru calc(l * 0.8) calc(c * 0.7) calc(h + 180))
978
991
  }
979
992
  `;
980
993
 
981
- console.debug(await transform(css, options));
994
+ const result = await transform(css, options);
995
+ console.debug(result.code);
982
996
 
983
997
  // .foo{height:calc(40px/3);width:3px}.selector{color:#0880b0}
984
998
 
985
999
  ```
986
1000
 
987
- ### Exemple 3: At-Rule
1001
+ ### Example 3: At-Rule
988
1002
 
989
1003
  the visitor is called on any at-rule
990
1004
 
@@ -1019,13 +1033,14 @@ const css = `
1019
1033
  }
1020
1034
  `;
1021
1035
 
1022
- console.debug(await transform(css, options));
1036
+ const result = await transform(css, options);
1037
+ console.debug(result.code);
1023
1038
 
1024
1039
  // .foo{height:calc(40px/3)}
1025
1040
 
1026
1041
  ```
1027
1042
 
1028
- ### Exemple 4: At-Rule
1043
+ ### Example 4: At-Rule
1029
1044
 
1030
1045
  the visitor is called only for at-rule media
1031
1046
 
@@ -1060,13 +1075,14 @@ const css = `
1060
1075
  }
1061
1076
  `;
1062
1077
 
1063
- console.debug(await transform(css, options));
1078
+ const result = await transform(css, options);
1079
+ console.debug(result.code);
1064
1080
 
1065
1081
  // .foo{height:calc(40px/3)}
1066
1082
 
1067
1083
  ```
1068
1084
 
1069
- ### Exemple 5: Rule
1085
+ ### Example 5: Rule
1070
1086
 
1071
1087
  the visitor is called on any Rule
1072
1088
 
@@ -1079,10 +1095,10 @@ const options: ParserOptions = {
1079
1095
 
1080
1096
  visitor: {
1081
1097
 
1082
-
1083
1098
  Rule(node: AstRule): AstRule {
1084
1099
 
1085
- return {...node, sel: '.foo,.bar,.fubar'};
1100
+ node.sel = '.foo,.bar,.fubar'
1101
+ return node;
1086
1102
  }
1087
1103
  }
1088
1104
  };
@@ -1095,21 +1111,20 @@ const css = `
1095
1111
  }
1096
1112
  `;
1097
1113
 
1098
- console.debug(await transform(css, options));
1114
+ const result = await transform(css, options);
1115
+ console.debug(result.code);
1099
1116
 
1100
1117
  // .foo,.bar,.fubar{height:calc(40px/3)}
1101
1118
 
1102
1119
  ```
1103
1120
 
1104
- ### Exemple 6: Rule
1121
+ ### Example 6: Rule
1105
1122
 
1106
1123
  Adding declarations to any rule
1107
1124
 
1108
1125
  ```typescript
1109
1126
 
1110
- import {transform} from "../src/node";
1111
- import {AstRule, ParserOptions} from "../src/@types";
1112
- import {parseDeclarations} from "../src/lib";
1127
+ import {AstRule, parseDeclarations, ParserOptions, transform} from '@tbela99/css-parser';
1113
1128
 
1114
1129
  const options: ParserOptions = {
1115
1130
 
@@ -1135,7 +1150,8 @@ const css = `
1135
1150
  }
1136
1151
  `;
1137
1152
 
1138
- console.debug(await transform(css, options));
1153
+ const result = await transform(css, options);
1154
+ console.debug(result.code);
1139
1155
 
1140
1156
  // .foo{width:3px}
1141
1157
 
@@ -562,6 +562,9 @@ var map = {
562
562
  },
563
563
  animation: {
564
564
  shorthand: "animation",
565
+ separator: {
566
+ typ: "Comma"
567
+ },
565
568
  pattern: "animation-name animation-duration animation-timing-function animation-delay animation-iteration-count animation-direction animation-fill-mode animation-play-state animation-timeline",
566
569
  "default": [
567
570
  "1",