-
Notifications
You must be signed in to change notification settings - Fork 20.6k
CSS: Trim whitespace surrounding CSS Custom Properties values #4930
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
Conversation
Don't we want to limit trimming to custom vars? I'm not sure it matters, but this trims all values. |
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.
Thanks for the PR! This is very close to what we need, see comments for the blockers.
We're also considering whether to trim in the setter as well. We can consider this separately to this PR, though, especially that we haven't reached a decision there yet.
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 awesome, thanks!
It's at +14 bytes now which is quite reasonable.
" --prop6: val6 ;\n" + | ||
" --prop7: val7 ;\n" + | ||
" --prop8:\"val8\";\n" + | ||
" --prop9:'val9';\n" + |
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 there be test cases with CSS whitespace other than U+0020 SPACE?
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.
Good point. That also makes me think, we should perhaps replace CSS whitespace instead of calling .trim()
, shouldn't we?
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.
Yes, definitely.
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'd be good to have a test case with some Unicode whitespace that is not CSS whitespace to confirm the new planned behavior.
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.
One comment about trim
plus more test cases needed.
src/css/curCSS.js
Outdated
|
||
computed = computed || getStyles( elem ); | ||
|
||
// getPropertyValue is needed for `.css('--customProperty')` (gh-3144) | ||
if ( computed ) { | ||
ret = computed.getPropertyValue( name ) || computed[ name ]; | ||
|
||
// trim whitespace for custom property (issue #4926) | ||
if ( isCustomProp ) { | ||
ret = ret.trim(); |
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.
@fecore1 As @gibson042 rightly noticed, we can't actually call .trim()
as that removes all Unicode whitespace:
https://262.ecma-international.org/12.0/#sec-trimstring
https://262.ecma-international.org/12.0/#prod-WhiteSpace
We need to only remove the CSS whitespace for which we already have a regex in src/selector/var/whitespace.js
, please use it.
This gotcha is a reason why we don't use String#trim
at all in jQuery anymore but we only rely on this regex.
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.
wow, thanks @gibson042 , a very professional opinion, and i will fix it as soon as possible
" --prop6: val6 ;\n" + | ||
" --prop7: val7 ;\n" + | ||
" --prop8:\"val8\";\n" + | ||
" --prop9:'val9';\n" + |
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'd be good to have a test case with some Unicode whitespace that is not CSS whitespace to confirm the new planned behavior.
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's hard to believe but this PR is actually smaller than main
and by 21 bytes! 😮 Even the non-gzipped minified version is smaller.
Just one tiny comment fix I'll apply myself, the code looks good to me. Thanks for following up with our review remarks!
@@ -5,8 +5,9 @@ import documentElement from "./var/documentElement.js"; | |||
import indexOf from "./var/indexOf.js"; | |||
import pop from "./var/pop.js"; | |||
import push from "./var/push.js"; | |||
import whitespace from "./selector/var/whitespace.js"; | |||
import whitespace from "./var/whitespace.js"; |
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.
Good call, it's way more than selector-related now.
@@ -71,7 +72,6 @@ var i, | |||
|
|||
// Leading and non-escaped trailing whitespace, capturing some non-whitespace characters preceding the latter | |||
rwhitespace = new RegExp( whitespace + "+", "g" ), | |||
rtrim = new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" ), |
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.
Nice that you found it!
The spec has recently changed and CSS Custom Properties values are trimmed now. This change makes jQuery polyfill that new behavior for all browsers. Ref w3c/csswg-drafts#774 Fixes jquerygh-4926 Closes jquerygh-4930 (partially cherry picked from efadfe9)
Ouch, I was too quick with merging that last iteration. The |
The original logic from PR jquerygh-4930 re-used the `rtrim` regex from the selector module but selectors have special escaping rules which means whitespace following a backslash character would not get trimmed. This change splits the logic at the unfortunate but necessary cost of size. Ref jquerygh-4926 Ref jquerygh-4930
A followup PR: #4938. |
The spec has recently changed and CSS Custom Properties values are trimmed now. This change makes jQuery polyfill that new behavior for all browsers. Ref w3c/csswg-drafts#774 Fixes jquerygh-4926 Closes jquerygh-4930 Closes jquerygh-4936 (partially cherry picked from efadfe9)
The spec has recently changed and CSS Custom Properties values are trimmed now. This change makes jQuery polyfill that new behavior for all browsers. Ref w3c/csswg-drafts#774 Fixes gh-4926 Closes gh-4930 (partially cherry picked from commit efadfe9)
|
||
computed = computed || getStyles( elem ); | ||
|
||
// getPropertyValue is needed for `.css('--customProperty')` (gh-3144) | ||
if ( computed ) { | ||
ret = computed.getPropertyValue( name ) || computed[ name ]; | ||
|
||
// trim whitespace for custom property (issue gh-4926) | ||
if ( isCustomProp ) { | ||
ret = ret.replace( rtrim, "$1" ); |
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 throws TypeError: ret is undefined
when the property isn’t set: #5105.
The original logic from PR jquerygh-4930 re-used the `rtrim` regex from the selector module but selectors have special escaping rules which means whitespace following a backslash character would not get trimmed. This change splits the logic at the unfortunate but necessary cost of size. Ref jquerygh-4926 Ref jquerygh-4930
The original logic from PR jquerygh-4930 re-used the `rtrim` regex from the selector module but selectors have special escaping rules which means whitespace following a backslash character would not get trimmed. This change splits the logic at the unfortunate but necessary cost of size. Ref jquerygh-4926 Ref jquerygh-4930
…erties The original logic from PR jquerygh-4930 re-used the `rtrim` regex from the selector module but selectors have special escaping rules which means whitespace following a backslash character would not get trimmed. This change splits the logic at the unfortunate but necessary cost of size. Ref jquerygh-4926 Ref jquerygh-4930
Summary
There was a recent spec change that require CSS Custom Properties values to be trimmed, and workaround trim it until all supported browsers adjust.
Fixes #4926
Checklist