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.
- Example
- Common node's properties
- Node types
- AnPlusB
- Atrule
- AtrulePrelude
- AttributeSelector
- Block
- Brackets
- CDC
- CDO
- ClassSelector
- Combinator
- Comment
- Declaration
- DeclarationList
- Dimension
- Function
- Hash
- IdSelector
- Identifier
- MediaFeature
- MediaQuery
- MediaQueryList
- Nth
- Number
- Operator
- Parentheses
- Percentage
- PseudoClassSelector
- PseudoElementSelector
- Ratio
- Raw
- Rule
- Selector
- SelectorList
- String
- StyleSheet
- TypeSelector
- UnicodeRange
- Url
- Value
- WhiteSpace
Assume we have a CSS:
body {
color: red;
}
An AST for this CSS might look like:
{
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 theList
class.
An AST structure (i.e. details level, include positions or not) is depend on options passed to parser. See Parsing CSS into AST for details.
All nodes have the following properties.
Type: String
Indicates the type of a node. The possible values are the ones listed in the Node types below.
Type: Object
or null
Information about the position in the source string that corresponds to the node. It has the following structure:
{
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.
Type: List
or null
Only certain types of nodes can contain this property, such as StyleSheet
or 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.
NOTE: Despite every node has a
loc
property, this property is excluded from definitions to reduce a noise.
Used for the An+B microsyntax.
{
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
.
{
type: "Atrule",
name: String,
prelude: <AtrulePrelude> | <Raw> | null,
block: <Block> | null
}
{
type: "AtrulePrelude",
children: List
}
{
type: "AttributeSelector",
name: <Identifier>,
matcher: String | null,
value: <String> | <Identifier> | null,
flags: String | null
}
{
type: "Block",
children: List
}
{
type: "Brackets",
children: List
}
{
type: "CDC"
}
{
type: "CDO"
}
{
type: "ClassSelector",
name: String
}
{
type: "Combinator",
name: String
}
{
type: "Comment",
value: String
}
{
type: "Declaration",
important: Boolean | String,
property: String,
value: <Value> | <Raw>
}
{
type: "DeclarationList",
children: List
}
{
type: "Dimension",
value: String,
unit: String
}
{
type: "Function",
name: String,
children: List
}
{
type: "Hash",
value: String
}
{
type: "IdSelector",
name: String
}
{
type: "Identifier",
name: String
}
{
type: "MediaFeature",
name: String,
value: <Identifier> | <Number> | <Dimension> | <Ratio> | null
}
{
type: "MediaQuery",
children: List
}
{
type: "MediaQueryList",
children: List
}
{
type: "Nth",
nth: <AnPlusB> | <Identifier>,
selector: <SelectorList> | null
}
{
type: "Number",
value: String
}
{
type: "Operator",
value: String
}
{
type: "Parentheses",
children: List
}
{
type: "Percentage",
value: String
}
{
type: "PseudoClassSelector",
name: String,
children: List | null
}
{
type: "PseudoElementSelector",
name: String,
children: List | null
}
{
type: "Ratio",
left: String,
right: String
}
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()
.
{
type: "Raw",
value: String
}
{
type: "Rule",
prelude: <SelectorList> | <Raw>,
block: <Block>
}
{
type: "Selector",
children: List
}
{
type: "SelectorList",
children: List
}
A sequence of characters enclosed in double quotes or single quotes.
{
type: "String",
value: String
}
{
type: "StyleSheet",
children: List
}
{
type: "TypeSelector",
name: String
}
Used for the Unicode-Range microsyntax.
{
type: "UnicodeRange",
value: String
}
{
type: "Url",
value: <String> | <Raw>
}
{
type: "Value",
children: List
}
A sequence of one or more white spaces, i.e.
(space), \t
, \r
, \n
and \f
.
{
type: "WhiteSpace",
value: String
}