Skip to content

Commit bb2bea2

Browse files
javachemeta-codesync[bot]
authored andcommitted
Add Fantom test for backgroundImage parsing (#54185)
Summary: Pull Request resolved: #54185 Validate that the changes in D83341309 work across both experiment variants. Changelog: [Internal] Reviewed By: andrewdacenko Differential Revision: D84924308
1 parent e13862c commit bb2bea2

File tree

16 files changed

+241
-23
lines changed

16 files changed

+241
-23
lines changed

packages/react-native/Libraries/Components/View/__tests__/View-itest.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @flow strict-local
8+
* @fantom_flags enableNativeCSSParsing:*
89
* @format
910
*/
1011

@@ -229,6 +230,56 @@ describe('<View>', () => {
229230
});
230231
});
231232
});
233+
234+
describe('background-image', () => {
235+
it('it parses CSS and object syntax', () => {
236+
const root = Fantom.createRoot();
237+
238+
Fantom.runTask(() => {
239+
root.render(
240+
<>
241+
<View
242+
style={{
243+
experimental_backgroundImage:
244+
'radial-gradient(#e66465, #9198e5)',
245+
}}
246+
/>
247+
<View
248+
style={{
249+
experimental_backgroundImage: [
250+
{
251+
type: 'radial-gradient',
252+
shape: 'ellipse',
253+
position: {top: '50%', right: '50%'},
254+
size: 'farthest-corner',
255+
colorStops: [{color: '#e66465'}, {color: '#9198e5'}],
256+
},
257+
],
258+
}}
259+
/>
260+
</>,
261+
);
262+
});
263+
264+
const expectedProps = {
265+
backgroundImage:
266+
'[radial-gradient(ellipse farthest-corner at 50% 50% , rgba(230, 100, 101, 1), rgba(145, 152, 229, 1))]',
267+
};
268+
269+
expect(root.getRenderedOutput().toJSON()).toEqual([
270+
{
271+
children: [],
272+
props: expectedProps,
273+
type: 'View',
274+
},
275+
{
276+
children: [],
277+
props: expectedProps,
278+
type: 'View',
279+
},
280+
]);
281+
});
282+
});
232283
});
233284

