Skip to content

Commit 1ad69d9

Browse files
jorge-cabfacebook-github-bot
authored andcommitted
Add <hash-token> consumption for CSSTokenizer (#44613)
Summary: Pull Request resolved: #44613 We are building a native CSS parser for Fabric, to allow broader support for CSS. One area not yet implemented are features required for color. <hash-token> is a pre-requisite. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D57180293 fbshipit-source-id: 0932c44d881f205aed55bcdf4fa23ad04b336c11
1 parent 93c079b commit 1ad69d9

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum class CSSTokenType {
3131
OpenSquare,
3232
Percentage,
3333
WhiteSpace,
34+
Hash,
3435
};
3536

3637
/*

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class CSSTokenizer {
6161
} else {
6262
return consumeDelim();
6363
}
64+
case '#':
65+
if (isIdent(peek(1))) {
66+
return consumeHash();
67+
} else {
68+
return consumeDelim();
69+
}
6470
}
6571

6672
if (isDigit(nextChar)) {
@@ -225,6 +231,14 @@ class CSSTokenizer {
225231
return {CSSTokenType::Ident, consumeRunningValue()};
226232
}
227233

234+
constexpr CSSToken consumeHash() {
235+
// https://www.w3.org/TR/css-syntax-3/#consume-token (U+0023 NUMBER SIGN)
236+
advance();
237+
consumeRunningValue();
238+
239+
return {CSSTokenType::Hash, consumeIdentSequence().stringValue()};
240+
}
241+
228242
constexpr std::string_view consumeRunningValue() {
229243
auto next = remainingCharacters_.substr(0, position_);
230244
remainingCharacters_ = remainingCharacters_.substr(next.size());

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,21 @@ TEST(CSSTokenizer, invalid_values) {
202202
CSSToken{CSSTokenType::EndOfFile});
203203
}
204204

205+
TEST(CSSTokenizer, hash_values) {
206+
EXPECT_TOKENS(
207+
"#Ff03BC",
208+
CSSToken{CSSTokenType::Hash, "Ff03BC"},
209+
CSSToken{CSSTokenType::EndOfFile});
210+
211+
EXPECT_TOKENS(
212+
"#identifier",
213+
CSSToken{CSSTokenType::Hash, "identifier"},
214+
CSSToken{CSSTokenType::EndOfFile});
215+
216+
EXPECT_TOKENS(
217+
"#*",
218+
CSSToken{CSSTokenType::Delim, "#"},
219+
CSSToken{CSSTokenType::Delim, "*"},
220+
CSSToken{CSSTokenType::EndOfFile});
221+
}
205222
} // namespace facebook::react

0 commit comments

Comments
 (0)