Skip to content

Commit a4b112c

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Do not consume delimeter when not consuming component value (#48841)
Summary: Pull Request resolved: #48841 Right now during parsing we can ask for a next component value, with a delimeter, and even if we don't have a component value to consume, we will consume the delimeter. This is kind of awkward since e.g. trailing comma can be consumed, then we think syntax is valid. Let's try changing this. Changelog: [Internal] Reviewed By: lenaic Differential Revision: D68474739 fbshipit-source-id: 47a942681bc8472ca28470eba821d4d95306ae5d
1 parent 5b3d8d3 commit a4b112c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/react-native/ReactCommon/react/renderer/css/CSSSyntaxParser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,14 @@ struct CSSComponentValueVisitorDispatcher {
233233
constexpr ReturnT consumeComponentValue(
234234
CSSDelimiter delimiter,
235235
const VisitorsT&... visitors) {
236+
auto originalParser = parser;
236237
if (!consumeDelimiter(delimiter)) {
238+
parser = originalParser;
237239
return {};
238240
}
239241

240242
if (parser.peek().type() == parser.terminator_) {
243+
parser = originalParser;
241244
return {};
242245
}
243246

packages/react-native/ReactCommon/react/renderer/css/tests/CSSSyntaxParserTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,31 @@ TEST(CSSSyntaxParser, solidus_or_whitespace) {
579579
EXPECT_FALSE(delimValue1);
580580
}
581581

582+
TEST(CSSSyntaxParser, delimeter_not_consumed_when_no_component_value) {
583+
CSSSyntaxParser parser{"foo ,"};
584+
585+
auto identValue = parser.consumeComponentValue<std::string_view>(
586+
[](const CSSPreservedToken& token) {
587+
EXPECT_EQ(token.type(), CSSTokenType::Ident);
588+
EXPECT_EQ(token.stringValue(), "foo");
589+
return token.stringValue();
590+
});
591+
592+
EXPECT_EQ(identValue, "foo");
593+
594+
auto identValue2 = parser.consumeComponentValue<bool>(
595+
CSSDelimiter::Comma,
596+
[](const CSSPreservedToken& /*token*/) { return true; });
597+
598+
EXPECT_FALSE(identValue2);
599+
600+
auto hasComma = parser.consumeComponentValue<bool>(
601+
CSSDelimiter::Whitespace, [](const CSSPreservedToken& token) {
602+
EXPECT_EQ(token.type(), CSSTokenType::Comma);
603+
return true;
604+
});
605+
606+
EXPECT_TRUE(hasComma);
607+
}
608+
582609
} // namespace facebook::react

0 commit comments

Comments
 (0)