Skip to main content

oxc_css_parser/parser/at_rule/
document.rs

1use super::Parser;
2use crate::{Parse, ast::*, error::PResult};
3
4// https://developer.mozilla.org/en-US/docs/Web/CSS/@document
5impl<'a> Parse<'a> for DocumentPrelude<'a> {
6    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
7        let first = input.parse::<DocumentPreludeMatcher>()?;
8        let mut span = first.span().clone();
9
10        let mut matchers = input.vec1(first);
11        let mut comma_spans = input.vec();
12        while let Some((_, comma_span)) = input.cursor.eat_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 { matchers, comma_spans, span })
22    }
23}
24
25impl<'a> Parse<'a> for DocumentPreludeMatcher<'a> {
26    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
27        if let Ok(url) = input.try_parse(Url::parse) {
28            Ok(DocumentPreludeMatcher::Url(url))
29        } else {
30            input.parse().map(DocumentPreludeMatcher::Function)
31        }
32    }
33}