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
+51-3Lines changed: 51 additions & 3 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) - Insert whitespace AST nodes and store raw formatting properties on nodes for identity round-trip. When `true`, whitespace between siblings is preserved as `CssWhitespaceAST` nodes, and raw formatting properties (`rawPrelude`, `rawBetween`, `rawValue`, `rawSource`) are stored on relevant nodes. 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. Walks the AST including whitespace nodes and uses raw formatting properties to reconstruct the original output. Inserted or modified nodes without raw properties are emitted in beautified format. Falls back to beautified output when `preserveFormatting` was not used. 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
@@ -92,7 +95,7 @@ type CssStylesheetAST = {
92
95
type:CssTypes.stylesheet;
93
96
stylesheet: {
94
97
source?:string;
95
-
rules:CssRuleAST[];
98
+
rules:Array<CssAtRuleAST|CssWhitespaceAST>;
96
99
parsingErrors?:CssParseError[];
97
100
};
98
101
};
@@ -188,8 +191,10 @@ Options for the stringifier.
188
191
189
192
```typescript
190
193
typeCompilerOptions= {
191
-
indent?:string; // Default: ' '
192
-
compress?:boolean; // Default: false
194
+
indent?:string; // Default: ' '
195
+
compress?:boolean; // Default: false
196
+
identity?:boolean; // Default: false
197
+
removeEmptyRules?:boolean; // Default: false
193
198
};
194
199
```
195
200
@@ -290,6 +295,49 @@ console.log(compressed);
290
295
// Output: .example{color:red;font-size:16px}
291
296
```
292
297
298
+
### Identity Round-Trip
299
+
300
+
Reproduce the original CSS exactly as it was written, preserving all whitespace, comments, and formatting:
When parsing with `preserveFormatting: true`, the parser stores additional formatting metadata on AST nodes to support exact round-trip via identity mode. These properties are optional and only present when formatting is preserved.
386
+
387
+
### Whitespace Nodes
388
+
389
+
`CssWhitespaceAST` nodes are inserted between sibling nodes in all arrays (rules, declarations, keyframes) to preserve the original whitespace and line breaks.
390
+
391
+
### Raw Properties on Nodes
392
+
393
+
-**`rawPrelude`** (on rules, at-rules with blocks): The exact original text before `{`, preserving whitespace between selector/condition and brace
394
+
-**`rawBetween`** (on declarations): The text between the property name and value, including `:` and any surrounding whitespace (e.g., `": "` or `": "`)
395
+
-**`rawValue`** (on declarations): The untrimmed original value text
396
+
-**`rawSource`** (on statement at-rules: import, charset, namespace, custom-media, layer statement): The exact original text of the entire at-rule
397
+
398
+
### AST Modification with Identity Mode
399
+
400
+
Because formatting data is stored per-node rather than as a single cached string, you can freely insert, remove, or modify AST nodes and identity mode will reflect the changes:
0 commit comments