From d0acce91775603695ea30520d2c82eab8a3adbbb Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 24 Feb 2026 00:03:05 -0800 Subject: [PATCH] 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 --- .../View/ReactNativeStyleAttributes.js | 6 +- .../renderer/components/view/conversions.h | 200 +++++++++++++++++- .../components/view/tests/ConversionsTest.cpp | 107 ++++++++++ .../react/renderer/css/CSSTransform.h | 5 + .../react/renderer/graphics/ValueUnit.h | 8 +- 5 files changed, 320 insertions(+), 6 deletions(-) diff --git a/packages/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js b/packages/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js index 54b64fc50db..c40b7b84353 100644 --- a/packages/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js +++ b/packages/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js @@ -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}, /** diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h index b39e27fe4a8..652791b14e4 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -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 &value) +{ + if (std::holds_alternative(value)) { + auto len = std::get(value); + if (len.unit != CSSLengthUnit::Px) { + return {}; + } + return {len.value, UnitType::Point}; + } else { + return {std::get(value).value, UnitType::Percent}; + } +} + +inline std::optional 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 { + using T = std::decay_t; + + if constexpr (std::is_same_v) { + auto radians = static_cast(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) { + auto radians = static_cast(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) { + auto radians = static_cast(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) { + auto radians = static_cast(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) { + 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) { + 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) { + 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) { + 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) { + 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) { + return TransformOperation{ + .type = TransformOperationType::Scale, .x = ValueUnit(func.value, UnitType::Point), .y = One, .z = One}; + } + + if constexpr (std::is_same_v) { + return TransformOperation{ + .type = TransformOperationType::Scale, .x = One, .y = ValueUnit(func.value, UnitType::Point), .z = One}; + } + + if constexpr (std::is_same_v) { + auto radians = static_cast(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) { + auto radians = static_cast(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) { + 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) { + 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>()); @@ -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(value); + if (!std::holds_alternative(transformList)) { + result = {}; + return; + } + + auto transformMatrix = Transform{}; + const auto &cssFuncs = std::get(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(cssFunc)) { + const auto &m = std::get(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()) { + 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>()) { diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp index 8c286f20694..a8d1cf19d85 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/tests/ConversionsTest.cpp @@ -9,6 +9,7 @@ #include #include +#include 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(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 diff --git a/packages/react-native/ReactCommon/react/renderer/css/CSSTransform.h b/packages/react-native/ReactCommon/react/renderer/css/CSSTransform.h index 2f66f891410..367080c9beb 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/CSSTransform.h +++ b/packages/react-native/ReactCommon/react/renderer/css/CSSTransform.h @@ -452,6 +452,11 @@ using CSSTransformFunction = CSSCompoundDataType< CSSSkewY, CSSPerspective>; +/** + * Variant of possible CSS transform function types + */ +using CSSTransformFunctionVariant = CSSVariantWithTypes; + /** * Represents the type. * https://drafts.csswg.org/css-transforms-1/#typedef-transform-list diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.h b/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.h index f8f2fd420eb..156d27abac6 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/ValueUnit.h @@ -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); }