Wire native CSS parsing for transform (#55677)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55677

Gate `processTransform` behind `enableNativeCSSParsing()`. When the flag is on, CSS transform strings like `"rotate(45deg) scale(2)"` are parsed natively using the existing CSS transform parser instead of being preprocessed in JS.

Changelog: [Internal]

Reviewed By: jorge-cab

Differential Revision: D94052735

fbshipit-source-id: 4d0b97b3587012853b10ca02270e3d631839ec80
This commit is contained in:
Nick Gerleman authored and meta-codesync[bot] committed 2026-02-24 00:03:05 -08:00
1 parent 6c83352a42
commit d0acce9177
5 files changed
+320 -6

No files matched your search

@@ -57,6 +57,10 @@ export const backgroundRepeatAttribute: AnyAttributeType = nativeCSSParsing
? true
: {process: processBackgroundRepeat};
export const transformAttribute: AnyAttributeType = nativeCSSParsing
? true
: {process: processTransform};
const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
/**
* Layout
@@ -150,7 +154,7 @@ const ReactNativeStyleAttributes: {[string]: AnyAttributeType, ...} = {
/**
* Transform
*/
transform: {process: processTransform},
transform: transformAttribute,
transformOrigin: {process: processTransformOrigin},
/**
@@ -9,6 +9,7 @@
#include <glog/logging.h>
#include <react/debug/react_native_expect.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/renderer/components/view/primitives.h>
#include <react/renderer/core/LayoutMetrics.h>
#include <react/renderer/core/PropsParserContext.h>
@@ -17,6 +18,9 @@
#include <react/renderer/css/CSSAngle.h>
#include <react/renderer/css/CSSNumber.h>
#include <react/renderer/css/CSSPercentage.h>
#include <react/renderer/css/CSSRatio.h>
#include <react/renderer/css/CSSTransform.h>
#include <react/renderer/css/CSSTransformOrigin.h>
#include <react/renderer/css/CSSValueParser.h>
#include <react/renderer/debug/flags.h>
#include <react/renderer/graphics/BackgroundPosition.h>
@@ -533,7 +537,136 @@ inline void fromRawValue(const PropsParserContext & /*context*/, const RawValue
result = toValueUnit(value);
}
inline void fromRawValue(const PropsParserContext & /*context*/, const RawValue &value, Transform &result)
inline ValueUnit cssLengthPercentageToValueUnit(const std::variant<CSSLength, CSSPercentage> &value)
{
if (std::holds_alternative<CSSLength>(value)) {
auto len = std::get<CSSLength>(value);
if (len.unit != CSSLengthUnit::Px) {
return {};
}
return {len.value, UnitType::Point};
} else {
return {std::get<CSSPercentage>(value).value, UnitType::Percent};
}
}
inline std::optional<TransformOperation> fromCSSTransformFunction(const CSSTransformFunctionVariant &cssTransform)
{
constexpr auto Zero = ValueUnit(0, UnitType::Point);
constexpr auto One = ValueUnit(1, UnitType::Point);
return std::visit(
[&](auto &&func) -> std::optional<TransformOperation> {
using T = std::decay_t<decltype(func)>;
if constexpr (std::is_same_v<T, CSSRotate>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = Zero, .z = ValueUnit(radians, UnitType::Point)};
}
if constexpr (std::is_same_v<T, CSSRotateX>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = ValueUnit(radians, UnitType::Point), .y = Zero, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSRotateY>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = ValueUnit(radians, UnitType::Point), .z = Zero};
}
if constexpr (std::is_same_v<T, CSSRotateZ>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = Zero, .z = ValueUnit(radians, UnitType::Point)};
}
if constexpr (std::is_same_v<T, CSSTranslate>) {
auto x = cssLengthPercentageToValueUnit(func.x);
auto y = cssLengthPercentageToValueUnit(func.y);
if (!x || !y) {
return std::nullopt;
}
return TransformOperation{.type = TransformOperationType::Translate, .x = x, .y = y, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSTranslateX>) {
auto x = cssLengthPercentageToValueUnit(func.value);
if (!x) {
return std::nullopt;
}
return TransformOperation{.type = TransformOperationType::Translate, .x = x, .y = Zero, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSTranslateY>) {
auto y = cssLengthPercentageToValueUnit(func.value);
if (!y) {
return std::nullopt;
}
return TransformOperation{.type = TransformOperationType::Translate, .x = Zero, .y = y, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSTranslate3D>) {
auto x = cssLengthPercentageToValueUnit(func.x);
auto y = cssLengthPercentageToValueUnit(func.y);
if (!x || !y || func.z.unit != CSSLengthUnit::Px) {
return std::nullopt;
}
return TransformOperation{
.type = TransformOperationType::Translate, .x = x, .y = y, .z = ValueUnit(func.z.value, UnitType::Point)};
}
if constexpr (std::is_same_v<T, CSSScale>) {
return TransformOperation{
.type = TransformOperationType::Scale,
.x = ValueUnit(func.x, UnitType::Point),
.y = ValueUnit(func.y, UnitType::Point),
.z = One};
}
if constexpr (std::is_same_v<T, CSSScaleX>) {
return TransformOperation{
.type = TransformOperationType::Scale, .x = ValueUnit(func.value, UnitType::Point), .y = One, .z = One};
}
if constexpr (std::is_same_v<T, CSSScaleY>) {
return TransformOperation{
.type = TransformOperationType::Scale, .x = One, .y = ValueUnit(func.value, UnitType::Point), .z = One};
}
if constexpr (std::is_same_v<T, CSSSkewX>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Skew, .x = ValueUnit(radians, UnitType::Point), .y = Zero, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSSkewY>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Skew, .x = Zero, .y = ValueUnit(radians, UnitType::Point), .z = Zero};
}
if constexpr (std::is_same_v<T, CSSPerspective>) {
if (func.length.unit != CSSLengthUnit::Px) {
return std::nullopt;
}
return TransformOperation{
.type = TransformOperationType::Perspective,
.x = ValueUnit(func.length.value, UnitType::Point),
.y = Zero,
.z = Zero};
}
if constexpr (std::is_same_v<T, CSSMatrix>) {
return TransformOperation{.type = TransformOperationType::Arbitrary, .x = Zero, .y = Zero, .z = Zero};
}
},
cssTransform);
}
inline void parseProcessedTransform(const PropsParserContext & /*context*/, const RawValue &value, Transform &result)
{
auto transformMatrix = Transform{};
react_native_expect(value.hasType<std::vector<RawValue>>());
@@ -770,6 +903,71 @@ inline void fromRawValue(const PropsParserContext & /*context*/, const RawValue
result = transformMatrix;
}
inline void parseUnprocessedTransformString(const std::string &value, Transform &result)
{
auto transformList = parseCSSProperty<CSSTransformList>(value);
if (!std::holds_alternative<CSSTransformList>(transformList)) {
result = {};
return;
}
auto transformMatrix = Transform{};
const auto &cssFuncs = std::get<CSSTransformList>(transformList);
transformMatrix.operations.reserve(cssFuncs.size());
for (const auto &cssFunc : cssFuncs) {
auto op = fromCSSTransformFunction(cssFunc);
if (!op.has_value()) {
result = {};
return;
}
if (op->type == TransformOperationType::Arbitrary) {
// CSSMatrix: expand 6-value 2D matrix to 4x4 matrix
if (std::holds_alternative<CSSMatrix>(cssFunc)) {
const auto &m = std::get<CSSMatrix>(cssFunc);
transformMatrix.matrix[0] = m.values[0];
transformMatrix.matrix[1] = m.values[1];
transformMatrix.matrix[2] = 0;
transformMatrix.matrix[3] = 0;
transformMatrix.matrix[4] = m.values[2];
transformMatrix.matrix[5] = m.values[3];
transformMatrix.matrix[6] = 0;
transformMatrix.matrix[7] = 0;
transformMatrix.matrix[8] = 0;
transformMatrix.matrix[9] = 0;
transformMatrix.matrix[10] = 1;
transformMatrix.matrix[11] = 0;
transformMatrix.matrix[12] = m.values[4];
transformMatrix.matrix[13] = m.values[5];
transformMatrix.matrix[14] = 0;
transformMatrix.matrix[15] = 1;
}
}
transformMatrix.operations.push_back(*op);
}
result = transformMatrix;
}
inline void parseUnprocessedTransform(const PropsParserContext &context, const RawValue &value, Transform &result)
{
if (value.hasType<std::string>()) {
parseUnprocessedTransformString((std::string)value, result);
} else {
parseProcessedTransform(context, value, result);
}
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, Transform &result)
{
if (ReactNativeFeatureFlags::enableNativeCSSParsing()) {
parseUnprocessedTransform(context, value, result);
} else {
parseProcessedTransform(context, value, result);
}
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, TransformOrigin &result)
{
if (!value.hasType<std::vector<RawValue>>()) {
@@ -9,6 +9,7 @@
#include <react/renderer/components/view/BoxShadowPropsConversions.h>
#include <react/renderer/components/view/FilterPropsConversions.h>
#include <react/renderer/components/view/conversions.h>
namespace facebook::react {
@@ -250,4 +251,110 @@ TEST(ConversionsTest, unprocessed_filter_objects_unknown_type) {
EXPECT_TRUE(filters.empty());
}
TEST(ConversionsTest, unprocessed_transform_css_string) {
Transform result;
parseUnprocessedTransformString(
"rotate(45deg) scale(2) translateX(10px)", result);
EXPECT_EQ(result.operations.size(), 3);
// rotate(45deg) -> Rotate, z = 45 * PI / 180
EXPECT_EQ(result.operations[0].type, TransformOperationType::Rotate);
EXPECT_NEAR(
result.operations[0].z.value,
static_cast<float>(45.0 * M_PI / 180.0),
0.001f);
// scale(2) -> Scale, x=2, y=2
EXPECT_EQ(result.operations[1].type, TransformOperationType::Scale);
EXPECT_EQ(result.operations[1].x.value, 2.0f);
EXPECT_EQ(result.operations[1].y.value, 2.0f);
// translateX(10px) -> Translate, x=10
EXPECT_EQ(result.operations[2].type, TransformOperationType::Translate);
EXPECT_EQ(result.operations[2].x.value, 10.0f);
EXPECT_EQ(result.operations[2].x.unit, UnitType::Point);
EXPECT_EQ(result.operations[2].y.value, 0.0f);
}
TEST(ConversionsTest, unprocessed_transform_css_translate_percent) {
Transform result;
parseUnprocessedTransformString("translate(10px, 50%)", result);
EXPECT_EQ(result.operations.size(), 1);
EXPECT_EQ(result.operations[0].type, TransformOperationType::Translate);
EXPECT_EQ(result.operations[0].x.value, 10.0f);
EXPECT_EQ(result.operations[0].x.unit, UnitType::Point);
EXPECT_EQ(result.operations[0].y.value, 50.0f);
EXPECT_EQ(result.operations[0].y.unit, UnitType::Percent);
}
TEST(ConversionsTest, unprocessed_transform_css_perspective) {
Transform result;
parseUnprocessedTransformString("perspective(500px)", result);
EXPECT_EQ(result.operations.size(), 1);
EXPECT_EQ(result.operations[0].type, TransformOperationType::Perspective);
EXPECT_EQ(result.operations[0].x.value, 500.0f);
}
TEST(ConversionsTest, unprocessed_transform_css_invalid_string) {
Transform result;
parseUnprocessedTransformString("not-a-transform", result);
EXPECT_TRUE(result.operations.empty());
}
TEST(ConversionsTest, unprocessed_transform_rawvalue_string) {
RawValue value{folly::dynamic("rotate(45deg) scale(2)")};
Transform result;
parseUnprocessedTransform(
PropsParserContext{-1, ContextContainer{}}, value, result);
EXPECT_EQ(result.operations.size(), 2);
EXPECT_EQ(result.operations[0].type, TransformOperationType::Rotate);
EXPECT_EQ(result.operations[1].type, TransformOperationType::Scale);
}
TEST(ConversionsTest, unprocessed_transform_rawvalue_array) {
RawValue value{folly::dynamic::array(
folly::dynamic::object("rotate", "45deg"),
folly::dynamic::object("scale", 2))};
Transform result;
parseUnprocessedTransform(
PropsParserContext{-1, ContextContainer{}}, value, result);
EXPECT_EQ(result.operations.size(), 2);
EXPECT_EQ(result.operations[0].type, TransformOperationType::Rotate);
EXPECT_EQ(result.operations[1].type, TransformOperationType::Scale);
EXPECT_EQ(result.operations[1].x.value, 2.0f);
}
TEST(ConversionsTest, unprocessed_transform_rawvalue_matrix) {
RawValue value{folly::dynamic::array(
folly::dynamic::object(
"matrix",
folly::dynamic::array(
1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)))};
Transform result;
parseUnprocessedTransform(
PropsParserContext{-1, ContextContainer{}}, value, result);
EXPECT_EQ(result.operations.size(), 1);
EXPECT_EQ(result.operations[0].type, TransformOperationType::Arbitrary);
}
TEST(ConversionsTest, unprocessed_transform_rawvalue_translate_percent) {
RawValue value{
folly::dynamic::array(folly::dynamic::object("translateX", "50%"))};
Transform result;
parseUnprocessedTransform(
PropsParserContext{-1, ContextContainer{}}, value, result);
EXPECT_EQ(result.operations.size(), 1);
EXPECT_EQ(result.operations[0].type, TransformOperationType::Translate);
EXPECT_EQ(result.operations[0].x.value, 50.0f);
EXPECT_EQ(result.operations[0].x.unit, UnitType::Percent);
}
} // namespace facebook::react
@@ -452,6 +452,11 @@ using CSSTransformFunction = CSSCompoundDataType<
CSSSkewY,
CSSPerspective>;
/**
* Variant of possible CSS transform function types
*/
using CSSTransformFunctionVariant = CSSVariantWithTypes<CSSTransformFunction>;
/**
* Represents the <transform-list> type.
* https://drafts.csswg.org/css-transforms-1/#typedef-transform-list
@@ -26,15 +26,15 @@ struct ValueUnit {
float value{0.0f};
UnitType unit{UnitType::Undefined};
ValueUnit() = default;
ValueUnit(float v, UnitType u) : value(v), unit(u) {}
constexpr ValueUnit() = default;
constexpr ValueUnit(float v, UnitType u) : value(v), unit(u) {}
bool operator==(const ValueUnit &other) const
constexpr bool operator==(const ValueUnit &other) const
{
return value == other.value && unit == other.unit;
}
bool operator!=(const ValueUnit &other) const
constexpr bool operator!=(const ValueUnit &other) const
{
return !(*this == other);
}