@tbela99/css-parser 1.4.3 → 1.4.5
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/CHANGELOG.md +196 -2
- package/README.md +68 -1120
- package/dist/config.json.js +43 -2
- package/dist/index-umd-web.js +18118 -15886
- package/dist/index.cjs +16838 -14611
- package/dist/index.d.ts +1804 -1684
- package/dist/lib/ast/clone.js +4 -3
- package/dist/lib/ast/expand.js +2 -2
- package/dist/lib/ast/features/calc.js +59 -54
- package/dist/lib/ast/features/if.js +64 -17
- package/dist/lib/ast/features/inlinecssvariables.js +2 -2
- package/dist/lib/ast/features/prefix.js +343 -6
- package/dist/lib/ast/features/transform.js +7 -4
- package/dist/lib/ast/find.js +5 -5
- package/dist/lib/ast/math/expression.js +12 -8
- package/dist/lib/ast/minify.js +28 -33
- package/dist/lib/ast/transform/minify.js +135 -131
- package/dist/lib/ast/transform/utils.js +3 -2
- package/dist/lib/ast/types.js +69 -8
- package/dist/lib/ast/walk.js +4 -5
- package/dist/lib/fs/resolve.js +71 -57
- package/dist/lib/parser/declaration/list.js +92 -3
- package/dist/lib/parser/declaration/map.js +5 -5
- package/dist/lib/parser/parse.js +530 -466
- package/dist/lib/parser/tokenize.js +110 -4
- package/dist/lib/parser/utils/angle.js +29 -0
- package/dist/lib/parser/utils/at-rule-container.js +2 -2
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +2 -2
- package/dist/lib/parser/utils/at-rule-generic.js +1 -1
- package/dist/lib/parser/utils/at-rule-media.js +2 -2
- package/dist/lib/parser/utils/at-rule-support.js +73 -8
- package/dist/lib/parser/utils/at-rule-when-else.js +2 -2
- package/dist/lib/parser/utils/at-rule.js +2 -8
- package/dist/lib/parser/utils/declaration.js +179 -76
- package/dist/lib/parser/utils/selector.js +53 -11
- package/dist/lib/parser/utils/token.js +13 -11
- package/dist/lib/renderer/render.js +501 -59
- package/dist/lib/syntax/color/alpha.js +64 -0
- package/dist/lib/syntax/color/cmyk.js +2 -2
- package/dist/lib/syntax/color/color-mix.js +283 -220
- package/dist/lib/syntax/color/color.js +37 -28
- package/dist/lib/syntax/color/hex.js +2 -2
- package/dist/lib/syntax/color/hsl.js +3 -3
- package/dist/lib/syntax/color/hwb.js +7 -7
- package/dist/lib/syntax/color/lab.js +5 -5
- package/dist/lib/syntax/color/lch.js +4 -4
- package/dist/lib/syntax/color/oklab.js +3 -3
- package/dist/lib/syntax/color/oklch.js +3 -3
- package/dist/lib/syntax/color/relativecolor.js +97 -12
- package/dist/lib/syntax/color/srgb.js +5 -5
- package/dist/lib/syntax/color/utils/color-type.js +18 -0
- package/dist/lib/syntax/color/utils/colorspace.js +46 -0
- package/dist/lib/syntax/color/utils/components.js +7 -3
- package/dist/lib/syntax/color/utils/distance.js +21 -4
- package/dist/lib/syntax/color/utils/make-color.js +56 -0
- package/dist/lib/syntax/color/xyz.js +2 -2
- package/dist/lib/syntax/constants.js +29 -1
- package/dist/lib/syntax/syntax.js +307 -39
- package/dist/lib/validation/config.js +2 -2
- package/dist/lib/validation/config.json.js +32 -17
- package/dist/lib/validation/match.js +239 -104
- package/dist/lib/validation/parser/parse.js +0 -1
- package/dist/lib/validation/parser/typedef.js +6 -0
- package/dist/node.js +64 -22
- package/dist/web.js +65 -18
- package/package.json +26 -15
- package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
- package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
- package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
- package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
- package/.repl_history +0 -4
- package/badges/coverage.svg +0 -20
- package/deno.lock +0 -2861
- package/dist/lib/ast/clone.d.ts +0 -10
- package/dist/lib/ast/expand.d.ts +0 -14
- package/dist/lib/ast/features/calc.d.ts +0 -10
- package/dist/lib/ast/features/if.d.ts +0 -10
- package/dist/lib/ast/features/index.d.ts +0 -6
- package/dist/lib/ast/features/inlinecssvariables.d.ts +0 -15
- package/dist/lib/ast/features/prefix.d.ts +0 -8
- package/dist/lib/ast/features/shorthand.d.ts +0 -12
- package/dist/lib/ast/features/transform.d.ts +0 -10
- package/dist/lib/ast/features/type.d.ts +0 -15
- package/dist/lib/ast/find.d.ts +0 -165
- package/dist/lib/ast/math/expression.d.ts +0 -18
- package/dist/lib/ast/math/math.d.ts +0 -6
- package/dist/lib/ast/minify.d.ts +0 -19
- package/dist/lib/ast/transform/compute.d.ts +0 -8
- package/dist/lib/ast/transform/matrix.d.ts +0 -22
- package/dist/lib/ast/transform/minify.d.ts +0 -5
- package/dist/lib/ast/transform/perspective.d.ts +0 -3
- package/dist/lib/ast/transform/rotate.d.ts +0 -12
- package/dist/lib/ast/transform/scale.d.ts +0 -6
- package/dist/lib/ast/transform/skew.d.ts +0 -4
- package/dist/lib/ast/transform/translate.d.ts +0 -6
- package/dist/lib/ast/transform/utils.d.ts +0 -9
- package/dist/lib/ast/types.d.ts +0 -903
- package/dist/lib/ast/walk.d.ts +0 -162
- package/dist/lib/fs/resolve.d.ts +0 -20
- package/dist/lib/parser/declaration/list.d.ts +0 -16
- package/dist/lib/parser/declaration/map.d.ts +0 -15
- package/dist/lib/parser/declaration/set.d.ts +0 -9
- package/dist/lib/parser/node.d.ts +0 -7
- package/dist/lib/parser/parse.d.ts +0 -107
- package/dist/lib/parser/tokenize.d.ts +0 -57
- package/dist/lib/parser/utils/at-rule-container.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule-generic.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule-import.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule-media.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule-page.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule-page.js +0 -28
- package/dist/lib/parser/utils/at-rule-support.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule-token.d.ts +0 -1
- package/dist/lib/parser/utils/at-rule-when-else.d.ts +0 -5
- package/dist/lib/parser/utils/at-rule.d.ts +0 -13
- package/dist/lib/parser/utils/cache.d.ts +0 -6
- package/dist/lib/parser/utils/config.d.ts +0 -2
- package/dist/lib/parser/utils/declaration-list.d.ts +0 -5
- package/dist/lib/parser/utils/declaration.d.ts +0 -18
- package/dist/lib/parser/utils/eq.d.ts +0 -1
- package/dist/lib/parser/utils/hash.d.ts +0 -21
- package/dist/lib/parser/utils/selector.d.ts +0 -5
- package/dist/lib/parser/utils/text.d.ts +0 -3
- package/dist/lib/parser/utils/token.d.ts +0 -14
- package/dist/lib/parser/utils/type.d.ts +0 -2
- package/dist/lib/renderer/render.d.ts +0 -28
- package/dist/lib/renderer/sourcemap/lib/encode.d.ts +0 -1
- package/dist/lib/renderer/sourcemap/sourcemap.d.ts +0 -26
- package/dist/lib/syntax/color/a98rgb.d.ts +0 -2
- package/dist/lib/syntax/color/cmyk.d.ts +0 -10
- package/dist/lib/syntax/color/color-mix.d.ts +0 -2
- package/dist/lib/syntax/color/color.d.ts +0 -42
- package/dist/lib/syntax/color/hex.d.ts +0 -16
- package/dist/lib/syntax/color/hsl.d.ts +0 -20
- package/dist/lib/syntax/color/hsv.d.ts +0 -2
- package/dist/lib/syntax/color/hwb.d.ts +0 -21
- package/dist/lib/syntax/color/lab.d.ts +0 -25
- package/dist/lib/syntax/color/lch.d.ts +0 -23
- package/dist/lib/syntax/color/oklab.d.ts +0 -22
- package/dist/lib/syntax/color/oklch.d.ts +0 -20
- package/dist/lib/syntax/color/p3.d.ts +0 -6
- package/dist/lib/syntax/color/prophotorgb.d.ts +0 -2
- package/dist/lib/syntax/color/rec2020.d.ts +0 -2
- package/dist/lib/syntax/color/relativecolor.d.ts +0 -13
- package/dist/lib/syntax/color/rgb.d.ts +0 -20
- package/dist/lib/syntax/color/srgb.d.ts +0 -23
- package/dist/lib/syntax/color/utils/components.d.ts +0 -2
- package/dist/lib/syntax/color/utils/distance.d.ts +0 -18
- package/dist/lib/syntax/color/utils/matrix.d.ts +0 -6
- package/dist/lib/syntax/color/xyz.d.ts +0 -5
- package/dist/lib/syntax/color/xyzd50.d.ts +0 -4
- package/dist/lib/syntax/constants.d.ts +0 -67
- package/dist/lib/syntax/syntax.d.ts +0 -38
- package/dist/lib/validation/config.d.ts +0 -14
- package/dist/lib/validation/json.d.ts +0 -2
- package/dist/lib/validation/match.d.ts +0 -38
- package/dist/lib/validation/parser/parse.d.ts +0 -8
- package/dist/lib/validation/parser/typedef.d.ts +0 -95
- package/dist/lib/validation/utils/list.d.ts +0 -4
- package/dist/lib/validation/utils/whitespace.d.ts +0 -2
- package/dist/node.d.ts +0 -207
- package/dist/web.d.ts +0 -169
- package/playground/index.html +0 -1328
- package/playground/sw.js +0 -55
- package/playground/tree.js +0 -176
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[](https://tbela99.github.io/css-parser/playground/) [](https://www.npmjs.com/package/@tbela99/css-parser) [](https://jsr.io/@tbela99/css-parser) [](https://github.com/tbela99/css-parser/actions) [](https://jsr.io/@tbela99/css-parser) [](https://github.com/tbela99/css-parser/actions) [](https://tbela99.github.io/css-parser/docs) [](https://www.npmjs.com/package/@tbela99/css-parser) [](https://www.npmjs.com/package/@tbela99/css-parser)
|
|
4
4
|
|
|
5
5
|
# css-parser
|
|
6
6
|
|
|
7
|
-
CSS parser, minifier and validator for node and the browser
|
|
7
|
+
CSS parser, transformer, minifier and validator for node and the browser
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
@@ -22,852 +22,67 @@ $ deno add @tbela99/css-parser
|
|
|
22
22
|
|
|
23
23
|
## Features
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
- CSS validation
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
25
|
+
* **Zero dependencies** — lightweight and easy to integrate into any project.
|
|
26
|
+
* **Standards-based CSS validation** powered by MDN data.
|
|
27
|
+
* **Full CSS Modules support** for modern component-based workflows.
|
|
28
|
+
* **Fault-tolerant parsing** that follows the CSS Syntax Module Level 3 specification.
|
|
29
|
+
* **High-performance minification** with safe optimizations and no unsafe transforms.
|
|
30
|
+
* **Advanced color processing** with support for modern color spaces and functions, including `color()`, `lab()`, `lch()`, `oklab()`, `oklch()`, `color-mix()`, `light-dark()`, system colors, and relative colors.
|
|
31
|
+
* **Color conversion engine** capable of transforming colors between all supported formats.
|
|
32
|
+
* **Automatic CSS nesting generation** from compatible selectors.
|
|
33
|
+
* **Source map generation** for easier debugging and development workflows.
|
|
34
|
+
* **Shorthand property computation** to reduce output size and improve optimization.
|
|
35
|
+
* **Transform function optimization** for more compact CSS output.
|
|
36
|
+
* **Math function evaluation**, including `calc()`, `clamp()`, `min()`, `max()`, and related expressions.
|
|
37
|
+
* **CSS variable inlining** where values can be safely resolved.
|
|
38
|
+
* **Duplicate declaration removal** to eliminate redundant rules.
|
|
39
|
+
* **`@import` flattening** to produce self-contained stylesheets.
|
|
40
|
+
|
|
41
|
+
## Vendor prefix removal
|
|
42
|
+
**Experimental vendor prefix cleanup** to modernize generated CSS.
|
|
43
|
+
|
|
44
|
+
## Syntax lowering
|
|
45
|
+
CSS-Parser can transform these modern CSS features into lower-level CSS syntax:
|
|
46
|
+
* **Nested CSS transpilation** to legacy-compatible syntax.
|
|
47
|
+
* **`color-mix()` function conversion** convert new color-mix() syntax to any of the supported colors.
|
|
48
|
+
|
|
49
|
+
## Benchmark
|
|
50
|
+
|
|
51
|
+
Across all tested datasets, css-parser consistently produces the smallest output among the benchmarked tools, including Lightning CSS, cssnano, csso, clean-css, css-tree, and esbuild.
|
|
52
|
+
|
|
53
|
+
The [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html) evaluates minification effectiveness on a diverse set of real-world stylesheets, including Bootstrap 4, Bootstrap 5, Tailwind CSS, Animate.css, Foundation, Font Awesome, Normalize.css, and others.
|
|
54
|
+
|
|
55
|
+
A sample result:
|
|
56
|
+
|
|
57
|
+
| File | ligthningcss | CSS Parser |
|
|
58
|
+
| ------------------------------ | ------------- | ------------- |
|
|
59
|
+
| tailwind.css - 2380419 bytes | 1864728 bytes | 1633251 bytes |
|
|
60
|
+
| bootstrap-4.css - 200078 bytes | 153616 bytes | 144700 bytes |
|
|
61
|
+
| bootstrap-5.css - 205481 bytes | 159987 bytes | 151258 bytes |
|
|
62
|
+
|
|
63
|
+
On the [complete benchmark suite](https://tbela99.github.io/css-parser/benchmark/index.html), css-parser generated a total output size of 2,204,888 bytes, compared to 2,494,113 bytes for Lightning CSS and larger outputs for all other tested minifiers.
|
|
64
|
+
While some tools prioritize raw execution speed, css-parser focuses on maximizing compression while preserving stylesheet semantics, resulting in consistently smaller production bundles.
|
|
49
65
|
|
|
50
66
|
## Playground
|
|
51
67
|
|
|
52
68
|
Try it [online](https://tbela99.github.io/css-parser/playground/)
|
|
53
69
|
|
|
54
|
-
##
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
import as a module
|
|
72
|
-
|
|
73
|
-
```javascript
|
|
74
|
-
|
|
75
|
-
import {transform} from '@tbela99/css-parser';
|
|
76
|
-
|
|
77
|
-
// ...
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
import as a CommonJS module
|
|
81
|
-
|
|
82
|
-
```javascript
|
|
83
|
-
|
|
84
|
-
const {transform} = require('@tbela99/css-parser/cjs');
|
|
85
|
-
|
|
86
|
-
// ...
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### Web
|
|
90
|
-
|
|
91
|
-
Programmatic import
|
|
92
|
-
|
|
93
|
-
```javascript
|
|
94
|
-
|
|
95
|
-
import {transform} from '@tbela99/css-parser/web';
|
|
96
|
-
|
|
97
|
-
// ...
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Javascript module from cdn
|
|
101
|
-
|
|
102
|
-
```html
|
|
103
|
-
|
|
104
|
-
<script type="module">
|
|
105
|
-
|
|
106
|
-
import {transform} from 'https://esm.sh/@tbela99/css-parser@1.3.4/web';
|
|
107
|
-
|
|
108
|
-
const css = `
|
|
109
|
-
.s {
|
|
110
|
-
|
|
111
|
-
background: color-mix(in hsl, color(display-p3 0 1 0) 80%, yellow);
|
|
112
|
-
}
|
|
113
|
-
`;
|
|
114
|
-
|
|
115
|
-
console.debug(await transform(css).then(r => r.code));
|
|
116
|
-
|
|
117
|
-
</script>
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Javascript module
|
|
121
|
-
|
|
122
|
-
```javascript
|
|
123
|
-
|
|
124
|
-
<script src="dist/web.js" type="module"></script>
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
Javascript umd module from cdn
|
|
128
|
-
|
|
129
|
-
```html
|
|
130
|
-
|
|
131
|
-
<script src="https://unpkg.com/@tbela99/css-parser@1.3.4/dist/index-umd-web.js"></script>
|
|
132
|
-
<script>
|
|
133
|
-
|
|
134
|
-
(async () => {
|
|
135
|
-
|
|
136
|
-
const css = `
|
|
137
|
-
|
|
138
|
-
.table {
|
|
139
|
-
border-collapse: collapse;
|
|
140
|
-
width: 100%;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
.table td, .table th {
|
|
144
|
-
border: 1px solid #ddd;
|
|
145
|
-
padding: 8px;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
.table tr:nth-child(even){background-color: #f2f2f2;}
|
|
149
|
-
|
|
150
|
-
.table tr:hover {background-color: #ddd;}
|
|
151
|
-
|
|
152
|
-
.table th {
|
|
153
|
-
padding-top: 12px;
|
|
154
|
-
padding-bottom: 12px;
|
|
155
|
-
text-align: left;
|
|
156
|
-
background-color: #4CAF50;
|
|
157
|
-
color: white;
|
|
158
|
-
}
|
|
159
|
-
`;
|
|
160
|
-
|
|
161
|
-
console.debug(await CSSParser.transform(css, {beautify: true, convertColor: CSSParser.ColorType.OKLCH}).then(r => r.code));
|
|
162
|
-
})();
|
|
163
|
-
|
|
164
|
-
</script>
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
## Exported functions
|
|
168
|
-
|
|
169
|
-
```ts
|
|
170
|
-
|
|
171
|
-
/* parse and render css */
|
|
172
|
-
transform(css: string | ReadableStream<string>, options?: TransformOptions = {}): Promise<TransformResult>
|
|
173
|
-
/* parse css */
|
|
174
|
-
parse(css: string | ReadableStream<string>, options?: ParseOptions = {}): Promise<ParseResult>;
|
|
175
|
-
/* render ast */
|
|
176
|
-
render(ast: AstNode, options?: RenderOptions = {}): RenderResult;
|
|
177
|
-
/* parse and render css file or url */
|
|
178
|
-
transformFile(filePathOrUrl: string, options?: TransformOptions = {}, asStream?: boolean): Promise<TransformResult>;
|
|
179
|
-
/* parse css file or url */
|
|
180
|
-
parseFile(filePathOrUrl: string, options?: ParseOptions = {}, asStream?: boolean): Promise<ParseResult>;
|
|
181
|
-
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
## Transform
|
|
185
|
-
|
|
186
|
-
Parse and render css in a single pass.
|
|
187
|
-
|
|
188
|
-
### Example
|
|
189
|
-
|
|
190
|
-
```javascript
|
|
191
|
-
|
|
192
|
-
import {transform} from '@tbela99/css-parser';
|
|
193
|
-
|
|
194
|
-
const {ast, code, map, errors, stats} = await transform(css, {minify: true, resolveImport: true, cwd: 'files/css'});
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
### Example
|
|
198
|
-
|
|
199
|
-
Read from stdin with node using readable stream
|
|
200
|
-
|
|
201
|
-
```typescript
|
|
202
|
-
import {transform} from "../src/node";
|
|
203
|
-
import {Readable} from "node:stream";
|
|
204
|
-
import type {TransformResult} from '../src/@types/index.d.ts';
|
|
205
|
-
|
|
206
|
-
const readableStream: ReadableStream<string> = Readable.toWeb(process.stdin) as ReadableStream<string>;
|
|
207
|
-
const result: TransformResult = await transform(readableStream, {beautify: true});
|
|
208
|
-
|
|
209
|
-
console.log(result.code);
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
### TransformOptions
|
|
213
|
-
|
|
214
|
-
Include ParseOptions and RenderOptions
|
|
215
|
-
|
|
216
|
-
#### ParseOptions
|
|
217
|
-
|
|
218
|
-
> Minify Options
|
|
219
|
-
|
|
220
|
-
- minify: boolean, optional. default to _true_. optimize ast.
|
|
221
|
-
- pass: number, optional. minification passes. default to 1
|
|
222
|
-
- nestingRules: boolean, optional. automatically generated nested rules.
|
|
223
|
-
- expandNestingRules: boolean, optional. convert nesting rules into separate rules. will automatically set nestingRules
|
|
224
|
-
to false.
|
|
225
|
-
- expandIfSyntax: experimental, convert css if() function into legacy syntax.
|
|
226
|
-
- removeDuplicateDeclarations: boolean, optional. remove duplicate declarations.
|
|
227
|
-
- computeTransform: boolean, optional. compute css transform functions.
|
|
228
|
-
- computeShorthand: boolean, optional. compute shorthand properties.
|
|
229
|
-
- computeCalcExpression: boolean, optional. evaluate calc() expression
|
|
230
|
-
- inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once
|
|
231
|
-
in the :root {} or html {} rule.
|
|
232
|
-
- removeEmpty: boolean, optional. remove empty rule lists from the ast.
|
|
233
|
-
|
|
234
|
-
> CSS modules Options
|
|
235
|
-
|
|
236
|
-
- module: boolean | ModuleCaseTransformEnum | ModuleScopeEnumOptions | ModuleOptions, optional. css modules options.
|
|
237
|
-
|
|
238
|
-
> CSS Prefix Removal Options
|
|
239
|
-
|
|
240
|
-
- removePrefix: boolean, optional. remove CSS prefixes.
|
|
241
|
-
|
|
242
|
-
> Validation Options
|
|
243
|
-
|
|
244
|
-
- validation: ValidationLevel | boolean, optional. enable validation. permitted values are:
|
|
245
|
-
- ValidationLevel.None: no validation
|
|
246
|
-
- ValidationLevel.Default: validate selectors and at-rules (default)
|
|
247
|
-
- ValidationLevel.All. validate all nodes
|
|
248
|
-
- true: same as ValidationLevel.All.
|
|
249
|
-
- false: same as ValidationLevel.None
|
|
250
|
-
- lenient: boolean, optional. preserve invalid tokens.
|
|
251
|
-
|
|
252
|
-
> Sourcemap Options
|
|
253
|
-
|
|
254
|
-
- src: string, optional. original css file location to be used with sourcemap, also used to resolve url().
|
|
255
|
-
- sourcemap: boolean, optional. preserve node location data.
|
|
256
|
-
|
|
257
|
-
> Ast Traversal Options
|
|
258
|
-
|
|
259
|
-
- visitor: VisitorNodeMap, optional. node visitor used to transform the ast.
|
|
260
|
-
|
|
261
|
-
> Urls and \@import Options
|
|
262
|
-
|
|
263
|
-
- resolveImport: boolean, optional. replace @import rule by the content of the referenced stylesheet.
|
|
264
|
-
- resolveUrls: boolean, optional. resolve css 'url()' according to the parameters 'src' and 'cwd'
|
|
265
|
-
|
|
266
|
-
> Misc Options
|
|
267
|
-
- removeCharset: boolean, optional. remove @charset.
|
|
268
|
-
- cwd: string, optional. destination directory used to resolve url().
|
|
269
|
-
- signal: AbortSignal, optional. abort parsing.
|
|
270
|
-
|
|
271
|
-
#### RenderOptions
|
|
272
|
-
|
|
273
|
-
> Minify Options
|
|
274
|
-
|
|
275
|
-
- beautify: boolean, optional. default to _false_. beautify css output.
|
|
276
|
-
- minify: boolean, optional. default to _true_. minify css values.
|
|
277
|
-
- withParents: boolean, optional. render this node and its parents.
|
|
278
|
-
- removeEmpty: boolean, optional. remove empty rule lists from the ast.
|
|
279
|
-
- expandNestingRules: boolean, optional. expand nesting rules.
|
|
280
|
-
- preserveLicense: boolean, force preserving comments starting with '/\*!' when minify is enabled.
|
|
281
|
-
- removeComments: boolean, remove comments in generated css.
|
|
282
|
-
- convertColor: boolean | ColorType, convert colors to the specified color. default to ColorType.HEX. supported values are:
|
|
283
|
-
- true: same as ColorType.HEX
|
|
284
|
-
- false: no color conversion
|
|
285
|
-
- ColorType.HEX
|
|
286
|
-
- ColorType.RGB or ColorType.RGBA
|
|
287
|
-
- ColorType.HSL
|
|
288
|
-
- ColorType.HWB
|
|
289
|
-
- ColorType.CMYK or ColorType.DEVICE_CMYK
|
|
290
|
-
- ColorType.SRGB
|
|
291
|
-
- ColorType.SRGB_LINEAR
|
|
292
|
-
- ColorType.DISPLAY_P3
|
|
293
|
-
- ColorType.PROPHOTO_RGB
|
|
294
|
-
- ColorType.A98_RGB
|
|
295
|
-
- ColorType.REC2020
|
|
296
|
-
- ColorType.XYZ or ColorType.XYZ_D65
|
|
297
|
-
- ColorType.XYZ_D50
|
|
298
|
-
- ColorType.LAB
|
|
299
|
-
- ColorType.LCH
|
|
300
|
-
- ColorType.OKLAB
|
|
301
|
-
- ColorType.OKLCH
|
|
302
|
-
|
|
303
|
-
> Sourcemap Options
|
|
304
|
-
|
|
305
|
-
- sourcemap: boolean | 'inline', optional. generate sourcemap.
|
|
306
|
-
|
|
307
|
-
> Misc Options
|
|
308
|
-
|
|
309
|
-
- indent: string, optional. css indention string. uses space character by default.
|
|
310
|
-
- newLine: string, optional. new line character.
|
|
311
|
-
- output: string, optional. file where to store css. url() are resolved according to the specified value. no file is
|
|
312
|
-
created though.
|
|
313
|
-
- cwd: string, optional. destination directory used to resolve url().
|
|
314
|
-
|
|
315
|
-
## Parsing
|
|
316
|
-
|
|
317
|
-
### Usage
|
|
318
|
-
|
|
319
|
-
```javascript
|
|
320
|
-
|
|
321
|
-
parse(css, parseOptions = {})
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
### Example
|
|
325
|
-
|
|
326
|
-
````javascript
|
|
327
|
-
|
|
328
|
-
const {ast, errors, stats} = await parse(css);
|
|
329
|
-
````
|
|
330
|
-
|
|
331
|
-
## Rendering
|
|
332
|
-
|
|
333
|
-
### Usage
|
|
334
|
-
|
|
335
|
-
```javascript
|
|
336
|
-
render(ast, RenderOptions = {});
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
### Examples
|
|
340
|
-
|
|
341
|
-
Rendering ast
|
|
342
|
-
|
|
343
|
-
```javascript
|
|
344
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
345
|
-
|
|
346
|
-
const css = `
|
|
347
|
-
@media screen and (min-width: 40em) {
|
|
348
|
-
.featurette-heading {
|
|
349
|
-
font-size: 50px;
|
|
350
|
-
}
|
|
351
|
-
.a {
|
|
352
|
-
color: red;
|
|
353
|
-
width: 3px;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
`;
|
|
357
|
-
|
|
358
|
-
const result = await parse(css, options);
|
|
359
|
-
|
|
360
|
-
// print declaration without parents
|
|
361
|
-
console.error(render(result.ast.chi[0].chi[1].chi[1], {withParents: false}));
|
|
362
|
-
// -> width:3px
|
|
363
|
-
|
|
364
|
-
// print declaration with parents
|
|
365
|
-
console.debug(render(result.ast.chi[0].chi[1].chi[1], {withParents: true}));
|
|
366
|
-
// -> @media screen and (min-width:40em){.a{width:3px}}
|
|
367
|
-
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
### CSS Modules
|
|
371
|
-
|
|
372
|
-
CSS modules features are fully supported. refer to the [CSS modules](https://tbela99.github.io/css-parser/docs/documents/Guide.CSS_modules.html) documentation for more information.
|
|
373
|
-
|
|
374
|
-
```javascript
|
|
375
|
-
import {transform} from '@tbela99/css-parser';
|
|
376
|
-
|
|
377
|
-
const css = `
|
|
378
|
-
.table {
|
|
379
|
-
border-collapse: collapse;
|
|
380
|
-
width: 100%;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
.table td, .table th {
|
|
384
|
-
border: 1px solid #ddd;
|
|
385
|
-
padding: 8px;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
.table tr:nth-child(even){background-color: #f2f2f2;}
|
|
389
|
-
|
|
390
|
-
.table tr:hover {background-color: #ddd;}
|
|
391
|
-
|
|
392
|
-
.table th {
|
|
393
|
-
padding-top: 12px;
|
|
394
|
-
padding-bottom: 12px;
|
|
395
|
-
text-align: left;
|
|
396
|
-
background-color: #4CAF50;
|
|
397
|
-
color: white;
|
|
398
|
-
}
|
|
399
|
-
`;
|
|
400
|
-
|
|
401
|
-
const result = await transform(css, {module: true});
|
|
402
|
-
|
|
403
|
-
// css code
|
|
404
|
-
console.log(result.code);
|
|
405
|
-
// css mapping
|
|
406
|
-
console.log(result.mapping);
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
### Convert colors
|
|
410
|
-
|
|
411
|
-
```javascript
|
|
412
|
-
import {transform, ColorType} from '@tbela99/css-parser';
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
const css = `
|
|
416
|
-
.hsl { color: #b3222280; }
|
|
417
|
-
`;
|
|
418
|
-
const result: TransformResult = await transform(css, {
|
|
419
|
-
beautify: true,
|
|
420
|
-
convertColor: ColorType.SRGB
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
console.log(result.css);
|
|
424
|
-
|
|
425
|
-
```
|
|
426
|
-
|
|
427
|
-
result
|
|
428
|
-
|
|
429
|
-
```css
|
|
430
|
-
.hsl {
|
|
431
|
-
color: color(srgb .7019607843137254 .13333333333333333 .13333333333333333/50%)
|
|
432
|
-
}
|
|
433
|
-
```
|
|
434
|
-
|
|
435
|
-
### Merge similar rules
|
|
436
|
-
|
|
437
|
-
CSS
|
|
438
|
-
|
|
439
|
-
```css
|
|
440
|
-
|
|
441
|
-
.clear {
|
|
442
|
-
width: 0;
|
|
443
|
-
height: 0;
|
|
444
|
-
color: transparent;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
.clearfix:before {
|
|
448
|
-
|
|
449
|
-
height: 0;
|
|
450
|
-
width: 0;
|
|
451
|
-
}
|
|
452
|
-
```
|
|
453
|
-
|
|
454
|
-
```javascript
|
|
455
|
-
|
|
456
|
-
import {transform} from '@tbela99/css-parser';
|
|
457
|
-
|
|
458
|
-
const result = await transform(css);
|
|
459
|
-
|
|
460
|
-
```
|
|
461
|
-
|
|
462
|
-
Result
|
|
463
|
-
|
|
464
|
-
```css
|
|
465
|
-
.clear, .clearfix:before {
|
|
466
|
-
height: 0;
|
|
467
|
-
width: 0
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
.clear {
|
|
471
|
-
color: #0000
|
|
472
|
-
}
|
|
473
|
-
```
|
|
474
|
-
|
|
475
|
-
### Automatic CSS Nesting
|
|
476
|
-
|
|
477
|
-
CSS
|
|
478
|
-
|
|
479
|
-
```javascript
|
|
480
|
-
const {parse, render} = require("@tbela99/css-parser/cjs");
|
|
481
|
-
|
|
482
|
-
const css = `
|
|
483
|
-
table.colortable td {
|
|
484
|
-
text-align:center;
|
|
485
|
-
}
|
|
486
|
-
table.colortable td.c {
|
|
487
|
-
text-transform:uppercase;
|
|
488
|
-
}
|
|
489
|
-
table.colortable td:first-child, table.colortable td:first-child+td {
|
|
490
|
-
border:1px solid black;
|
|
491
|
-
}
|
|
492
|
-
table.colortable th {
|
|
493
|
-
text-align:center;
|
|
494
|
-
background:black;
|
|
495
|
-
color:white;
|
|
496
|
-
}
|
|
497
|
-
`;
|
|
498
|
-
|
|
499
|
-
const result = await parse(css, {nestingRules: true}).then(result => render(result.ast, {minify: false}).code);
|
|
500
|
-
```
|
|
501
|
-
|
|
502
|
-
Result
|
|
503
|
-
|
|
504
|
-
```css
|
|
505
|
-
table.colortable {
|
|
506
|
-
& td {
|
|
507
|
-
text-align: center;
|
|
508
|
-
|
|
509
|
-
&.c {
|
|
510
|
-
text-transform: uppercase
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
&:first-child, &:first-child + td {
|
|
514
|
-
border: 1px solid #000
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
& th {
|
|
519
|
-
text-align: center;
|
|
520
|
-
background: #000;
|
|
521
|
-
color: #fff
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
### CSS Validation
|
|
527
|
-
|
|
528
|
-
CSS
|
|
529
|
-
|
|
530
|
-
```css
|
|
531
|
-
|
|
532
|
-
#404 {
|
|
533
|
-
--animate-duration: 1s;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
.s, #404 {
|
|
537
|
-
--animate-duration: 1s;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
.s [type="text" {
|
|
541
|
-
--animate-duration: 1s;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
.s [type="text"]]
|
|
545
|
-
{
|
|
546
|
-
--animate-duration: 1s;
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
.s [type="text"] {
|
|
550
|
-
--animate-duration: 1s;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
.s [type="text" i] {
|
|
554
|
-
--animate-duration: 1s;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
.s [type="text" s]
|
|
558
|
-
{
|
|
559
|
-
--animate-duration: 1s
|
|
560
|
-
;
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
.s [type="text" b]
|
|
564
|
-
{
|
|
565
|
-
--animate-duration: 1s;
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
.s [type="text" b],{
|
|
569
|
-
--animate-duration: 1s
|
|
570
|
-
;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
.s [type="text" b]
|
|
574
|
-
+ {
|
|
575
|
-
--animate-duration: 1s;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
.s [type="text" b]
|
|
579
|
-
+ b {
|
|
580
|
-
--animate-duration: 1s;
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
.s [type="text" i] + b {
|
|
584
|
-
--animate-duration: 1s;
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
.s [type="text"())]{
|
|
589
|
-
--animate-duration: 1s;
|
|
590
|
-
}
|
|
591
|
-
.s(){
|
|
592
|
-
--animate-duration: 1s;
|
|
593
|
-
}
|
|
594
|
-
.s:focus {
|
|
595
|
-
--animate-duration: 1s;
|
|
596
|
-
}
|
|
597
|
-
```
|
|
598
|
-
|
|
599
|
-
with validation enabled
|
|
600
|
-
|
|
601
|
-
```javascript
|
|
602
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
603
|
-
|
|
604
|
-
const options = {minify: true, validate: true};
|
|
605
|
-
const {code} = await parse(css, options).then(result => render(result.ast, {minify: false}));
|
|
606
|
-
//
|
|
607
|
-
console.debug(code);
|
|
608
|
-
```
|
|
609
|
-
|
|
610
|
-
```css
|
|
611
|
-
.s:is([type=text],[type=text i],[type=text s],[type=text i]+b,:focus) {
|
|
612
|
-
--animate-duration: 1s
|
|
613
|
-
}
|
|
614
|
-
```
|
|
615
|
-
|
|
616
|
-
with validation disabled
|
|
617
|
-
|
|
618
|
-
```javascript
|
|
619
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
620
|
-
|
|
621
|
-
const options = {minify: true, validate: false};
|
|
622
|
-
const {code} = await parse(css, options).then(result => render(result.ast, {minify: false}));
|
|
623
|
-
//
|
|
624
|
-
console.debug(code);
|
|
625
|
-
```
|
|
626
|
-
|
|
627
|
-
```css
|
|
628
|
-
.s:is([type=text],[type=text i],[type=text s],[type=text b],[type=text b]+b,[type=text i]+b,:focus) {
|
|
629
|
-
--animate-duration: 1s
|
|
630
|
-
}
|
|
631
|
-
```
|
|
632
|
-
|
|
633
|
-
### Nested CSS Expansion
|
|
634
|
-
|
|
635
|
-
CSS
|
|
636
|
-
|
|
637
|
-
```css
|
|
638
|
-
table.colortable {
|
|
639
|
-
& td {
|
|
640
|
-
text-align: center;
|
|
641
|
-
|
|
642
|
-
&.c {
|
|
643
|
-
text-transform: uppercase
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
&:first-child, &:first-child + td {
|
|
647
|
-
border: 1px solid #000
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
& th {
|
|
652
|
-
text-align: center;
|
|
653
|
-
background: #000;
|
|
654
|
-
color: #fff
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
```
|
|
658
|
-
|
|
659
|
-
Javascript
|
|
660
|
-
|
|
661
|
-
```javascript
|
|
662
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
663
|
-
|
|
664
|
-
const options = {minify: true};
|
|
665
|
-
const {code} = await parse(css, options).then(result => render(result.ast, {minify: false, expandNestingRules: true}));
|
|
666
|
-
//
|
|
667
|
-
console.debug(code);
|
|
668
|
-
```
|
|
669
|
-
|
|
670
|
-
Result
|
|
671
|
-
|
|
672
|
-
```css
|
|
673
|
-
|
|
674
|
-
table.colortable td {
|
|
675
|
-
text-align: center;
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
table.colortable td.c {
|
|
679
|
-
text-transform: uppercase;
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
table.colortable td:first-child, table.colortable td:first-child + td {
|
|
683
|
-
border: 1px solid black;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
table.colortable th {
|
|
687
|
-
text-align: center;
|
|
688
|
-
background: black;
|
|
689
|
-
color: white;
|
|
690
|
-
}
|
|
691
|
-
```
|
|
692
|
-
|
|
693
|
-
### CSS if() function expansion
|
|
694
|
-
|
|
695
|
-
```typescript
|
|
696
|
-
|
|
697
|
-
const css = `
|
|
698
|
-
button {
|
|
699
|
-
background: linear-gradient(
|
|
700
|
-
if(media(min-width: 768px): to right; else: to bottom),
|
|
701
|
-
if(style(--dark-mode): #333; else: #fff),
|
|
702
|
-
if(style(--dark-mode): #000; else: #ccc)
|
|
703
|
-
);
|
|
704
|
-
}`;
|
|
705
|
-
|
|
706
|
-
result = await transform(css, {
|
|
707
|
-
|
|
708
|
-
beautify: true,
|
|
709
|
-
expandIfSyntax: true
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
console.log(result.code);
|
|
715
|
-
```
|
|
716
|
-
|
|
717
|
-
output
|
|
718
|
-
|
|
719
|
-
```css
|
|
720
|
-
button {
|
|
721
|
-
background: linear-gradient(to bottom,#fff,#ccc);
|
|
722
|
-
@media (min-width:768px) {
|
|
723
|
-
background: linear-gradient(to right,#fff,#ccc);
|
|
724
|
-
@container style(--dark-mode) {
|
|
725
|
-
background: linear-gradient(to right,#333,#000)
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
@container style(--dark-mode) {
|
|
729
|
-
background: linear-gradient(to bottom,#333,#000)
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
```
|
|
733
|
-
|
|
734
|
-
### Calc() resolution
|
|
735
|
-
|
|
736
|
-
```javascript
|
|
737
|
-
|
|
738
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
739
|
-
|
|
740
|
-
const css = `
|
|
741
|
-
a {
|
|
742
|
-
|
|
743
|
-
width: calc(100px * log(625, 5));
|
|
744
|
-
}
|
|
745
|
-
.foo-bar {
|
|
746
|
-
width: calc(100px * 2);
|
|
747
|
-
height: calc(((75.37% - 63.5px) - 900px) + (2 * 100px));
|
|
748
|
-
max-width: calc(3.5rem + calc(var(--bs-border-width) * 2));
|
|
749
|
-
}
|
|
750
|
-
`;
|
|
751
|
-
|
|
752
|
-
const prettyPrint = await parse(css).then(result => render(result.ast, {minify: false}).code);
|
|
753
|
-
|
|
754
|
-
```
|
|
755
|
-
|
|
756
|
-
result
|
|
757
|
-
|
|
758
|
-
```css
|
|
759
|
-
a {
|
|
760
|
-
width: 400px;
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
.foo-bar {
|
|
764
|
-
width: 200px;
|
|
765
|
-
height: calc(75.37% - 763.5px);
|
|
766
|
-
max-width: calc(3.5rem + var(--bs-border-width) * 2)
|
|
767
|
-
}
|
|
768
|
-
```
|
|
769
|
-
|
|
770
|
-
### CSS variable inlining
|
|
771
|
-
|
|
772
|
-
```javascript
|
|
773
|
-
|
|
774
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
775
|
-
|
|
776
|
-
const css = `
|
|
777
|
-
|
|
778
|
-
:root {
|
|
779
|
-
|
|
780
|
-
--preferred-width: 20px;
|
|
781
|
-
}
|
|
782
|
-
.foo-bar {
|
|
783
|
-
|
|
784
|
-
width: calc(calc(var(--preferred-width) + 1px) / 3 + 5px);
|
|
785
|
-
height: calc(100% / 4);}
|
|
786
|
-
`
|
|
787
|
-
|
|
788
|
-
const prettyPrint = await parse(css, {inlineCssVariables: true}).then(result => render(result.ast, {minify: false}).code);
|
|
789
|
-
|
|
790
|
-
```
|
|
791
|
-
|
|
792
|
-
result
|
|
793
|
-
|
|
794
|
-
```css
|
|
795
|
-
.foo-bar {
|
|
796
|
-
width: 12px;
|
|
797
|
-
height: 25%
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
```
|
|
801
|
-
|
|
802
|
-
### CSS variable inlining and relative color
|
|
803
|
-
|
|
804
|
-
```javascript
|
|
805
|
-
|
|
806
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
807
|
-
|
|
808
|
-
const css = `
|
|
809
|
-
|
|
810
|
-
:root {
|
|
811
|
-
--color: green;
|
|
812
|
-
}
|
|
813
|
-
._19_u :focus {
|
|
814
|
-
color: hsl(from var(--color) calc(h * 2) s l);
|
|
815
|
-
|
|
816
|
-
}
|
|
817
|
-
`
|
|
818
|
-
|
|
819
|
-
const prettyPrint = await parse(css, {inlineCssVariables: true}).then(result => render(result.ast, {minify: false}).code);
|
|
820
|
-
|
|
821
|
-
```
|
|
822
|
-
|
|
823
|
-
result
|
|
824
|
-
|
|
825
|
-
```css
|
|
826
|
-
._19_u :focus {
|
|
827
|
-
color: navy
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
```
|
|
831
|
-
|
|
832
|
-
### CSS variable inlining and relative color
|
|
833
|
-
|
|
834
|
-
```javascript
|
|
835
|
-
|
|
836
|
-
import {parse, render} from '@tbela99/css-parser';
|
|
837
|
-
|
|
838
|
-
const css = `
|
|
839
|
-
|
|
840
|
-
html { --bluegreen: oklab(54.3% -22.5% -5%); }
|
|
841
|
-
.overlay {
|
|
842
|
-
background: oklab(from var(--bluegreen) calc(1.0 - l) calc(a * 0.8) b);
|
|
843
|
-
}
|
|
844
|
-
`
|
|
845
|
-
|
|
846
|
-
const prettyPrint = await parse(css, {inlineCssVariables: true}).then(result => render(result.ast, {minify: false}).code);
|
|
847
|
-
|
|
848
|
-
```
|
|
849
|
-
|
|
850
|
-
result
|
|
851
|
-
|
|
852
|
-
```css
|
|
853
|
-
.overlay {
|
|
854
|
-
background: #0c6464
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
```
|
|
858
|
-
|
|
859
|
-
# Node Walker
|
|
860
|
-
|
|
861
|
-
```javascript
|
|
862
|
-
|
|
863
|
-
import {walk} from '@tbela99/css-parser';
|
|
864
|
-
|
|
865
|
-
for (const {node, parent, root} of walk(ast)) {
|
|
866
|
-
|
|
867
|
-
// do something
|
|
868
|
-
}
|
|
869
|
-
```
|
|
870
|
-
|
|
70
|
+
## Documentation
|
|
71
|
+
|
|
72
|
+
- [Installation](https://tbela99.github.io/css-parser/docs/documents/Guide.Getting_started.html#installation)
|
|
73
|
+
- [Usage](https://tbela99.github.io/css-parser/docs/documents/Guide.Usage.html)
|
|
74
|
+
- [Parsing CSS](https://tbela99.github.io/css-parser/docs/documents/Guide.Usage.html#parsing)
|
|
75
|
+
- [Parsing files](https://tbela99.github.io/css-parser/docs/documents/Guide.Usage.html#parsing-files)
|
|
76
|
+
- [Parsing streams](https://tbela99.github.io/css-parser/docs/documents/Guide.Usage.html#parsing-from-a-readable-stream)
|
|
77
|
+
- [Rendering AST](https://tbela99.github.io/css-parser/docs/documents/Guide.Usage.html#rendering-ast)
|
|
78
|
+
- [Transform](https://tbela99.github.io/css-parser/docs/documents/Guide.Usage.html#transform)
|
|
79
|
+
- [Validation](https://tbela99.github.io/css-parser/docs/documents/Guide.Validation.html)
|
|
80
|
+
- [CSS Modules](https://tbela99.github.io/css-parser/docs/documents/Guide.CSS_Modules.html)
|
|
81
|
+
- [Minification](https://tbela99.github.io/css-parser/docs/documents/Guide.Minification.html)
|
|
82
|
+
- [Custom Transform](https://tbela99.github.io/css-parser/docs/documents/Guide.Custom_Transform.html)
|
|
83
|
+
- [Ast Manipulation](https://tbela99.github.io/css-parser/docs/documents/Guide.Ast_Manipulation.html)
|
|
84
|
+
- [Utility Functions](https://tbela99.github.io/css-parser/docs/documents/Guide.Utility_Functions.html)
|
|
85
|
+
-
|
|
871
86
|
## AST
|
|
872
87
|
|
|
873
88
|
### Comment
|
|
@@ -880,18 +95,24 @@ for (const {node, parent, root} of walk(ast)) {
|
|
|
880
95
|
- typ: number
|
|
881
96
|
- nam: string, declaration name
|
|
882
97
|
- val: array of tokens
|
|
98
|
+
- state: EnumAstNodeStatus, validation state
|
|
99
|
+
- errors: ErrorDescription[], validation errors
|
|
883
100
|
|
|
884
101
|
### Rule
|
|
885
102
|
|
|
886
103
|
- typ: number
|
|
887
104
|
- sel: string, css selector
|
|
888
105
|
- chi: array of children
|
|
106
|
+
- state: EnumAstNodeStatus, validation state
|
|
107
|
+
- errors: ErrorDescription[], validation errors
|
|
889
108
|
|
|
890
109
|
### AtRule
|
|
891
110
|
|
|
892
111
|
- typ: number
|
|
893
112
|
- nam: string. AtRule name
|
|
894
113
|
- val: rule prelude
|
|
114
|
+
- state: EnumAstNodeStatus, validation state
|
|
115
|
+
- errors: ErrorDescription[], validation errors
|
|
895
116
|
|
|
896
117
|
### AtRuleStyleSheet
|
|
897
118
|
|
|
@@ -903,32 +124,12 @@ for (const {node, parent, root} of walk(ast)) {
|
|
|
903
124
|
- typ: number
|
|
904
125
|
- sel: string, css selector
|
|
905
126
|
- chi: array of children
|
|
127
|
+
- state: EnumAstNodeStatus, validation state
|
|
128
|
+
- errors: ErrorDescription[], validation errors
|
|
906
129
|
|
|
907
130
|
## Sourcemap
|
|
908
131
|
|
|
909
|
-
- [x]
|
|
910
|
-
|
|
911
|
-
## Minification
|
|
912
|
-
|
|
913
|
-
- [x] minify keyframes
|
|
914
|
-
- [x] minify transform functions
|
|
915
|
-
- [x] evaluate math functions calc(), clamp(), min(), max(), round(), mod(), rem(), sin(), cos(), tan(), asin(),
|
|
916
|
-
acos(), atan(), atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), sign()
|
|
917
|
-
- [x] minify colors
|
|
918
|
-
- [x] minify numbers and Dimensions tokens
|
|
919
|
-
- [x] multi-pass minification
|
|
920
|
-
- [x] inline css variables
|
|
921
|
-
- [x] merge identical rules
|
|
922
|
-
- [x] merge adjacent rules
|
|
923
|
-
- [x] compute shorthand: see the list below
|
|
924
|
-
- [x] remove redundant declarations
|
|
925
|
-
- [x] conditionally unwrap :is()
|
|
926
|
-
- [x] automatic css nesting
|
|
927
|
-
- [x] automatically wrap selectors using :is()
|
|
928
|
-
- [x] avoid reparsing (declarations, selectors, at-rule)
|
|
929
|
-
- [x] node and browser versions
|
|
930
|
-
- [x] decode and replace utf-8 escape sequence
|
|
931
|
-
- [x] experimental CSS prefix removal
|
|
132
|
+
- [x] Sourcemap generation
|
|
932
133
|
|
|
933
134
|
## Computed shorthands properties
|
|
934
135
|
|
|
@@ -984,257 +185,4 @@ for (const {node, parent, root} of walk(ast)) {
|
|
|
984
185
|
|
|
985
186
|
## Performance
|
|
986
187
|
|
|
987
|
-
- [x]
|
|
988
|
-
|
|
989
|
-
## Node Transformation
|
|
990
|
-
|
|
991
|
-
Ast can be transformed using node visitors
|
|
992
|
-
|
|
993
|
-
### Example 1: Declaration
|
|
994
|
-
|
|
995
|
-
the visitor is called for any declaration encountered
|
|
996
|
-
|
|
997
|
-
```typescript
|
|
998
|
-
|
|
999
|
-
import {AstDeclaration, ParserOptions} from "../src/@types";
|
|
1000
|
-
|
|
1001
|
-
const options: ParserOptions = {
|
|
1002
|
-
|
|
1003
|
-
visitor: {
|
|
1004
|
-
|
|
1005
|
-
Declaration: (node: AstDeclaration) => {
|
|
1006
|
-
|
|
1007
|
-
if (node.nam == '-webkit-transform') {
|
|
1008
|
-
|
|
1009
|
-
node.nam = 'transform'
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
const css = `
|
|
1016
|
-
|
|
1017
|
-
.foo {
|
|
1018
|
-
-webkit-transform: scale(calc(100 * 2/ 15));
|
|
1019
|
-
}
|
|
1020
|
-
`;
|
|
1021
|
-
|
|
1022
|
-
const result = await transform(css, options);
|
|
1023
|
-
console.debug(result.code);
|
|
1024
|
-
|
|
1025
|
-
// .foo{transform:scale(calc(40/3))}
|
|
1026
|
-
```
|
|
1027
|
-
|
|
1028
|
-
### Example 2: Declaration
|
|
1029
|
-
|
|
1030
|
-
the visitor is called only on 'height' declarations
|
|
1031
|
-
|
|
1032
|
-
```typescript
|
|
1033
|
-
|
|
1034
|
-
import {AstDeclaration, EnumToken, LengthToken, ParserOptions, transform} from '@tbela99/css-parser';
|
|
1035
|
-
|
|
1036
|
-
const options: ParserOptions = {
|
|
1037
|
-
|
|
1038
|
-
visitor: {
|
|
1039
|
-
|
|
1040
|
-
Declaration: {
|
|
1041
|
-
|
|
1042
|
-
// called only for height declaration
|
|
1043
|
-
height: (node: AstDeclaration): AstDeclaration[] => {
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
return [
|
|
1047
|
-
node,
|
|
1048
|
-
{
|
|
1049
|
-
|
|
1050
|
-
typ: EnumToken.DeclarationNodeType,
|
|
1051
|
-
nam: 'width',
|
|
1052
|
-
val: [
|
|
1053
|
-
<LengthToken>{
|
|
1054
|
-
typ: EnumToken.LengthTokenType,
|
|
1055
|
-
val: 3,
|
|
1056
|
-
unit: 'px'
|
|
1057
|
-
}
|
|
1058
|
-
]
|
|
1059
|
-
}
|
|
1060
|
-
];
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
};
|
|
1065
|
-
|
|
1066
|
-
const css = `
|
|
1067
|
-
|
|
1068
|
-
.foo {
|
|
1069
|
-
height: calc(100px * 2/ 15);
|
|
1070
|
-
}
|
|
1071
|
-
.selector {
|
|
1072
|
-
color: lch(from peru calc(l * 0.8) calc(c * 0.7) calc(h + 180))
|
|
1073
|
-
}
|
|
1074
|
-
`;
|
|
1075
|
-
|
|
1076
|
-
const result = await transform(css, options);
|
|
1077
|
-
console.debug(result.code);
|
|
1078
|
-
|
|
1079
|
-
// .foo{height:calc(40px/3);width:3px}.selector{color:#0880b0}
|
|
1080
|
-
|
|
1081
|
-
```
|
|
1082
|
-
|
|
1083
|
-
### Example 3: At-Rule
|
|
1084
|
-
|
|
1085
|
-
the visitor is called on any at-rule
|
|
1086
|
-
|
|
1087
|
-
```typescript
|
|
1088
|
-
|
|
1089
|
-
import {AstAtRule, ParserOptions} from "../src/@types";
|
|
1090
|
-
import {transform} from "../src/node";
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
const options: ParserOptions = {
|
|
1094
|
-
|
|
1095
|
-
visitor: {
|
|
1096
|
-
|
|
1097
|
-
AtRule: (node: AstAtRule): AstAtRule => {
|
|
1098
|
-
|
|
1099
|
-
if (node.nam == 'media') {
|
|
1100
|
-
|
|
1101
|
-
return {...node, val: 'all'}
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
};
|
|
1106
|
-
|
|
1107
|
-
const css = `
|
|
1108
|
-
|
|
1109
|
-
@media screen {
|
|
1110
|
-
|
|
1111
|
-
.foo {
|
|
1112
|
-
|
|
1113
|
-
height: calc(100px * 2/ 15);
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
`;
|
|
1117
|
-
|
|
1118
|
-
const result = await transform(css, options);
|
|
1119
|
-
console.debug(result.code);
|
|
1120
|
-
|
|
1121
|
-
// .foo{height:calc(40px/3)}
|
|
1122
|
-
|
|
1123
|
-
```
|
|
1124
|
-
|
|
1125
|
-
### Example 4: At-Rule
|
|
1126
|
-
|
|
1127
|
-
the visitor is called only for at-rule media
|
|
1128
|
-
|
|
1129
|
-
```typescript
|
|
1130
|
-
|
|
1131
|
-
import {AstAtRule, ParserOptions} from "../src/@types";
|
|
1132
|
-
import {transform} from "../src/node";
|
|
1133
|
-
|
|
1134
|
-
const options: ParserOptions = {
|
|
1135
|
-
|
|
1136
|
-
visitor: {
|
|
1137
|
-
|
|
1138
|
-
AtRule: {
|
|
1139
|
-
|
|
1140
|
-
media: (node: AstAtRule): AstAtRule => {
|
|
1141
|
-
|
|
1142
|
-
node.val = 'all';
|
|
1143
|
-
return node
|
|
1144
|
-
}
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
};
|
|
1148
|
-
|
|
1149
|
-
const css = `
|
|
1150
|
-
|
|
1151
|
-
@media screen {
|
|
1152
|
-
|
|
1153
|
-
.foo {
|
|
1154
|
-
|
|
1155
|
-
height: calc(100px * 2/ 15);
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
`;
|
|
1159
|
-
|
|
1160
|
-
const result = await transform(css, options);
|
|
1161
|
-
console.debug(result.code);
|
|
1162
|
-
|
|
1163
|
-
// .foo{height:calc(40px/3)}
|
|
1164
|
-
|
|
1165
|
-
```
|
|
1166
|
-
|
|
1167
|
-
### Example 5: Rule
|
|
1168
|
-
|
|
1169
|
-
the visitor is called on any Rule
|
|
1170
|
-
|
|
1171
|
-
```typescript
|
|
1172
|
-
|
|
1173
|
-
import {AstAtRule, ParserOptions} from "../src/@types";
|
|
1174
|
-
import {transform} from "../src/node";
|
|
1175
|
-
|
|
1176
|
-
const options: ParserOptions = {
|
|
1177
|
-
|
|
1178
|
-
visitor: {
|
|
1179
|
-
|
|
1180
|
-
Rule(node: AstRule): AstRule {
|
|
1181
|
-
|
|
1182
|
-
node.sel = '.foo,.bar,.fubar'
|
|
1183
|
-
return node;
|
|
1184
|
-
}
|
|
1185
|
-
}
|
|
1186
|
-
};
|
|
1187
|
-
|
|
1188
|
-
const css = `
|
|
1189
|
-
|
|
1190
|
-
.foo {
|
|
1191
|
-
|
|
1192
|
-
height: calc(100px * 2/ 15);
|
|
1193
|
-
}
|
|
1194
|
-
`;
|
|
1195
|
-
|
|
1196
|
-
const result = await transform(css, options);
|
|
1197
|
-
console.debug(result.code);
|
|
1198
|
-
|
|
1199
|
-
// .foo,.bar,.fubar{height:calc(40px/3)}
|
|
1200
|
-
|
|
1201
|
-
```
|
|
1202
|
-
|
|
1203
|
-
### Example 6: Rule
|
|
1204
|
-
|
|
1205
|
-
Adding declarations to any rule
|
|
1206
|
-
|
|
1207
|
-
```typescript
|
|
1208
|
-
|
|
1209
|
-
import {AstRule, parseDeclarations, ParserOptions, transform} from '@tbela99/css-parser';
|
|
1210
|
-
|
|
1211
|
-
const options: ParserOptions = {
|
|
1212
|
-
|
|
1213
|
-
removeEmpty: false,
|
|
1214
|
-
visitor: {
|
|
1215
|
-
|
|
1216
|
-
Rule: async (node: AstRule): Promise<AstRule | null> => {
|
|
1217
|
-
|
|
1218
|
-
if (node.sel == '.foo') {
|
|
1219
|
-
|
|
1220
|
-
node.chi.push(...await parseDeclarations('width: 3px'));
|
|
1221
|
-
return node;
|
|
1222
|
-
}
|
|
1223
|
-
|
|
1224
|
-
return null;
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
};
|
|
1228
|
-
|
|
1229
|
-
const css = `
|
|
1230
|
-
|
|
1231
|
-
.foo {
|
|
1232
|
-
}
|
|
1233
|
-
`;
|
|
1234
|
-
|
|
1235
|
-
const result = await transform(css, options);
|
|
1236
|
-
console.debug(result.code);
|
|
1237
|
-
|
|
1238
|
-
// .foo{width:3px}
|
|
1239
|
-
|
|
1240
|
-
```
|
|
188
|
+
- [x] Bundle file referenced by the @import statement.
|