Skip to content

feat(JS) - url function support in backgroundImage - #58246

Open
intergalacticspacehighway wants to merge 8 commits into
react:mainfrom
intergalacticspacehighway:background-image-url-parser
Open

intergalacticspacehighway wants to merge 8 commits into
react:mainfrom
intergalacticspacehighway:background-image-url-parser

Conversation

@intergalacticspacehighway

@intergalacticspacehighway intergalacticspacehighway commented Aug 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary:

Recreated from #54995, which was auto-closed when my fork was deleted.

Changelog:

[GENERAL] [ADDED] - url support with backgroundImage

Test Plan:

Screenshots

Each url() example in RN Tester, opened by deep link, with all three PRs merged.

Android emulator:

URL Image URL Image from local file Local file at its natural size (@3x asset, 33x33)

iOS simulator:

URL Image URL Image from local file Local file at its natural size (@3x asset, 33x33)

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 30, 2026
@intergalacticspacehighway
intergalacticspacehighway marked this pull request as draft August 30, 2026 13:05
@intergalacticspacehighway
intergalacticspacehighway force-pushed the background-image-url-parser branch 2 times, most recently from 54957cd to e6f8c65 Compare August 30, 2026 13:17
@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_BREAKING

@intergalacticspacehighway
intergalacticspacehighway force-pushed the background-image-url-parser branch 3 times, most recently from fb31afc to 0daf82c Compare August 31, 2026 08:49
type: 'url',
uri: require('../../assets/bunny.png'),
},
],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the mask PR requires commit from this one, as mentioned here

lint fix

add local image example
@github-actions

Copy link
Copy Markdown

Visual regression

1 visual difference(s) at a3c2c3960b39. Visual differences need maintainer approval. Review the report, then apply visual-change-approved.

ios button/color-red/default: Changed, 280048 px (11.234%)

diff

Expected and Captured
Expected Captured
expected captured

URLBackgroundImage::toDynamic() wrote only the type and uri, so with
enablePropsUpdateReconciliationAndroid a require() asset lost its
intrinsic size and Android drew it at its decoded pixel size.
With enableNativeCSSParsing, backgroundImage was sent unprocessed, and
native code cannot resolve an asset id, so require() url layers were
dropped. Resolve only those in JS and leave the rest to native parsing.
- The JS parser follows the CSS Syntax url token rules instead of a loose
  regex: an unquoted url cannot contain whitespace, quotes or parentheses,
  a quoted url must be a single string, and escapes are decoded. This
  matches the native parser, so a style no longer works only with one
  setting of enableNativeCSSParsing.
- An empty uri in the object syntax is rejected in JS and in native code.
  The native url conversion is shared by both parsing paths.
- Remove an unused forward declaration from BackgroundImage.h.
- Add tests for urls with parentheses, data uris and invalid urls.
A @3x asset with auto background size draws at its size in points, which
shows that the intrinsic size from the asset registry is used.
- consumePreservedToken for CSSURLFunction is no longer constexpr.
  CSSURLFunction holds a std::string, so with GCC 11's libstdc++ its
  std::optional is not a literal type and the Fantom build failed.
- Run clang-format on CSSBackgroundImage.h and its test.
- Update the C++ API snapshots to match the generated output.
Checked against Chrome:
- An empty url (`url()`, `url("")`, `{uri: ''}`) is valid. It stays a
  layer that draws nothing, so the other layers and their background
  sizes and positions keep their index.
- An invalid url (`url(my photo.png)`) makes the whole list invalid,
  in the JS string parser and natively.
- A malformed url object (missing or non-string uri, or an unknown
  asset) makes the whole list invalid, like an invalid gradient.
The native CSS parser treats a backslash in url() as an ordinary
character. The JS parser now does the same, so both parsers give the
same result.
@intergalacticspacehighway
intergalacticspacehighway marked this pull request as ready for review October 29, 2026 14:35
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants