Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Parsing CSS into AST
> NOTE: Currenly parser omits redundant separators, spaces and comments (except exclamation comments, i.e. `/*! comment */`) on AST build.
## parse(source[, options])
Parses CSS into AST.
```js
// simple parsing with no options
var ast = csstree.parse('.example { color: red }');
// parse with options
var ast = csstree.parse('.foo.bar', {
context: 'selector',
positions: true
});
```
Options (optional):
<!-- MarkdownTOC -->
- [context](#context)
- [atrule](#atrule)
- [positions](#positions)
- [onParseError](#onparseerror)
- [filename](#filename)
- [offset](#offset)
- [line](#line)
- [column](#column)
- [parseAtrulePrelude](#parseatruleprelude)
- [parseRulePrelude](#parseruleprelude)
- [parseValue](#parsevalue)
- [parseCustomProperty](#parsecustomproperty)
<!-- /MarkdownTOC -->
### context
Type: `string`
Default: `'stylesheet'`
Defines what part of CSS is parsing.
Contexts:
- `stylesheet` (default) – regular stylesheet, should be suitable in most cases
- `atrule` – at-rule (e.g. `@media screen, print { ... }`)
- `atrulePrelude` – at-rule prelude (`screen, print` for example above)
- `mediaQueryList` – used to parse comma separated media query list
- `mediaQuery` – used to parse media query
- `rule` – rule (e.g. `.foo, .bar:hover { color: red; border: 1px solid black; }`)
- `selectorList` – selector group (`.foo, .bar:hover` for rule example)
- `selector` – selector (`.foo` or `.bar:hover` for rule example)
- `block` – block with curly braces (`{ color: red; border: 1px solid black; }` for rule example)
- `declarationList` – block content w/o curly braces (`color: red; border: 1px solid black;` for rule example), useful for parsing HTML `style` attribute value
- `declaration` – declaration (`color: red` or `border: 1px solid black` for rule example)
- `value` – declaration value (`red` or `1px solid black` for rule example)
### atrule
Type: `string` or `null`
Default: `null`
Using for `atrulePrelude` context to apply atrule specific parse rules.
### positions
Type: `boolean`
Default: `false`
Specify to store locations of node content in original source. Location is storing as `loc` field of nodes. `loc` property is always `null` when option is `false`. The structure of `loc`:
```
loc: {
source: 'value of `filename` option or `<unknown>`',
start: {
offset: <number>,
line: <number>,
column: <number>
},
end: {
offset: <number>,
line: <number>,
column: <number>
}
}
```
### onParseError
Type: `function(error, fallbackNode)` or `null`
Default: `null`
Parsing is tolerant by default, i.e. any text may to be parsed with no an raised exception. However, mistakes in CSS may make it imposible to parse some part, e.g. a selector or declaration. In that case bad content is wrapping into a `Raw` node and `onParseError` is invoking.
```js
csstree.parse('example { foo; bar: 1! }', {
onParseError: function(error) {
console.log(error.formattedMessage);
}
});
// Parse error: Colon is expected
// 1 |example { foo; bar: 1! }
// --------------------^
// Parse error: Identifier is expected
// 1 |example { foo; bar: 1! }
// ------------------------------^
```
### filename
Type: `string`
Default: `'<unknown>'`
Filename of source. This value adds to `loc` as `source` property when `positions` option is `true`. Using for source map generation.
### offset
Type: `number`
Default: `0`
Start offset. Useful when parsing a fragment of CSS to store a correct positions for node's `loc` property.
### line
Type: `number`
Default: `1`
Start line number. Useful when parsing fragment of CSS to store correct positions in node's `loc` property.
### column
Type: `number`
Default: `1`
Start column number. Useful when parsing fragment of CSS to store correct positions in node's `loc` property.
### parseAtrulePrelude
Type: `boolean`
Default: `true`
Defines to parse a at-rule prelude in details (represents as `AtruleExpresion`, `MediaQueryList` or `SelectorList` if any). Otherwise represents prelude as `Raw` node.
```js
csstree.parse('@example 1 2;');
// {
// "type": "Atrule",
// "prelude": {
// "type": "AtrulePrelude",
// "children": [
// { "type": "Number", "value": "1" },
// { "type": "WhiteSpace", "value": " " },
// { "type": "Number", "value": "2" }
// ]
// },
// "block": null
// }
csstree.parse('@example 1 2;', { parseAtrulePrelude: false });
// {
// "type": "Atrule",
// "prelude": {
// "type": "Raw",
// "value": "1 2"
// },
// "block": null
// }
```
### parseRulePrelude
Type: `boolean`
Default: `true`
Defines to parse a rule prelude in details or left unparsed (represents as `Raw` node).
```js
csstree.parse('.foo {}');
// {
// "type": "Rule",
// "prelude": {
// "type": "SelectorList",
// "children": [
// {
// "type": "Selector",
// "children": [
// { "type": "ClassSelector", "name": "foo" }
// ]
// }
// ]
// },
// "block": {
// "type": "Block",
// "children": []
// }
// }
csstree.parse('.foo {}', { parseRulePrelude: false });
// {
// "type": "Rule",
// "prelude": {
// "type": "Raw",
// "value": ".foo"
// },
// "block": {
// "type": "Block",
// "children": []
// }
// }
```
### parseValue
Type: `boolean`
Default: `true`
Defines to parse a declaration value in details (represents as `Value`). Otherwise represents value as `Raw` node.
```js
csstree.parse('color: #aabbcc', { context: 'declaration' });
// {
// "type": "Declaration",
// "important": false,
// "property": "color",
// "value": {
// "type": "Value",
// "children": [
// {
// "type": "HexColor",
// "value": "aabbcc"
// }
// ]
// }
// }
csstree.parse('color: #aabbcc', { context: 'declaration', parseValue: false });
// {
// "type": "Declaration",
// "important": false,
// "property": "color",
// "value": {
// "type": "Raw",
// "value": " #aabbcc"
// }
// }
```
### parseCustomProperty
Type: `boolean`
Default: `false`
Defines to parse a custom property value and a `var()` fallback in details (represents as `Value`). Otherwise represents value as `Raw` node.
```js
csstree.parse('--custom: #aabbcc', { context: 'declaration' });
// {
// "type": "Declaration",
// "important": false,
// "property": "--custom",
// "value": {
// "type": "Raw",
// "value": " #aabbcc"
// }
// }
csstree.parse('--custom: #aabbcc', { context: 'declaration', parseCustomProperty: true });
// {
// "type": "Declaration",
// "important": false,
// "property": "--custom",
// "value": {
// "type": "Value",
// "children": [
// {
// "type": "HexColor",
// "value": "aabbcc"
// }
// ]
// }
// }
```