Bug 1357117 part 3: Serialize -webkit-linear-gradient() expressions using the (less-expressive) -webkit-linear-gradient syntax, instead of -moz-linear-gradient. r?xidorn draft
authorDaniel Holbert <dholbert@cs.stanford.edu>
Wed, 19 Apr 2017 14:51:33 -0700
changeset 565477 bbb4f1865bfe1e21fd764118cb91f3f1f63042ba
parent 565476 093e7a58572406546786180b8c8f43d590b8dcf9
child 624991 d04ed9d1011bc818064fb5e673fece799a9c6d64
push id54868
push userdholbert@mozilla.com
push dateWed, 19 Apr 2017 21:51:46 +0000
reviewersxidorn
bugs1357117
milestone55.0a1
Bug 1357117 part 3: Serialize -webkit-linear-gradient() expressions using the (less-expressive) -webkit-linear-gradient syntax, instead of -moz-linear-gradient. r?xidorn Specifically: * This patch uses a flag added in a prior patch to let us use the author's chosen prefix (-webkit or -moz) when serializing. (We treat the -moz version as a special case, because that makes it more straightforward to unsupport -moz if/when we can.) * This patch makes us share the linear-gradient() side-or-corner serialization codepath when serializing points for -webkit-linear-gradient, rather than using the -moz-linear-gradient codepath which defaults to serializing with percent values 0%/100% for sides & corners (which are invalid in -webkit-linear-gradient()). Notably, we have to skip the "to " token in the legacy syntax -- that was a late addition in the spec and only exists in the unprefixed modern syntax. MozReview-Commit-ID: 9Oqo8nG1XDU
layout/style/nsCSSValue.cpp
layout/style/nsComputedDOMStyle.cpp
--- a/layout/style/nsCSSValue.cpp
+++ b/layout/style/nsCSSValue.cpp
@@ -1772,17 +1772,21 @@ nsCSSValue::AppendToString(nsCSSProperty
   }
   else if (eCSSUnit_Percent < unit) {  // length unit
     aResult.AppendFloat(GetFloatValue());
   }
   else if (eCSSUnit_Gradient == unit) {
     nsCSSValueGradient* gradient = GetGradientValue();
 
     if (gradient->mIsLegacySyntax) {
-      aResult.AppendLiteral("-moz-");
+      if (gradient->mIsMozLegacySyntax) {
+        aResult.AppendLiteral("-moz-");
+      } else {
+        aResult.AppendLiteral("-webkit-");
+      }
     }
     if (gradient->mIsRepeating) {
       aResult.AppendLiteral("repeating-");
     }
     if (gradient->mIsRadial) {
       aResult.AppendLiteral("radial-gradient(");
     } else {
       aResult.AppendLiteral("linear-gradient(");
@@ -1824,26 +1828,27 @@ nsCSSValue::AppendToString(nsCSSProperty
         if (gradient->GetRadiusY().GetUnit() != eCSSUnit_None) {
           aResult.Append(' ');
           gradient->GetRadiusY().AppendToString(aProperty, aResult,
                                                 aSerialization);
         }
         needSep = true;
       }
     }
-    if (!gradient->mIsRadial && !gradient->mIsLegacySyntax) {
+    if (!gradient->mIsRadial &&
+        !(gradient->mIsLegacySyntax && gradient->mIsMozLegacySyntax)) {
       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");
-        // XXXdholbert Note: this AppendLiteral call will become conditional
-        // in a later patch in this series.
-        aResult.AppendLiteral("to ");
+        if (!gradient->mIsLegacySyntax) {
+          aResult.AppendLiteral("to ");
+        }
         bool didAppendX = false;
         if (!(gradient->mBgPos.mXValue.GetIntValue() & NS_STYLE_IMAGELAYER_POSITION_CENTER)) {
           gradient->mBgPos.mXValue.AppendToString(eCSSProperty_background_position_x,
                                                   aResult, aSerialization);
           didAppendX = true;
         }
         if (!(gradient->mBgPos.mYValue.GetIntValue() & NS_STYLE_IMAGELAYER_POSITION_CENTER)) {
           if (didAppendX) {
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -2014,28 +2014,37 @@ AppendCSSGradientLength(const nsStyleCoo
   aString.Append(tokenString);
 }
 
 static void
 AppendCSSGradientToBoxPosition(const nsStyleGradient* aGradient,
                                nsAString&             aString,
                                bool&                  aNeedSep)
 {
+  // This function only supports box position keywords. Make sure we're not
+  // calling it with inputs that would have coordinates that aren't
+  // representable with box-position keywords.
+  MOZ_ASSERT(aGradient->mShape == NS_STYLE_GRADIENT_SHAPE_LINEAR &&
+             !(aGradient->mLegacySyntax && aGradient->mMozLegacySyntax),
+             "Only call me for linear-gradient and -webkit-linear-gradient");
+
   float xValue = aGradient->mBgPosX.GetPercentValue();
   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");
 
-  // XXXdholbert Note: this AppendLiteral call will become conditional in a
-  // later patch in this series.
-  aString.AppendLiteral("to ");
+  if (!aGradient->mLegacySyntax) {
+    // Modern syntax explicitly includes the word "to". Old syntax does not
+    // (and is implicitly "from" the given position instead).
+    aString.AppendLiteral("to ");
+  }
 
   if (yValue == 0.0f) {
     aString.AppendLiteral("top");
   } else if (yValue == 1.0f) {
     aString.AppendLiteral("bottom");
   } else if (yValue != 0.5f) { // do not write "center" keyword
     NS_NOTREACHED("invalid box position");
   }
@@ -2058,17 +2067,21 @@ AppendCSSGradientToBoxPosition(const nsS
 
 void
 nsComputedDOMStyle::GetCSSGradientString(const nsStyleGradient* aGradient,
                                          nsAString& aString)
 {
   if (!aGradient->mLegacySyntax) {
     aString.Truncate();
   } else {
-    aString.AssignLiteral("-moz-");
+    if (aGradient->mMozLegacySyntax) {
+      aString.AssignLiteral("-moz-");
+    } else {
+      aString.AssignLiteral("-webkit-");
+    }
   }
   if (aGradient->mRepeating) {
     aString.AppendLiteral("repeating-");
   }
   bool isRadial = aGradient->mShape != NS_STYLE_GRADIENT_SHAPE_LINEAR;
   if (isRadial) {
     aString.AppendLiteral("radial-gradient(");
   } else {
@@ -2101,17 +2114,19 @@ nsComputedDOMStyle::GetCSSGradientString
         aString.Append(' ');
         AppendCSSGradientLength(aGradient->mRadiusY, tmpVal, aString);
       }
       needSep = true;
     }
   }
   if (aGradient->mBgPosX.GetUnit() != eStyleUnit_None) {
     MOZ_ASSERT(aGradient->mBgPosY.GetUnit() != eStyleUnit_None);
-    if (!isRadial && !aGradient->mLegacySyntax) {
+    if (!isRadial &&
+        !(aGradient->mLegacySyntax && aGradient->mMozLegacySyntax)) {
+      // linear-gradient() or -webkit-linear-gradient()
       AppendCSSGradientToBoxPosition(aGradient, aString, needSep);
     } else if (aGradient->mBgPosX.GetUnit() != eStyleUnit_Percent ||
                aGradient->mBgPosX.GetPercentValue() != 0.5f ||
                aGradient->mBgPosY.GetUnit() != eStyleUnit_Percent ||
                aGradient->mBgPosY.GetPercentValue() != (isRadial ? 0.5f : 1.0f)) {
       if (isRadial && !aGradient->mLegacySyntax) {
         if (needSep) {
           aString.Append(' ');