/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ //! Helper types and traits for the handling of CSS values. use app_units::Au; use cssparser::UnicodeRange; use std::fmt; /// The real `ToCss` trait can't be implemented for types in crates that don't /// depend on each other. pub trait ToCss { /// Serialize `self` in CSS syntax, writing to `dest`. fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write; /// Serialize `self` in CSS syntax and return a string. /// /// (This is a convenience wrapper for `to_css` and probably should not be overridden.) #[inline] fn to_css_string(&self) -> String { let mut s = String::new(); self.to_css(&mut s).unwrap(); s } } impl<'a, T> ToCss for &'a T where T: ToCss + ?Sized { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { (*self).to_css(dest) } } /// Marker trait to automatically implement ToCss for Vec. pub trait OneOrMoreCommaSeparated {} impl OneOrMoreCommaSeparated for UnicodeRange {} impl ToCss for Vec where T: ToCss + OneOrMoreCommaSeparated { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { let mut iter = self.iter(); iter.next().unwrap().to_css(dest)?; for item in iter { dest.write_str(", ")?; item.to_css(dest)?; } Ok(()) } } impl ToCss for Box { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write, { (**self).to_css(dest) } } impl ToCss for Au { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { write!(dest, "{}px", self.to_f64_px()) } } macro_rules! impl_to_css_for_predefined_type { ($name: ty) => { impl<'a> ToCss for $name { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { ::cssparser::ToCss::to_css(self, dest) } } }; } impl_to_css_for_predefined_type!(f32); impl_to_css_for_predefined_type!(i32); impl_to_css_for_predefined_type!(u32); impl_to_css_for_predefined_type!(::cssparser::Token<'a>); impl_to_css_for_predefined_type!(::cssparser::RGBA); impl_to_css_for_predefined_type!(::cssparser::Color); impl_to_css_for_predefined_type!(::cssparser::UnicodeRange); #[macro_export] macro_rules! define_css_keyword_enum { ($name: ident: values { $( $css: expr => $variant: ident),+, } aliases { $( $alias: expr => $alias_variant: ident ),+, }) => { __define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ] [ $( $alias => $alias_variant ),+ ]); }; ($name: ident: values { $( $css: expr => $variant: ident),+, } aliases { $( $alias: expr => $alias_variant: ident ),* }) => { __define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ] [ $( $alias => $alias_variant ),* ]); }; ($name: ident: values { $( $css: expr => $variant: ident),+ } aliases { $( $alias: expr => $alias_variant: ident ),+, }) => { __define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ] [ $( $alias => $alias_variant ),+ ]); }; ($name: ident: values { $( $css: expr => $variant: ident),+ } aliases { $( $alias: expr => $alias_variant: ident ),* }) => { __define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ] [ $( $alias => $alias_variant ),* ]); }; ($name: ident: $( $css: expr => $variant: ident ),+,) => { __define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ] []); }; ($name: ident: $( $css: expr => $variant: ident ),+) => { __define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ] []); }; } #[cfg(feature = "servo")] #[macro_export] macro_rules! __define_css_keyword_enum__add_optional_traits { ($name: ident [ $( $css: expr => $variant: ident ),+ ] [ $( $alias: expr => $alias_variant: ident),* ]) => { __define_css_keyword_enum__actual! { $name [ Deserialize, Serialize, HeapSizeOf ] [ $( $css => $variant ),+ ] [ $( $alias => $alias_variant ),* ] } }; } #[cfg(not(feature = "servo"))] #[macro_export] macro_rules! __define_css_keyword_enum__add_optional_traits { ($name: ident [ $( $css: expr => $variant: ident ),+ ] [ $( $alias: expr => $alias_variant: ident),* ]) => { __define_css_keyword_enum__actual! { $name [] [ $( $css => $variant ),+ ] [ $( $alias => $alias_variant ),* ] } }; } #[macro_export] macro_rules! __define_css_keyword_enum__actual { ($name: ident [ $( $derived_trait: ident),* ] [ $( $css: expr => $variant: ident ),+ ] [ $( $alias: expr => $alias_variant: ident ),* ]) => { #[allow(non_camel_case_types, missing_docs)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq $(, $derived_trait )* )] pub enum $name { $( $variant ),+ } impl $name { /// Parse this property from a CSS input stream. pub fn parse(input: &mut ::cssparser::Parser) -> Result<$name, ()> { let ident = input.expect_ident()?; Self::from_ident(&ident) } /// Parse this property from an already-tokenized identifier. pub fn from_ident(ident: &str) -> Result<$name, ()> { match_ignore_ascii_case! { ident, $( $css => Ok($name::$variant), )+ $( $alias => Ok($name::$alias_variant), )* _ => Err(()) } } } impl ToCss for $name { fn to_css(&self, dest: &mut W) -> ::std::fmt::Result where W: ::std::fmt::Write { match *self { $( $name::$variant => dest.write_str($css) ),+ } } } } } /// Helper types for the handling of specified values. pub mod specified { use app_units::Au; use std::cmp; /// Whether to allow negative lengths or not. #[repr(u8)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum AllowedLengthType { /// Allow all kind of lengths. All, /// Allow only non-negative lengths. NonNegative } impl AllowedLengthType { /// Whether value is valid for this allowed length type. #[inline] pub fn is_ok(&self, value: f32) -> bool { match *self { AllowedLengthType::All => true, AllowedLengthType::NonNegative => value >= 0., } } /// Clamp the value following the rules of this numeric type. #[inline] pub fn clamp(&self, val: Au) -> Au { match *self { AllowedLengthType::All => val, AllowedLengthType::NonNegative => cmp::max(Au(0), val), } } } /// Whether to allow negative lengths or not. #[repr(u8)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd)] pub enum AllowedNumericType { /// Allow all kind of numeric values. All, /// Allow only non-negative numeric values. NonNegative, /// Allow only numeric values greater or equal to 1.0. AtLeastOne, } impl AllowedNumericType { /// Whether the value fits the rules of this numeric type. #[inline] pub fn is_ok(&self, val: f32) -> bool { match *self { AllowedNumericType::All => true, AllowedNumericType::NonNegative => val >= 0.0, AllowedNumericType::AtLeastOne => val >= 1.0, } } /// Clamp the value following the rules of this numeric type. #[inline] pub fn clamp(&self, val: f32) -> f32 { match *self { AllowedNumericType::NonNegative if val < 0. => 0., AllowedNumericType::AtLeastOne if val < 1. => 1., _ => val, } } } } /// Wrap CSS types for serialization with `write!` or `format!` macros. /// Used by ToCss of SpecifiedOperation. pub struct Css(pub T); impl fmt::Display for Css { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.to_css(f) } }