lewp_css/domain/
custom_ident.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    super::Atom,
6    crate::CustomParseError,
7    cssparser::{CowRcStr, ParseError, ToCss},
8    std::fmt,
9};
10
11/// <https://drafts.csswg.org/css-values-4/#custom-idents>
12#[derive(Clone, Debug, Eq, Hash, PartialEq)]
13pub struct CustomIdent(pub Atom);
14
15impl CustomIdent {
16    /// Parse an already-tokenizer identifier
17    pub(crate) fn from_ident<'i>(
18        ident: &CowRcStr<'i>,
19        excluding: &[&str],
20    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
21        match_ignore_ascii_case! {
22            ident,
23            "initial" | "inherit" | "unset" | "default" => return Err(ParseError::from(CustomParseError::UnexpectedCustomIdent(ident.clone()))),
24            _ =>
25            {
26            }
27        };
28
29        if excluding.iter().any(|s| ident.eq_ignore_ascii_case(s)) {
30            Err(ParseError::from(CustomParseError::CustomIdentWasExcluded(
31                ident.clone(),
32            )))
33        } else {
34            Ok(CustomIdent(Atom::from(ident)))
35        }
36    }
37}
38
39impl ToCss for CustomIdent {
40    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
41        self.0.to_css(dest)
42    }
43}