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
+3-6Lines changed: 3 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +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
+
-`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`
26
26
27
27
#### Returns
28
28
@@ -54,7 +54,7 @@ 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`
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
58
-`removeEmptyRules` (boolean) - Remove rules with empty declaration blocks from the output. Works in all modes (beautified, compressed, identity). Default: `false`
59
59
60
60
#### Returns
@@ -95,9 +95,8 @@ type CssStylesheetAST = {
95
95
type:CssTypes.stylesheet;
96
96
stylesheet: {
97
97
source?:string;
98
-
rules:CssRuleAST[];
98
+
rules:Array<CssAtRuleAST|CssWhitespaceAST>;
99
99
parsingErrors?:CssParseError[];
100
-
originalSource?:string; // Set when preserveFormatting is true
101
100
};
102
101
};
103
102
```
@@ -163,12 +162,10 @@ type CssPosition = {
163
162
start: {
164
163
line:number;
165
164
column:number;
166
-
offset?:number; // Set when preserveFormatting is true
167
165
};
168
166
end: {
169
167
line:number;
170
168
column:number;
171
-
offset?:number; // Set when preserveFormatting is true
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