forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip ci] CSSDataTypeParser consume() function (facebook#48986)
Summary: This adds a new `consume()` function to data type parsers which passes a raw parser. This can be used for types which are compounds of other data types, where we may want to accept more than the first token. This will be used for shadow parsing, but also fixes a hypothetical future bug with ratios. E.g. `calc(foo) / calc(bar)` may be a valid ratio, not starting with a token. We instead just want to try to parse a number data type from the stream. The form of parsing a preserved token + rest is removed, with the assumption that anything parsing more than a single token should use compound parsing. Changelog: [Internal] Reviewed By: lenaic Differential Revision: D68735370
- Loading branch information
1 parent
7465607
commit 55d6ae6
Showing
5 changed files
with
174 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/react-native/ReactCommon/react/renderer/css/tests/CSSValueParserTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include <react/renderer/css/CSSDataType.h> | ||
#include <react/renderer/css/CSSNumber.h> | ||
#include <react/renderer/css/CSSValueParser.h> | ||
|
||
namespace facebook::react { | ||
|
||
struct ConsumeDataType { | ||
float number{}; | ||
|
||
constexpr bool operator==(const ConsumeDataType& other) const = default; | ||
}; | ||
|
||
template <> | ||
struct CSSDataTypeParser<ConsumeDataType> { | ||
constexpr static std::optional<ConsumeDataType> consume( | ||
CSSSyntaxParser& parser) { | ||
auto val = parseNextCSSValue<CSSNumber>(parser); | ||
if (std::holds_alternative<CSSNumber>(val)) { | ||
return ConsumeDataType{std::get<CSSNumber>(val).value}; | ||
} | ||
|
||
return {}; | ||
} | ||
}; | ||
|
||
static_assert(CSSDataType<ConsumeDataType>); | ||
|
||
TEST(CSSValueParser, consume_multiple_with_delimeter) { | ||
CSSSyntaxParser parser{"1 2, 3, 4 / 5"}; | ||
|
||
auto next = parseNextCSSValue<ConsumeDataType>(parser); | ||
EXPECT_TRUE(std::holds_alternative<ConsumeDataType>(next)); | ||
EXPECT_EQ(std::get<ConsumeDataType>(next).number, 1); | ||
|
||
next = parseNextCSSValue<ConsumeDataType>(parser, CSSDelimiter::None); | ||
EXPECT_FALSE(std::holds_alternative<ConsumeDataType>(next)); | ||
|
||
next = parseNextCSSValue<ConsumeDataType>(parser, CSSDelimiter::Whitespace); | ||
EXPECT_TRUE(std::holds_alternative<ConsumeDataType>(next)); | ||
EXPECT_EQ(std::get<ConsumeDataType>(next).number, 2); | ||
|
||
next = parseNextCSSValue<ConsumeDataType>(parser, CSSDelimiter::Comma); | ||
EXPECT_TRUE(std::holds_alternative<ConsumeDataType>(next)); | ||
EXPECT_EQ(std::get<ConsumeDataType>(next).number, 3); | ||
|
||
next = parseNextCSSValue<ConsumeDataType>(parser, CSSDelimiter::Comma); | ||
EXPECT_TRUE(std::holds_alternative<ConsumeDataType>(next)); | ||
EXPECT_EQ(std::get<ConsumeDataType>(next).number, 4); | ||
|
||
next = parseNextCSSValue<ConsumeDataType>(parser, CSSDelimiter::Solidus); | ||
EXPECT_TRUE(std::holds_alternative<ConsumeDataType>(next)); | ||
EXPECT_EQ(std::get<ConsumeDataType>(next).number, 5); | ||
|
||
next = parseNextCSSValue<ConsumeDataType>(parser); | ||
EXPECT_FALSE(std::holds_alternative<ConsumeDataType>(next)); | ||
} | ||
|
||
} // namespace facebook::react |