Skip to content
Open
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
20 changes: 20 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,26 @@ describe('processSrcSetSync', () => {
).toBe('"/base/nested/asset.png" 1x, "/base/nested/asset.png" 2x')
})

test('keep the url when a density descriptor omits its leading zero', async () => {
const devBase = '/base/'
expect(
processSrcSetSync(
'./nested/asset.png .5x, ./nested/asset.png 1x',
({ url }) => path.posix.join(devBase, url),
),
).toBe('/base/nested/asset.png .5x, /base/nested/asset.png 1x')
})

test('keep the quoted url when a density descriptor omits its leading zero', async () => {
const devBase = '/base/'
expect(
processSrcSetSync(
'"./nested/asset.png" .75x,"./nested/asset.png" 1x',
({ url }) => `"${path.posix.join(devBase, url.slice(1, -1))}"`,
),
).toBe('"/base/nested/asset.png" .75x, "/base/nested/asset.png" 1x')
})

test('should not split the comma inside base64 value', async () => {
const base64 =
'data:image/avif;base64,aA+/0= 400w, data:image/avif;base64,bB+/9= 800w'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ function joinSrcset(ret: ImageCandidate[]) {
The `descriptor` is anything after the space and before the comma.
*/
const imageCandidateRegex =
/(?:^|\s|(?<=,))(?<url>[\w-]+\([^)]*\)|"[^"]*"|'[^']*'|[^,]\S*[^,])\s*(?:\s(?<descriptor>\w[^,]+))?(?:,|$)/g
/(?:^|\s|(?<=,))(?<url>[\w-]+\([^)]*\)|"[^"]*"|'[^']*'|[^,]\S*[^,])\s*(?:\s(?<descriptor>[\w.][^,]+))?(?:,|$)/g
const escapedSpaceCharacters = /(?: |\\t|\\n|\\f|\\r)+/g

export function parseSrcset(string: string): ImageCandidate[] {
Expand Down
Loading