Don't reject out of bound calc() values at parsing-time

https://bugzilla.mozilla.org/show_bug.cgi?id=1355014
This commit is contained in:
Anthony Ramine 2017-04-12 11:46:22 +02:00
parent 12d46e7d01
commit dfbd89860a
5 changed files with 70 additions and 29 deletions

View file

@ -150,6 +150,7 @@ macro_rules! __define_css_keyword_enum__actual {
/// 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)]
@ -175,13 +176,47 @@ pub mod specified {
/// Clamp the value following the rules of this numeric type.
#[inline]
pub fn clamp(&self, val: Au) -> Au {
use std::cmp;
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,
}
}
}
}