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(assets): avoid splitting ,
inside query parameter of image URI in srcset property
#16081
Conversation
Run & review this pull request in StackBlitz Codeflow. |
I'm not sure what the protocol is for adding new dependencies. This package covers the case my PR aims to solve. It also covers the base64 cases. https://github.com/sindresorhus/srcset/ Should Vite leverage this package instead of hand writing a srcset parser? |
Here is an alternative solution that uses the srcset package. caseycarroll/vite@main...caseycarroll:vite:add-srcset-dep |
bca6dad
to
7081b71
Compare
Decided to change my original approach. |
packages/vite/src/node/utils.ts
Outdated
startIndex = splitIndex + 1 | ||
} while (splitIndex !== -1) | ||
return parts | ||
return srcs.split(/,\s+/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the algorithm is a bit more complex, see:
If present, its value must consist of one or more image candidate strings, each separated from the next by a U+002C COMMA character (,). If an image candidate string contains no descriptors and no ASCII whitespace after the URL, the following image candidate string, if there is one, must begin with one or more ASCII whitespace.
From https://html.spec.whatwg.org/multipage/images.html#srcset-attribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good callout. If I understand the spec correctly, image candidate strings can be separated by a comma with 0 whitespace. My solution expects a comma and whitespace after each image candidate string. My solution would fail to parse:
image.jpg/400 400w,image.jpg/500 500w
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated my PR with a different approach. I added an additional pattern in the cleanSrSetRE
RegEx. This pattern matches a string that contains a question mark followed by a comma. This pattern is intended to match the part of image URLs with query strings that contain commas.
If matched, the string is replaced by blank spaces so the following code can split the srcset by commas that specifically follow descriptors.
7081b71
to
8d29c13
Compare
fixes #16077
Description
When building list of image candidates,
splitSrcSet
breaks image URIs that contain query parameters with comma delimited values i.e.asset.png?param1=test,param2=test
.Current implementation looks at srcset string and splits it at comma delimiters, including commas found in imageURIs. As a result, this srcset:
...is split into
...creating invalid image links that do not match the original HTML.
Additional context
Update
splitSrcSet
to split string of srcs by commas followed by whitespace characters. This excludes commas found in the imageURI.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).