Skip to main content

oxc_css_parser/parser/
postcss_simple_vars.rs

1use super::Parser;
2use crate::{Parse, ast::*, config::Syntax, error::PResult, pos::Span, tokenizer::Token};
3
4impl<'a> Parse<'a> for PostcssSimpleVar<'a> {
5    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
6        debug_assert!(input.syntax == Syntax::Css && input.options.allow_postcss_simple_vars);
7
8        let (name, span) = input.parse_dollar_var_ident()?;
9        Ok(PostcssSimpleVar { name, span })
10    }
11}
12
13impl<'a> Parse<'a> for PostcssSimpleVarDeclaration<'a> {
14    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
15        debug_assert!(input.syntax == Syntax::Css && input.options.allow_postcss_simple_vars);
16
17        let name = input.parse::<PostcssSimpleVar>()?;
18        let (_, colon_span) = input.cursor.expect_colon()?;
19        let mut value = input.parse_declaration_value()?;
20        // postcss-simple-vars is textual substitution; `!important` is just part
21        // of the value, not a structural declaration modifier (unlike CSS's
22        // `Declaration.important`). Keep it in the value stream.
23        if let Token::Exclamation(..) = &input.cursor.peek()?.token {
24            let important = input.parse::<ImportantAnnotation>()?;
25            value.push(ComponentValue::ImportantAnnotation(important));
26        }
27
28        let end = value.last().map(|v| v.span().end).unwrap_or(colon_span.end);
29        let span = Span { start: name.span.start, end };
30
31        Ok(PostcssSimpleVarDeclaration { name, colon_span, value, span })
32    }
33}