CSSWhitespaceSeparatedList (#49152)

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

For parsing a variable number of whitespace separated data types.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D68849561

fbshipit-source-id: be3314990d9e7c202c02deba463d79e50985c0b7
This commit is contained in:
Nick Gerleman
2025-02-04 12:16:40 -08:00
committed by Facebook GitHub Bot
parent 68946780d0
commit b985831702
4 changed files with 72 additions and 21 deletions
@@ -15,22 +15,17 @@
namespace facebook::react {
/**
* Represents a comma-separated repetition of a given single type.
* https://www.w3.org/TR/css-values-4/#mult-comma
*/
template <CSSDataType AllowedTypeT>
struct CSSCommaSeparatedList : public std::vector<AllowedTypeT> {};
template <CSSDataType AllowedTypeT, CSSDelimiter Delim>
struct CSSList : public std::vector<AllowedTypeT> {};
template <CSSDataType AllowedTypeT>
struct CSSDataTypeParser<CSSCommaSeparatedList<AllowedTypeT>> {
template <CSSDataType AllowedTypeT, CSSDelimiter Delim>
struct CSSDataTypeParser<CSSList<AllowedTypeT, Delim>> {
static inline auto consume(CSSSyntaxParser& parser)
-> std::optional<CSSCommaSeparatedList<AllowedTypeT>> {
CSSCommaSeparatedList<AllowedTypeT> result;
-> std::optional<CSSList<AllowedTypeT, Delim>> {
CSSList<AllowedTypeT, Delim> result;
for (auto nextValue = parseNextCSSValue<AllowedTypeT>(parser);
!std::holds_alternative<std::monostate>(nextValue);
nextValue =
parseNextCSSValue<AllowedTypeT>(parser, CSSDelimiter::Comma)) {
nextValue = parseNextCSSValue<AllowedTypeT>(parser, Delim)) {
result.push_back(std::move(std::get<AllowedTypeT>(nextValue)));
}
@@ -42,4 +37,19 @@ struct CSSDataTypeParser<CSSCommaSeparatedList<AllowedTypeT>> {
}
};
/**
* Represents a comma-separated repetition of a given single type.
* https://www.w3.org/TR/css-values-4/#mult-comma
*/
template <CSSDataType AllowedTypeT>
using CSSCommaSeparatedList = CSSList<AllowedTypeT, CSSDelimiter::Comma>;
/**
* Represents a whitespace-separated repetition of a given single type.
* https://www.w3.org/TR/css-values-4/#component-combinators
*/
template <CSSDataType AllowedTypeT>
using CSSWhitespaceSeparatedList =
CSSList<AllowedTypeT, CSSDelimiter::Whitespace>;
} // namespace facebook::react
@@ -11,10 +11,10 @@
#include <tuple>
#include <react/renderer/css/CSSColor.h>
#include <react/renderer/css/CSSCommaSeparatedList.h>
#include <react/renderer/css/CSSDataType.h>
#include <react/renderer/css/CSSKeyword.h>
#include <react/renderer/css/CSSLength.h>
#include <react/renderer/css/CSSList.h>
#include <react/renderer/css/CSSValueParser.h>
#include <react/utils/to_underlying.h>
@@ -6,13 +6,13 @@
*/
#include <gtest/gtest.h>
#include <react/renderer/css/CSSCommaSeparatedList.h>
#include <react/renderer/css/CSSList.h>
#include <react/renderer/css/CSSNumber.h>
#include <react/renderer/css/CSSValueParser.h>
namespace facebook::react {
TEST(CSSCommaSeparatedList, empty_values) {
TEST(CSSList, empty_values) {
auto emptyValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("");
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
@@ -23,7 +23,7 @@ TEST(CSSCommaSeparatedList, empty_values) {
auto commaValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(",");
}
TEST(CSSCommaSeparatedList, single_value) {
TEST(CSSList, single_value) {
auto simpleValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20");
EXPECT_TRUE(
std::holds_alternative<CSSCommaSeparatedList<CSSNumber>>(simpleValue));
@@ -41,12 +41,12 @@ TEST(CSSCommaSeparatedList, single_value) {
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[0].value, 20);
}
TEST(CSSCommaSeparatedList, wrong_type) {
TEST(CSSList, wrong_type) {
auto simpleValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20px");
EXPECT_TRUE(std::holds_alternative<std::monostate>(simpleValue));
}
TEST(CSSCommaSeparatedList, multiple_values) {
TEST(CSSList, multiple_comma_values) {
auto simpleValue =
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20, 30, 40");
EXPECT_TRUE(
@@ -73,13 +73,54 @@ TEST(CSSCommaSeparatedList, multiple_values) {
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[2].value, 40);
}
TEST(CSSCommaSeparatedList, extra_tokens) {
TEST(CSSList, multiple_space_values) {
auto simpleValue =
parseCSSProperty<CSSWhitespaceSeparatedList<CSSNumber>>("20 30 40");
EXPECT_TRUE(std::holds_alternative<CSSWhitespaceSeparatedList<CSSNumber>>(
simpleValue));
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(simpleValue).size(), 3);
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(simpleValue)[0].value,
20);
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(simpleValue)[1].value,
30);
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(simpleValue)[2].value,
40);
auto whitespaceValue =
parseCSSProperty<CSSWhitespaceSeparatedList<CSSNumber>>(" 20 \n 30 40 ");
EXPECT_TRUE(std::holds_alternative<CSSWhitespaceSeparatedList<CSSNumber>>(
whitespaceValue));
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(whitespaceValue).size(),
3);
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(whitespaceValue)[0].value,
20);
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(whitespaceValue)[1].value,
30);
EXPECT_EQ(
std::get<CSSWhitespaceSeparatedList<CSSNumber>>(whitespaceValue)[2].value,
40);
}
TEST(CSSList, extra_comma_tokens) {
auto extraTokensValue =
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20, 30, 40 50");
EXPECT_TRUE(std::holds_alternative<std::monostate>(extraTokensValue));
}
TEST(CSSCommaSeparatedList, extra_commas) {
TEST(CSSList, extra_space_tokens) {
auto extraTokensValue =
parseCSSProperty<CSSWhitespaceSeparatedList<CSSNumber>>("20 30 40 ,50");
EXPECT_TRUE(std::holds_alternative<std::monostate>(extraTokensValue));
}
TEST(CSSList, extra_commas) {
auto prefixCommaValue =
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(",20");
EXPECT_TRUE(std::holds_alternative<std::monostate>(prefixCommaValue));
@@ -6,7 +6,7 @@
*/
#include <gtest/gtest.h>
#include <react/renderer/css/CSSCommaSeparatedList.h>
#include <react/renderer/css/CSSList.h>
#include <react/renderer/css/CSSShadow.h>
#include <react/renderer/css/CSSValueParser.h>