Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 42 additions & 68 deletions packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,50 @@

#include <react/cxxstableapi/FrameworksGuard.h>

#include <array>
#include <cstdint>
#include <optional>
#include <string_view>

#include <React/Utils.h>

namespace facebook::react {

namespace detail {
enum class HexColorType {
Long,
Short,
};

constexpr uint8_t hexToNumeric(std::string_view hex, HexColorType hexType)
{
int result = 0;
for (char c : hex) {
int value = 0;
if (c >= '0' && c <= '9') {
value = c - '0';
} else {
value = toLower(c) - 'a' + 10;
}
result *= 16;
result += value;
inline constexpr auto hexDigits = [] {
std::array<uint8_t, 256> digits;
digits.fill(0xff);
for (uint8_t digit = 0; digit < 10; ++digit) {
digits['0' + digit] = digit;
}

if (hexType == HexColorType::Short) {
return static_cast<uint8_t>(result * 16 + result);
} else {
return static_cast<uint8_t>(result);
for (uint8_t digit = 0; digit < 6; ++digit) {
digits['A' + digit] = 10 + digit;
digits['a' + digit] = 10 + digit;
}
}
return digits;
}();

constexpr bool isHexDigit(char c)
template <typename CSSColor, size_t DigitCount>
constexpr std::optional<CSSColor> parseHexDigits(const char *value)
{
return (c >= '0' && c <= '9') || (toLower(c) >= 'a' && toLower(c) <= 'f');
}

constexpr bool isValidHexColor(std::string_view hex)
{
// The syntax of a <hex-color> is a <hash-token> token whose value consists
// of 3, 4, 6, or 8 hexadecimal digits.
if (hex.size() != 3 && hex.size() != 4 && hex.size() != 6 && hex.size() != 8) {
return false;
std::array<uint8_t, DigitCount> digits;
uint8_t combined = 0;
for (size_t index = 0; index < DigitCount; ++index) {
digits[index] = hexDigits[static_cast<uint8_t>(value[index])];
combined |= digits[index];
}

for (auto c : hex) {
if (!isHexDigit(c)) {
return false;
}
if (combined > 0xf) {
return {};
}

return true;
constexpr size_t digitsPerChannel = DigitCount < 6 ? 1 : 2;
constexpr size_t channelCount = DigitCount / digitsPerChannel;

auto decodeChannel = [&](size_t channel) -> uint8_t {
auto offset = channel * digitsPerChannel;
return static_cast<uint8_t>((digits[offset] << 4) | digits[offset + digitsPerChannel - 1]);
};

return CSSColor{
decodeChannel(0), decodeChannel(1), decodeChannel(2), channelCount == 4 ? decodeChannel(3) : uint8_t{255}};
}
} // namespace detail

Expand All @@ -74,34 +64,18 @@ constexpr bool isValidHexColor(std::string_view hex)
template <typename CSSColor>
constexpr std::optional<CSSColor> parseCSSHexColor(std::string_view hexColorValue)
{
if (detail::isValidHexColor(hexColorValue)) {
if (hexColorValue.length() == 3) {
return CSSColor{
hexToNumeric(hexColorValue.substr(0, 1), detail::HexColorType::Short),
hexToNumeric(hexColorValue.substr(1, 1), detail::HexColorType::Short),
hexToNumeric(hexColorValue.substr(2, 1), detail::HexColorType::Short),
255u};
} else if (hexColorValue.length() == 4) {
return CSSColor{
hexToNumeric(hexColorValue.substr(0, 1), detail::HexColorType::Short),
hexToNumeric(hexColorValue.substr(1, 1), detail::HexColorType::Short),
hexToNumeric(hexColorValue.substr(2, 1), detail::HexColorType::Short),
hexToNumeric(hexColorValue.substr(3, 1), detail::HexColorType::Short)};
} else if (hexColorValue.length() == 6) {
return CSSColor{
hexToNumeric(hexColorValue.substr(0, 2), detail::HexColorType::Long),
hexToNumeric(hexColorValue.substr(2, 2), detail::HexColorType::Long),
hexToNumeric(hexColorValue.substr(4, 2), detail::HexColorType::Long),
255u};
} else if (hexColorValue.length() == 8) {
return CSSColor{
hexToNumeric(hexColorValue.substr(0, 2), detail::HexColorType::Long),
hexToNumeric(hexColorValue.substr(2, 2), detail::HexColorType::Long),
hexToNumeric(hexColorValue.substr(4, 2), detail::HexColorType::Long),
hexToNumeric(hexColorValue.substr(6, 2), detail::HexColorType::Long)};
}
switch (hexColorValue.size()) {
case 3:
return detail::parseHexDigits<CSSColor, 3>(hexColorValue.data());
case 4:
return detail::parseHexDigits<CSSColor, 4>(hexColorValue.data());
case 6:
return detail::parseHexDigits<CSSColor, 6>(hexColorValue.data());
case 8:
return detail::parseHexDigits<CSSColor, 8>(hexColorValue.data());
default:
return {};
}
return {};
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ TEST(CSSColor, hex_color_values) {
EXPECT_EQ(std::get<CSSColor>(hexAlphaTestValue).a, 204);
}

TEST(CSSColor, hex_color_digit_validation) {
constexpr auto shorthand = parseCSSHexColor<CSSColor>("1aB7");
static_assert(shorthand == CSSColor{0x11, 0xaa, 0xbb, 0x77});
constexpr auto full = parseCSSHexColor<CSSColor>("01aB2398");
static_assert(full == CSSColor{0x01, 0xab, 0x23, 0x98});

for (const auto value :
{"12",
"12345",
"1234567",
"g12",
"123g",
"12345g",
"1234567g",
"1234567\xff"}) {
EXPECT_FALSE(parseCSSHexColor<CSSColor>(value)) << value;
}
}

TEST(CSSColor, named_colors) {
auto invalidNamedColorTestValue = parseCSSProperty<CSSColor>("redd");
EXPECT_TRUE(
Expand Down
11 changes: 3 additions & 8 deletions scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -8467,19 +8467,19 @@ template <typename T>
void facebook::react::exceptions::throwIfObjectAlreadyConsumed(const T& t, const char* msg);


constexpr bool facebook::react::detail::isHexDigit(char c);
constexpr bool facebook::react::detail::isValidHexColor(std::string_view hex);
constexpr auto facebook::react::detail::hexDigits;
constexpr float facebook::react::detail::hueToRgb(float p, float q, float t);
constexpr std::optional<float> facebook::react::detail::normalizeNumberComponent(const std::variant<std::monostate, facebook::react::CSSNumber>& component);
constexpr uint8_t facebook::react::detail::clamp255Component(float f);
constexpr uint8_t facebook::react::detail::clampAlpha(std::optional<float> alpha);
constexpr uint8_t facebook::react::detail::hexToNumeric(std::string_view hex, facebook::react::detail::HexColorType hexType);
float facebook::react::detail::normalizeHue(float hue);
std::optional<float> facebook::react::detail::normalizeHueComponent(const std::variant<std::monostate, facebook::react::CSSNumber, facebook::react::CSSAngle>& component);
std::tuple<uint8_t, uint8_t, uint8_t> facebook::react::detail::hslToRgb(float h, float s, float l);
std::tuple<uint8_t, uint8_t, uint8_t> facebook::react::detail::hwbToRgb(float h, float w, float b);
template <CSSDataType... FirstComponentAllowedTypesT>
constexpr bool facebook::react::detail::isLegacyColorFunction(facebook::react::CSSValueParser& parser);
template <typename CSSColor, size_t DigitCount>
constexpr std::optional<facebook::react::CSSColor> facebook::react::detail::parseHexDigits(const char* value);
template <typename CSSColor>
constexpr std::optional<facebook::react::CSSColor> facebook::react::detail::parseLegacyRgbFunction(facebook::react::CSSValueParser& parser);
template <typename CSSColor>
Expand Down Expand Up @@ -8508,11 +8508,6 @@ class facebook::react::detail::OwnedBytesBuffer : public facebook::jsi::MutableB
public virtual uint8_t* data() override;
}

enum facebook::react::detail::HexColorType {
Long,
Short,
}

template <CSSDataType AlllowedType1T, CSSDataType AlllowedType2T, CSSMaybeCompoundDataType... RestT>
struct facebook::react::detail::merge_data_types<AlllowedType1T, AlllowedType2T, RestT...> {
public using type = typename facebook::react::detail::merge_data_types<std::variant<AlllowedType1T, AlllowedType2T>, RestT...>::type;
Expand Down
11 changes: 3 additions & 8 deletions scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -10322,19 +10322,19 @@ bool facebook::react::HostPlatformViewTraitsInitializer::formsView(const faceboo
bool facebook::react::HostPlatformViewTraitsInitializer::isKeyboardFocusable(const facebook::react::ViewProps&);


constexpr bool facebook::react::detail::isHexDigit(char c);
constexpr bool facebook::react::detail::isValidHexColor(std::string_view hex);
constexpr auto facebook::react::detail::hexDigits;
constexpr float facebook::react::detail::hueToRgb(float p, float q, float t);
constexpr std::optional<float> facebook::react::detail::normalizeNumberComponent(const std::variant<std::monostate, facebook::react::CSSNumber>& component);
constexpr uint8_t facebook::react::detail::clamp255Component(float f);
constexpr uint8_t facebook::react::detail::clampAlpha(std::optional<float> alpha);
constexpr uint8_t facebook::react::detail::hexToNumeric(std::string_view hex, facebook::react::detail::HexColorType hexType);
float facebook::react::detail::normalizeHue(float hue);
std::optional<float> facebook::react::detail::normalizeHueComponent(const std::variant<std::monostate, facebook::react::CSSNumber, facebook::react::CSSAngle>& component);
std::tuple<uint8_t, uint8_t, uint8_t> facebook::react::detail::hslToRgb(float h, float s, float l);
std::tuple<uint8_t, uint8_t, uint8_t> facebook::react::detail::hwbToRgb(float h, float w, float b);
template <CSSDataType... FirstComponentAllowedTypesT>
constexpr bool facebook::react::detail::isLegacyColorFunction(facebook::react::CSSValueParser& parser);
template <typename CSSColor, size_t DigitCount>
constexpr std::optional<facebook::react::CSSColor> facebook::react::detail::parseHexDigits(const char* value);
template <typename CSSColor>
constexpr std::optional<facebook::react::CSSColor> facebook::react::detail::parseLegacyRgbFunction(facebook::react::CSSValueParser& parser);
template <typename CSSColor>
Expand Down Expand Up @@ -10363,11 +10363,6 @@ class facebook::react::detail::OwnedBytesBuffer : public facebook::jsi::MutableB
public virtual uint8_t* data() override;
}

enum facebook::react::detail::HexColorType {
Long,
Short,
}

template <CSSDataType AlllowedType1T, CSSDataType AlllowedType2T, CSSMaybeCompoundDataType... RestT>
struct facebook::react::detail::merge_data_types<AlllowedType1T, AlllowedType2T, RestT...> {
public using type = typename facebook::react::detail::merge_data_types<std::variant<AlllowedType1T, AlllowedType2T>, RestT...>::type;
Expand Down
11 changes: 3 additions & 8 deletions scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -5656,19 +5656,19 @@ struct facebook::react::Bridging<std::shared_ptr<T>, std::enable_if_t<std::is_ba
}


constexpr bool facebook::react::detail::isHexDigit(char c);
constexpr bool facebook::react::detail::isValidHexColor(std::string_view hex);
constexpr auto facebook::react::detail::hexDigits;
constexpr float facebook::react::detail::hueToRgb(float p, float q, float t);
constexpr std::optional<float> facebook::react::detail::normalizeNumberComponent(const std::variant<std::monostate, facebook::react::CSSNumber>& component);
constexpr uint8_t facebook::react::detail::clamp255Component(float f);
constexpr uint8_t facebook::react::detail::clampAlpha(std::optional<float> alpha);
constexpr uint8_t facebook::react::detail::hexToNumeric(std::string_view hex, facebook::react::detail::HexColorType hexType);
float facebook::react::detail::normalizeHue(float hue);
std::optional<float> facebook::react::detail::normalizeHueComponent(const std::variant<std::monostate, facebook::react::CSSNumber, facebook::react::CSSAngle>& component);
std::tuple<uint8_t, uint8_t, uint8_t> facebook::react::detail::hslToRgb(float h, float s, float l);
std::tuple<uint8_t, uint8_t, uint8_t> facebook::react::detail::hwbToRgb(float h, float w, float b);
template <CSSDataType... FirstComponentAllowedTypesT>
constexpr bool facebook::react::detail::isLegacyColorFunction(facebook::react::CSSValueParser& parser);
template <typename CSSColor, size_t DigitCount>
constexpr std::optional<facebook::react::CSSColor> facebook::react::detail::parseHexDigits(const char* value);
template <typename CSSColor>
constexpr std::optional<facebook::react::CSSColor> facebook::react::detail::parseLegacyRgbFunction(facebook::react::CSSValueParser& parser);
template <typename CSSColor>
Expand Down Expand Up @@ -5697,11 +5697,6 @@ class facebook::react::detail::OwnedBytesBuffer : public facebook::jsi::MutableB
public virtual uint8_t* data() override;
}

enum facebook::react::detail::HexColorType {
Long,
Short,
}

template <CSSDataType AlllowedType1T, CSSDataType AlllowedType2T, CSSMaybeCompoundDataType... RestT>
struct facebook::react::detail::merge_data_types<AlllowedType1T, AlllowedType2T, RestT...> {
public using type = typename facebook::react::detail::merge_data_types<std::variant<AlllowedType1T, AlllowedType2T>, RestT...>::type;
Expand Down
Loading