Skip to content

Commit 50caf67

Browse files
authored
fix(assets): avoid splitting , inside query parameter of image URI in srcset property (#16081)
1 parent 6f8a320 commit 50caf67

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ describe('processSrcSetSync', () => {
339339
expect(processSrcSetSync(base64, ({ url }) => url)).toBe(base64)
340340
})
341341

342+
test('should not split the comma inside image URI', async () => {
343+
const imageURIWithComma =
344+
'asset.png?param1=true,param2=false 400w, asset.png?param1=true,param2=false 800w'
345+
expect(processSrcSetSync(imageURIWithComma, ({ url }) => url)).toBe(
346+
imageURIWithComma,
347+
)
348+
})
349+
350+
test('should handle srcset when descriptor is not present', async () => {
351+
const srcsetNoDescriptor = 'asset.png, test.png 400w'
352+
const result = 'asset.png, test.png 400w'
353+
expect(processSrcSetSync(srcsetNoDescriptor, ({ url }) => url)).toBe(result)
354+
})
355+
342356
test('should not break a regular URL in srcSet', async () => {
343357
const source = 'https://anydomain/image.jpg'
344358
expect(

packages/vite/src/node/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,17 @@ export function processSrcSetSync(
772772
}
773773

774774
const cleanSrcSetRE =
775-
/(?:url|image|gradient|cross-fade)\([^)]*\)|"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|data:\w+\/[\w.+\-]+;base64,[\w+/=]+/g
775+
/(?:url|image|gradient|cross-fade)\([^)]*\)|"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|data:\w+\/[\w.+\-]+;base64,[\w+/=]+|\?\S+,/g
776776
function splitSrcSet(srcs: string) {
777777
const parts: string[] = []
778-
// There could be a ',' inside of url(data:...), linear-gradient(...), "data:..." or data:...
778+
/**
779+
* There could be a ',' inside of:
780+
* - url(data:...)
781+
* - linear-gradient(...)
782+
* - "data:..."
783+
* - data:...
784+
* - query parameter ?...
785+
*/
779786
const cleanedSrcs = srcs.replace(cleanSrcSetRE, blankReplacer)
780787
let startIndex = 0
781788
let splitIndex: number

0 commit comments

Comments
 (0)