Bug 1369588 - Make AllowedLengthType.is_ok() returning true if parsing mode allows all numeric values. r?emilio draft
authorHiroyuki Ikezoe <hikezoe@mozilla.com>
Tue, 13 Jun 2017 11:04:31 +0900
changeset 592975 82f5f263c3a0f904a851e52786ba296fe8ac8ad2
parent 592974 37403dcf4c56673babbdd2b4ceca4b3a8313bbba
child 592976 c216ade22b0c22cedf5342137a4be89065e1f870
push id63561
push userhikezoe@mozilla.com
push dateTue, 13 Jun 2017 02:04:52 +0000
reviewersemilio
bugs1369588
milestone56.0a1
Bug 1369588 - Make AllowedLengthType.is_ok() returning true if parsing mode allows all numeric values. r?emilio Even if the type is NonNegative and the given value is a negative. MozReview-Commit-ID: 42oJCoTZboO
servo/components/style/values/specified/length.rs
servo/components/style_traits/values.rs
servo/components/style_traits/viewport.rs
--- a/servo/components/style/values/specified/length.rs
+++ b/servo/components/style/values/specified/length.rs
@@ -606,19 +606,19 @@ impl Length {
     #[inline]
     fn parse_internal<'i, 't>(context: &ParserContext,
                               input: &mut Parser<'i, 't>,
                               num_context: AllowedLengthType,
                               allow_quirks: AllowQuirks)
                               -> Result<Length, ParseError<'i>> {
         let token = try!(input.next());
         match token {
-            Token::Dimension(ref value, ref unit) if num_context.is_ok(value.value) =>
+            Token::Dimension(ref value, ref unit) if num_context.is_ok(context.parsing_mode, value.value) =>
                 Length::parse_dimension(context, value.value, unit),
-            Token::Number(ref value) if num_context.is_ok(value.value) => {
+            Token::Number(ref value) if num_context.is_ok(context.parsing_mode, value.value) => {
                 if value.value != 0. &&
                    !context.parsing_mode.allows_unitless_lengths() &&
                    !allow_quirks.allowed(context.quirks_mode) {
                     return Err(StyleParseError::UnspecifiedError.into())
                 }
                 Ok(Length::NoCalc(NoCalcLength::Absolute(AbsoluteLength::Px(value.value))))
             },
             Token::Function(ref name) if name.eq_ignore_ascii_case("calc") =>
@@ -796,21 +796,21 @@ impl LengthOrPercentage {
     fn parse_internal<'i, 't>(context: &ParserContext,
                               input: &mut Parser<'i, 't>,
                               num_context: AllowedLengthType,
                               allow_quirks: AllowQuirks)
                               -> Result<LengthOrPercentage, ParseError<'i>>
     {
         let token = try!(input.next());
         match token {
-            Token::Dimension(ref value, ref unit) if num_context.is_ok(value.value) =>
+            Token::Dimension(ref value, ref unit) if num_context.is_ok(context.parsing_mode, value.value) =>
                 NoCalcLength::parse_dimension(context, value.value, unit).map(LengthOrPercentage::Length),
-            Token::Percentage(ref value) if num_context.is_ok(value.unit_value) =>
+            Token::Percentage(ref value) if num_context.is_ok(context.parsing_mode, value.unit_value) =>
                 return Ok(LengthOrPercentage::Percentage(Percentage(value.unit_value))),
-            Token::Number(value) if num_context.is_ok(value.value) => {
+            Token::Number(value) if num_context.is_ok(context.parsing_mode, value.value) => {
                 if value.value != 0. &&
                    !context.parsing_mode.allows_unitless_lengths() &&
                    !allow_quirks.allowed(context.quirks_mode) {
                     Err(())
                 } else {
                     return Ok(LengthOrPercentage::Length(NoCalcLength::from_px(value.value)))
                 }
             }
@@ -930,21 +930,21 @@ impl From<Percentage> for LengthOrPercen
 impl LengthOrPercentageOrAuto {
     fn parse_internal<'i, 't>(context: &ParserContext,
                               input: &mut Parser<'i, 't>,
                               num_context: AllowedLengthType,
                               allow_quirks: AllowQuirks)
                               -> Result<Self, ParseError<'i>> {
         let token = try!(input.next());
         match token {
-            Token::Dimension(ref value, ref unit) if num_context.is_ok(value.value) =>
+            Token::Dimension(ref value, ref unit) if num_context.is_ok(context.parsing_mode, value.value) =>
                 NoCalcLength::parse_dimension(context, value.value, unit).map(LengthOrPercentageOrAuto::Length),
-            Token::Percentage(ref value) if num_context.is_ok(value.unit_value) =>
+            Token::Percentage(ref value) if num_context.is_ok(context.parsing_mode, value.unit_value) =>
                 Ok(LengthOrPercentageOrAuto::Percentage(Percentage(value.unit_value))),
-            Token::Number(ref value) if num_context.is_ok(value.value) => {
+            Token::Number(ref value) if num_context.is_ok(context.parsing_mode, value.value) => {
                 if value.value != 0. &&
                    !context.parsing_mode.allows_unitless_lengths() &&
                    !allow_quirks.allowed(context.quirks_mode) {
                     return Err(StyleParseError::UnspecifiedError.into())
                 }
                 Ok(LengthOrPercentageOrAuto::Length(
                     NoCalcLength::Absolute(AbsoluteLength::Px(value.value))
                 ))
@@ -1026,21 +1026,21 @@ impl LengthOrPercentageOrNone {
     fn parse_internal<'i, 't>(context: &ParserContext,
                               input: &mut Parser<'i, 't>,
                               num_context: AllowedLengthType,
                               allow_quirks: AllowQuirks)
                               -> Result<LengthOrPercentageOrNone, ParseError<'i>>
     {
         let token = try!(input.next());
         match token {
-            Token::Dimension(ref value, ref unit) if num_context.is_ok(value.value) =>
+            Token::Dimension(ref value, ref unit) if num_context.is_ok(context.parsing_mode, value.value) =>
                 NoCalcLength::parse_dimension(context, value.value, unit).map(LengthOrPercentageOrNone::Length),
-            Token::Percentage(ref value) if num_context.is_ok(value.unit_value) =>
+            Token::Percentage(ref value) if num_context.is_ok(context.parsing_mode, value.unit_value) =>
                 Ok(LengthOrPercentageOrNone::Percentage(Percentage(value.unit_value))),
-            Token::Number(value) if num_context.is_ok(value.value) => {
+            Token::Number(value) if num_context.is_ok(context.parsing_mode, value.value) => {
                 if value.value != 0. && !context.parsing_mode.allows_unitless_lengths() &&
                    !allow_quirks.allowed(context.quirks_mode) {
                     return Err(StyleParseError::UnspecifiedError.into())
                 }
                 Ok(LengthOrPercentageOrNone::Length(
                     NoCalcLength::Absolute(AbsoluteLength::Px(value.value))
                 ))
             }
@@ -1108,20 +1108,20 @@ pub enum LengthOrPercentageOrAutoOrConte
 
 impl LengthOrPercentageOrAutoOrContent {
     /// Parse a non-negative LengthOrPercentageOrAutoOrContent.
     pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
                                       -> Result<Self, ParseError<'i>> {
         let num_context = AllowedLengthType::NonNegative;
         let token = try!(input.next());
         match token {
-            Token::Dimension(ref value, ref unit) if num_context.is_ok(value.value) =>
+            Token::Dimension(ref value, ref unit) if num_context.is_ok(context.parsing_mode, value.value) =>
                 NoCalcLength::parse_dimension(context, value.value, unit)
                              .map(LengthOrPercentageOrAutoOrContent::Length),
-            Token::Percentage(ref value) if num_context.is_ok(value.unit_value) =>
+            Token::Percentage(ref value) if num_context.is_ok(context.parsing_mode, value.unit_value) =>
                 Ok(LengthOrPercentageOrAutoOrContent::Percentage(Percentage(value.unit_value))),
             Token::Number(ref value) if value.value == 0. =>
                 Ok(Self::zero()),
             Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
                 Ok(LengthOrPercentageOrAutoOrContent::Auto),
             Token::Ident(ref value) if value.eq_ignore_ascii_case("content") =>
                 Ok(LengthOrPercentageOrAutoOrContent::Content),
             Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
--- a/servo/components/style_traits/values.rs
+++ b/servo/components/style_traits/values.rs
@@ -199,16 +199,17 @@ macro_rules! __define_css_keyword_enum__
                 }
             }
         }
     }
 }
 
 /// Helper types for the handling of specified values.
 pub mod specified {
+    use ParsingMode;
     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 {
@@ -223,17 +224,20 @@ pub mod specified {
         fn default() -> Self {
             AllowedLengthType::All
         }
     }
 
     impl AllowedLengthType {
         /// Whether value is valid for this allowed length type.
         #[inline]
-        pub fn is_ok(&self, value: f32) -> bool {
+        pub fn is_ok(&self, parsing_mode: ParsingMode, value: f32) -> bool {
+            if parsing_mode.allows_all_numeric_values() {
+                return true;
+            }
             match *self {
                 AllowedLengthType::All => true,
                 AllowedLengthType::NonNegative => value >= 0.,
             }
         }
 
         /// Clamp the value following the rules of this numeric type.
         #[inline]
--- a/servo/components/style_traits/viewport.rs
+++ b/servo/components/style_traits/viewport.rs
@@ -4,17 +4,16 @@
 
 //! Helper types for the `@viewport` rule.
 
 use {CSSPixel, PinchZoomFactor, ParseError};
 use cssparser::{Parser, ToCss, ParseError as CssParseError, BasicParseError};
 use euclid::size::TypedSize2D;
 use std::ascii::AsciiExt;
 use std::fmt;
-use values::specified::AllowedLengthType;
 
 define_css_keyword_enum!(UserZoom:
                          "zoom" => Zoom,
                          "fixed" => Fixed);
 
 define_css_keyword_enum!(Orientation:
                          "auto" => Auto,
                          "portrait" => Portrait,
@@ -136,22 +135,24 @@ impl ToCss for Zoom {
     }
 }
 
 impl Zoom {
     /// Parse a zoom value per:
     ///
     /// https://drafts.csswg.org/css-device-adapt/#descdef-viewport-zoom
     pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Zoom, ParseError<'i>> {
+        use PARSING_MODE_DEFAULT;
         use cssparser::Token;
+        use values::specified::AllowedLengthType::NonNegative;
 
         match try!(input.next()) {
-            Token::Percentage(ref value) if AllowedLengthType::NonNegative.is_ok(value.unit_value) =>
+            Token::Percentage(ref value) if NonNegative.is_ok(PARSING_MODE_DEFAULT, value.unit_value) =>
                 Ok(Zoom::Percentage(value.unit_value)),
-            Token::Number(ref value) if AllowedLengthType::NonNegative.is_ok(value.value) =>
+            Token::Number(ref value) if NonNegative.is_ok(PARSING_MODE_DEFAULT, value.value) =>
                 Ok(Zoom::Number(value.value)),
             Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
                 Ok(Zoom::Auto),
             t => Err(CssParseError::Basic(BasicParseError::UnexpectedToken(t)))
         }
     }
 
     /// Get this zoom value as a float value. Returns `None` if the value is the