Bug 1357117 part 1: Change linear-gradient serialization code to group space separator with the "to" token. r=heycam draft
authorDaniel Holbert <dholbert@cs.stanford.edu>
Tue, 25 Apr 2017 11:48:37 -0700
changeset 568072 9f8b5c801b2f4352dd2ace3b79c40edb1795b361
parent 566044 91c19f2d71f35f1bb4ee5325870275d648b0c2db
child 568073 cabd4a140d575794a8a656c47692bb9d3a02de88
push id55750
push userdholbert@mozilla.com
push dateTue, 25 Apr 2017 18:48:48 +0000
reviewersheycam
bugs1357117
milestone55.0a1
Bug 1357117 part 1: Change linear-gradient serialization code to group space separator with the "to" token. r=heycam This patch doesn't change our behavior -- we'll still produce the same serialization that we would've before. MOTIVATION: A later patch will make us share this codepath to serialize into -webkit-linear-gradient() syntax. That syntax uses the same representation for points as unprefixed modern linear-gradient() (with box-side-or-corner keywords "top", "right", etc.), but it does *not* use the word "to". So we'd like to allow "to"-and-its-subsequent-space-character to be optional. Hence, this patch groups the space together with "to", rather than as a prefix on the next token, so that we can skip right to printing the point (without a leading space) when we add support for -webkit-linear-gradient() serialization. MozReview-Commit-ID: 5fCzx4NmpcC
layout/style/nsCSSValue.cpp
layout/style/nsComputedDOMStyle.cpp
--- a/layout/style/nsCSSValue.cpp
+++ b/layout/style/nsCSSValue.cpp
@@ -1831,24 +1831,31 @@ nsCSSValue::AppendToString(nsCSSProperty
     }
     if (!gradient->mIsRadial && !gradient->mIsLegacySyntax) {
       if (gradient->mBgPos.mXValue.GetUnit() != eCSSUnit_None ||
           gradient->mBgPos.mYValue.GetUnit() != eCSSUnit_None) {
         MOZ_ASSERT(gradient->mAngle.GetUnit() == eCSSUnit_None);
         MOZ_ASSERT(gradient->mBgPos.mXValue.GetUnit() == eCSSUnit_Enumerated &&
                    gradient->mBgPos.mYValue.GetUnit() == eCSSUnit_Enumerated,
                    "unexpected unit");
-        aResult.AppendLiteral("to");
+        // XXXdholbert Note: this AppendLiteral call will become conditional
+        // in a later patch in this series.
+        aResult.AppendLiteral("to ");
+        bool didAppendX = false;
         if (!(gradient->mBgPos.mXValue.GetIntValue() & NS_STYLE_IMAGELAYER_POSITION_CENTER)) {
-          aResult.Append(' ');
           gradient->mBgPos.mXValue.AppendToString(eCSSProperty_background_position_x,
                                                   aResult, aSerialization);
+          didAppendX = true;
         }
         if (!(gradient->mBgPos.mYValue.GetIntValue() & NS_STYLE_IMAGELAYER_POSITION_CENTER)) {
-          aResult.Append(' ');
+          if (didAppendX) {
+            // We're appending both an x-keyword and a y-keyword.
+            // Add a space between them here.
+            aResult.Append(' ');
+          }
           gradient->mBgPos.mYValue.AppendToString(eCSSProperty_background_position_y,
                                                   aResult, aSerialization);
         }
         needSep = true;
       } else if (gradient->mAngle.GetUnit() != eCSSUnit_None) {
         gradient->mAngle.AppendToString(aProperty, aResult, aSerialization);
         needSep = true;
       }
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -2023,30 +2023,38 @@ AppendCSSGradientToBoxPosition(const nsS
   float yValue = aGradient->mBgPosY.GetPercentValue();
 
   if (yValue == 1.0f && xValue == 0.5f) {
     // omit "to bottom"
     return;
   }
   NS_ASSERTION(yValue != 0.5f || xValue != 0.5f, "invalid box position");
 
-  aString.AppendLiteral("to");
+  // XXXdholbert Note: this AppendLiteral call will become conditional in a
+  // later patch in this series.
+  aString.AppendLiteral("to ");
 
   if (yValue == 0.0f) {
-    aString.AppendLiteral(" top");
+    aString.AppendLiteral("top");
   } else if (yValue == 1.0f) {
-    aString.AppendLiteral(" bottom");
+    aString.AppendLiteral("bottom");
   } else if (yValue != 0.5f) { // do not write "center" keyword
     NS_NOTREACHED("invalid box position");
   }
 
+  if (yValue != 0.5f && xValue != 0.5f) {
+    // We're appending both a y-keyword and an x-keyword.
+    // Add a space between them here.
+    aString.AppendLiteral(" ");
+  }
+
   if (xValue == 0.0f) {
-    aString.AppendLiteral(" left");
+    aString.AppendLiteral("left");
   } else if (xValue == 1.0f) {
-    aString.AppendLiteral(" right");
+    aString.AppendLiteral("right");
   } else if (xValue != 0.5f) { // do not write "center" keyword
     NS_NOTREACHED("invalid box position");
   }
 
   aNeedSep = true;
 }
 
 void