@tbela99/css-parser 1.0.0 → 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 (86) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/README.md +16 -11
  3. package/dist/index-umd-web.js +3613 -1829
  4. package/dist/index.cjs +3611 -1827
  5. package/dist/index.d.ts +160 -50
  6. package/dist/lib/ast/expand.js +2 -1
  7. package/dist/lib/ast/features/calc.js +12 -1
  8. package/dist/lib/ast/features/inlinecssvariables.js +47 -24
  9. package/dist/lib/ast/features/prefix.js +117 -86
  10. package/dist/lib/ast/features/shorthand.js +29 -6
  11. package/dist/lib/ast/features/transform.js +10 -3
  12. package/dist/lib/ast/features/type.js +7 -0
  13. package/dist/lib/ast/math/expression.js +7 -1
  14. package/dist/lib/ast/math/math.js +6 -0
  15. package/dist/lib/ast/minify.js +165 -77
  16. package/dist/lib/ast/transform/compute.js +1 -0
  17. package/dist/lib/ast/transform/matrix.js +1 -0
  18. package/dist/lib/ast/types.js +17 -15
  19. package/dist/lib/ast/walk.js +33 -7
  20. package/dist/lib/fs/resolve.js +10 -0
  21. package/dist/lib/parser/declaration/list.js +48 -45
  22. package/dist/lib/parser/declaration/map.js +1 -0
  23. package/dist/lib/parser/declaration/set.js +2 -1
  24. package/dist/lib/parser/parse.js +364 -276
  25. package/dist/lib/parser/tokenize.js +147 -72
  26. package/dist/lib/parser/utils/declaration.js +4 -3
  27. package/dist/lib/parser/utils/type.js +2 -1
  28. package/dist/lib/renderer/color/a98rgb.js +2 -1
  29. package/dist/lib/renderer/color/color-mix.js +10 -7
  30. package/dist/lib/renderer/color/color.js +171 -153
  31. package/dist/lib/renderer/color/hex.js +2 -1
  32. package/dist/lib/renderer/color/hsl.js +2 -1
  33. package/dist/lib/renderer/color/hwb.js +2 -1
  34. package/dist/lib/renderer/color/lab.js +2 -1
  35. package/dist/lib/renderer/color/lch.js +2 -1
  36. package/dist/lib/renderer/color/oklab.js +2 -1
  37. package/dist/lib/renderer/color/oklch.js +2 -1
  38. package/dist/lib/renderer/color/p3.js +2 -1
  39. package/dist/lib/renderer/color/rec2020.js +2 -1
  40. package/dist/lib/renderer/color/relativecolor.js +17 -11
  41. package/dist/lib/renderer/color/rgb.js +4 -3
  42. package/dist/lib/renderer/color/srgb.js +18 -17
  43. package/dist/lib/renderer/color/utils/components.js +6 -5
  44. package/dist/lib/renderer/color/utils/constants.js +47 -3
  45. package/dist/lib/renderer/color/xyz.js +2 -1
  46. package/dist/lib/renderer/color/xyzd50.js +2 -1
  47. package/dist/lib/renderer/render.js +48 -20
  48. package/dist/lib/syntax/syntax.js +253 -140
  49. package/dist/lib/validation/at-rules/container.js +75 -97
  50. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  51. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  52. package/dist/lib/validation/at-rules/document.js +22 -27
  53. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  54. package/dist/lib/validation/at-rules/import.js +30 -81
  55. package/dist/lib/validation/at-rules/keyframes.js +18 -22
  56. package/dist/lib/validation/at-rules/layer.js +5 -5
  57. package/dist/lib/validation/at-rules/media.js +42 -52
  58. package/dist/lib/validation/at-rules/namespace.js +19 -23
  59. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  60. package/dist/lib/validation/at-rules/page.js +8 -7
  61. package/dist/lib/validation/at-rules/supports.js +73 -82
  62. package/dist/lib/validation/at-rules/when.js +32 -36
  63. package/dist/lib/validation/atrule.js +15 -14
  64. package/dist/lib/validation/config.js +24 -1
  65. package/dist/lib/validation/config.json.js +563 -63
  66. package/dist/lib/validation/parser/parse.js +196 -185
  67. package/dist/lib/validation/parser/types.js +1 -1
  68. package/dist/lib/validation/selector.js +3 -3
  69. package/dist/lib/validation/syntax.js +828 -0
  70. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  71. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  72. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  73. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
  76. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  78. package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
  79. package/dist/lib/validation/syntaxes/url.js +18 -22
  80. package/dist/lib/validation/utils/list.js +2 -1
  81. package/dist/lib/validation/utils/whitespace.js +2 -1
  82. package/dist/node/index.js +4 -2
  83. package/dist/node/load.js +5 -0
  84. package/dist/web/index.js +4 -2
  85. package/dist/web/load.js +5 -0
  86. package/package.json +12 -11
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/README.md CHANGED
@@ -23,7 +23,7 @@ $ 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)
@@ -38,6 +38,7 @@ $ deno add @tbela99/css-parser
38
38
  - inline css variables
39
39
  - remove duplicate properties
40
40
  - flatten @import rules
41
+ - experimental CSS prefix removal
41
42
 
42
43
  ## Playground
43
44
 
@@ -95,7 +96,7 @@ Javascript module from cdn
95
96
 
96
97
  <script type="module">
97
98
 
98
- import {transform} from 'https://esm.sh/@tbela99/css-parser@1.0.0/web';
99
+ import {transform} from 'https://esm.sh/@tbela99/css-parser@1.1.0/web';
99
100
 
100
101
 
101
102
  const css = `
@@ -165,11 +166,19 @@ Include ParseOptions and RenderOptions
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
 
@@ -205,7 +214,7 @@ Include ParseOptions and RenderOptions
205
214
 
206
215
  > Sourcemap Options
207
216
 
208
- - sourcemap: boolean, optional. generate sourcemap
217
+ - sourcemap: boolean | 'inline', optional. generate sourcemap.
209
218
 
210
219
  > Misc Options
211
220
 
@@ -706,7 +715,7 @@ for (const {node, parent, root} of walk(ast)) {
706
715
 
707
716
  ## Minification
708
717
 
709
- - [x] minify keyframs
718
+ - [x] minify keyframes
710
719
  - [x] minify transform
711
720
  - [x] evaluate math functions calc(), clamp(), min(), max(), round(), mod(), rem(), sin(), cos(), tan(), asin(),
712
721
  acos(), atan(), atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), sign()
@@ -1030,7 +1039,3 @@ console.debug(await transform(css, options));
1030
1039
  // .foo{width:3px}
1031
1040
 
1032
1041
  ```
1033
-
1034
- ---
1035
-
1036
- Thanks to [Jetbrains](https://jetbrains.com) for sponsoring this project with a free license