1 | /**
|
2 | * CSS parser / stringifier for Node.js
|
3 | */
|
4 |
|
5 | /**
|
6 | * css.parse options
|
7 | */
|
8 | export interface ParserOptions {
|
9 | /** Silently fail on parse errors */
|
10 | silent?: boolean | undefined;
|
11 | /** The path to the file containing css. Makes errors and source maps more helpful, by letting them know where code comes from. */
|
12 | source?: string | undefined;
|
13 | }
|
14 |
|
15 | /**
|
16 | * css.stringify options
|
17 | */
|
18 | export interface StringifyOptions {
|
19 | /** The string used to indent the output. Defaults to two spaces. */
|
20 | indent?: string | undefined;
|
21 | /** Omit comments and extraneous whitespace. */
|
22 | compress?: boolean | undefined;
|
23 | /** Return a sourcemap along with the CSS output.
|
24 | * Using the source option of css.parse is strongly recommended
|
25 | * when creating a source map. Specify sourcemap: 'generator'
|
26 | * to return the SourceMapGenerator object instead of serializing the source map.
|
27 | */
|
28 | sourcemap?: string | undefined;
|
29 | /** (enabled by default, specify false to disable)
|
30 | * Reads any source maps referenced by the input files
|
31 | * when generating the output source map. When enabled,
|
32 | * file system access may be required for reading the referenced source maps.
|
33 | */
|
34 | inputSourcemaps?: boolean | undefined;
|
35 | }
|
36 |
|
37 | /**
|
38 | * Error thrown during parsing.
|
39 | */
|
40 | export interface ParserError {
|
41 | /** The full error message with the source position. */
|
42 | message?: string | undefined;
|
43 | /** The error message without position. */
|
44 | reason?: string | undefined;
|
45 | /** The value of options.source if passed to css.parse. Otherwise undefined. */
|
46 | filename?: string | undefined;
|
47 | line?: number | undefined;
|
48 | column?: number | undefined;
|
49 | /** The portion of code that couldn't be parsed. */
|
50 | source?: string | undefined;
|
51 | }
|
52 |
|
53 | // ---------------------------------------------------------------------------------
|
54 | // AST Tree
|
55 | // ---------------------------------------------------------------------------------
|
56 |
|
57 | /**
|
58 | * Information about a position in the code.
|
59 | * The line and column numbers are 1-based: The first line is 1 and the first column of a line is 1 (not 0).
|
60 | */
|
61 | export interface Position {
|
62 | line?: number | undefined;
|
63 | column?: number | undefined;
|
64 | }
|
65 |
|
66 | /**
|
67 | * Base AST Tree Node.
|
68 | */
|
69 | export interface BaseNode {
|
70 | /** A reference to the parent node, or null if the node has no parent. */
|
71 | parent?: Node | undefined;
|
72 | /** Information about the position in the source string that corresponds to the node. */
|
73 | position?: {
|
74 | start?: Position | undefined;
|
75 | end?: Position | undefined;
|
76 | /** The value of options.source if passed to css.parse. Otherwise undefined. */
|
77 | source?: string | undefined;
|
78 | /** The full source string passed to css.parse. */
|
79 | content?: string | undefined;
|
80 | } | undefined;
|
81 | }
|
82 |
|
83 | export interface Rule extends BaseNode {
|
84 | type: "rule";
|
85 | /** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
|
86 | selectors?: string[] | undefined;
|
87 | /** Array of nodes with the types declaration and comment. */
|
88 | declarations?: Array<Declaration | Comment> | undefined;
|
89 | }
|
90 |
|
91 | export interface Declaration extends BaseNode {
|
92 | type: "declaration";
|
93 | /** The property name, trimmed from whitespace and comments. May not be empty. */
|
94 | property?: string | undefined;
|
95 | /** The value of the property, trimmed from whitespace and comments. Empty values are allowed. */
|
96 | value?: string | undefined;
|
97 | }
|
98 |
|
99 | /**
|
100 | * A rule-level or declaration-level comment. Comments inside selectors, properties and values etc. are lost.
|
101 | */
|
102 | export interface Comment extends BaseNode {
|
103 | type: "comment";
|
104 | comment?: string | undefined;
|
105 | }
|
106 |
|
107 | /**
|
108 | * The @charset at-rule.
|
109 | */
|
110 | export interface Charset extends BaseNode {
|
111 | type: "charset";
|
112 | /** The part following @charset. */
|
113 | charset?: string | undefined;
|
114 | }
|
115 |
|
116 | /**
|
117 | * The @custom-media at-rule
|
118 | */
|
119 | export interface CustomMedia extends BaseNode {
|
120 | type: "custom-media";
|
121 | /** The ---prefixed name. */
|
122 | name?: string | undefined;
|
123 | /** The part following the name. */
|
124 | media?: string | undefined;
|
125 | }
|
126 |
|
127 | /**
|
128 | * The @document at-rule.
|
129 | */
|
130 | export interface Document extends BaseNode {
|
131 | type: "document";
|
132 | /** The part following @document. */
|
133 | document?: string | undefined;
|
134 | /** The vendor prefix in @document, or undefined if there is none. */
|
135 | vendor?: string | undefined;
|
136 | /** Array of nodes with the types rule, comment and any of the at-rule types. */
|
137 | rules?: Array<Rule | Comment | AtRule> | undefined;
|
138 | }
|
139 |
|
140 | /**
|
141 | * The @font-face at-rule.
|
142 | */
|
143 | export interface FontFace extends BaseNode {
|
144 | type: "font-face";
|
145 | /** Array of nodes with the types declaration and comment. */
|
146 | declarations?: Array<Declaration | Comment> | undefined;
|
147 | }
|
148 |
|
149 | /**
|
150 | * The @host at-rule.
|
151 | */
|
152 | export interface Host extends BaseNode {
|
153 | type: "host";
|
154 | /** Array of nodes with the types rule, comment and any of the at-rule types. */
|
155 | rules?: Array<Rule | Comment | AtRule> | undefined;
|
156 | }
|
157 |
|
158 | /**
|
159 | * The @import at-rule.
|
160 | */
|
161 | export interface Import extends BaseNode {
|
162 | type: "import";
|
163 | /** The part following @import. */
|
164 | import?: string | undefined;
|
165 | }
|
166 |
|
167 | /**
|
168 | * The @keyframes at-rule.
|
169 | */
|
170 | export interface KeyFrames extends BaseNode {
|
171 | type: "keyframes";
|
172 | /** The name of the keyframes rule. */
|
173 | name?: string | undefined;
|
174 | /** The vendor prefix in @keyframes, or undefined if there is none. */
|
175 | vendor?: string | undefined;
|
176 | /** Array of nodes with the types keyframe and comment. */
|
177 | keyframes?: Array<KeyFrame | Comment> | undefined;
|
178 | }
|
179 |
|
180 | export interface KeyFrame extends BaseNode {
|
181 | type: "keyframe";
|
182 | /** The list of "selectors" of the keyframe rule, split on commas. Each “selector” is trimmed from whitespace. */
|
183 | values?: string[] | undefined;
|
184 | /** Array of nodes with the types declaration and comment. */
|
185 | declarations?: Array<Declaration | Comment> | undefined;
|
186 | }
|
187 |
|
188 | /**
|
189 | * The @media at-rule.
|
190 | */
|
191 | export interface Media extends BaseNode {
|
192 | type: "media";
|
193 | /** The part following @media. */
|
194 | media?: string | undefined;
|
195 | /** Array of nodes with the types rule, comment and any of the at-rule types. */
|
196 | rules?: Array<Rule | Comment | AtRule> | undefined;
|
197 | }
|
198 |
|
199 | /**
|
200 | * The @namespace at-rule.
|
201 | */
|
202 | export interface Namespace extends BaseNode {
|
203 | type: "namespace";
|
204 | /** The part following @namespace. */
|
205 | namespace?: string | undefined;
|
206 | }
|
207 |
|
208 | /**
|
209 | * The @page at-rule.
|
210 | */
|
211 | export interface Page extends BaseNode {
|
212 | type: "page";
|
213 | /** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
|
214 | selectors?: string[] | undefined;
|
215 | /** Array of nodes with the types declaration and comment. */
|
216 | declarations?: Array<Declaration | Comment> | undefined;
|
217 | }
|
218 |
|
219 | /**
|
220 | * The @supports at-rule.
|
221 | */
|
222 | export interface Supports extends BaseNode {
|
223 | type: "supports";
|
224 | /** The part following @supports. */
|
225 | supports?: string | undefined;
|
226 | /** Array of nodes with the types rule, comment and any of the at-rule types. */
|
227 | rules?: Array<Rule | Comment | AtRule> | undefined;
|
228 | }
|
229 |
|
230 | /** All at-rules. */
|
231 | export type AtRule =
|
232 | | Charset
|
233 | | CustomMedia
|
234 | | Document
|
235 | | FontFace
|
236 | | Host
|
237 | | Import
|
238 | | KeyFrames
|
239 | | Media
|
240 | | Namespace
|
241 | | Page
|
242 | | Supports;
|
243 |
|
244 | /**
|
245 | * A collection of rules
|
246 | */
|
247 | export interface StyleRules {
|
248 | /** Array of nodes with the types rule, comment and any of the at-rule types. */
|
249 | rules: Array<Rule | Comment | AtRule>;
|
250 | /** Array of Errors. Errors collected during parsing when option silent is true. */
|
251 | parsingErrors?: ParserError[] | undefined;
|
252 | }
|
253 |
|
254 | /**
|
255 | * The root node returned by css.parse.
|
256 | */
|
257 | export interface Stylesheet extends BaseNode {
|
258 | type: "stylesheet";
|
259 | stylesheet?: StyleRules | undefined;
|
260 | }
|
261 |
|
262 | /**
|
263 | * All possible AST nodes.
|
264 | */
|
265 | export type Node = Rule | Declaration | Comment | AtRule | Stylesheet;
|
266 |
|
267 | // ---------------------------------------------------------------------------------
|
268 |
|
269 | /**
|
270 | * Accepts a CSS string and returns an AST object.
|
271 | *
|
272 | * @param {string} code - CSS code.
|
273 | * @param {ParserOptions} options - CSS parser options.
|
274 | * @return {Stylesheet} AST object built using provides CSS code.
|
275 | */
|
276 | export function parse(code: string, options?: ParserOptions): Stylesheet;
|
277 |
|
278 | /**
|
279 | * Accepts an AST object (as css.parse produces) and returns a CSS string.
|
280 | *
|
281 | * @param {Stylesheet} stylesheet - AST tree.
|
282 | * @param {StringifyOptions} options - AST tree to string serializaiton options.
|
283 | * @return {string} CSS code.
|
284 | */
|
285 | export function stringify(stylesheet: Stylesheet, options?: StringifyOptions): string;
|