Skip to content

[WIP] Fix url() css function removal in styled-components - #5639

Merged
quantizor merged 5 commits into
mainfrom
copilot/fix-url-css-function-issue
Jan 13, 2026
Merged

quantizor merged 5 commits into
mainfrom
copilot/fix-url-css-function-issue

Conversation

Copilot AI commented Jan 13, 2026 •

Copy link
Copy Markdown
Contributor
  • Analyze the issue: url() CSS function values containing // (like https://) were being stripped because // was treated as a line comment
  • Implement fix: Track url() context in stripLineComments function to skip comment detection inside URL functions
  • Microbenchmark and optimize (addressing @quantizor feedback):
    • Removed unnecessary isWordChar function and word boundary check
    • Used | 32 for efficient case-insensitive comparison
    • Final overhead: ~2.7% vs original (down from ~14%)
    • Bundle size reduced from 11.96KB to 11.89KB
  • Add comprehensive tests for all URL scenarios
  • All tests pass (420 tests)
  • Build passes with bundle size under limit (11.89KB < 12KB)
  • Create changeset
  • CodeQL security scan passed (0 alerts)
  • Run formatter and add rule to AGENTS.md to always run formatter before committing
Original prompt

This section details on the original issue you should resolve

<issue_title>Using url() css function from prop value is considered as comment and removed from styles</issue_title>
<issue_description>Using url('https://${prop.$url}') with prop is considered as comment since [email protected] and removed from generated styles in some cases.

Example:

The following code:

const ImagePlaceholder = styled.div`
  background-image: ${({ $placeHolderUrl }) => `url(${$placeHolderUrl})`};
  background-repeat: no-repeat;
  background-size: 100% 100%;
  width: 750px;
  height: 750px;
`

<ImagePlaceholder $placeHolderUrl="https://static.jowugc.com/750x750/placeholders/plate.png" />

Results in:
Image
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@changeset-bot

changeset-bot Bot commented Jan 13, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2e0a7db

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@quantizor

Copy link
Copy Markdown
Contributor

@copilot Ensure this is comprehensively fixed for all types of CSS functions and single/double/no-quote scenarios. This includes @font-face statements that have URLs and any other realistic scenario where a URL might occur in modern CSS. Use jest for both testing and experimentation to avoid dealing with typescript conversion issues.

Copilot AI commented Jan 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@copilot Ensure this is comprehensively fixed for all types of CSS functions and single/double/no-quote scenarios. This includes @font-face statements that have URLs and any other realistic scenario w...

Fixed in commits cca71a8 and 861cd9d. The fix now comprehensively handles all URL scenarios:

  • Unquoted URLs: url(https://example.com), url(http://...), url(file://...)
  • Protocol-relative URLs: url(//example.com)
  • Quoted URLs: url("https://..."), url('https://...')
  • Data URLs: url(data:image/png;base64,...)
  • @font-face with single/multiple src URLs
  • @import url(...)
  • image-set(url(...) 1x, url(...) 2x)
  • Mix of url() and actual line comments

Also added word boundary checking to prevent false matches like foourl( and bounds checking for safety. All 420 tests pass and bundle size is 11.96KB (under 12KB limit).

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • firebasedynamiclinks.googleapis.com
    • Triggering command: /usr/local/bin/node node /home/REDACTED/work/styled-components/styled-components/node_modules/.bin/bundlewatch (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@quantizor

Copy link
Copy Markdown
Contributor

@copilot This is one of the hottest paths in the system, please microbenchmark your approach and remove anything "extra" that does not directly contribute to the fix.

Copilot AI commented Jan 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@copilot This is one of the hottest paths in the system, please microbenchmark your approach and remove anything "extra" that does not directly contribute to the fix.

Optimized in ad51318. Microbenchmark results:

  • Original overhead reduced from ~14% to ~2.7%
  • Removed isWordChar function and word boundary check (unnecessary - string tracking handles foourl( cases)
  • Used | 32 for efficient case-insensitive char comparison
  • Bundle size reduced from 11.96KB to 11.89KB

Benchmark: ~304ns per call, adding only ~8ns overhead to the original implementation.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • firebasedynamiclinks.googleapis.com
    • Triggering command: /usr/local/bin/node node /home/REDACTED/work/styled-components/styled-components/node_modules/.bin/bundlewatch (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@quantizor
quantizor marked this pull request as ready for review January 13, 2026 15:46
@quantizor

Copy link
Copy Markdown
Contributor

@copilot run the formatter, add an instruction to AGENTS.md to ensure the formatter is always run before committing code changes

Copilot AI commented Jan 13, 2026

Copy link
Copy Markdown
Contributor Author

@copilot run the formatter, add an instruction to AGENTS.md to ensure the formatter is always run before committing code changes

Done in 2e0a7db. Ran the formatter and added the following rule to AGENTS.md:

- Always run the formatter (`pnpm --filter styled-components prettier`) before committing code changes

Copilot AI requested a review from quantizor January 13, 2026 15:51
@quantizor
quantizor merged commit 189bc17 into main Jan 13, 2026
@quantizor
quantizor deleted the copilot/fix-url-css-function-issue branch January 13, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using url() css function from prop value is considered as comment and removed from styles

2 participants