@tbela99/css-parser 0.9.1 → 1.1.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 (99) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/LICENSE.md +1 -1
  3. package/README.md +29 -17
  4. package/dist/index-umd-web.js +7461 -4360
  5. package/dist/index.cjs +8608 -5507
  6. package/dist/index.d.ts +203 -61
  7. package/dist/lib/ast/expand.js +2 -1
  8. package/dist/lib/ast/features/calc.js +19 -11
  9. package/dist/lib/ast/features/index.js +1 -0
  10. package/dist/lib/ast/features/inlinecssvariables.js +47 -29
  11. package/dist/lib/ast/features/prefix.js +117 -91
  12. package/dist/lib/ast/features/shorthand.js +34 -14
  13. package/dist/lib/ast/features/transform.js +67 -0
  14. package/dist/lib/ast/features/type.js +7 -0
  15. package/dist/lib/ast/math/expression.js +20 -10
  16. package/dist/lib/ast/math/math.js +20 -2
  17. package/dist/lib/ast/minify.js +209 -80
  18. package/dist/lib/ast/transform/compute.js +337 -0
  19. package/dist/lib/ast/transform/convert.js +33 -0
  20. package/dist/lib/ast/transform/matrix.js +112 -0
  21. package/dist/lib/ast/transform/minify.js +296 -0
  22. package/dist/lib/ast/transform/perspective.js +10 -0
  23. package/dist/lib/ast/transform/rotate.js +40 -0
  24. package/dist/lib/ast/transform/scale.js +32 -0
  25. package/dist/lib/ast/transform/skew.js +23 -0
  26. package/dist/lib/ast/transform/translate.js +32 -0
  27. package/dist/lib/ast/transform/utils.js +198 -0
  28. package/dist/lib/ast/types.js +18 -15
  29. package/dist/lib/ast/walk.js +54 -22
  30. package/dist/lib/fs/resolve.js +10 -0
  31. package/dist/lib/parser/declaration/list.js +48 -45
  32. package/dist/lib/parser/declaration/map.js +1 -0
  33. package/dist/lib/parser/declaration/set.js +2 -1
  34. package/dist/lib/parser/parse.js +449 -340
  35. package/dist/lib/parser/tokenize.js +147 -72
  36. package/dist/lib/parser/utils/declaration.js +5 -4
  37. package/dist/lib/parser/utils/type.js +2 -1
  38. package/dist/lib/renderer/color/a98rgb.js +2 -1
  39. package/dist/lib/renderer/color/{colormix.js → color-mix.js} +16 -7
  40. package/dist/lib/renderer/color/color.js +264 -170
  41. package/dist/lib/renderer/color/hex.js +19 -8
  42. package/dist/lib/renderer/color/hsl.js +9 -3
  43. package/dist/lib/renderer/color/hwb.js +2 -1
  44. package/dist/lib/renderer/color/lab.js +10 -1
  45. package/dist/lib/renderer/color/lch.js +10 -1
  46. package/dist/lib/renderer/color/oklab.js +10 -1
  47. package/dist/lib/renderer/color/oklch.js +10 -1
  48. package/dist/lib/renderer/color/p3.js +2 -1
  49. package/dist/lib/renderer/color/rec2020.js +2 -1
  50. package/dist/lib/renderer/color/relativecolor.js +27 -32
  51. package/dist/lib/renderer/color/rgb.js +14 -10
  52. package/dist/lib/renderer/color/srgb.js +48 -23
  53. package/dist/lib/renderer/color/utils/components.js +18 -6
  54. package/dist/lib/renderer/color/utils/constants.js +47 -3
  55. package/dist/lib/renderer/color/xyz.js +2 -1
  56. package/dist/lib/renderer/color/xyzd50.js +2 -1
  57. package/dist/lib/renderer/render.js +108 -43
  58. package/dist/lib/syntax/syntax.js +267 -136
  59. package/dist/lib/validation/at-rules/container.js +81 -103
  60. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  61. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  62. package/dist/lib/validation/at-rules/document.js +22 -27
  63. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  64. package/dist/lib/validation/at-rules/import.js +30 -81
  65. package/dist/lib/validation/at-rules/keyframes.js +19 -23
  66. package/dist/lib/validation/at-rules/layer.js +5 -5
  67. package/dist/lib/validation/at-rules/media.js +42 -53
  68. package/dist/lib/validation/at-rules/namespace.js +19 -23
  69. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  70. package/dist/lib/validation/at-rules/page.js +8 -7
  71. package/dist/lib/validation/at-rules/supports.js +73 -82
  72. package/dist/lib/validation/at-rules/when.js +32 -36
  73. package/dist/lib/validation/atrule.js +15 -18
  74. package/dist/lib/validation/config.js +24 -1
  75. package/dist/lib/validation/config.json.js +563 -63
  76. package/dist/lib/validation/parser/parse.js +196 -185
  77. package/dist/lib/validation/parser/types.js +1 -1
  78. package/dist/lib/validation/selector.js +8 -5
  79. package/dist/lib/validation/syntax.js +724 -1405
  80. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  81. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  82. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  83. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  84. package/dist/lib/validation/syntaxes/keyframe-block-list.js +6 -5
  85. package/dist/lib/validation/syntaxes/keyframe-selector.js +23 -105
  86. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  87. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  88. package/dist/lib/validation/syntaxes/relative-selector.js +17 -15
  89. package/dist/lib/validation/syntaxes/url.js +18 -22
  90. package/dist/lib/validation/utils/list.js +20 -2
  91. package/dist/lib/validation/utils/whitespace.js +2 -1
  92. package/dist/node/index.js +4 -2
  93. package/dist/node/load.js +6 -1
  94. package/dist/web/index.js +4 -2
  95. package/dist/web/load.js +5 -0
  96. package/package.json +16 -15
  97. package/dist/lib/renderer/color/prophotoRgb.js +0 -56
  98. package/dist/lib/validation/declaration.js +0 -94
  99. package/dist/lib/validation/syntaxes/image.js +0 -29
