Skip to content

Commit 367e044

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 06e8778 commit 367e044

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-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,91 @@
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+
}
47+
48+
TEST(CSSCommaSeparatedList, multiple_values) {
49+
auto simpleValue =
50+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20, 30, 40");
51+
EXPECT_TRUE(
52+
std::holds_alternative<CSSCommaSeparatedList<CSSNumber>>(simpleValue));
53+
EXPECT_EQ(std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue).size(), 3);
54+
EXPECT_EQ(
55+
std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue)[0].value, 20);
56+
EXPECT_EQ(
57+
std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue)[1].value, 30);
58+
EXPECT_EQ(
59+
std::get<CSSCommaSeparatedList<CSSNumber>>(simpleValue)[2].value, 40);
60+
61+
auto whitespaceValue =
62+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(" 20 , 30 , 40 ");
63+
EXPECT_TRUE(std::holds_alternative<CSSCommaSeparatedList<CSSNumber>>(
64+
whitespaceValue));
65+
EXPECT_EQ(
66+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue).size(), 3);
67+
EXPECT_EQ(
68+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[0].value, 20);
69+
EXPECT_EQ(
70+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[1].value, 30);
71+
EXPECT_EQ(
72+
std::get<CSSCommaSeparatedList<CSSNumber>>(whitespaceValue)[2].value, 40);
73+
}
74+
75+
TEST(CSSCommaSeparatedList, extra_tokens) {
76+
auto extraTokensValue =
77+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20, 30, 40 50");
78+
EXPECT_TRUE(std::holds_alternative<std::monostate>(extraTokensValue));
79+
}
80+
81+
TEST(CSSCommaSeparatedList, extra_commas) {
82+
auto prefixCommaValue =
83+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>(",20");
84+
EXPECT_TRUE(std::holds_alternative<std::monostate>(prefixCommaValue));
85+
86+
auto suffixCommaValue =
87+
parseCSSProperty<CSSCommaSeparatedList<CSSNumber>>("20,");
88+
EXPECT_TRUE(std::holds_alternative<std::monostate>(suffixCommaValue));
89+
}
90+
91+
} // namespace facebook::react

0 commit comments

Comments
 (0)