From 0a60ec9deab31bcefa7ca9947276222f078b0c36 Mon Sep 17 00:00:00 2001 From: MayankSharma-2812 Date: Thu, 9 Jul 2026 16:01:37 -0700 Subject: [PATCH] fix: decimal values in transformOrigin string parser (#57487) Summary: Fixes incorrect parsing of decimal percentage and pixel values in the string form of `transformOrigin`. The current tokenizer only matches integer numeric values followed by `%` or `px`. As a result, decimal pixel values are silently corrupted. For example: ```js processTransformOrigin('12.5px 7.5px'); ``` currently returns: ```js [5, 5, 0] ``` instead of: ```js [12.5, 7.5, 0] ``` Decimal percentage values are also incorrectly tokenized. For example, `50.5% 30.2%` is split into partial matches instead of preserving the decimal values. This updates the numeric token pattern from `\d+(?:%|px)` to `\d*\.?\d+(?:%|px)`, supporting decimal values such as `12.5px`, `50.5%`, and leading-dot decimal values such as `.5%`. Regression coverage is added for decimal percentages, decimal pixels, and leading-dot decimal values. ## Changelog: [GENERAL] [FIXED] - Fix `transformOrigin` string parsing for decimal percentage and pixel values. Pull Request resolved: https://github.com/react/react-native/pull/57487 Test Plan: Added regression coverage in `processTransformOrigin-itest.js` for: * decimal percentages: `50.5% 30.2%` * decimal pixels: `12.5px 7.5px` * leading-dot decimals: `.5% .5px` A focused local execution against `processTransformOrigin` confirmed: ```text Before fix: "12.5px 7.5px" -> [5, 5, 0] After fix: "12.5px 7.5px" -> [12.5, 7.5, 0] ``` Also attempted the targeted Fantom integration test with: ```text node node_modules/jest/bin/jest.js --config private/react-native-fantom/config/jest.config.js packages/react-native/Libraries/StyleSheet/__tests__/processTransformOrigin-itest.js ``` The Fantom test could not complete locally because Metro failed to resolve a generated runtime setup path containing Windows path separators. Reviewed By: lenaic Differential Revision: D111240577 Pulled By: javache fbshipit-source-id: 425aeb598d1a454d9245f747441bd8040acf3288 --- .../__tests__/processTransformOrigin-itest.js | 9 ++++++ .../StyleSheet/processTransformOrigin.js | 3 +- .../css/tests/CSSTransformOriginTest.cpp | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/StyleSheet/__tests__/processTransformOrigin-itest.js b/packages/react-native/Libraries/StyleSheet/__tests__/processTransformOrigin-itest.js index 976060faab2..81a882f4175 100644 --- a/packages/react-native/Libraries/StyleSheet/__tests__/processTransformOrigin-itest.js +++ b/packages/react-native/Libraries/StyleSheet/__tests__/processTransformOrigin-itest.js @@ -113,5 +113,14 @@ describe('processTransformOrigin', () => { processTransformOrigin('top 30% center'); }).toThrow('Could not parse transform-origin: top 30% center'); }); + it('should handle decimal percentage and pixel values', () => { + expect(processTransformOrigin('50.5% 30.2%')).toEqual([ + '50.5%', + '30.2%', + 0, + ]); + expect(processTransformOrigin('12.5px 7.5px')).toEqual([12.5, 7.5, 0]); + expect(processTransformOrigin('.5% .5px')).toEqual(['.5%', 0.5, 0]); + }); }); }); diff --git a/packages/react-native/Libraries/StyleSheet/processTransformOrigin.js b/packages/react-native/Libraries/StyleSheet/processTransformOrigin.js index 75e4acacbd6..96cd040d814 100644 --- a/packages/react-native/Libraries/StyleSheet/processTransformOrigin.js +++ b/packages/react-native/Libraries/StyleSheet/processTransformOrigin.js @@ -11,7 +11,8 @@ import invariant from 'invariant'; // Pre-compiled regex pattern for performance - avoids regex compilation on each call -const TRANSFORM_ORIGIN_REGEX = /(top|bottom|left|right|center|\d+(?:%|px)|0)/gi; +const TRANSFORM_ORIGIN_REGEX = + /(top|bottom|left|right|center|\d*\.?\d+(?:%|px)|0)/gi; const INDEX_X = 0; const INDEX_Y = 1; diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTransformOriginTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTransformOriginTest.cpp index fbfbeac8bc0..3df2920f069 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTransformOriginTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSTransformOriginTest.cpp @@ -425,4 +425,34 @@ TEST(CSSTransformOrigin, percentage_percentage) { EXPECT_EQ(origin.z, CSSLength{}); } +TEST(CSSTransformOrigin, decimal_length_length) { + auto val = parseCSSProperty("12.5px 7.5px"); + EXPECT_TRUE(std::holds_alternative(val)); + auto& origin = std::get(val); + + EXPECT_TRUE(std::holds_alternative(origin.x)); + EXPECT_EQ(std::get(origin.x).value, 12.5f); + EXPECT_EQ(std::get(origin.x).unit, CSSLengthUnit::Px); + + EXPECT_TRUE(std::holds_alternative(origin.y)); + EXPECT_EQ(std::get(origin.y).value, 7.5f); + EXPECT_EQ(std::get(origin.y).unit, CSSLengthUnit::Px); + + EXPECT_EQ(origin.z, CSSLength{}); +} + +TEST(CSSTransformOrigin, decimal_percentage_percentage) { + auto val = parseCSSProperty("50.5% 30.2%"); + EXPECT_TRUE(std::holds_alternative(val)); + auto& origin = std::get(val); + + EXPECT_TRUE(std::holds_alternative(origin.x)); + EXPECT_EQ(std::get(origin.x).value, 50.5f); + + EXPECT_TRUE(std::holds_alternative(origin.y)); + EXPECT_EQ(std::get(origin.y).value, 30.2f); + + EXPECT_EQ(origin.z, CSSLength{}); +} + } // namespace facebook::react