package/CHANGELOG.md ADDED
@@ -0,0 +1,265 @@
1
+ # Changelog
2
+
3
+ ## v1.1.0
4
+
5
+ - [x] inline sourcemap
6
+ - [x] CSS validation using mdn-data
7
+ - [x] prefix removal now remove prefixes from all nodes. prefixed linear gradients are not supported
8
+
9
+ # v1.0.0
10
+
11
+ - [x] current color parse error when used in color functions
12
+ - [x] minification : CSS transform module level 2
13
+ - [x] translate
14
+ - [x] scale
15
+ - [x] rotate
16
+ - [x] skew
17
+ - [x] perspective
18
+ - [x] matrix
19
+ - [x] matrix3d
20
+ - [x] keyframes
21
+ - [x] remove consecutive keyframes with the same name
22
+ - [x] reduce keyframe selector 'from' to '0%'
23
+ - [x] reduce keyframe selector '100%' to 'to'
24
+ - [x] remove keyframe selector ignored declarations
25
+ - [x] merge keyframe rules and declarations
26
+ - [x] trim extra space in declaration value 'url() 15%' -> 'url()15%', '1% !important' -> '1%!important'
27
+ - [x] allow empty value for css variable declaration '--color: ;'
28
+ - [x] change generate nesting rule default to true
29
+
30
+ # v0.9.1
31
+
32
+ - [x] minification passes #66
33
+ - [x] nesting selector cannot match pseudo element #67
34
+
35
+ # v0.9.0
36
+
37
+ validation
38
+ - [x] validate invalid pseudo classes
39
+ - [x] rewrite selector validation
40
+ - [x] lenient mode that preserves
41
+ - [x] unknown at-rules
42
+ - [x] unknown declarations
43
+ - [x] unknown pseudo classes
44
+
45
+ media query level 5
46
+ - [x] at-rule custom-media
47
+ - [x] at-rule when-else custom media
48
+ - [x] at-rule charset validation
49
+ - [x] media query error handling
50
+ - [x] at-rule container
51
+ - [ ] expand at-rule custom-media
52
+ - [ ] expand at-rule when-else
53
+
54
+ selector validation
55
+ - [ ] pseudo class arguments validation
56
+ - [ ] pseudo class validation
57
+
58
+ declaration validation
59
+ - [ ] validate declaration
60
+
61
+ error validation
62
+ - [ ] when a parent is marked as invalid node, do not parse or validate descendant nodes
63
+
64
+ # v0.8.0
65
+
66
+ - [x] validate selectors using mdn data
67
+ - [x] at-rules prefix removal
68
+ - [x] at rules validation
69
+ - [x] at-rule prelude
70
+ - [x] at-rule body
71
+ - [x] keyframe validation
72
+ - [ ] :not() does not accept pseudo classes
73
+ - [ ] do not validate declarations in @supports
74
+ - [ ] declarations validation
75
+ - [x] evaluate math functions: calc(), clamp(), min(), max(), round(), mod(), rem(), sin(), cos(), tan(), asin(),
76
+ acos(), atan(), atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), sign() #49
77
+ - [x] incorrectly parse compound selector #51
78
+
79
+ # v0.7.1
80
+
81
+ - [x] fix nesting rules expansion #45
82
+
83
+ # v0.7.0
84
+
85
+ - [x] fix merging rules
86
+ - [ ] experimental CSS prefix removal
87
+ - [x] declaration name
88
+ - [ ] declaration value
89
+ - [ ] exclude -webkit-* gradients
90
+ - [x] css selector validation
91
+ - [x] pseudo element
92
+ - [x] partial pseudo class validation. does not validate parameters
93
+ - [x] attribute selector
94
+ - [x] combinator
95
+ - [x] simple selector
96
+ - [x] nested selector
97
+ - [x] strict vs permissive validation: allow unknown items such as pseudo classes
98
+ - [x] allow unknown pseudo classes
99
+ - [x] allow unknown attribute selectors
100
+ - [x] strip universal selector when possible
101
+
102
+ # v0.6.0
103
+
104
+ - [x] light-dark() color
105
+ - [x] system color
106
+
107
+ ## V0.5.4
108
+
109
+ - [x] incorrectly expand css nesting rules
110
+
111
+ ## v0.5.3
112
+
113
+ - [x] incorrectly expand css nesting rules
114
+
115
+ ## v0.5.1
116
+
117
+ - [x] failed to flatten @import when using url() syntax
118
+
119
+ ## v0.5.0
120
+
121
+ - [x] render node with parents
122
+ - [x] fix relative color from xyz
123
+ - [x] fix bug when inlineCss is true but no css variable exists
124
+ - [x] compute more shorthands
125
+ - [x] (web) fetch imported css files from external domains using cors
126
+
127
+ ## v0.4.1
128
+
129
+ no code change
130
+
131
+ ## v0.4.0
132
+
133
+ Parsing
134
+
135
+ - [x] allow async node visitors
136
+ - [x] adding declaration parsing helper async parseDeclarations(source: string): Promise<AstDeclarations[]>
137
+
138
+ CSS color level 4 & 5
139
+
140
+ - [x] color space: srgb, srgb-linear, display-p3, prophoto-rgb, a98-rgb, rec2020, xyz, xyz-d50
141
+ - [x] color-mix()
142
+ - [x] color()
143
+ - [x] relative color
144
+ - [x] lab()
145
+ - [x] lch()
146
+ - [x] oklab()
147
+ - [x] oklch()
148
+
149
+ ## v0.3.0
150
+
151
+ ### shorthands
152
+
153
+ - [x] column-rule
154
+ - [x] columns
155
+ - [x] container
156
+ - [x] flex
157
+ - [x] flex-flow
158
+ - [x] gap
159
+
160
+ ### Other
161
+
162
+ - [x] renamed RenderOptions.colorConvert to RenderOptions.convertColor
163
+ - [x] support none keyword in color
164
+ - [x] css relative color syntax for rgb(), hsl() and hwb() colors https://www.w3.org/TR/css-color-5/#relative-colors
165
+ - [x] rgb
166
+ - [x] hex
167
+ - [x] hsl
168
+ - [x] hwb
169
+ - [x] calc()
170
+ - [x] calc() and inline var()
171
+
172
+ ## v0.2.0
173
+
174
+ - [x] cancellable parser promise using abortSignal
175
+ - [x] node visitor (callback) :
176
+ - [x] Declaration visitor
177
+ - [x] selector visitor
178
+ - [x] at-rule visitor
179
+ - [x] support mixing units with calc()
180
+
181
+ ### shorthands
182
+
183
+ - [x] transition
184
+ - [x] list-style
185
+ - [x] text-emphasis
186
+ - [x] animation
187
+
188
+ ### Minification
189
+
190
+ - [x] parsing column combinator
191
+ - [x] css selector level 4 attribute modifiers
192
+ - [x] selector-4 parsing https://www.w3.org/TR/selectors-4/
193
+
194
+ ## v0.1.0
195
+
196
+ - [x] sourcemap generation
197
+ - [x] reduce calc():
198
+ - [x] inline css variables
199
+ - [x] configure duplicate declarations removal
200
+ - [x] configure shorthand properties computation
201
+
202
+ ## v0.0.1
203
+
204
+ ### Minification
205
+
206
+ - [x] merge identical rules
207
+ - [x] merge adjacent rules
208
+ - [x] minify colors
209
+ - [x] minify numbers and Dimensions tokens
210
+ - [x] compute shorthand: see the list below
211
+ - [x] remove redundant declarations
212
+ - [x] simple shorthand properties (padding, margin, etc). must have all required properties
213
+ - [x] complex shorthand properties (background, font, etc.). may have optional properties
214
+ - [x] conditionally unwrap :is()
215
+ - [x] automatic css nesting
216
+ - [x] automatically wrap selectors using :is()
217
+ - [x] multi-level shorthand properties (
218
+ border - [border-width, border-color, border-style, etc.]) https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties
219
+ - [x] avoid reparsing (declarations, selectors, at-rule)
220
+ - [x] node and browser versions
221
+ - [x] decode and replace utf-8 escape sequence
222
+
223
+ ### Computed shorthands
224
+
225
+ - [x] background
226
+ - [x] border
227
+ - [x] border-bottom
228
+ - [x] border-color
229
+ - [x] border-left
230
+ - [x] border-radius
231
+ - [x] border-right
232
+ - [x] border-style
233
+ - [x] border-top
234
+ - [x] border-width
235
+ - [x] font
236
+ - [x] inset
237
+ - [x] margin
238
+ - [x] outline
239
+ - [x] overflow
240
+ - [x] padding
241
+ - [x] text-decoration
242
+
243
+ ### Performance
244
+
245
+ - [x] flatten @import
246
+
247
+ ### Error handling
248
+
249
+ - [x] parse bad comments / cdo comments
250
+ - [x] parse bad string 1
251
+ - [x] parse bad string 2
252
+ - [x] parse empty declaration
253
+ - [x] parse unclosed rule
254
+ - [x] parse unclosed at-rule
255
+ - [x] parse bad import
256
+
257
+ # Testing
258
+
259
+ - [x] node tests
260
+ - [x] browser tests
261
+
262
+ # Code Coverage
263
+
264
+ - [x] node
265
+ - [x] browser
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 Thierry Bela
3
+ Copyright (c) 2023-present Thierry Bela
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -23,22 +23,22 @@ $ deno add @tbela99/css-parser
23
23
  ## Features
