2015-09-04 15:46:11 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-12-31 14:19:02 +03:00
|
|
|
//! Helper types and traits for the handling of CSS values.
|
|
|
|
|
2016-11-07 08:26:08 +03:00
|
|
|
use app_units::Au;
|
2017-02-15 14:58:38 +03:00
|
|
|
use cssparser::UnicodeRange;
|
2016-11-10 17:28:39 +03:00
|
|
|
use std::fmt;
|
2016-11-07 08:26:08 +03:00
|
|
|
|
2016-11-14 15:59:35 +03:00
|
|
|
/// The real `ToCss` trait can't be implemented for types in crates that don't
|
2016-11-07 08:26:08 +03:00
|
|
|
/// depend on each other.
|
|
|
|
pub trait ToCss {
|
|
|
|
/// Serialize `self` in CSS syntax, writing to `dest`.
|
|
|
|
fn to_css<W>(&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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 16:09:30 +03:00
|
|
|
/// Marker trait to automatically implement ToCss for Vec<T>.
|
|
|
|
pub trait OneOrMoreCommaSeparated {}
|
|
|
|
|
2017-02-15 14:58:38 +03:00
|
|
|
impl OneOrMoreCommaSeparated for UnicodeRange {}
|
|
|
|
|
2017-02-03 16:09:30 +03:00
|
|
|
impl<T> ToCss for Vec<T> where T: ToCss + OneOrMoreCommaSeparated {
|
|
|
|
fn to_css<W>(&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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 13:21:36 +03:00
|
|
|
impl<T: ToCss> ToCss for Box<T> {
|
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
|
|
|
|
where W: fmt::Write,
|
|
|
|
{
|
|
|
|
(**self).to_css(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:26:08 +03:00
|
|
|
impl ToCss for Au {
|
|
|
|
fn to_css<W>(&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<W>(&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);
|
2017-02-15 14:58:38 +03:00
|
|
|
impl_to_css_for_predefined_type!(::cssparser::UnicodeRange);
|
2016-11-07 08:26:08 +03:00
|
|
|
|
2015-09-04 15:46:11 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! define_css_keyword_enum {
|
|
|
|
($name: ident: $( $css: expr => $variant: ident ),+,) => {
|
2016-06-22 17:43:20 +03:00
|
|
|
__define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ]);
|
2015-09-04 15:46:11 +03:00
|
|
|
};
|
|
|
|
($name: ident: $( $css: expr => $variant: ident ),+) => {
|
2016-06-22 17:43:20 +03:00
|
|
|
__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 ),+ ]) => {
|
|
|
|
__define_css_keyword_enum__actual! {
|
|
|
|
$name [ Deserialize, Serialize, HeapSizeOf ] [ $( $css => $variant ),+ ]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "servo"))]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __define_css_keyword_enum__add_optional_traits {
|
|
|
|
($name: ident [ $( $css: expr => $variant: ident ),+ ]) => {
|
|
|
|
__define_css_keyword_enum__actual! {
|
|
|
|
$name [] [ $( $css => $variant ),+ ]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __define_css_keyword_enum__actual {
|
|
|
|
($name: ident [ $( $derived_trait: ident),* ] [ $( $css: expr => $variant: ident ),+ ]) => {
|
2016-12-31 14:19:02 +03:00
|
|
|
#[allow(non_camel_case_types, missing_docs)]
|
2017-02-24 06:19:34 +03:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq $(, $derived_trait )* )]
|
2015-09-04 15:46:11 +03:00
|
|
|
pub enum $name {
|
|
|
|
$( $variant ),+
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
2016-12-31 14:19:02 +03:00
|
|
|
/// Parse this property from a CSS input stream.
|
2015-09-04 15:46:11 +03:00
|
|
|
pub fn parse(input: &mut ::cssparser::Parser) -> Result<$name, ()> {
|
2017-02-26 13:19:32 +03:00
|
|
|
let ident = input.expect_ident()?;
|
|
|
|
match_ignore_ascii_case! { &ident,
|
2016-01-21 20:15:51 +03:00
|
|
|
$( $css => Ok($name::$variant), )+
|
2015-09-04 15:46:11 +03:00
|
|
|
_ => Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:26:08 +03:00
|
|
|
impl ToCss for $name {
|
2015-09-04 15:46:11 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> ::std::fmt::Result
|
2016-12-31 14:19:02 +03:00
|
|
|
where W: ::std::fmt::Write
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
$( $name::$variant => dest.write_str($css) ),+
|
2015-09-04 15:46:11 +03:00
|
|
|
}
|
2016-12-31 14:19:02 +03:00
|
|
|
}
|
2015-09-04 15:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 14:19:02 +03:00
|
|
|
/// Helper types for the handling of specified values.
|
2015-09-04 15:46:11 +03:00
|
|
|
pub mod specified {
|
2016-09-02 19:10:01 +03:00
|
|
|
use app_units::Au;
|
|
|
|
|
2016-12-31 14:19:02 +03:00
|
|
|
/// Whether to allow negative values or not.
|
2016-09-01 19:41:35 +03:00
|
|
|
#[repr(u8)]
|
|
|
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
2015-09-04 15:46:11 +03:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub enum AllowedNumericType {
|
2016-12-31 14:19:02 +03:00
|
|
|
/// Allow all kind of numeric values.
|
2015-09-04 15:46:11 +03:00
|
|
|
All,
|
2016-12-31 14:19:02 +03:00
|
|
|
/// Allow only non-negative values.
|
2015-09-04 15:46:11 +03:00
|
|
|
NonNegative
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AllowedNumericType {
|
2016-12-31 14:19:02 +03:00
|
|
|
/// Whether value is valid for this allowed numeric type.
|
2015-09-04 15:46:11 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn is_ok(&self, value: f32) -> bool {
|
|
|
|
match *self {
|
|
|
|
AllowedNumericType::All => true,
|
|
|
|
AllowedNumericType::NonNegative => value >= 0.,
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 19:10:01 +03:00
|
|
|
|
2016-12-31 14:19:02 +03:00
|
|
|
/// Clamp the value following the rules of this numeric type.
|
2016-09-02 19:10:01 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn clamp(&self, val: Au) -> Au {
|
|
|
|
use std::cmp;
|
|
|
|
match *self {
|
|
|
|
AllowedNumericType::All => val,
|
|
|
|
AllowedNumericType::NonNegative => cmp::max(Au(0), val),
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 15:46:11 +03:00
|
|
|
}
|
|
|
|
}
|