Bug 1338388 - Part 10: stylo: Support nonstandard CSS_PROPERTY_NUMBERS_ARE_PIXELS behavior; r?heycam draft
authorManish Goregaokar <manishearth@gmail.com>
Thu, 09 Feb 2017 17:43:52 -0800
changeset 486533 9b1713a4bc7147eb5cc9e65959c96e4066b6c920
parent 486532 998b816e75f7f1057b7ecc29c03efd603e63f1ec
child 486534 3ec8c878b33045d316cea6472ab4521d7197fd7e
push id46017
push userbmo:manishearth@gmail.com
push dateSat, 18 Feb 2017 08:18:38 +0000
reviewersheycam
bugs1338388
milestone54.0a1
Bug 1338388 - Part 10: stylo: Support nonstandard CSS_PROPERTY_NUMBERS_ARE_PIXELS behavior; r?heycam MozReview-Commit-ID: 4QKKzJ1DVYP
servo/components/style/properties/longhand/inherited_svg.mako.rs
servo/components/style/values/specified/length.rs
--- a/servo/components/style/properties/longhand/inherited_svg.mako.rs
+++ b/servo/components/style/properties/longhand/inherited_svg.mako.rs
@@ -63,17 +63,17 @@
     products="gecko",
     animatable=False,
     boxed=True,
     spec="https://www.w3.org/TR/SVG2/painting.html#SpecifyingStrokePaint")}
 
 ${helpers.predefined_type(
     "stroke-width", "LengthOrPercentage",
     "computed::LengthOrPercentage::one()",
-    "parse_non_negative",
+    "parse_numbers_are_pixels_non_negative",
     products="gecko",
     animatable=True,
     needs_context=False,
     boxed=True,
     spec="https://www.w3.org/TR/SVG2/painting.html#StrokeWidth")}
 
 ${helpers.single_keyword("stroke-linecap", "butt round square",
                          products="gecko", animatable=False,
@@ -99,19 +99,21 @@
                           products="gecko",
                           animatable="False",
                           space_separated_allowed="True",
                           spec="https://www.w3.org/TR/SVG2/painting.html#StrokeDashing")}
 
 ${helpers.predefined_type(
     "stroke-dashoffset", "LengthOrPercentage",
     "computed::LengthOrPercentage::zero()",
+    "parse_numbers_are_pixels",
     products="gecko",
     animatable=True,
     boxed=True,
+    needs_context=False,
     spec="https://www.w3.org/TR/SVG2/painting.html#StrokeDashing")}
 
 // Section 14 - Clipping, Masking and Compositing
 ${helpers.single_keyword("clip-rule", "nonzero evenodd",
                          products="gecko",
                          gecko_enum_prefix="StyleFillRule",
                          gecko_inexhaustive=True,
                          animatable=False,
--- a/servo/components/style/values/specified/length.rs
+++ b/servo/components/style/values/specified/length.rs
@@ -982,16 +982,44 @@ impl LengthOrPercentage {
     }
 
     /// Parse a non-negative length.
     #[inline]
     pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
         LengthOrPercentage::parse_internal(input, AllowedNumericType::NonNegative)
     }
 
+    /// Parse a length, treating dimensionless numbers as pixels
+    ///
+    /// https://www.w3.org/TR/SVG2/types.html#presentation-attribute-css-value
+    pub fn parse_numbers_are_pixels(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
+        if let Ok(lop) = input.try(|i| Self::parse_internal(i, AllowedNumericType::All)) {
+            Ok(lop)
+        } else {
+            let num = input.expect_number()?;
+            Ok(LengthOrPercentage::Length(NoCalcLength::Absolute(Au((AU_PER_PX * num) as i32))))
+        }
+    }
+
+    /// Parse a non-negative length, treating dimensionless numbers as pixels
+    ///
+    /// This is nonstandard behavior used by Firefox for SVG
+    pub fn parse_numbers_are_pixels_non_negative(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
+        if let Ok(lop) = input.try(|i| Self::parse_internal(i, AllowedNumericType::NonNegative)) {
+            Ok(lop)
+        } else {
+            let num = input.expect_number()?;
+            if num >= 0. {
+                Ok(LengthOrPercentage::Length(NoCalcLength::Absolute(Au((AU_PER_PX * num) as i32))))
+            } else {
+                Err(())
+            }
+        }
+    }
+
     /// Extract value from ref without a clone, replacing it with a 0 Au
     ///
     /// Use when you need to move out of a length array without cloning
     #[inline]
     pub fn take(&mut self) -> Self {
         mem::replace(self, LengthOrPercentage::zero())
     }
 }