Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): escape double quotes in url() when lightningcss is used #18997

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3237,21 +3237,25 @@ async function compileLightningCSS(
let css = decoder.decode(res.code)
for (const dep of res.dependencies!) {
switch (dep.type) {
case 'url':
case 'url': {
let replaceUrl: string
if (skipUrlReplacer(dep.url)) {
css = css.replace(dep.placeholder, () => dep.url)
break
}
if (urlReplacer) {
const replaceUrl = await urlReplacer(
dep.url,
toAbsolute(dep.loc.filePath),
)
css = css.replace(dep.placeholder, () => replaceUrl)
replaceUrl = dep.url
} else if (urlReplacer) {
replaceUrl = await urlReplacer(dep.url, toAbsolute(dep.loc.filePath))
} else {
css = css.replace(dep.placeholder, () => dep.url)
replaceUrl = dep.url
}

css = css.replace(
dep.placeholder,
// lightningcss always generates `url("placeholder")`
// (`url('placeholder')`, `url(placeholder)` is not generated)
// so escape double quotes
() => replaceUrl.replaceAll('"', '\\"'),
)
break
}
default:
throw new Error(`Unsupported dependency type: ${dep.type}`)
}
Expand Down
6 changes: 6 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ describe('css url() references', () => {
expect(bg).toMatch(assetMatch)
})

test('preinlined SVG', async () => {
expect(await getBg('.css-url-preinlined-svg')).toMatch(
/data:image\/svg\+xml,.+/,
)
})

Comment on lines +279 to +284
Copy link
Member Author

Choose a reason for hiding this comment

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

When lightningcss is not used, preinlined SVGs were working without this PR. I added this so that we don't break this usecase in future.

test.runIf(isBuild)('generated paths in CSS', () => {
const css = findAssetFile(/index-[-\w]{8}\.css$/, 'foo')

Expand Down
5 changes: 5 additions & 0 deletions playground/assets/css/css-url.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ <h2>CSS url references</h2>
<div class="css-url-aliased">
<span style="background: #fff">CSS background (aliased)</span>
</div>
<div class="css-url-preinlined-svg">
<span style="background: #fff">CSS background (pre inlined SVG)</span>
</div>
<div class="css-manual-chunks-relative">
<span style="background: #fff"
>CSS nested manual chunks relative base background</span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ test('aliased asset', async () => {
const bg = await getBg('.css-url-aliased')
expect(bg).toMatch('data:image/svg+xml,')
})

test('preinlined SVG', async () => {
expect(await getBg('.css-url-preinlined-svg')).toMatch(
/data:image\/svg\+xml,.+/,
)
})
5 changes: 5 additions & 0 deletions playground/css-lightningcss/css-url.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions playground/css-lightningcss/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ <h1>Lightning CSS</h1>
<div class="css-url-aliased">
<span style="background: #fff">CSS background (aliased)</span>
</div>
<div class="css-url-preinlined-svg">
<span style="background: #fff">CSS background (pre inlined SVG)</span>
</div>
</div>

<script type="module" src="./main.js"></script>
Loading