Skip to main content

oxc_css_parser/parser/at_rule/
layer.rs

1use super::Parser;
2use crate::{
3    Parse,
4    ast::*,
5    error::{Error, PResult},
6    pos::Span,
7    tokenizer::{Token, TokenWithSpan},
8    util,
9};
10
11// https://drafts.csswg.org/css-cascade-5/#layering
12impl<'a> Parse<'a> for LayerNames<'a> {
13    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
14        let first = input.parse::<LayerName>()?;
15        let mut span = first.span.clone();
16
17        let mut names = input.vec1(first);
18        let mut comma_spans = input.vec();
19        while let Some((_, comma_span)) = input.cursor.eat_comma()? {
20            comma_spans.push(comma_span);
21            names.push(input.parse()?);
22        }
23
24        if let Some(last) = names.last() {
25            span.end = last.span.end;
26        }
27        Ok(LayerNames { names, comma_spans, span })
28    }
29}
30
31// https://drafts.csswg.org/css-cascade-5/#layer-names
32impl<'a> Parse<'a> for LayerName<'a> {
33    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
34        let first = input.parse::<InterpolableIdent>()?;
35        let start = first.span().start;
36        let mut end = first.span().end;
37
38        let mut idents = input.vec1(first);
39        while let TokenWithSpan { token: Token::Dot(..), span } = input.cursor.peek()? {
40            if span.start == end {
41                let span = input.cursor.bump()?.span;
42                let ident = input.parse::<InterpolableIdent>()?;
43                util::assert_no_ws_or_comment(&span, ident.span())?;
44                end = ident.span().end;
45                idents.push(ident);
46            } else {
47                break;
48            }
49        }
50
51        let invalid_ident = idents.iter().find(|ident| match &ident {
52            InterpolableIdent::Literal(ident) => util::is_css_wide_keyword(ident.name),
53            _ => false,
54        });
55        if let Some(invalid_ident) = invalid_ident {
56            input.recoverable_errors.push(Error {
57                kind: crate::error::ErrorKind::CSSWideKeywordDisallowed,
58                span: invalid_ident.span().clone(),
59            });
60        }
61
62        let span = Span { start, end };
63        Ok(LayerName { idents, span })
64    }
65}