Skip to content

Commit

Permalink
fix: Correctly process urls when they are rewritten to contain space
Browse files Browse the repository at this point in the history
If a CSS file contains

background: url(folder/foo.png)

and the processing of the URL causes "folder" to be rewritten to e.g. "other folder", then the end result cannot be

background: url(other folder/foo.png)

because using space without wrapping the url with ' or " is not allowed.

This rewrites the url in this case to

background: url('other folder/foo.png')
  • Loading branch information
Artur- committed Mar 25, 2022
1 parent d649dab commit 294fa91
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Expand Up @@ -54,6 +54,12 @@ test('css import from js', async () => {
await untilUpdated(() => getColor(atImport), 'blue')
})

test('css import asset with space', async () => {
const importedWithSpace = await page.$('.import-with-space')

expect(await getBg(importedWithSpace)).toMatch(/.*ok\..*png/)
})

test('postcss config', async () => {
const imported = await page.$('.postcss .nesting')
expect(await getColor(imported)).toBe('pink')
Expand Down
Binary file added packages/playground/css/folder with space/ok.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/playground/css/folder with space/space.css
@@ -0,0 +1,5 @@
.import-with-space {
color: green;
background: url(spacefolder/ok.png);
background-position: center;
}
1 change: 1 addition & 0 deletions packages/playground/css/imported.css
@@ -1,4 +1,5 @@
@import './imported-at-import.css';
@import 'spacefolder/space.css';

.imported {
color: green;
Expand Down
4 changes: 4 additions & 0 deletions packages/playground/css/index.html
Expand Up @@ -10,6 +10,10 @@ <h1>CSS</h1>
<p class="imported-at-import">
@import in import from js: This should be purple
</p>
<p class="import-with-space">
@import from file with space: This should be green and have a background
image
</p>
<p>Imported css string:</p>
<pre class="imported-css"></pre>
<pre class="imported-css-glob"></pre>
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/css/vite.config.js
Expand Up @@ -9,7 +9,8 @@ module.exports = {
},
resolve: {
alias: {
'@': __dirname
'@': __dirname,
spacefolder: __dirname + '/folder with space'
}
},
css: {
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -997,7 +997,13 @@ async function doUrlReplace(
return matched
}

return `url(${wrap}${await replacer(rawUrl)}${wrap})`
const newUrl = await replacer(rawUrl)
if (wrap === '' && newUrl != encodeURI(newUrl)) {
// The new url might need wrapping even if the original did not have it, e.g. if a space was added during replacement
wrap = "'"
}
const ret = `url(${wrap}${newUrl}${wrap})`
return ret
}

async function doImportCSSReplace(
Expand Down

0 comments on commit 294fa91

Please sign in to comment.