You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/API.md
+53-2Lines changed: 53 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,7 @@ Parses CSS code and returns an Abstract Syntax Tree (AST).
22
22
-`options` (object, optional) - Parsing options
23
23
-`silent` (boolean) - Silently fail on parse errors instead of throwing. When `true`, errors are collected in `ast.stylesheet.parsingErrors`
24
24
-`source` (string) - File path for better error reporting
25
+
-`preserveFormatting` (boolean) - Store source character offsets and original CSS text on the AST for identity round-trip. When `true`, position nodes include an `offset` field and `ast.stylesheet.originalSource` is set. Default: `false`
25
26
26
27
#### Returns
27
28
@@ -53,6 +54,8 @@ Converts a CSS AST back to CSS string with configurable formatting.
-`compress` (boolean) - Whether to compress/minify the output (default: `false`)
57
+
-`identity` (boolean) - Reproduce the original CSS exactly as parsed. Requires `preserveFormatting: true` during parsing. Falls back to beautified output when original source is not available. Default: `false`
58
+
-`removeEmptyRules` (boolean) - Remove rules with empty declaration blocks from the output. Works in all modes (beautified, compressed, identity). Default: `false`
56
59
57
60
#### Returns
58
61
@@ -94,6 +97,7 @@ type CssStylesheetAST = {
94
97
source?:string;
95
98
rules:CssRuleAST[];
96
99
parsingErrors?:CssParseError[];
100
+
originalSource?:string; // Set when preserveFormatting is true
97
101
};
98
102
};
99
103
```
@@ -159,10 +163,12 @@ type CssPosition = {
159
163
start: {
160
164
line:number;
161
165
column:number;
166
+
offset?:number; // Set when preserveFormatting is true
162
167
};
163
168
end: {
164
169
line:number;
165
170
column:number;
171
+
offset?:number; // Set when preserveFormatting is true
166
172
};
167
173
};
168
174
```
@@ -188,8 +194,10 @@ Options for the stringifier.
188
194
189
195
```typescript
190
196
typeCompilerOptions= {
191
-
indent?:string; // Default: ' '
192
-
compress?:boolean; // Default: false
197
+
indent?:string; // Default: ' '
198
+
compress?:boolean; // Default: false
199
+
identity?:boolean; // Default: false
200
+
removeEmptyRules?:boolean; // Default: false
193
201
};
194
202
```
195
203
@@ -290,6 +298,49 @@ console.log(compressed);
290
298
// Output: .example{color:red;font-size:16px}
291
299
```
292
300
301
+
### Identity Round-Trip
302
+
303
+
Reproduce the original CSS exactly as it was written, preserving all whitespace, comments, and formatting:
0 commit comments