24
24
 
25
25
  - no dependency
26
- - partial css validation based upon mdn-data: at-rules and selectors validation
26
+ - CSS validation based upon mdn-data
27
27
  - fault-tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
28
28
  - fast and efficient minification without unsafe transforms,
29
29
  see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
30
- - minify colors.
31
- - support css color level 4 & 5: color(), lab(), lch(), oklab(), oklch(), color-mix(), light-dark(), system colors and
30
+ - minify colors: color(), lab(), lch(), oklab(), oklch(), color-mix(), light-dark(), system colors and
32
31
  relative color
33
32
  - generate nested css rules
34
33
  - convert nested css rules to legacy syntax
35
34
  - generate sourcemap
36
35
  - compute css shorthands. see supported properties list below
37
- - evaluate math functions: calc(), clamp(), min(), max(), round(), mod(), rem(), sin(), cos(), tan(), asin(), acos(),
38
- atan(), atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), sign()
36
+ - css transform functions minification
37
+ - evaluate math functions: calc(), clamp(), min(), max(), etc.
39
38
  - inline css variables
40
39
  - remove duplicate properties
41
40
  - flatten @import rules
41
+ - experimental CSS prefix removal
42
42
 
43
43
  ## Playground
44
44
 
@@ -96,7 +96,7 @@ Javascript module from cdn
96
96
 
