Introduce a style::values::CustomIdent type

This commit is contained in:
Simon Sapin 2017-04-14 05:24:04 +02:00
parent 5daf92dd5a
commit 4993a80074
2 changed files with 32 additions and 1 deletions

View file

@ -444,6 +444,7 @@ impl CSSWideKeyword {
/// to a CSSWideKeyword.
pub fn from_ident<'i>(ident: &Cow<'i, str>) -> Option<Self> {
match_ignore_ascii_case! { ident,
// If modifying this set of keyword, also update values::CustomIdent::from_ident
"initial" => Some(CSSWideKeyword::Initial),
"inherit" => Some(CSSWideKeyword::Inherit),
"unset" => Some(CSSWideKeyword::Unset),

View file

@ -8,8 +8,11 @@
#![deny(missing_docs)]
pub use cssparser::{RGBA, Parser};
use Atom;
pub use cssparser::{RGBA, Parser, serialize_identifier};
use parser::{Parse, ParserContext};
use std::ascii::AsciiExt;
use std::borrow::Cow;
use std::fmt::{self, Debug};
use style_traits::ToCss;
@ -211,6 +214,33 @@ impl<A: ToComputedValue, B: ToComputedValue> ToComputedValue for Either<A, B> {
}
}
/// https://drafts.csswg.org/css-values-4/#custom-idents
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct CustomIdent(pub Atom);
impl CustomIdent {
/// Parse an already-tokenizer identifier
pub fn from_ident(ident: Cow<str>, excluding: &[&str]) -> Result<Self, ()> {
match_ignore_ascii_case! { &ident,
"initial" | "inherit" | "unset" | "default" => return Err(()),
_ => {}
};
if excluding.iter().any(|s| ident.eq_ignore_ascii_case(s)) {
Err(())
} else {
Ok(CustomIdent(ident.into()))
}
}
}
impl ToCss for CustomIdent {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
serialize_identifier(&self.0.to_string(), dest)
}
}
// A type for possible values for min- and max- flavors of width,
// height, block-size, and inline-size.
define_css_keyword_enum!(ExtremumLength: