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: multiple css url separation (fix #3922) #3926

Merged
merged 3 commits into from
Jun 24, 2021
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
24 changes: 24 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,27 @@ test('PostCSS dir-dependency', async () => {
await untilUpdated(() => getColor(el2), 'black')
}
})

test('Url separation', async () => {
const urlSeparated = await page.$('.url-separated')
const baseUrl = 'url(images/dog.webp)'
const cases = new Array(5)
.fill('')
.flatMap((_, i) =>
[',', ' ,', ', ', ' , '].map(
(sep) => `background-image:${new Array(i + 1).fill(baseUrl).join(sep)};`
)
)

// Insert the base case
cases.unshift('background-image:url(images/cat.webp),url(images/dog.webp)')

for (const [c, i] of cases.map((c, i) => [c, i]) as [string, number][]) {
// Replace the previous case
if (i > 0) editFile('imported.css', (code) => code.replace(cases[i - 1], c))

expect(await getBg(urlSeparated)).toMatch(
/^url\(.+\)(?:\s*,\s*url\(.+\))*$/
)
}
})
5 changes: 5 additions & 0 deletions packages/playground/css/imported.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ pre {
color: pink;
}
}

/* test url comma separation */
.url-separated {
background-image:url(images/cat.webp),url(images/dog.webp);
}
4 changes: 4 additions & 0 deletions packages/playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ <h1>CSS</h1>
<p class="dir-dep-2">
PostCSS dir-dependency (file 2): this should be grey too
</p>

<p class="url-separated">
Url separation preservation: should have valid background-image
</p>
</div>

<script type="module" src="./main.js"></script>
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,9 @@ async function doUrlReplace(
return matched
}

return `url(${wrap}${await replacer(rawUrl)}${wrap})`
// #3926
const initialComma = matched[0] === ',' ? ',' : ''
antfu marked this conversation as resolved.
Show resolved Hide resolved
return `${initialComma}url(${wrap}${await replacer(rawUrl)}${wrap})`
}

let CleanCSS: any
Expand Down