Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parser to support generic data types #48718

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/react-native/ReactCommon/react/renderer/css/CSSAngle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

#pragma once

#include <optional>

#include <react/renderer/css/CSSAngleUnit.h>
#include <react/renderer/css/CSSDataType.h>

namespace facebook::react {

/**
* Representation of CSS <angle> data type
* https://www.w3.org/TR/css-values-4/#angles
*/
struct CSSAngle {
float degrees{};
};

template <>
struct CSSDataTypeParser<CSSAngle> {
static constexpr auto consumePreservedToken(const CSSPreservedToken& token)
-> std::optional<CSSAngle> {
if (token.type() == CSSTokenType::Dimension) {
if (auto unit = parseCSSAngleUnit(token.unit())) {
return CSSAngle{canonicalize(token.numericValue(), *unit)};
}
}
return {};
}
};

static_assert(CSSDataType<CSSAngle>);

} // namespace facebook::react
46 changes: 46 additions & 0 deletions packages/react-native/ReactCommon/react/renderer/css/CSSColor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/

#pragma once

#include <optional>

#include <react/renderer/css/CSSDataType.h>
#include <react/renderer/css/CSSHexColor.h>
#include <react/renderer/css/CSSNamedColor.h>

namespace facebook::react {

/**
* Representation of CSS <color> data type
* https://www.w3.org/TR/css-color-5/#typedef-color
*/
struct CSSColor {
uint8_t r{};
uint8_t g{};
uint8_t b{};
uint8_t a{};
};

template <>
struct CSSDataTypeParser<CSSColor> {
static constexpr auto consumePreservedToken(const CSSPreservedToken& token)
-> std::optional<CSSColor> {
switch (token.type()) {
case CSSTokenType::Ident:
return parseCSSNamedColor<CSSColor>(token.stringValue());
case CSSTokenType::Hash:
return parseCSSHexColor<CSSColor>(token.stringValue());
default:
return {};
}
}
};

static_assert(CSSDataType<CSSColor>);

} // namespace facebook::react
Loading
Loading