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-06-09 15:00:45 +03:00
|
|
|
use cssparser::{UnicodeRange, serialize_string};
|
2017-06-22 11:19:51 +03:00
|
|
|
use std::fmt::{self, Write};
|
2016-11-07 08:26:08 +03:00
|
|
|
|
2017-06-07 18:55:08 +03:00
|
|
|
/// Serialises a value according to its CSS representation.
|
|
|
|
///
|
2017-06-09 15:00:45 +03:00
|
|
|
/// This trait is implemented for `str` and its friends, serialising the string
|
|
|
|
/// contents as a CSS quoted string.
|
|
|
|
///
|
2017-06-07 18:55:08 +03:00
|
|
|
/// This trait is derivable with `#[derive(ToCss)]`, with the following behaviour:
|
|
|
|
/// * unit variants get serialised as the `snake-case` representation
|
|
|
|
/// of their name;
|
|
|
|
/// * unit variants whose name starts with "Moz" or "Webkit" are prepended
|
|
|
|
/// with a "-";
|
|
|
|
/// * variants with fields get serialised as the space-separated serialisations
|
|
|
|
/// of their fields.
|
2016-11-07 08:26:08 +03:00
|
|
|
pub trait ToCss {
|
|
|
|
/// Serialize `self` in CSS syntax, writing to `dest`.
|
2017-06-22 11:19:51 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write;
|
2016-11-07 08:26:08 +03:00
|
|
|
|
|
|
|
/// 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-04-04 23:30:23 +03:00
|
|
|
impl<'a, T> ToCss for &'a T where T: ToCss + ?Sized {
|
2017-06-22 11:19:51 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write {
|
2017-04-04 23:30:23 +03:00
|
|
|
(*self).to_css(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 15:00:45 +03:00
|
|
|
impl ToCss for str {
|
|
|
|
#[inline]
|
2017-06-22 11:19:51 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write {
|
2017-06-09 15:00:45 +03:00
|
|
|
serialize_string(self, dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToCss for String {
|
|
|
|
#[inline]
|
2017-06-22 11:19:51 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write {
|
2017-06-09 15:00:45 +03:00
|
|
|
serialize_string(self, dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 11:19:51 +03:00
|
|
|
impl<T> ToCss for Option<T>
|
|
|
|
where
|
|
|
|
T: ToCss,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write {
|
|
|
|
self.as_ref().map_or(Ok(()), |value| value.to_css(dest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience wrapper to serialise CSS values separated by a given string.
|
|
|
|
pub struct SequenceWriter<'a, W> {
|
|
|
|
writer: TrackedWriter<W>,
|
|
|
|
separator: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, W> SequenceWriter<'a, W>
|
|
|
|
where
|
|
|
|
W: Write,
|
|
|
|
{
|
|
|
|
/// Create a new sequence writer.
|
|
|
|
#[inline]
|
|
|
|
pub fn new(writer: W, separator: &'a str) -> Self {
|
|
|
|
SequenceWriter {
|
|
|
|
writer: TrackedWriter::new(writer),
|
|
|
|
separator: separator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serialises a CSS value, writing any separator as necessary.
|
|
|
|
///
|
|
|
|
/// The separator is never written before any `item` produces any output,
|
|
|
|
/// and is written in subsequent calls only if the `item` produces some
|
|
|
|
/// output on its own again. This lets us handle `Option<T>` fields by
|
|
|
|
/// just not printing anything on `None`.
|
|
|
|
#[inline]
|
|
|
|
pub fn item<T>(&mut self, item: &T) -> fmt::Result
|
|
|
|
where
|
|
|
|
T: ToCss,
|
|
|
|
{
|
|
|
|
if self.writer.has_written {
|
|
|
|
item.to_css(&mut PrefixedWriter::new(&mut self.writer, self.separator))
|
|
|
|
} else {
|
|
|
|
item.to_css(&mut self.writer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TrackedWriter<W> {
|
|
|
|
writer: W,
|
|
|
|
has_written: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W> TrackedWriter<W>
|
|
|
|
where
|
|
|
|
W: Write,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn new(writer: W) -> Self {
|
|
|
|
TrackedWriter {
|
|
|
|
writer: writer,
|
|
|
|
has_written: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W> Write for TrackedWriter<W>
|
|
|
|
where
|
|
|
|
W: Write,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
if !s.is_empty() {
|
|
|
|
self.has_written = true;
|
|
|
|
}
|
|
|
|
self.writer.write_str(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write_char(&mut self, c: char) -> fmt::Result {
|
|
|
|
self.has_written = true;
|
|
|
|
self.writer.write_char(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PrefixedWriter<'a, W> {
|
|
|
|
writer: W,
|
|
|
|
prefix: Option<&'a str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, W> PrefixedWriter<'a, W>
|
|
|
|
where
|
|
|
|
W: Write,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn new(writer: W, prefix: &'a str) -> Self {
|
|
|
|
PrefixedWriter {
|
|
|
|
writer: writer,
|
|
|
|
prefix: Some(prefix),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, W> Write for PrefixedWriter<'a, W>
|
|
|
|
where
|
|
|
|
W: Write,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
if !s.is_empty() {
|
|
|
|
if let Some(prefix) = self.prefix.take() {
|
|
|
|
self.writer.write_str(prefix)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.writer.write_str(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write_char(&mut self, c: char) -> fmt::Result {
|
|
|
|
if let Some(prefix) = self.prefix.take() {
|
|
|
|
self.writer.write_str(prefix)?;
|
|
|
|
}
|
|
|
|
self.writer.write_char(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 13:08:11 +03:00
|
|
|
/// Type used as the associated type in the `OneOrMoreSeparated` trait on a
|
|
|
|
/// type to indicate that a serialized list of elements of this type is
|
|
|
|
/// separated by commas.
|
|
|
|
pub struct CommaSeparator;
|
2017-02-03 16:09:30 +03:00
|
|
|
|
2017-06-20 13:08:11 +03:00
|
|
|
/// Type used as the associated type in the `OneOrMoreSeparated` trait on a
|
|
|
|
/// type to indicate that a serialized list of elements of this type is
|
|
|
|
/// separated by spaces.
|
|
|
|
pub struct SpaceSeparator;
|
2017-02-15 14:58:38 +03:00
|
|
|
|
2017-06-20 13:08:11 +03:00
|
|
|
/// A trait satisfied by the types corresponding to separators.
|
|
|
|
pub trait Separator {
|
|
|
|
/// The separator string that the satisfying separator type corresponds to.
|
|
|
|
fn separator() -> &'static str;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Separator for CommaSeparator {
|
|
|
|
fn separator() -> &'static str {
|
|
|
|
", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Separator for SpaceSeparator {
|
|
|
|
fn separator() -> &'static str {
|
|
|
|
" "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait that indicates that satisfying separator types are comma separators.
|
|
|
|
/// This seems kind of redundant, but we aren't able to express type equality
|
|
|
|
/// constraints yet.
|
|
|
|
/// https://github.com/rust-lang/rust/issues/20041
|
|
|
|
pub trait IsCommaSeparator {}
|
|
|
|
|
|
|
|
impl IsCommaSeparator for CommaSeparator {}
|
|
|
|
|
|
|
|
/// Marker trait on T to automatically implement ToCss for Vec<T> when T's are
|
|
|
|
/// separated by some delimiter `delim`.
|
|
|
|
pub trait OneOrMoreSeparated {
|
|
|
|
/// Associated type indicating which separator is used.
|
|
|
|
type S: Separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OneOrMoreSeparated for UnicodeRange {
|
|
|
|
type S = CommaSeparator;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ToCss for Vec<T> where T: ToCss + OneOrMoreSeparated {
|
2017-06-22 11:19:51 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write {
|
2017-02-03 16:09:30 +03:00
|
|
|
let mut iter = self.iter();
|
|
|
|
iter.next().unwrap().to_css(dest)?;
|
|
|
|
for item in iter {
|
2017-06-20 13:08:11 +03:00
|
|
|
dest.write_str(<T as OneOrMoreSeparated>::S::separator())?;
|
2017-02-03 16:09:30 +03:00
|
|
|
item.to_css(dest)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 15:00:45 +03:00
|
|
|
impl<T> ToCss for Box<T> where T: ?Sized + ToCss {
|
2017-02-09 13:21:36 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
|
2017-06-22 11:19:51 +03:00
|
|
|
where W: Write,
|
2017-02-09 13:21:36 +03:00
|
|
|
{
|
|
|
|
(**self).to_css(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:26:08 +03:00
|
|
|
impl ToCss for Au {
|
2017-06-22 11:19:51 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write {
|
2016-11-07 08:26:08 +03:00
|
|
|
write!(dest, "{}px", self.to_f64_px())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_to_css_for_predefined_type {
|
|
|
|
($name: ty) => {
|
|
|
|
impl<'a> ToCss for $name {
|
2017-06-22 11:19:51 +03:00
|
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: Write {
|
2016-11-07 08:26:08 +03:00
|
|
|
::cssparser::ToCss::to_css(self, dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_to_css_for_predefined_type!(f32);
|
|
|
|
impl_to_css_for_predefined_type!(i32);
|
2017-05-19 01:09:15 +03:00
|
|
|
impl_to_css_for_predefined_type!(u16);
|
2016-11-07 08:26:08 +03:00
|
|
|
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 {
|
2017-04-17 04:26:18 +03:00
|
|
|
($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 ),* ]);
|
|
|
|
};
|
2015-09-04 15:46:11 +03:00
|
|
|
($name: ident: $( $css: expr => $variant: ident ),+,) => {
|
2017-04-17 04:26:18 +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 ),+) => {
|
2017-04-17 04:26:18 +03:00
|
|
|
__define_css_keyword_enum__add_optional_traits!($name [ $( $css => $variant ),+ ] []);
|
2016-06-22 17:43:20 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "servo")]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __define_css_keyword_enum__add_optional_traits {
|
2017-04-17 04:26:18 +03:00
|
|
|
($name: ident [ $( $css: expr => $variant: ident ),+ ]
|
|
|
|
[ $( $alias: expr => $alias_variant: ident),* ]) => {
|
2016-06-22 17:43:20 +03:00
|
|
|
__define_css_keyword_enum__actual! {
|
2017-04-17 04:26:18 +03:00
|
|
|
$name [ Deserialize, Serialize, HeapSizeOf ]
|
|
|
|
[ $( $css => $variant ),+ ]
|
|
|
|
[ $( $alias => $alias_variant ),* ]
|
2016-06-22 17:43:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "servo"))]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __define_css_keyword_enum__add_optional_traits {
|
2017-04-17 04:26:18 +03:00
|
|
|
($name: ident [ $( $css: expr => $variant: ident ),+ ] [ $( $alias: expr => $alias_variant: ident),* ]) => {
|
2016-06-22 17:43:20 +03:00
|
|
|
__define_css_keyword_enum__actual! {
|
2017-04-17 04:26:18 +03:00
|
|
|
$name [] [ $( $css => $variant ),+ ] [ $( $alias => $alias_variant ),* ]
|
2016-06-22 17:43:20 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __define_css_keyword_enum__actual {
|
2017-04-17 04:26:18 +03:00
|
|
|
($name: ident [ $( $derived_trait: ident),* ]
|
|
|
|
[ $( $css: expr => $variant: ident ),+ ]
|
|
|
|
[ $( $alias: expr => $alias_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.
|
2017-06-10 00:31:48 +03:00
|
|
|
pub fn parse<'i, 't>(input: &mut ::cssparser::Parser<'i, 't>)
|
|
|
|
-> Result<$name, $crate::ParseError<'i>> {
|
2017-02-26 13:19:32 +03:00
|
|
|
let ident = input.expect_ident()?;
|
2017-03-27 03:21:49 +03:00
|
|
|
Self::from_ident(&ident)
|
2017-06-10 00:31:48 +03:00
|
|
|
.map_err(|()| ::cssparser::ParseError::Basic(
|
|
|
|
::cssparser::BasicParseError::UnexpectedToken(
|
|
|
|
::cssparser::Token::Ident(ident))))
|
2017-03-27 03:21:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse this property from an already-tokenized identifier.
|
|
|
|
pub fn from_ident(ident: &str) -> Result<$name, ()> {
|
|
|
|
match_ignore_ascii_case! { ident,
|
2016-01-21 20:15:51 +03:00
|
|
|
$( $css => Ok($name::$variant), )+
|
2017-04-17 04:26:18 +03:00
|
|
|
$( $alias => Ok($name::$alias_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 {
|
2017-06-14 05:25:09 +03:00
|
|
|
use ParsingMode;
|
2016-09-02 19:10:01 +03:00
|
|
|
use app_units::Au;
|
2017-04-12 21:31:21 +03:00
|
|
|
use std::cmp;
|
2016-09-02 19:10:01 +03:00
|
|
|
|
2017-04-12 21:31:21 +03:00
|
|
|
/// Whether to allow negative lengths 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)]
|
2017-04-12 21:31:21 +03:00
|
|
|
pub enum AllowedLengthType {
|
|
|
|
/// Allow all kind of lengths.
|
|
|
|
All,
|
|
|
|
/// Allow only non-negative lengths.
|
|
|
|
NonNegative
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:06:19 +03:00
|
|
|
impl Default for AllowedLengthType {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
|
|
|
AllowedLengthType::All
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:31:21 +03:00
|
|
|
impl AllowedLengthType {
|
|
|
|
/// Whether value is valid for this allowed length type.
|
|
|
|
#[inline]
|
2017-06-14 05:25:09 +03:00
|
|
|
pub fn is_ok(&self, parsing_mode: ParsingMode, value: f32) -> bool {
|
|
|
|
if parsing_mode.allows_all_numeric_values() {
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-12 21:31:21 +03:00
|
|
|
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)]
|
2015-09-04 15:46:11 +03:00
|
|
|
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,
|
2017-04-12 21:31:21 +03:00
|
|
|
/// Allow only non-negative numeric values.
|
|
|
|
NonNegative,
|
|
|
|
/// Allow only numeric values greater or equal to 1.0.
|
|
|
|
AtLeastOne,
|
2015-09-04 15:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AllowedNumericType {
|
2017-04-12 21:31:21 +03:00
|
|
|
/// Whether the value fits the rules of this numeric type.
|
2015-09-04 15:46:11 +03:00
|
|
|
#[inline]
|
2017-06-14 05:25:09 +03:00
|
|
|
pub fn is_ok(&self, parsing_mode: ParsingMode, val: f32) -> bool {
|
|
|
|
if parsing_mode.allows_all_numeric_values() {
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-04 15:46:11 +03:00
|
|
|
match *self {
|
|
|
|
AllowedNumericType::All => true,
|
2017-04-12 21:31:21 +03:00
|
|
|
AllowedNumericType::NonNegative => val >= 0.0,
|
|
|
|
AllowedNumericType::AtLeastOne => val >= 1.0,
|
2015-09-04 15:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
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]
|
2017-04-12 21:31:21 +03:00
|
|
|
pub fn clamp(&self, val: f32) -> f32 {
|
2016-09-02 19:10:01 +03:00
|
|
|
match *self {
|
2017-04-12 21:31:21 +03:00
|
|
|
AllowedNumericType::NonNegative if val < 0. => 0.,
|
|
|
|
AllowedNumericType::AtLeastOne if val < 1. => 1.,
|
|
|
|
_ => val,
|
2016-09-02 19:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 15:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-04 23:30:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
/// Wrap CSS types for serialization with `write!` or `format!` macros.
|
|
|
|
/// Used by ToCss of SpecifiedOperation.
|
|
|
|
pub struct Css<T>(pub T);
|
|
|
|
|
|
|
|
impl<T: ToCss> fmt::Display for Css<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.to_css(f)
|
|
|
|
}
|
|
|
|
}
|