Skip to content

Commit 189bc17

Browse files
Copilotquantizor
andauthored
[WIP] Fix url() css function removal in styled-components (#5639)
* Initial plan * fix: preserve // in url() CSS functions to prevent incorrect comment stripping Co-authored-by: quantizor <[email protected]> * fix: add word boundary check and bounds check for url() detection Co-authored-by: quantizor <[email protected]> * perf: optimize url() tracking with minimal overhead (~2.7%) Co-authored-by: quantizor <[email protected]> * chore: run formatter and add formatting rule to AGENTS.md Co-authored-by: quantizor <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: quantizor <[email protected]>
1 parent 816720a commit 189bc17

4 files changed

Lines changed: 249 additions & 4 deletions

File tree

‎.changeset/fix-url-css-function.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"styled-components": patch
3+
---
4+
5+
Fix url() CSS function values being incorrectly stripped when using unquoted URLs containing `//` (e.g., `url(https://example.com)`). The `//` in protocol URLs like `https://`, `http://`, `file://`, and protocol-relative URLs was incorrectly being treated as a JavaScript-style line comment.

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Agent rules
1414

1515
- Create changesets for public-facing changes
1616
- Do not edit the CHANGELOG.md file, it is auto-generated by changesets
17+
- Always run the formatter (`pnpm --filter styled-components prettier`) before committing code changes
1718

1819
Root Level
1920

‎packages/styled-components/src/utils/stylis.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ const OPEN_BRACE = 123; // {
1616
const CLOSE_BRACE = 125; // }
1717
const SEMICOLON = 59; // ;
1818
const NEWLINE = 10; // \n
19+
const OPEN_PAREN = 40; // (
20+
const CLOSE_PAREN = 41; // )
1921

2022
/**
2123
* Strips JS-style line comments (//) from CSS, handling comments anywhere
22-
* in the line while preserving strings and valid CSS.
24+
* in the line while preserving strings, url() contents, and valid CSS.
2325
* Optimized with early bail and charCodeAt for performance.
2426
*/
2527
function stripLineComments(css: string): string {
@@ -31,6 +33,7 @@ function stripLineComments(css: string): string {
3133
let start = 0;
3234
let i = 0;
3335
let inString = 0; // 0 = none, DOUBLE_QUOTE or SINGLE_QUOTE when in string
36+
let urlDepth = 0; // Track nesting depth inside url()
3437

3538
while (i < len) {
3639
const code = css.charCodeAt(i);
@@ -55,8 +58,29 @@ function stripLineComments(css: string): string {
5558
continue;
5659
}
5760

58-
// Check for line comment
59-
if (code === SLASH && css.charCodeAt(i + 1) === SLASH) {
61+
// Track url() context - check for 'url(' (case insensitive via | 32)
62+
if (
63+
code === OPEN_PAREN &&
64+
i >= 3 &&
65+
(css.charCodeAt(i - 1) | 32) === 108 && // l
66+
(css.charCodeAt(i - 2) | 32) === 114 && // r
67+
(css.charCodeAt(i - 3) | 32) === 117 // u
68+
) {
69+
urlDepth = 1;
70+
i++;
71+
continue;
72+
}
73+
74+
// Track nested parentheses inside url()
75+
if (urlDepth > 0) {
76+
if (code === CLOSE_PAREN) urlDepth--;
77+
else if (code === OPEN_PAREN) urlDepth++;
78+
i++;
79+
continue;
80+
}
81+
82+
// Check for line comment (only when not in url())
83+
if (code === SLASH && i + 1 < len && css.charCodeAt(i + 1) === SLASH) {
6084
if (i > start) parts.push(css.substring(start, i));
6185
// Skip to end of line
6286
while (i < len && css.charCodeAt(i) !== NEWLINE) {
@@ -69,7 +93,7 @@ function stripLineComments(css: string): string {
6993
i++;
7094
}
7195

72-
// No comments found after indexOf check means // was in a string
96+
// No comments found after indexOf check means // was in a string or url()
7397
if (start === 0) return css;
7498
if (start < len) parts.push(css.substring(start));
7599
return parts.join('');

‎packages/styled-components/src/utils/test/stylis.test.ts‎

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,219 @@ background-color: green;`)
268268
`);
269269
});
270270
});
271+
272+
describe('url() function handling', () => {
273+
it('preserves unquoted https:// URL in url()', () => {
274+
expect(
275+
stylisTest(`
276+
background-image: url(https://example.com/image.png);
277+
background-color: green;
278+
`)
279+
).toMatchInlineSnapshot(`
280+
[
281+
".a{background-image:url(https://example.com/image.png);background-color:green;}",
282+
]
283+
`);
284+
});
285+
286+
it('preserves unquoted http:// URL in url()', () => {
287+
expect(
288+
stylisTest(`
289+
background-image: url(http://example.com/image.png);
290+
background-color: green;
291+
`)
292+
).toMatchInlineSnapshot(`
293+
[
294+
".a{background-image:url(http://example.com/image.png);background-color:green;}",
295+
]
296+
`);
297+
});
298+
299+
it('preserves double-quoted URL in url()', () => {
300+
expect(
301+
stylisTest(`
302+
background-image: url("https://example.com/image.png");
303+
background-color: green;
304+
`)
305+
).toMatchInlineSnapshot(`
306+
[
307+
".a{background-image:url("https://example.com/image.png");background-color:green;}",
308+
]
309+
`);
310+
});
311+
312+
it('preserves single-quoted URL in url()', () => {
313+
expect(
314+
stylisTest(`
315+
background-image: url('https://example.com/image.png');
316+
background-color: green;
317+
`)
318+
).toMatchInlineSnapshot(`
319+
[
320+
".a{background-image:url('https://example.com/image.png');background-color:green;}",
321+
]
322+
`);
323+
});
324+
325+
it('preserves data URL in url()', () => {
326+
expect(
327+
stylisTest(`
328+
background-image: url(data:image/png;base64,iVBORw0KGgo=);
329+
background-color: green;
330+
`)
331+
).toMatchInlineSnapshot(`
332+
[
333+
".a{background-image:url(data:image/png;base64,iVBORw0KGgo=);background-color:green;}",
334+
]
335+
`);
336+
});
337+
338+
it('preserves URL with path and query params', () => {
339+
expect(
340+
stylisTest(`
341+
background-image: url(https://example.com/path/to/image.png?v=1&size=large);
342+
background-color: green;
343+
`)
344+
).toMatchInlineSnapshot(`
345+
[
346+
".a{background-image:url(https://example.com/path/to/image.png?v=1&size=large);background-color:green;}",
347+
]
348+
`);
349+
});
350+
351+
it('preserves multiple url() declarations', () => {
352+
expect(
353+
stylisTest(`
354+
background-image: url(https://example.com/bg.png);
355+
cursor: url(https://example.com/cursor.png), auto;
356+
list-style-image: url(https://example.com/bullet.png);
357+
`)
358+
).toMatchInlineSnapshot(`
359+
[
360+
".a{background-image:url(https://example.com/bg.png);cursor:url(https://example.com/cursor.png),auto;list-style-image:url(https://example.com/bullet.png);}",
361+
]
362+
`);
363+
});
364+
365+
it('preserves @font-face with unquoted URL', () => {
366+
expect(
367+
stylisTest(`
368+
@font-face {
369+
font-family: 'MyFont';
370+
src: url(https://example.com/fonts/myfont.woff2) format('woff2');
371+
}
372+
`)
373+
).toMatchInlineSnapshot(`
374+
[
375+
"@font-face{font-family:'MyFont';src:url(https://example.com/fonts/myfont.woff2) format('woff2');}",
376+
]
377+
`);
378+
});
379+
380+
it('preserves @font-face with multiple src URLs', () => {
381+
expect(
382+
stylisTest(`
383+
@font-face {
384+
font-family: 'MyFont';
385+
src: url(https://example.com/fonts/myfont.woff2) format('woff2'),
386+
url(https://example.com/fonts/myfont.woff) format('woff');
387+
}
388+
`)
389+
).toMatchInlineSnapshot(`
390+
[
391+
"@font-face{font-family:'MyFont';src:url(https://example.com/fonts/myfont.woff2) format('woff2'),url(https://example.com/fonts/myfont.woff) format('woff');}",
392+
]
393+
`);
394+
});
395+
396+
it('preserves @import with URL', () => {
397+
expect(
398+
stylisTest(`
399+
@import url(https://example.com/styles.css);
400+
background-color: green;
401+
`)
402+
).toMatchInlineSnapshot(`
403+
[
404+
".a{background-color:green;}",
405+
"@import url(https://example.com/styles.css);",
406+
]
407+
`);
408+
});
409+
410+
it('preserves url() with file:// protocol', () => {
411+
expect(
412+
stylisTest(`
413+
background-image: url(file:///path/to/image.png);
414+
background-color: green;
415+
`)
416+
).toMatchInlineSnapshot(`
417+
[
418+
".a{background-image:url(file:///path/to/image.png);background-color:green;}",
419+
]
420+
`);
421+
});
422+
423+
it('preserves url() with protocol-relative URL', () => {
424+
expect(
425+
stylisTest(`
426+
background-image: url(//example.com/image.png);
427+
background-color: green;
428+
`)
429+
).toMatchInlineSnapshot(`
430+
[
431+
".a{background-image:url(//example.com/image.png);background-color:green;}",
432+
]
433+
`);
434+
});
435+
436+
it('handles mix of url() and line comments', () => {
437+
expect(
438+
stylisTest(`
439+
background-image: url(https://example.com/image.png); // this is a comment
440+
background-color: green;
441+
`)
442+
).toMatchInlineSnapshot(`
443+
[
444+
".a{background-image:url(https://example.com/image.png);background-color:green;}",
445+
]
446+
`);
447+
});
448+
449+
it('preserves srcset URLs in image-set()', () => {
450+
expect(
451+
stylisTest(`
452+
background-image: image-set(url(https://example.com/image.png) 1x, url(https://example.com/[email protected]) 2x);
453+
background-color: green;
454+
`)
455+
).toMatchInlineSnapshot(`
456+
[
457+
".a{background-image:image-set(url(https://example.com/image.png) 1x, url(https://example.com/[email protected]) 2x);background-color:green;}",
458+
]
459+
`);
460+
});
461+
462+
it('does not match url inside other words (word boundary check)', () => {
463+
// 'foourl(' should not be treated as url()
464+
expect(
465+
stylisTest(`
466+
width: 100px; // comment
467+
content: "foourl(https://example.com)";
468+
background-color: green;
469+
`)
470+
).toMatchInlineSnapshot(`
471+
[
472+
".a{width:100px;content:"foourl(https://example.com)";background-color:green;}",
473+
]
474+
`);
475+
});
476+
477+
it('preserves url() at the start of CSS', () => {
478+
expect(stylisTest(`background-image: url(https://example.com/image.png);`))
479+
.toMatchInlineSnapshot(`
480+
[
481+
".a{background-image:url(https://example.com/image.png);}",
482+
]
483+
`);
484+
});
485+
});
271486
});

0 commit comments

Comments
 (0)