Skip to content

Commit b50e1b4

Browse files
fix(utils): handle dot in srcset density descriptor (#23346)
1 parent fdef04f commit b50e1b4

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎packages/vite/src/node/__tests__/utils.spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,26 @@ describe('processSrcSetSync', () => {
620620
).toBe('"/base/nested/asset.png" 1x, "/base/nested/asset.png" 2x')
621621
})
622622

623+
test('keep the url when a density descriptor omits its leading zero', async () => {
624+
const devBase = '/base/'
625+
expect(
626+
processSrcSetSync(
627+
'./nested/asset.png .5x, ./nested/asset.png 1x',
628+
({ url }) => path.posix.join(devBase, url),
629+
),
630+
).toBe('/base/nested/asset.png .5x, /base/nested/asset.png 1x')
631+
})
632+
633+
test('keep the quoted url when a density descriptor omits its leading zero', async () => {
634+
const devBase = '/base/'
635+
expect(
636+
processSrcSetSync(
637+
'"./nested/asset.png" .75x,"./nested/asset.png" 1x',
638+
({ url }) => `"${path.posix.join(devBase, url.slice(1, -1))}"`,
639+
),
640+
).toBe('"/base/nested/asset.png" .75x, "/base/nested/asset.png" 1x')
641+
})
642+
623643
test('should not split the comma inside base64 value', async () => {
624644
const base64 =
625645
'data:image/avif;base64,aA+/0= 400w, data:image/avif;base64,bB+/9= 800w'

‎packages/vite/src/node/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ function joinSrcset(ret: ImageCandidate[]) {
814814
The `descriptor` is anything after the space and before the comma.
815815
*/
816816
const imageCandidateRegex =
817-
/(?:^|\s|(?<=,))(?<url>[\w-]+\([^)]*\)|"[^"]*"|'[^']*'|[^,]\S*[^,])\s*(?:\s(?<descriptor>\w[^,]+))?(?:,|$)/g
817+
/(?:^|\s|(?<=,))(?<url>[\w-]+\([^)]*\)|"[^"]*"|'[^']*'|[^,]\S*[^,])\s*(?:\s(?<descriptor>[\w.][^,]+))?(?:,|$)/g
818818
const escapedSpaceCharacters = /(?: |\\t|\\n|\\f|\\r)+/g
819819

820820
export function parseSrcset(string: string): ImageCandidate[] {

0 commit comments

Comments
 (0)