Skip to main content

oxc_css_parser/parser/at_rule/
document.rs

1use super::Parser;
2use crate::{Parse, Spanned, ast::*, eat, error::PResult};
3
4// https://developer.mozilla.org/en-US/docs/Web/CSS/@document
5impl<'cmt, 's: 'cmt> Parse<'cmt, 's> for DocumentPrelude<'s> {
6    fn parse(input: &mut Parser<'cmt, 's>) -> PResult<Self> {
7        let first = input.parse::<DocumentPreludeMatcher>()?;
8        let mut span = first.span().clone();
9
10        let mut matchers = vec![first];
11        let mut comma_spans = vec![];
12        while let Some((_, comma_span)) = eat!(input, Comma) {
13            comma_spans.push(comma_span);
14            matchers.push(input.parse()?);
15        }
16        debug_assert_eq!(comma_spans.len() + 1, matchers.len());
17
18        if let Some(last) = matchers.last() {
19            span.end = last.span().end;
20        }
21        Ok(DocumentPrelude {
22            matchers,
23            comma_spans,
24            span,
25        })
26    }
27}
28
29impl<'cmt, 's: 'cmt> Parse<'cmt, 's> for DocumentPreludeMatcher<'s> {
30    fn parse(input: &mut Parser<'cmt, 's>) -> PResult<Self> {
31        if let Ok(url) = input.try_parse(Url::parse) {
32            Ok(DocumentPreludeMatcher::Url(url))
33        } else {
34            input.parse().map(DocumentPreludeMatcher::Function)
35        }
36    }
37}