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

Do not validate language in extended filename parameter #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jonchurch
Copy link
Member

closes #47

@jonchurch jonchurch force-pushed the drop-langauge-validation branch from db288e7 to bb78e30 Compare March 5, 2025 18:02
@jonchurch jonchurch added the bug label Mar 5, 2025
Copy link
Member

@wesleytodd wesleytodd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one nit.

* value-chars = *( pct-encoded / attr-char )
* pct-encoded = "%" HEXDIG HEXDIG
* attr-char = ALPHA / DIGIT
* / "!" / "#" / "$" / "&" / "+" / "-" / "."
* / "^" / "_" / "`" / "|" / "~"
* @private
*/

var EXT_VALUE_REGEXP = /^([A-Za-z0-9!#$%&+\-^_`{}~]+)'(?:[A-Za-z]{2,3}(?:-[A-Za-z]{3}){0,3}|[A-Za-z]{4,8}|)'((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$/
var EXT_VALUE_REGEXP = /([A-Za-z0-9!#$%&+\-^_`{}~]+)'(?:[^']*)'((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@jonchurch jonchurch Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yar, I prefer to break regex down into pieces you can semantically name like:

// Define each part of the regex:
const charset = "([A-Za-z0-9!#$%&+\\-^_`{}~]+)";       // Captures the charset (e.g. "UTF-8")
const openQuote = "'";                                  // Literal opening quote
const language = "(?:[^']*)";                           // Matches any characters except a single quote (ignores language)
const closeQuote = "'";                                 // Literal closing quote
const valueChars = "((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$"; // Captures the percent-encoded value-chars

// Combine them into the final regex string:
const regexStr = "^" + charset + openQuote + language + closeQuote + valueChars;

// Then compile the regex:
const EXT_VALUE_REGEXP = new RegExp(regexStr);

But it does make it harder to just copy the full thing out

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to write parsers instead of regex, solves the same problems. But yeah, until someone opens a PR to convert all this regex to a parser I am good with doing what you propose here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the approach Jon proposes, it really helps with readability for people who are not very good with regex.

@wesleytodd
Copy link
Member

Oh, we should discuss the semver-ness of this. I think because this used to throw and now will not, this is a breaking change. How do you feel about that?

Co-authored-by: Wes Todd <[email protected]>
@jonchurch
Copy link
Member Author

Semver major, same input has different output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Certain languages can't be matched correctly by the Regular Expression EXT_VALUE_REGEXP like en-US or zh_cn
3 participants