Skip to content

Commit

Permalink
fix: srcSet with optional descriptor (#15905)
Browse files Browse the repository at this point in the history
Co-authored-by: patak <[email protected]>
  • Loading branch information
murad-khafizov and patak-dev authored Feb 20, 2024
1 parent 571a3fd commit 81b3bd0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ describe('processSrcSetSync', () => {
'data:image/avif;base64,aA+/0= 400w, data:image/avif;base64,bB+/9= 800w'
expect(processSrcSetSync(base64, ({ url }) => url)).toBe(base64)
})

test('should not break a regular URL in srcSet', async () => {
const source = 'https://anydomain/image.jpg'
expect(
processSrcSetSync('https://anydomain/image.jpg', ({ url }) => url),
).toBe(source)
})
})

describe('flattenId', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,9 @@ interface ImageCandidate {
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g
const imageSetUrlRE = /^(?:[\w\-]+\(.*?\)|'.*?'|".*?"|\S*)/
function joinSrcset(ret: ImageCandidate[]) {
return ret.map(({ url, descriptor }) => `${url} ${descriptor}`).join(', ')
return ret
.map(({ url, descriptor }) => url + (descriptor ? ` ${descriptor}` : ''))
.join(', ')
}

function splitSrcSetDescriptor(srcs: string): ImageCandidate[] {
Expand Down

0 comments on commit 81b3bd0

Please sign in to comment.