Skip to content
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

Merged
merged 7 commits into from Mar 17, 2024

Conversation

caseycarroll
Copy link
Contributor

@caseycarroll caseycarroll commented Mar 3, 2024

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:

asset.png?param1=test,param2=test 400w, asset.png?param1=test,param2=test 800w,

...is split into

["asset.png?param1=test,", "param2=test 400w", "asset.png?param1=test,", "param2=test 800w", ]

...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?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

Copy link

stackblitz bot commented Mar 3, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@caseycarroll
Copy link
Contributor Author

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?

@caseycarroll
Copy link
Contributor Author

Here is an alternative solution that uses the srcset package. caseycarroll/vite@main...caseycarroll:vite:add-srcset-dep

@caseycarroll
Copy link
Contributor Author

Decided to change my original approach. srcs is split using a regex that matches commas followed by whitespace.

startIndex = splitIndex + 1
} while (splitIndex !== -1)
return parts
return srcs.split(/,\s+/)
Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

@sapphi-red sapphi-red added feat: html p3-minor-bug An edge case that only affects very specific usage (priority) labels Mar 17, 2024
@patak-dev patak-dev merged commit 50caf67 into vitejs:main Mar 17, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: html p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vite adds spaces around commas in image url within srcset (HTML files)
3 participants