File: ast.md

package info (click to toggle)
node-css-tree 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,184 kB
  • sloc: javascript: 17,452; makefile: 5
file content (670 lines) | stat: -rw-r--r-- 12,579 bytes parent folder | download
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# AST format

CSSTree's AST consists of nodes (leaves). Each node is an object with a set of properties that depends on node's type. Nodes can refer 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)
    - [Condition](#condition)
    - [Declaration](#declaration)
    - [DeclarationList](#declarationlist)
    - [Dimension](#dimension)
    - [Feature](#feature)
    - [FeatureFunction](#featurefunction)
    - [FeatureRange](#featurerange)
    - [Function](#function)
    - [GeneralEnclosed](#generalenclosed)
    - [Hash](#hash)
    - [IdSelector](#idselector)
    - [Identifier](#identifier)
    - [Layer](#layer)
    - [LayerList](#layerlist)
    - [MediaQuery](#mediaquery)
    - [MediaQueryList](#mediaquerylist)
    - [NestingSelector](#nestingselector)
    - [Nth](#nth)
    - [Number](#number)
    - [Operator](#operator)
    - [Parentheses](#parentheses)
    - [Percentage](#percentage)
    - [PseudoClassSelector](#pseudoclassselector)
    - [PseudoElementSelector](#pseudoelementselector)
    - [Ratio](#ratio)
    - [Raw](#raw)
    - [Rule](#rule)
    - [Scope](#scope)
    - [Selector](#selector)
    - [SelectorList](#selectorlist)
    - [String](#string)
    - [StyleSheet](#stylesheet)
    - [SupportsDeclaration](#supportsdeclaration)
    - [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).

```ts
type AnPlusB = {
    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

```ts
type Atrule = {
    type: "Atrule";
    name: string;
    prelude: AtrulePrelude | Raw | null;
    block: Block | null;
}
```

### AtrulePrelude

```ts
type AtrulePrelude = {
    type: "AtrulePrelude";
    children: List<any>;
}
```

### AttributeSelector

```ts
type AttributeSelector = {
    type: "AttributeSelector";
    name: Identifier;
    matcher: string | null;
    value: String | Identifier | null;
    flags: string | null;
}
```

### Block

```ts
type Block = {
    type: "Block";
    children: List<Atrule | Rule | Declaration>;
}
```

### Brackets

```ts
type Brackets = {
    type: "Brackets";
    children: List<any>;
}
```

### CDC

```ts
type CDC = {
    type: "CDC";
}
```

### CDO

```ts
type CDO = {
    type: "CDO";
}
```

### ClassSelector

```ts
type ClassSelector = {
    type: "ClassSelector";
    name: string;
}
```

### Combinator

```ts
type Combinator = {
    type: "Combinator";
    name: string;
}
```

### Comment

```ts
type Comment = {
    type: "Comment";
    value: string;
}
```

### Condition

```ts
type Condition = {
    type: "Condition";
    kind: string;
    children: List<Identifier | Feature | FeatureFunction | FeatureRange | SupportsDeclaration>;
}
```

### Declaration

```ts
type Declaration = {
    type: "Declaration";
    important: boolean | string;
    property: string;
    value: Value | Raw;
}
```

### DeclarationList

```ts
type DeclarationList = {
    type: "DeclarationList";
    children: List<Declaration | Atrule | Rule>;
}
```

### Dimension

```ts
type Dimension = {
    type: "Dimension";
    value: string;
    unit: string;
}
```

### Feature

```ts
type Feature = {
    type: "Feature";
    kind: string;
    name: string;
    value: Identifier | Number | Dimension | Ratio | Function | null;
}
```

### FeatureFunction

```ts
type FeatureFunction = {
    type: "FeatureFunction";
    kind: string;
    feature: string;
    value: Declaration | Selector;
}
```

### FeatureRange

```ts
type FeatureRange = {
    type: "FeatureRange";
    kind: string;
    left: Identifier | Number | Dimension | Ratio | Function;
    leftComparison: string;
    middle: Identifier | Number | Dimension | Ratio | Function;
    rightComparison: string | null;
    right: Identifier | Number | Dimension | Ratio | Function | null;
}
```

### Function

```ts
type Function = {
    type: "Function";
    name: string;
    children: List<any>;
}
```

### GeneralEnclosed

```ts
type GeneralEnclosed = {
    type: "GeneralEnclosed";
    kind: string;
    function: string | null;
    children: List<any>;
}
```

### Hash

```ts
type Hash = {
    type: "Hash";
    value: string;
}
```

### IdSelector

```ts
type IdSelector = {
    type: "IdSelector";
    name: string;
}
```

### Identifier

```ts
type Identifier = {
    type: "Identifier";
    name: string;
}
```

### Layer

```ts
type Layer = {
    type: "Layer";
    name: string;
}
```

### LayerList

```ts
type LayerList = {
    type: "LayerList";
    children: List<Layer>;
}
```

### MediaQuery

```ts
type MediaQuery = {
    type: "MediaQuery";
    modifier: string | null;
    mediaType: string | null;
    condition: Condition | null;
}
```

### MediaQueryList

```ts
type MediaQueryList = {
    type: "MediaQueryList";
    children: List<MediaQuery>;
}
```

### NestingSelector

```ts
type NestingSelector = {
    type: "NestingSelector";
}
```

### Nth

```ts
type Nth = {
    type: "Nth";
    nth: AnPlusB | Identifier;
    selector: SelectorList | null;
}
```

### Number

```ts
type Number = {
    type: "Number";
    value: string;
}
```

### Operator

```ts
type Operator = {
    type: "Operator";
    value: string;
}
```

### Parentheses

```ts
type Parentheses = {
    type: "Parentheses";
    children: List<any>;
}
```

### Percentage

```ts
type Percentage = {
    type: "Percentage";
    value: string;
}
```

### PseudoClassSelector

```ts
type PseudoClassSelector = {
    type: "PseudoClassSelector";
    name: string;
    children: List<Raw> | null;
}
```

### PseudoElementSelector

```ts
type PseudoElementSelector = {
    type: "PseudoElementSelector";
    name: string;
    children: List<Raw> | null;
}
```

### Ratio

```ts
type Ratio = {
    type: "Ratio";
    left: Number | Function;
    right: Number | Function | null;
}
```

### 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()`.

```ts
type Raw = {
    type: "Raw";
    value: string;
}
```

### Rule

```ts
type Rule = {
    type: "Rule";
    prelude: SelectorList | Raw;
    block: Block;
}
```

### Scope

```ts
type Scope = {
    type: "Scope";
    root: SelectorList | Raw | null;
    limit: SelectorList | Raw | null;
}
```

### Selector

```ts
type Selector = {
    type: "Selector";
    children: List<TypeSelector | IdSelector | ClassSelector | AttributeSelector | PseudoClassSelector | PseudoElementSelector | Combinator>;
}
```

### SelectorList

```ts
type SelectorList = {
    type: "SelectorList";
    children: List<Selector | Raw>;
}
```

### String

A sequence of characters enclosed in double quotes or single quotes.

```ts
type String = {
    type: "String";
    value: string;
}
```

### StyleSheet

```ts
type StyleSheet = {
    type: "StyleSheet";
    children: List<Comment | CDO | CDC | Atrule | Rule | Raw>;
}
```

### SupportsDeclaration

```ts
type SupportsDeclaration = {
    type: "SupportsDeclaration";
    declaration: Declaration;
}
```

### TypeSelector

```ts
type TypeSelector = {
    type: "TypeSelector";
    name: string;
}
```

### UnicodeRange

Used for [the Unicode-Range microsyntax](https://drafts.csswg.org/css-syntax/#urange).

```ts
type UnicodeRange = {
    type: "UnicodeRange";
    value: string;
}
```

### Url

```ts
type Url = {
    type: "Url";
    value: string;
}
```

### Value

```ts
type Value = {
    type: "Value";
    children: List<any>;
}
```

### WhiteSpace

A sequence of one or more white spaces, i.e. ` ` (space), `\t`, `\r`, `\n` and `\f`.

```ts
type WhiteSpace = {
    type: "WhiteSpace";
    value: string;
}
```

<!-- /node types -->