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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# AST format
CSSTree's AST consists of nodes (leafs). Each node is an object with a set of properties that depends on node's type. Nodes can refers to other nodes and contain a list of nested nodes.
Interactively explore the AST with [AST Explorer](https://astexplorer.net/#/gist/244e2fb4da940df52bf0f4b94277db44/e79aff44611020b22cfd9708f3a99ce09b7d67a8).
<!-- MarkdownTOC -->
- [Example](#example)
- [Common node's properties](#common-nodes-properties)
- [type](#type)
- [loc](#loc)
- [children](#children)
- [Node types](#node-types)
- [AnPlusB](#anplusb)
- [Atrule](#atrule)
- [AtrulePrelude](#atruleprelude)
- [AttributeSelector](#attributeselector)
- [Block](#block)
- [Brackets](#brackets)
- [CDC](#cdc)
- [CDO](#cdo)
- [ClassSelector](#classselector)
- [Combinator](#combinator)
- [Comment](#comment)
- [Declaration](#declaration)
- [DeclarationList](#declarationlist)
- [Dimension](#dimension)
- [Function](#function)
- [HexColor](#hexcolor)
- [IdSelector](#idselector)
- [Identifier](#identifier)
- [MediaFeature](#mediafeature)
- [MediaQuery](#mediaquery)
- [MediaQueryList](#mediaquerylist)
- [Nth](#nth)
- [Number](#number)
- [Operator](#operator)
- [Parentheses](#parentheses)
- [Percentage](#percentage)
- [PseudoClassSelector](#pseudoclassselector)
- [PseudoElementSelector](#pseudoelementselector)
- [Ratio](#ratio)
- [Raw](#raw)
- [Rule](#rule)
- [Selector](#selector)
- [SelectorList](#selectorlist)
- [String](#string)
- [StyleSheet](#stylesheet)
- [TypeSelector](#typeselector)
- [UnicodeRange](#unicoderange)
- [Url](#url)
- [Value](#value)
- [WhiteSpace](#whitespace)
<!-- /MarkdownTOC -->
## Example
Assume we have a CSS:
```css
body {
color: red;
}
```
An AST for this CSS might look like:
```js
{
type: 'StyleSheet',
loc: null,
children: [
{
type: 'Rule',
loc: null,
prelude: {
type: 'SelectorList',
loc: null,
children: [
{
type: 'Selector',
loc: null,
children: [
{
type: 'TypeSelector',
loc: null,
name: 'body'
}
]
}
]
},
block: {
type: 'Block',
loc: null,
children: [
{
type: 'Declaration',
loc: null,
important: false,
property: 'color',
value: {
type: 'Value',
loc: null,
children: [
{
type: 'Identifier',
loc: null,
name: 'red'
}
]
}
}
]
}
}
]
}
```
> NOTE: The example uses arrays for the values of the property `children`. In fact, the values of this property are instances of the [`List`](List.md) class.
An AST structure (i.e. details level, include positions or not) is depend on options passed to parser. See [Parsing CSS into AST](parsing.md) for details.
## Common node's properties
All nodes have the following properties.
### type
Type: `String`
Indicates the type of a node. The possible values are the ones listed in the [Node types](#node-types) below.
### loc
Type: `Object` or `null`
Information about the position in the source string that corresponds to the node. It has the following structure:
```js
{
source: String,
start: {
offset: Number,
line: Number,
column: Number
},
end: {
offset: Number,
line: Number,
column: Number
}
}
```
The `source` property contains value of `options.filename` if passed to `csstree.parse()`, otherwise `"<unknown>"`.
The `offset` number is zero-based, indicates the index in a source string passed to the parser.
The `line` and `column` numbers are 1-based: the first line is `1` and the first column of a line is `1`.
The `loc` property lets you know from which source file the node comes from (if available) and what part of that file was parsed into the node. By default parser doesn't include `loc` data into the AST (sets `null` for this property), you should pass `options.positions` equal to `true` to make `loc` filled.
### children
Type: `List` or `null`
Only certain types of nodes can contain this property, such as [`StyleSheet`](#stylesheet) or [`Block`](#block). However, this is the only property that can store a list of nested nodes.
Most node types always store an instance of the `List` in this property, even if there is no nested nodes (the list is empty). Only some node types, such as `PseudoClassSelector` and `PseudoElementSelector`, can store a `null` instead of a list. This is due to the fact that in the absence of a list such node types is represent a pseudo-selector, and in the presence of a list, a functional pseudo-selector. See definition of each node type for details.
## Node types
> NOTE: Despite every node has a `loc` property, this property is excluded from definitions to reduce a noise.
<!-- node types -->
### AnPlusB
Used for [the An+B microsyntax](https://drafts.csswg.org/css-syntax/#anb-microsyntax).
```js
{
type: "AnPlusB",
a: String | null,
b: String | null
}
```
`a` or `b` fields may have no value (equals to `null`) but not both at the same time. Parser normalizes `a` value to store a valid integer, i.e. parser will store `-1` for `-n` and `1` for `n`.
### Atrule
```js
{
type: "Atrule",
name: String,
prelude: <AtrulePrelude> | <Raw> | null,
block: <Block> | null
}
```
### AtrulePrelude
```js
{
type: "AtrulePrelude",
children: List
}
```
### AttributeSelector
```js
{
type: "AttributeSelector",
name: <Identifier>,
matcher: String | null,
value: <String> | <Identifier> | null,
flags: String | null
}
```
### Block
```js
{
type: "Block",
children: List
}
```
### Brackets
```js
{
type: "Brackets",
children: List
}
```
### CDC
```js
{
type: "CDC"
}
```
### CDO
```js
{
type: "CDO"
}
```
### ClassSelector
```js
{
type: "ClassSelector",
name: String
}
```
### Combinator
```js
{
type: "Combinator",
name: String
}
```
### Comment
```js
{
type: "Comment",
value: String
}
```
### Declaration
```js
{
type: "Declaration",
important: Boolean | String,
property: String,
value: <Value> | <Raw>
}
```
### DeclarationList
```js
{
type: "DeclarationList",
children: List
}
```
### Dimension
```js
{
type: "Dimension",
value: String,
unit: String
}
```
### Function
```js
{
type: "Function",
name: String,
children: List
}
```
### HexColor
```js
{
type: "HexColor",
value: String
}
```
### IdSelector
```js
{
type: "IdSelector",
name: String
}
```
### Identifier
```js
{
type: "Identifier",
name: String
}
```
### MediaFeature
```js
{
type: "MediaFeature",
name: String,
value: <Identifier> | <Number> | <Dimension> | <Ratio> | null
}
```
### MediaQuery
```js
{
type: "MediaQuery",
children: List
}
```
### MediaQueryList
```js
{
type: "MediaQueryList",
children: List
}
```
### Nth
```js
{
type: "Nth",
nth: <AnPlusB> | <Identifier>,
selector: <SelectorList> | null
}
```
### Number
```js
{
type: "Number",
value: String
}
```
### Operator
```js
{
type: "Operator",
value: String
}
```
### Parentheses
```js
{
type: "Parentheses",
children: List
}
```
### Percentage
```js
{
type: "Percentage",
value: String
}
```
### PseudoClassSelector
```js
{
type: "PseudoClassSelector",
name: String,
children: List | null
}
```
### PseudoElementSelector
```js
{
type: "PseudoElementSelector",
name: String,
children: List | null
}
```
### Ratio
```js
{
type: "Ratio",
left: String,
right: String
}
```
### Raw
A sequence of any characters. This node type is used for unparsed fragments of CSS, e.g. due to parse error or parser settings, and for quirk parts like content of some functions, such as `url()` or `expression()`.
```js
{
type: "Raw",
value: String
}
```
### Rule
```js
{
type: "Rule",
prelude: <SelectorList> | <Raw>,
block: <Block>
}
```
### Selector
```js
{
type: "Selector",
children: List
}
```
### SelectorList
```js
{
type: "SelectorList",
children: List
}
```
### String
A sequence of characters enclosed in double quotes or single quotes.
```js
{
type: "String",
value: String
}
```
### StyleSheet
```js
{
type: "StyleSheet",
children: List
}
```
### TypeSelector
```js
{
type: "TypeSelector",
name: String
}
```
### UnicodeRange
Used for [the Unicode-Range microsyntax](https://drafts.csswg.org/css-syntax/#urange).
```js
{
type: "UnicodeRange",
value: String
}
```
### Url
```js
{
type: "Url",
value: <String> | <Raw>
}
```
### Value
```js
{
type: "Value",
children: List
}
```
### WhiteSpace
A sequence of one or more white spaces, i.e. ` ` (space), `\t`, `\r`, `\n` and `\f`.
```js
{
type: "WhiteSpace",
value: String
}
```
<!-- /node types -->