-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Don't escape underscores for the first parameter of var()
#14776
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
Merged
adamwathan
merged 2 commits into
next
from
10-24-don_t_escape_underscores_for_the_first_parameter_of_var_
Oct 24, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| import * as ValueParser from '../value-parser' | ||
| import { addWhitespaceAroundMathOperators } from './math-operators' | ||
|
|
||
| export function decodeArbitraryValue(input: string): string { | ||
| // We do not want to normalize anything inside of a url() because if we | ||
| // replace `_` with ` `, then it will very likely break the url. | ||
| if (input.startsWith('url(')) { | ||
| return input | ||
| // There are definitely no functions in the input, so bail early | ||
| if (input.indexOf('(') === -1) { | ||
| return convertUnderscoresToWhitespace(input) | ||
| } | ||
|
Comment on lines
+6
to
8
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check was moved up from being inside the |
||
|
|
||
| input = convertUnderscoresToWhitespace(input) | ||
| let ast = ValueParser.parse(input) | ||
| recursivelyDecodeArbitraryValues(ast) | ||
| input = ValueParser.toCss(ast) | ||
|
|
||
| input = addWhitespaceAroundMathOperators(input) | ||
|
|
||
| return input | ||
|
|
@@ -41,3 +44,45 @@ function convertUnderscoresToWhitespace(input: string) { | |
|
|
||
| return output | ||
| } | ||
|
|
||
| function recursivelyDecodeArbitraryValues(ast: ValueParser.ValueAstNode[]) { | ||
| for (let node of ast) { | ||
| switch (node.kind) { | ||
| case 'function': { | ||
| if (node.value === 'url' || node.value.endsWith('_url')) { | ||
| // Don't decode underscores in url() but do decode the function name | ||
| node.value = convertUnderscoresToWhitespace(node.value) | ||
| break | ||
| } | ||
|
|
||
| if (node.value === 'var' || node.value.endsWith('_var')) { | ||
| // Don't decode underscores in the first argument of var() but do | ||
| // decode the function name | ||
| node.value = convertUnderscoresToWhitespace(node.value) | ||
| for (let i = 0; i < node.nodes.length; i++) { | ||
| if (i == 0 && node.nodes[i].kind === 'word') { | ||
| continue | ||
| } | ||
| recursivelyDecodeArbitraryValues([node.nodes[i]]) | ||
| } | ||
| break | ||
| } | ||
|
|
||
| node.value = convertUnderscoresToWhitespace(node.value) | ||
| recursivelyDecodeArbitraryValues(node.nodes) | ||
| break | ||
| } | ||
| case 'separator': | ||
| case 'word': { | ||
| node.value = convertUnderscoresToWhitespace(node.value) | ||
| break | ||
| } | ||
| default: | ||
| never() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function never(): never { | ||
| throw new Error('This should never happen') | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This test case didn't have the proper number of closing quotes. I remember that @RobinMalfait once mentioned that we do validate this already at the parser level.
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 think we only validate the parens in your CSS file (and throw), but we don't validate if it happens in a candidate.
Input:
Output:
Uh oh!
There was an error while loading. Please reload this page.
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 see, lol!
With this change the closing parentheses would be automatically inserted actually (because we can't encode the absence of them in the
ValueParser—That's how I found out about this example in the first place! But I think it would be better if we throw away candidates like this 🤔