Skip to content

Commit 566c292

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
CSSCommaSeparatedList (#48987)
Summary: Adds a data type parser for a variable number of values of a given single data type (at least 1). E.g. `CSSCommaSeparatedList<CSSShadow>` will represent the syntax of `<shadow>#` (ie the value produced by box-shadow). Changelog: [internal] Reviewed By: lenaic Differential Revision: D68738165
1 parent ff105a8 commit 566c292

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <optional>
11+
#include <variant>
12+
13+
#include <react/renderer/css/CSSDataType.h>
14+
#include <react/renderer/css/CSSValueParser.h>
15+
16+
namespace facebook::react {
17+
18+
/**
19+
* Represents a comma-separated repetition of a given single type.
20+
* https://www.w3.org/TR/css-values-4/#mult-comma
21+
*/
22+
template <CSSDataType AllowedTypeT>
23+
struct CSSCommaSeparatedList : public std::vector<AllowedTypeT> {};
24+
25+
template <CSSDataType AllowedTypeT>
26+
struct CSSDataTypeParser<CSSCommaSeparatedList<AllowedTypeT>> {
27+
static inline auto consume(CSSSyntaxParser& parser)
28+
-> std::optional<CSSCommaSeparatedList<AllowedTypeT>> {
29+
CSSCommaSeparatedList<AllowedTypeT> result;
30+
for (auto nextValue = parseNextCSSValue<AllowedTypeT>(parser);
31+
!std::holds_alternative<std::monostate>(nextValue);
32+
nextValue =
33+
parseNextCSSValue<AllowedTypeT>(parser, CSSDelimiter::Comma)) {
34+
result.push_back(std::move(std::get<AllowedTypeT>(nextValue)));
35+
}
36+
37+
if (result.empty()) {
38+
return {};
39+
}
40+
41+
return result;
42+
}
43+
};
44+
45+
} // namespace facebook::react
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <gtest/gtest.h>
9+
#include <react/renderer/css/CSSCommaSeparatedList.h>
10+
#include <react/renderer/css/CSSNumber.h>
11+
#include <react/renderer/css/CSSValueParser.h>
12+
13+
namespace facebook::react {
14+
15+
TEST(CSSCommaSeparatedList, empty_values) {
16+
auto emptyValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("");
17+
EXPECT_TRUE(std::holds_alternative<std::monostate>(emptyValue));
18+
19+
auto whitespaceValue =
20+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(" ");
21+
EXPECT_TRUE(std::holds_alternative<std::monostate>(whitespaceValue));
22+
23+
auto commaValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(",");
24+
}
25+
26+
TEST(CSSCommaSeparatedList, single_value) {
27+
auto simpleValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20");
28+
EXPECT_TRUE(
29+
std::holds_alternative<CSSCommaSeparatedList<CSSNumber>>(simpleValue));
30+
EXPECT_EQ(std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue).size(), 1);
31+
EXPECT_EQ(
32+
std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue)[0].value, 20);
33+
34+
auto whitespaceValue =
35+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(" 20 ");
36+
EXPECT_TRUE(std::holds_alternative<CSSCommaSeparatedList<CSSNumber>>(
37+
whitespaceValue));
38+
EXPECT_EQ(
39+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue).size(), 1);
40+
EXPECT_EQ(
41+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[0].value, 20);
42+
}
43+
44+
TEST(CSSCommaSeparatedList, wrong_type) {
45+
auto simpleValue = parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20px");
46+
EXPECT_TRUE(std::holds_alternative<std::monostate>(simpleValue));
47+
}
48+
49+
TEST(CSSCommaSeparatedList, multiple_values) {
50+
auto simpleValue =
51+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20, 30, 40");
52+
EXPECT_TRUE(
53+
std::holds_alternative<CSSCommaSeparatedList<CSSNumber>>(simpleValue));
54+
EXPECT_EQ(std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue).size(), 3);
55+
EXPECT_EQ(
56+
std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue)[0].value, 20);
57+
EXPECT_EQ(
58+
std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue)[1].value, 30);
59+
EXPECT_EQ(
60+
std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue)[2].value, 40);
61+
62+
auto whitespaceValue =
63+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(" 20 , 30 , 40 ");
64+
EXPECT_TRUE(std::holds_alternative<CSSCommaSeparatedList<CSSNumber>>(
65+
whitespaceValue));
66+
EXPECT_EQ(
67+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue).size(), 3);
68+
EXPECT_EQ(
69+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[0].value, 20);
70+
EXPECT_EQ(
71+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[1].value, 30);
72+
EXPECT_EQ(
73+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[2].value, 40);
74+
}
75+
76+
TEST(CSSCommaSeparatedList, extra_tokens) {
77+
auto extraTokensValue =
78+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20, 30, 40 50");
79+
EXPECT_TRUE(std::holds_alternative<std::monostate>(extraTokensValue));
80+
}
81+
82+
TEST(CSSCommaSeparatedList, extra_commas) {
83+
auto prefixCommaValue =
84+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(",20");
85+
EXPECT_TRUE(std::holds_alternative<std::monostate>(prefixCommaValue));
86+
87+
auto suffixCommaValue =
88+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20,");
89+
EXPECT_TRUE(std::holds_alternative<std::monostate>(suffixCommaValue));
90+
}
91+
92+
} // namespace facebook::react

0 commit comments

Comments
 (0)