Bug 1355348 - Factor out method to remove minus sign from property values; r=hiro
authorBrian Birtles <birtles@gmail.com>
Wed, 26 Apr 2017 13:00:11 +0900
changeset 569347 e5f624959d3dbae43c5f5e0f2364cabf62fc5936
parent 569346 7dbc4fb20502929868f0e855665507214455b328
child 569348 d3553e38c07dd3fd7704db10635a7f527594af80
push id56143
push userbmo:jeremychen@mozilla.com
push dateThu, 27 Apr 2017 10:17:21 +0000
reviewershiro
bugs1355348
milestone55.0a1
Bug 1355348 - Factor out method to remove minus sign from property values; r=hiro This is just a minor refactoring to simplify ValueFromStringHelper before we refactor it further in the next patch (and so we can re-use this definition to produce debug warning for Servo since we don't handle negative values there yet). MozReview-Commit-ID: CfcnI2Be5di
dom/smil/nsSMILCSSValueType.cpp
--- a/dom/smil/nsSMILCSSValueType.cpp
+++ b/dom/smil/nsSMILCSSValueType.cpp
@@ -331,49 +331,61 @@ GetPresContextForElement(Element* aElem)
     // and remove anonymous animated content from the document as a result.
     // See bug 534975.
     return nullptr;
   }
   nsIPresShell* shell = doc->GetShell();
   return shell ? shell->GetPresContext() : nullptr;
 }
 
+static const nsDependentSubstring
+GetNonNegativePropValue(const nsAString& aString, nsCSSPropertyID aPropID,
+                        bool& aIsNegative)
+{
+  // If value is negative, we'll strip off the "-" so the CSS parser won't
+  // barf, and then manually make the parsed value negative.
+  // (This is a partial solution to let us accept some otherwise out-of-bounds
+  // CSS values. Bug 501188 will provide a more complete fix.)
+  aIsNegative = false;
+  uint32_t subStringBegin = 0;
+
+  // NOTE: We need to opt-out 'stroke-dasharray' from the negative-number
+  // check.  Its values might look negative (e.g. by starting with "-1"), but
+  // they're more complicated than our simple negation logic here can handle.
+  if (aPropID != eCSSProperty_stroke_dasharray) {
+    int32_t absValuePos = nsSMILParserUtils::CheckForNegativeNumber(aString);
+    if (absValuePos > 0) {
+      aIsNegative = true;
+      subStringBegin = (uint32_t)absValuePos; // Start parsing after '-' sign
+    }
+  }
+
+  return Substring(aString, subStringBegin);
+}
+
 // Helper function to parse a string into a StyleAnimationValue
 static bool
 ValueFromStringHelper(nsCSSPropertyID aPropID,
                       Element* aTargetElement,
                       nsPresContext* aPresContext,
                       const nsAString& aString,
                       StyleAnimationValue& aStyleAnimValue,
                       bool* aIsContextSensitive)
 {
-  // If value is negative, we'll strip off the "-" so the CSS parser won't
-  // barf, and then manually make the parsed value negative.
-  // (This is a partial solution to let us accept some otherwise out-of-bounds
-  // CSS values. Bug 501188 will provide a more complete fix.)
-  bool isNegative = false;
-  uint32_t subStringBegin = 0;
-
-  // NOTE: We need to opt-out 'stroke-dasharray' from the negative-number
-  // check.  Its values might look negative (e.g. by starting with "-1"), but
-  // they're more complicated than our simple negation logic here can handle.
-  if (aPropID != eCSSProperty_stroke_dasharray) {
-    int32_t absValuePos = nsSMILParserUtils::CheckForNegativeNumber(aString);
-    if (absValuePos > 0) {
-      isNegative = true;
-      subStringBegin = (uint32_t)absValuePos; // Start parsing after '-' sign
-    }
-  }
   RefPtr<nsStyleContext> styleContext =
     nsComputedDOMStyle::GetStyleContext(aTargetElement, nullptr,
                                         aPresContext->PresShell());
   if (!styleContext) {
     return false;
   }
-  nsDependentSubstring subString(aString, subStringBegin);
+
+  bool isNegative = false;
+  const nsDependentSubstring subString =
+    GetNonNegativePropValue(aString, aPropID, isNegative);
+
   if (!StyleAnimationValue::ComputeValue(aPropID, aTargetElement, styleContext,
                                          subString, true, aStyleAnimValue,
                                          aIsContextSensitive)) {
     return false;
   }
   if (isNegative) {
     InvertSign(aStyleAnimValue);
   }