Skip to content

Commit c1e8beb

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
[skip ci] Do not consume delimeter when not consuming component value (facebook#48841)
Summary: 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
1 parent 4628eb2 commit c1e8beb

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: packages/react-native/ReactCommon/react/renderer/css/CSSSyntaxParser.h

+3
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

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

+27
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)