-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Ensure existing spaces in attribute selectors are valid #14703
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
Ensure existing spaces in attribute selectors are valid #14703
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. Join @RobinMalfait and the rest of your teammates on |
d5f1b79 to
d239cc5
Compare
packages/tailwindcss/src/variants.ts
Outdated
| let value = match.slice(1).trim() | ||
|
|
||
| // If the value is already quoted, skip. | ||
| if (match[1] === "'" || match[1] === '"') { | ||
| if (value[0] === "'" || value[0] === '"') { |
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.
_ values in the value are already converted to which means that we can just trim the valid (if we ignore the first = character)
|
Do we actually want people to be able to write stuff like |
|
@adamwathan I noticed it while working on the codemods and created an internal issue so that I didn't forget about it. Fun thing is, that you can put spaces in that position in CSS (see https://jsfiddle.net/ezu8qjdk/) and since arbitrary values should be valid CSS (as an escape hatch if the value isn't used), I figured that it is best to at least generate the correct CSS if you put it in that way. We can of course throw it out (or even fix it at a codemod level), but since it's valid CSS it might be good to keep it? I'm happy with either solution, just want to fix the scenario that exists today where the currently generated CSS is invalid. |
|
The part I'm missing is that you can't put spaces if you use |
d239cc5 to
9ff2d99
Compare
9ff2d99 to
5e26aec
Compare
packages/tailwindcss/src/variants.ts
Outdated
| function quoteAttributeValue(value: string) { | ||
| if (value.includes('=')) { | ||
| // Do not allow invalid operators in attribute values | ||
| if (/[*$^|~]\s+=/.test(value)) { |
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.
Should we try to always precompile regular expressions?
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.
That's something we could do, it's also a non-global one (/g) so it's stateless.
This only ever runs in data-* or aria-* variants when using arbitrary values that contain an = so I'm assuming this will be used very rarely. Especially because the aria-foobar already translates to aria-[foobar="true"], but this doesn't go via this function since it's a bare value instead of an arbitrary value.
packages/tailwindcss/src/variants.ts
Outdated
| function quoteAttributeValue(value: string) { | ||
| if (value.includes('=')) { | ||
| // Do not allow invalid operators in attribute values | ||
| if (/[*$^|~]\s+=/.test(value)) { |
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 this condition would break this variant:
data-[potato="^_="]
Feeling like the better overall solution to this problem should rely on segment — do we just need to segment the value on = and quote the last chunk if it isn't already quoted? Maybe we don't need to protect people from writing data-[potato_^_=_foo] and just let that be user error that generates junk.
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.
Reworked it by using segment instead of the regex.
71c24fe to
5120b3b
Compare
6aa4ed3 to
7b0465b
Compare
philipp-spiess
left a comment
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.
Looks good now and feels like a simple fix. Just one idea regarding the change log entry.
If you inject existing spaces in the attribute selector, then invalid CSS was generated. Lightning CSS itself throws it out, but if you are in an environment where Lightning CSS doesn't run then invalid CSS exists in your final CSS file. This fixes that by ensuring that the spaces around the `=` don't cause any issues.
This test will verify that the usage of `segment` is correct. Before we were relying on a regex and check for `(=.*)` which would also match _inside_ of the quoted value.
Co-authored-by: Adam Wathan <[email protected]>
Co-authored-by: Adam Wathan <[email protected]>
5e4504d to
8d48594
Compare


This PR fixes an issue where spaces in a selector generated invalid CSS.
Lightning CSS will throw those incorrect lines of CSS out, but if you are in an environment where Lightning CSS doesn't run then invalid CSS is generated.
Given this input:
This will be generated:
With this PR in place, the generated CSS will now be: