UNPKG

8.27 kBTypeScriptView Raw
1/**
2 * CSS parser / stringifier for Node.js
3 */
4
5/**
6 * css.parse options
7 */
8export 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 */
18export 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 */
40export 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 */
61export interface Position {
62 line?: number | undefined;
63 column?: number | undefined;
64}
65
66/**
67 * Base AST Tree Node.
68 */
69export interface Node {
70 /** The possible values are the ones listed in the Types section on https://github.com/reworkcss/css page. */
71 type?: string | undefined;
72 /** A reference to the parent node, or null if the node has no parent. */
73 parent?: Node | undefined;
74 /** Information about the position in the source string that corresponds to the node. */
75 position?: {
76 start?: Position | undefined;
77 end?: Position | undefined;
78 /** The value of options.source if passed to css.parse. Otherwise undefined. */
79 source?: string | undefined;
80 /** The full source string passed to css.parse. */
81 content?: string | undefined;
82 } | undefined;
83}
84
85export interface Rule extends Node {
86 /** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
87 selectors?: string[] | undefined;
88 /** Array of nodes with the types declaration and comment. */
89 declarations?: Array<Declaration | Comment> | undefined;
90}
91
92export interface Declaration extends Node {
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 */
102export interface Comment extends Node {
103 comment?: string | undefined;
104}
105
106/**
107 * The @charset at-rule.
108 */
109export interface Charset extends Node {
110 /** The part following @charset. */
111 charset?: string | undefined;
112}
113
114/**
115 * The @custom-media at-rule
116 */
117export interface CustomMedia extends Node {
118 /** The ---prefixed name. */
119 name?: string | undefined;
120 /** The part following the name. */
121 media?: string | undefined;
122}
123
124/**
125 * The @document at-rule.
126 */
127export interface Document extends Node {
128 /** The part following @document. */
129 document?: string | undefined;
130 /** The vendor prefix in @document, or undefined if there is none. */
131 vendor?: string | undefined;
132 /** Array of nodes with the types rule, comment and any of the at-rule types. */
133 rules?: Array<Rule | Comment | AtRule> | undefined;
134}
135
136/**
137 * The @font-face at-rule.
138 */
139export interface FontFace extends Node {
140 /** Array of nodes with the types declaration and comment. */
141 declarations?: Array<Declaration | Comment> | undefined;
142}
143
144/**
145 * The @host at-rule.
146 */
147export interface Host extends Node {
148 /** Array of nodes with the types rule, comment and any of the at-rule types. */
149 rules?: Array<Rule | Comment | AtRule> | undefined;
150}
151
152/**
153 * The @import at-rule.
154 */
155export interface Import extends Node {
156 /** The part following @import. */
157 import?: string | undefined;
158}
159
160/**
161 * The @keyframes at-rule.
162 */
163export interface KeyFrames extends Node {
164 /** The name of the keyframes rule. */
165 name?: string | undefined;
166 /** The vendor prefix in @keyframes, or undefined if there is none. */
167 vendor?: string | undefined;
168 /** Array of nodes with the types keyframe and comment. */
169 keyframes?: Array<KeyFrame | Comment> | undefined;
170}
171
172export interface KeyFrame extends Node {
173 /** The list of "selectors" of the keyframe rule, split on commas. Each “selector” is trimmed from whitespace. */
174 values?: string[] | undefined;
175 /** Array of nodes with the types declaration and comment. */
176 declarations?: Array<Declaration | Comment> | undefined;
177}
178
179/**
180 * The @media at-rule.
181 */
182export interface Media extends Node {
183 /** The part following @media. */
184 media?: string | undefined;
185 /** Array of nodes with the types rule, comment and any of the at-rule types. */
186 rules?: Array<Rule | Comment | AtRule> | undefined;
187}
188
189/**
190 * The @namespace at-rule.
191 */
192export interface Namespace extends Node {
193 /** The part following @namespace. */
194 namespace?: string | undefined;
195}
196
197/**
198 * The @page at-rule.
199 */
200export interface Page extends Node {
201 /** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
202 selectors?: string[] | undefined;
203 /** Array of nodes with the types declaration and comment. */
204 declarations?: Array<Declaration | Comment> | undefined;
205}
206
207/**
208 * The @supports at-rule.
209 */
210export interface Supports extends Node {
211 /** The part following @supports. */
212 supports?: string | undefined;
213 /** Array of nodes with the types rule, comment and any of the at-rule types. */
214 rules?: Array<Rule | Comment | AtRule> | undefined;
215}
216
217/** All at-rules. */
218export type AtRule =
219 | Charset
220 | CustomMedia
221 | Document
222 | FontFace
223 | Host
224 | Import
225 | KeyFrames
226 | Media
227 | Namespace
228 | Page
229 | Supports;
230
231/**
232 * A collection of rules
233 */
234export interface StyleRules {
235 /** Array of nodes with the types rule, comment and any of the at-rule types. */
236 rules: Array<Rule | Comment | AtRule>;
237 /** Array of Errors. Errors collected during parsing when option silent is true. */
238 parsingErrors?: ParserError[] | undefined;
239}
240
241/**
242 * The root node returned by css.parse.
243 */
244export interface Stylesheet extends Node {
245 stylesheet?: StyleRules | undefined;
246}
247
248// ---------------------------------------------------------------------------------
249
250/**
251 * Accepts a CSS string and returns an AST object.
252 *
253 * @param {string} code - CSS code.
254 * @param {ParserOptions} options - CSS parser options.
255 * @return {Stylesheet} AST object built using provides CSS code.
256 */
257export function parse(code: string, options?: ParserOptions): Stylesheet;
258
259/**
260 * Accepts an AST object (as css.parse produces) and returns a CSS string.
261 *
262 * @param {Stylesheet} stylesheet - AST tree.
263 * @param {StringifyOptions} options - AST tree to string serializaiton options.
264 * @return {string} CSS code.
265 */
266export function stringify(stylesheet: Stylesheet, options?: StringifyOptions): string;