234285
describe('pointerEvents', () => {

packages/react-native/ReactCommon/react/renderer/components/view/BackgroundImagePropsConversions.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <react/renderer/core/RawProps.h>
1313
#include <react/renderer/graphics/BackgroundImage.h>
1414

15-
#include <string>
16-
1715
namespace facebook::react {
1816

1917
void parseProcessedBackgroundImage(

packages/react-native/ReactCommon/react/renderer/components/view/BaseViewProps.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,10 @@ SharedDebugStringConvertibleList BaseViewProps::getDebugProps() const {
612612
defaultBaseViewProps.pointerEvents),
613613
debugStringConvertibleItem(
614614
"transform", transform, defaultBaseViewProps.transform),
615+
debugStringConvertibleItem(
616+
"backgroundImage",
617+
backgroundImage,
618+
defaultBaseViewProps.backgroundImage),
615619
};
616620
}
617621
#endif

packages/react-native/ReactCommon/react/renderer/components/view/conversions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <react/renderer/css/CSSNumber.h>
1919
#include <react/renderer/css/CSSPercentage.h>
2020
#include <react/renderer/css/CSSValueParser.h>
21+
#include <react/renderer/debug/flags.h>
2122
#include <react/renderer/graphics/BlendMode.h>
2223
#include <react/renderer/graphics/Isolation.h>
2324
#include <react/renderer/graphics/LinearGradient.h>
@@ -565,7 +566,7 @@ inline void fromRawValue(
565566
}
566567

567568
inline void fromRawValue(
568-
const PropsParserContext& context,
569+
const PropsParserContext& /*context*/,
569570
const RawValue& value,
570571
Transform& result) {
571572
auto transformMatrix = Transform{};
@@ -1225,6 +1226,7 @@ inline void fromRawValue(
12251226
result = isolation.value();
12261227
}
12271228

1229+
#if RN_DEBUG_STRING_CONVERTIBLE
12281230
template <size_t N>
12291231
inline std::string toString(const std::array<float, N> vec) {
12301232
std::string s;
@@ -1422,5 +1424,6 @@ inline std::string toString(const Transform& transform) {
14221424
result += "]";
14231425
return result;
14241426
}
1427+
#endif
14251428

14261429
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/core/graphicsConversions.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,7 @@ inline int toAndroidRepr(const SharedColor& color) {
4646
#endif
4747

4848
inline std::string toString(const SharedColor& value) {
49-
ColorComponents components = colorComponentsFromColor(value);
50-
std::array<char, 255> buffer{};
51-
std::snprintf(
52-
buffer.data(),
53-
buffer.size(),
54-
"rgba(%.0f, %.0f, %.0f, %g)",
55-
components.red * 255.f,
56-
components.green * 255.f,
57-
components.blue * 255.f,
58-
components.alpha);
59-
return buffer.data();
49+
return value.toString();
6050
}
6151

6252
#pragma mark - Geometry

packages/react-native/ReactCommon/react/renderer/graphics/BackgroundImage.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#pragma once
99

10-
#include <react/renderer/graphics/ColorComponents.h>
1110
#include <react/renderer/graphics/LinearGradient.h>
1211
#include <react/renderer/graphics/RadialGradient.h>
1312

@@ -19,4 +18,27 @@ using BackgroundImage = std::variant<LinearGradient, RadialGradient>;
1918
folly::dynamic toDynamic(const BackgroundImage& backgroundImage);
2019
#endif
2120

21+
#if RN_DEBUG_STRING_CONVERTIBLE
22+
inline std::string toString(std::vector<BackgroundImage>& value) {
23+
std::stringstream ss;
24+
25+
ss << "[";
26+
for (size_t i = 0; i < value.size(); i++) {
27+
if (i > 0) {
28+
ss << ", ";
29+
}
30+
31+
const auto& backgroundImage = value[i];
32+
if (std::holds_alternative<LinearGradient>(backgroundImage)) {
33+
std::get<LinearGradient>(backgroundImage).toString(ss);
34+
} else if (std::holds_alternative<RadialGradient>(backgroundImage)) {
35+
std::get<RadialGradient>(backgroundImage).toString(ss);
36+
}
37+
}
38+
ss << "]";
39+
40+
return ss.str();
41+
}
42+
#endif
43+
2244
}; // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77

88
#include "Color.h"
99

10+
#include <array>
11+
1012
namespace facebook::react {
1113

14+
std::string SharedColor::toString() const noexcept {
15+
ColorComponents components = colorComponentsFromColor(*this);
16+
std::array<char, 255> buffer{};
17+
std::snprintf(
18+
buffer.data(),
19+
buffer.size(),
20+
"rgba(%.0f, %.0f, %.0f, %g)",
21+
components.red * 255.f,
22+
components.green * 255.f,
23+
components.blue * 255.f,
24+
components.alpha);
25+
return buffer.data();
26+
}
27+
1228
bool isColorMeaningful(const SharedColor& color) noexcept {
1329
if (!color) {
1430
return false;

packages/react-native/ReactCommon/react/renderer/graphics/Color.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
#pragma once
99

10-
#include <functional>
11-
1210
#include <react/renderer/graphics/ColorComponents.h>
1311
#include <react/renderer/graphics/HostPlatformColor.h>
1412

13+
#include <functional>
14+
#include <string>
15+
1516
#ifdef RN_SERIALIZABLE_STATE
1617
#include <folly/dynamic.h>
1718
#endif
@@ -52,6 +53,8 @@ class SharedColor {
5253
return color_ != HostPlatformColor::UndefinedColor;
5354
}
5455

56+
std::string toString() const noexcept;
57+
5558
private:
5659
Color color_;
5760
};

packages/react-native/ReactCommon/react/renderer/graphics/ColorStop.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
#pragma once
99

10+
#include <react/renderer/debug/flags.h>
1011
#include <react/renderer/graphics/Color.h>
1112
#include <react/renderer/graphics/Float.h>
1213
#include <react/renderer/graphics/ValueUnit.h>
1314
#include <optional>
15+
#include <sstream>
1416

1517
namespace facebook::react {
1618

@@ -22,6 +24,16 @@ struct ColorStop {
2224
#ifdef RN_SERIALIZABLE_STATE
2325
folly::dynamic toDynamic() const;
2426
#endif
27+
28+
#if RN_DEBUG_STRING_CONVERTIBLE
29+
void toString(std::stringstream& ss) const {
30+
ss << color.toString();
31+
if (position.unit != UnitType::Undefined) {
32+
ss << " ";
33+
ss << position.toString();
34+
}
35+
}
36+
#endif
2537
};
2638

2739
struct ProcessedColorStop {

packages/react-native/ReactCommon/react/renderer/graphics/DoubleConversions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ std::string toString(double doubleValue, char suffix) {
2929
// Serialize infinite and NaN as 0
3030
builder.AddCharacter('0');
3131
}
32-
builder.AddCharacter(suffix);
32+
if (suffix != '\0') {
33+
builder.AddCharacter(suffix);
34+
}
3335
return builder.Finalize();
3436
}
3537

0 commit comments

Comments
 (0)