97
97
  <script type="module">
98
98
 
99
- import {transform} from 'https://esm.sh/@tbela99/css-parser@0.9.0/web';
99
+ import {transform} from 'https://esm.sh/@tbela99/css-parser@1.1.0/web';
100
100
 
101
101
 
102
102
  const css = `
@@ -159,30 +159,44 @@ Include ParseOptions and RenderOptions
159
159
  - expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules
160
160
  to false.
161
161
  - removeDuplicateDeclarations: boolean, optional. remove duplicate declarations.
162
+ - computeTransform: boolean, optional. compute css transform functions.
162
163
  - computeShorthand: boolean, optional. compute shorthand properties.
163
164
  - computeCalcExpression: boolean, optional. evaluate calc() expression
164
165
  - inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once
165
166
  in the :root {} or html {} rule.
166
167
  - removeEmpty: boolean, optional. remove empty rule lists from the ast.
167
168
 
169
+ > CSS Prefix Removal Options
170
+
171
+ - removePrefix: boolean, optional. remove CSS prefixes.
172
+
168
173
  > Validation Options
169
174
 
170
- - validation: boolean, optional. enable strict css validation using (mdn data)[https://github.com/mdn/data]. only the
171
- selector is validated at this time.
172
- - lenient: boolean, optional. ignore unknown at-rules, pseudo-classes and declarations.
175
+ - validation: ValidationLevel | boolean, optional. enable validation. permitted values are:
176
+ - ValidationLevel.None: no validation
177
+ - ValidationLevel.Default: validate selectors and at-rules (default)
178
+ - ValidationLevel.All. validate all nodes
179
+ - true: same as ValidationLevel.All.
180
+ - false: same as ValidationLevel.None
181
+ - lenient: boolean, optional. preserve invalid tokens.
173
182
 
174
183
  > Sourcemap Options
175
184
 
176
185
  - src: string, optional. original css file location to be used with sourcemap, also used to resolve url().
177
186
  - sourcemap: boolean, optional. preserve node location data.
178
187
 
179
- > Misc Options
188
+ > Ast Traversal Options
189
+
190
+ - visitor: VisitorNodeMap, optional. node visitor used to transform the ast.
191
+
192
+ > Urls and \@import Options
180
193
 
194
+ - resolveImport: boolean, optional. replace @import rule by the content of the referenced stylesheet.
181
195
  - resolveUrls: boolean, optional. resolve css 'url()' according to the parameters 'src' and 'cwd'
182
- - resolveImport: boolean, optional. replace @import rule by the content of its referenced stylesheet.
196
+
197
+ > Misc Options
183
198
  - removeCharset: boolean, optional. remove @charset.
184
199
  - cwd: string, optional. destination directory used to resolve url().
185
- - visitor: VisitorNodeMap, optional. node visitor used to transform the ast.
186
200
  - signal: AbortSignal, optional. abort parsing.
187
201
 
188
202
  #### RenderOptions
@@ -200,7 +214,7 @@ Include ParseOptions and RenderOptions
200
214
 
201
215
  > Sourcemap Options
202
216
 
203
- - sourcemap: boolean, optional. generate sourcemap
217
+ - sourcemap: boolean | 'inline', optional. generate sourcemap.
204
218
 
205
219
  > Misc Options
206
220
 
@@ -701,6 +715,8 @@ for (const {node, parent, root} of walk(ast)) {
701
715
 
702
716
  ## Minification
703
717
 
718
+ - [x] minify keyframes
719
+ - [x] minify transform
704
720
  - [x] evaluate math functions calc(), clamp(), min(), max(), round(), mod(), rem(), sin(), cos(), tan(), asin(),
705
721
  acos(), atan(), atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), sign()
706
722
  - [x] multi-pass minification
@@ -1023,7 +1039,3 @@ console.debug(await transform(css, options));
1023
1039
  // .foo{width:3px}
1024
1040
 
1025
1041
  ```
1026
-
1027
- ---
1028
-
1029
- Thanks to [Jetbrains](https://jetbrains.com) for sponsoring this project with a free license