Title: [272226] trunk/Source/WebCore
Revision
272226
Author
[email protected]
Date
2021-02-02 12:13:54 -0800 (Tue, 02 Feb 2021)

Log Message

Fix long standing FIXME in parseNumericColor about not doubly clamping color components
https://bugs.webkit.org/show_bug.cgi?id=221194

Reviewed by Darin Adler.

* css/parser/CSSParserFastPaths.cpp:
(WebCore::parseColorIntOrPercentage):
(WebCore::parseAlphaValue):
Switch to returning an Optional<uint8_t> and add assertion that the component
is clamped. Use convertFloatAlphaTo helper in parseAlphaValue to remove some
duplicate logic.

(WebCore::parseNumericColor):
Replace call to makeFromComponentsClamping with calling the constructors directly
now that the types are correct.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (272225 => 272226)


--- trunk/Source/WebCore/ChangeLog	2021-02-02 19:34:29 UTC (rev 272225)
+++ trunk/Source/WebCore/ChangeLog	2021-02-02 20:13:54 UTC (rev 272226)
@@ -1,3 +1,21 @@
+2021-02-02  Sam Weinig  <[email protected]>
+
+        Fix long standing FIXME in parseNumericColor about not doubly clamping color components
+        https://bugs.webkit.org/show_bug.cgi?id=221194
+
+        Reviewed by Darin Adler.
+
+        * css/parser/CSSParserFastPaths.cpp:
+        (WebCore::parseColorIntOrPercentage):
+        (WebCore::parseAlphaValue):
+        Switch to returning an Optional<uint8_t> and add assertion that the component
+        is clamped. Use convertFloatAlphaTo helper in parseAlphaValue to remove some
+        duplicate logic.
+
+        (WebCore::parseNumericColor):
+        Replace call to makeFromComponentsClamping with calling the constructors directly
+        now that the types are correct.
+
 2021-02-02  Jer Noble  <[email protected]>
 
         REGRESSION (r271219): [Mojave] imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video_timeupdate_on_seek.html is a flaky failure

Modified: trunk/Source/WebCore/css/parser/CSSParserFastPaths.cpp (272225 => 272226)


--- trunk/Source/WebCore/css/parser/CSSParserFastPaths.cpp	2021-02-02 19:34:29 UTC (rev 272225)
+++ trunk/Source/WebCore/css/parser/CSSParserFastPaths.cpp	2021-02-02 20:13:54 UTC (rev 272226)
@@ -250,7 +250,7 @@
 }
 
 template <typename CharacterType>
-static bool parseColorIntOrPercentage(const CharacterType*& string, const CharacterType* end, const char terminator, CSSUnitType& expect, int& value)
+static Optional<uint8_t> parseColorIntOrPercentage(const CharacterType*& string, const CharacterType* end, const char terminator, CSSUnitType& expect)
 {
     const CharacterType* current = string;
     double localValue = 0;
@@ -262,7 +262,7 @@
         current++;
     }
     if (current == end || !isASCIIDigit(*current))
-        return false;
+        return WTF::nullopt;
     while (current != end && isASCIIDigit(*current)) {
         double newValue = localValue * 10 + *current++ - '0';
         if (newValue >= 255) {
@@ -276,10 +276,10 @@
     }
 
     if (current == end)
-        return false;
+        return WTF::nullopt;
 
     if (expect == CSSUnitType::CSS_NUMBER && (*current == '.' || *current == '%'))
-        return false;
+        return WTF::nullopt;
 
     if (*current == '.') {
         // We already parsed the integral part, try to parse
@@ -287,15 +287,15 @@
         double percentage = 0;
         int numCharactersParsed = parseDouble(current, end, '%', percentage);
         if (!numCharactersParsed)
-            return false;
+            return WTF::nullopt;
         current += numCharactersParsed;
         if (*current != '%')
-            return false;
+            return WTF::nullopt;
         localValue += percentage;
     }
 
     if (expect == CSSUnitType::CSS_PERCENTAGE && *current != '%')
-        return false;
+        return WTF::nullopt;
 
     if (*current == '%') {
         expect = CSSUnitType::CSS_PERCENTAGE;
@@ -304,18 +304,18 @@
         if (localValue > 255)
             localValue = 255;
         current++;
-    } else {
+    } else
         expect = CSSUnitType::CSS_NUMBER;
-    }
 
     while (current != end && isHTMLSpace<CharacterType>(*current))
         current++;
     if (current == end || *current++ != terminator)
-        return false;
+        return WTF::nullopt;
+    string = current;
+
     // Clamp negative values at zero.
-    value = negative ? 0 : static_cast<int>(localValue);
-    string = current;
-    return true;
+    ASSERT(localValue <= 255);
+    return negative ? 0 : static_cast<uint8_t>(localValue);
 }
 
 template <typename CharacterType>
@@ -333,7 +333,7 @@
 }
 
 template <typename CharacterType>
