Handle "transparent" color ident (#48716)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48716

`transparent` is specced as a special `<named-color>` outside the table the others were derived from. Let's add it, since it is supported today by `normalizeColor`.

https://www.w3.org/TR/css-color-4/#named-colors

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D68246770

fbshipit-source-id: 3ff7ed68ebb3d6bc59b24a25d35342620670f0c3
This commit is contained in:
Nick Gerleman
2025-01-16 11:42:34 -08:00
committed by Facebook GitHub Bot
parent de5bccf080
commit ddf68eb949
3 changed files with 16 additions and 0 deletions
@@ -12,6 +12,7 @@
#include <optional>
namespace facebook::react {
// https://www.w3.org/TR/css-color-4/#named-colors
template <typename CSSValueT>
constexpr std::optional<CSSValueT> parseCSSNamedColor(std::string_view name) {
@@ -298,6 +299,8 @@ constexpr std::optional<CSSValueT> parseCSSNamedColor(std::string_view name) {
return CSSValueT::color(216, 191, 216, 255);
case fnv1a("tomato"):
return CSSValueT::color(255, 99, 71, 255);
case fnv1a("transparent"):
return CSSValueT::color(0, 0, 0, 0);
case fnv1a("turquoise"):
return CSSValueT::color(64, 224, 208, 255);
case fnv1a("violet"):
@@ -82,6 +82,10 @@ struct CSSAngle {
float degrees{};
};
/**
* Representation of CSS <color> data type
* https://www.w3.org/TR/css-color-5/#typedef-color
*/
struct CSSColor {
uint8_t r{};
uint8_t g{};
@@ -415,5 +415,14 @@ TEST(CSSValueParser, named_colors) {
EXPECT_EQ(namedColorMixedCaseTestValue.getColor().g, 255);
EXPECT_EQ(namedColorMixedCaseTestValue.getColor().b, 127);
EXPECT_EQ(namedColorMixedCaseTestValue.getColor().a, 255);
auto transparentColor =
parseCSSValue<CSSWideKeyword, CSSColor>("transparent");
EXPECT_EQ(transparentColor.type(), CSSValueType::Color);
EXPECT_EQ(transparentColor.getColor().r, 0);
EXPECT_EQ(transparentColor.getColor().g, 0);
EXPECT_EQ(transparentColor.getColor().b, 0);
EXPECT_EQ(transparentColor.getColor().a, 0);
}
} // namespace facebook::react