Bug 1369588 - AllowedNumericType.is_ok() takes ParingMode as well. r?emilio
And it returns true if ParsingMode.allows_all_numeric_values is true regardless
of AllowedNumericType itself.
MozReview-Commit-ID: 7TLzsKaqA2G
--- a/servo/components/style/values/specified/length.rs
+++ b/servo/components/style/values/specified/length.rs
@@ -710,30 +710,33 @@ impl ToCss for Percentage {
where W: fmt::Write,
{
write!(dest, "{}%", self.0 * 100.)
}
}
impl Percentage {
/// Parse a specific kind of percentage.
- pub fn parse_with_clamping_mode<'i, 't>(input: &mut Parser<'i, 't>,
- context: AllowedNumericType)
+ pub fn parse_with_clamping_mode<'i, 't>(context: &ParserContext,
+ input: &mut Parser<'i, 't>,
+ num_context: AllowedNumericType)
-> Result<Self, ParseError<'i>> {
match try!(input.next()) {
- Token::Percentage(ref value) if context.is_ok(value.unit_value) => {
+ Token::Percentage(ref value) if num_context.is_ok(context.parsing_mode, value.unit_value) => {
Ok(Percentage(value.unit_value))
}
t => Err(BasicParseError::UnexpectedToken(t).into())
}
}
/// Parses a percentage token, but rejects it if it's negative.
- pub fn parse_non_negative<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
- Self::parse_with_clamping_mode(input, AllowedNumericType::NonNegative)
+ pub fn parse_non_negative<'i, 't>(context: &ParserContext,
+ input: &mut Parser<'i, 't>)
+ -> Result<Self, ParseError<'i>> {
+ Self::parse_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
}
/// 0%
#[inline]
pub fn zero() -> Self {
Percentage(0.)
}
@@ -741,18 +744,18 @@ impl Percentage {
#[inline]
pub fn hundred() -> Self {
Percentage(1.)
}
}
impl Parse for Percentage {
#[inline]
- fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
- Self::parse_with_clamping_mode(input, AllowedNumericType::All)
+ fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
+ Self::parse_with_clamping_mode(context, input, AllowedNumericType::All)
}
}
impl ComputedValueAsSpecified for Percentage {}
/// A length or a percentage value.
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
--- a/servo/components/style/values/specified/mod.rs
+++ b/servo/components/style/values/specified/mod.rs
@@ -115,17 +115,17 @@ pub fn parse_number<'i, 't>(context: &Pa
}
/// Parse a `<number>` value, with a given clamping mode.
pub fn parse_number_with_clamping_mode<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>,
clamping_mode: AllowedNumericType)
-> Result<Number, ParseError<'i>> {
match try!(input.next()) {
- Token::Number(ref value) if clamping_mode.is_ok(value.value) => {
+ Token::Number(ref value) if clamping_mode.is_ok(context.parsing_mode, value.value) => {
Ok(Number {
value: value.value.min(f32::MAX).max(f32::MIN),
calc_clamping_mode: None,
})
},
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
let result = try!(input.parse_nested_block(|i| {
CalcNode::parse_number(context, i)
@@ -359,24 +359,26 @@ impl Time {
was_calc: true,
}
}
fn parse_with_clamping_mode<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>,
clamping_mode: AllowedNumericType)
-> Result<Self, ParseError<'i>> {
+ use style_traits::PARSING_MODE_DEFAULT;
+
match input.next() {
- Ok(Token::Dimension(ref value, ref unit)) if clamping_mode.is_ok(value.value) => {
+ Ok(Token::Dimension(ref value, ref unit)) if clamping_mode.is_ok(PARSING_MODE_DEFAULT, value.value) => {
Time::parse_dimension(value.value, &unit, /* from_calc = */ false)
.map_err(|()| StyleParseError::UnspecifiedError.into())
}
Ok(Token::Function(ref name)) if name.eq_ignore_ascii_case("calc") => {
match input.parse_nested_block(|i| CalcNode::parse_time(context, i)) {
- Ok(time) if clamping_mode.is_ok(time.seconds) => Ok(time),
+ Ok(time) if clamping_mode.is_ok(PARSING_MODE_DEFAULT, time.seconds) => Ok(time),
_ => Err(StyleParseError::UnspecifiedError.into()),
}
}
Ok(t) => Err(BasicParseError::UnexpectedToken(t).into()),
Err(e) => Err(e.into())
}
}
@@ -452,31 +454,23 @@ impl Number {
/// Returns the numeric value, clamped if needed.
pub fn get(&self) -> f32 {
self.calc_clamping_mode.map_or(self.value, |mode| mode.clamp(self.value))
}
#[allow(missing_docs)]
pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Number, ParseError<'i>> {
- if context.parsing_mode.allows_all_numeric_values() {
- parse_number(context, input)
- } else {
- parse_number_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
- }
+ parse_number_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
}
#[allow(missing_docs)]
pub fn parse_at_least_one<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Number, ParseError<'i>> {
- if context.parsing_mode.allows_all_numeric_values() {
- parse_number(context, input)
- } else {
- parse_number_with_clamping_mode(context, input, AllowedNumericType::AtLeastOne)
- }
+ parse_number_with_clamping_mode(context, input, AllowedNumericType::AtLeastOne)
}
}
impl ToComputedValue for Number {
type ComputedValue = CSSFloat;
#[inline]
fn to_computed_value(&self, _: &Context) -> CSSFloat { self.get() }
@@ -517,17 +511,17 @@ pub enum NumberOrPercentage {
no_viewport_percentage!(NumberOrPercentage);
impl NumberOrPercentage {
fn parse_with_clamping_mode<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>,
type_: AllowedNumericType)
-> Result<Self, ParseError<'i>> {
- if let Ok(per) = input.try(|i| Percentage::parse_with_clamping_mode(i, type_)) {
+ if let Ok(per) = input.try(|i| Percentage::parse_with_clamping_mode(context, i, type_)) {
return Ok(NumberOrPercentage::Percentage(per));
}
parse_number_with_clamping_mode(context, input, type_).map(NumberOrPercentage::Number)
}
/// Parse a non-negative number or percentage.
pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
--- a/servo/components/style_traits/values.rs
+++ b/servo/components/style_traits/values.rs
@@ -260,17 +260,20 @@ pub mod specified {
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 {
+ pub fn is_ok(&self, parsing_mode: ParsingMode, val: f32) -> bool {
+ if parsing_mode.allows_all_numeric_values() {
+ return true;
+ }
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.