-static inline bool parseAlphaValue(const CharacterType*& string, const CharacterType* end, const char terminator, int& value)
+static inline Optional<uint8_t> parseAlphaValue(const CharacterType*& string, const CharacterType* end, const char terminator)
 {
     while (string != end && isHTMLSpace<CharacterType>(*string))
         string++;
@@ -345,45 +345,40 @@
         string++;
     }
 
-    value = 0;
-
     int length = end - string;
     if (length < 2)
-        return false;
+        return WTF::nullopt;
 
     if (string[length - 1] != terminator || !isASCIIDigit(string[length - 2]))
-        return false;
+        return WTF::nullopt;
 
     if (string[0] != '0' && string[0] != '1' && string[0] != '.') {
         if (checkForValidDouble(string, end, terminator)) {
-            value = negative ? 0 : 255;
             string = end;
-            return true;
+            return negative ? 0 : 255;
         }
-        return false;
+        return WTF::nullopt;
     }
 
     if (length == 2 && string[0] != '.') {
-        value = !negative && string[0] == '1' ? 255 : 0;
+        uint8_t result = !negative && string[0] == '1' ? 255 : 0;
         string = end;
-        return true;
+        return result;
     }
 
     if (isTenthAlpha(string, length - 1)) {
-        static const int tenthAlphaValues[] = { 0, 26, 51, 77, 102, 128, 153, 179, 204, 230 };
-        value = negative ? 0 : tenthAlphaValues[string[length - 2] - '0'];
+        static constexpr uint8_t tenthAlphaValues[] = { 0, 26, 51, 77, 102, 128, 153, 179, 204, 230 };
+        uint8_t result = negative ? 0 : tenthAlphaValues[string[length - 2] - '0'];
         string = end;
-        return true;
+        return result;
     }
 
     double alpha = 0;
     if (!parseDouble(string, end, terminator, alpha))
-        return false;
+        return WTF::nullopt;
 
-    // W3 standard stipulates a 2.55 alpha value multiplication factor.
-    value = negative ? 0 : static_cast<int>(lroundf(clampTo<double>(alpha, 0.0, 1.0) * 255.0f));
     string = end;
-    return true;
+    return negative ? 0 : convertFloatAlphaTo<uint8_t>(alpha);
 }
 
 template <typename CharacterType>
@@ -473,21 +468,21 @@
     if (mightBeRGBA(characters, length)) {
         auto current = characters + 5;
         auto end = characters + length;
-        int red;
-        if (!parseColorIntOrPercentage(current, end, ',', expect, red))
+        auto red = parseColorIntOrPercentage(current, end, ',', expect);
+        if (!red)
             return WTF::nullopt;
-        int green;
-        if (!parseColorIntOrPercentage(current, end, ',', expect, green))
+        auto green = parseColorIntOrPercentage(current, end, ',', expect);
+        if (!green)
             return WTF::nullopt;
-        int blue;
-        if (!parseColorIntOrPercentage(current, end, ',', expect, blue))
+        auto blue = parseColorIntOrPercentage(current, end, ',', expect);
+        if (!blue)
             return WTF::nullopt;
-        int alpha;
-        if (!parseAlphaValue(current, end, ')', alpha))
+        auto alpha = parseAlphaValue(current, end, ')');
+        if (!alpha)
             return WTF::nullopt;
         if (current != end)
             return WTF::nullopt;
-        return makeFromComponentsClamping<SRGBA<uint8_t>>(red, green, blue, alpha); // FIXME: Already clamped, doesn't need to re-clamp. Update parseColorIntOrPercentage/parseAlphaValue to return uint8_t and replace call to makeFromComponentsClamping<SRGBA<uint8_t>> with direct construction of SRGBA<uint8_t>.
+        return SRGBA<uint8_t> { *red, *green, *blue, *alpha };
     }
 
     // Try rgb() syntax.
@@ -494,18 +489,18 @@
     if (mightBeRGB(characters, length)) {
         auto current = characters + 4;
         auto end = characters + length;
-        int red;
-        if (!parseColorIntOrPercentage(current, end, ',', expect, red))
+        auto red = parseColorIntOrPercentage(current, end, ',', expect);
+        if (!red)
             return WTF::nullopt;
-        int green;
-        if (!parseColorIntOrPercentage(current, end, ',', expect, green))
+        auto green = parseColorIntOrPercentage(current, end, ',', expect);
+        if (!green)
             return WTF::nullopt;
-        int blue;
-        if (!parseColorIntOrPercentage(current, end, ')', expect, blue))
+        auto blue = parseColorIntOrPercentage(current, end, ')', expect);
+        if (!blue)
             return WTF::nullopt;
         if (current != end)
             return WTF::nullopt;
-        return makeFromComponentsClamping<SRGBA<uint8_t>>(red, green, blue); // FIXME: Already clamped, doesn't need to re-clamp. Update parseColorIntOrPercentage/parseAlphaValue to return uint8_t and replace call to makeFromComponentsClamping<SRGBA<uint8_t>> with direct construction of SRGBA<uint8_t>.
+        return SRGBA<uint8_t> { *red, *green, *blue };
     }
 
     return WTF::nullopt;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to