From 4993a80074f1fe3d69c732e65a67480349c48d25 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 14 Apr 2017 05:24:04 +0200 Subject: [PATCH] Introduce a style::values::CustomIdent type --- .../style/properties/properties.mako.rs | 1 + components/style/values/mod.rs | 32 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 0b34c5193e2..a0234c52bad 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -444,6 +444,7 @@ impl CSSWideKeyword { /// to a CSSWideKeyword. pub fn from_ident<'i>(ident: &Cow<'i, str>) -> Option { 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), diff --git a/components/style/values/mod.rs b/components/style/values/mod.rs index 2b371b74da9..7379cbad846 100644 --- a/components/style/values/mod.rs +++ b/components/style/values/mod.rs @@ -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 ToComputedValue for Either { } } +/// 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, excluding: &[&str]) -> Result { + 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(&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: