Skip to content

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

Merged
merged 5 commits into from
Sep 23, 2021

Conversation

fecore1
Copy link
Contributor

@fecore1 fecore1 commented Sep 13, 2021

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

Sorry, something went wrong.

@timmywil
Copy link
Member

Don't we want to limit trimming to custom vars? I'm not sure it matters, but this trims all values.

Copy link
Member

@mgol mgol left a 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.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 14, 2021

CLA Signed

The committers are authorized under a signed CLA.

@fecore1
Copy link
Contributor Author

fecore1 commented Sep 14, 2021

CLA Signed

The committers are authorized under a signed CLA.

@mgol
Copy link
Member

mgol commented Sep 14, 2021

@fecore1 yes; we had to change the CLA system we use; see #4931 for more details.

@mgol
Copy link
Member

mgol commented Sep 16, 2021

@fecore1 Can you sign the new CLA? Unfortunately, as outlined in #4931, we're not allowed to merge any new contribution (which includes updates to existing PRs) without such a signature.

@fecore1
Copy link
Contributor Author

fecore1 commented Sep 17, 2021

@fecore1 Can you sign the new CLA? Unfortunately, as outlined in #4931, we're not allowed to merge any new contribution (which includes updates to existing PRs) without such a signature.

done

Copy link
Member

@mgol mgol left a 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.

@mgol mgol added the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Sep 17, 2021
@mgol mgol added this to the 3.6.1 milestone Sep 17, 2021
@timmywil timmywil removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Sep 20, 2021
" --prop6: val6 ;\n" +
" --prop7: val7 ;\n" +
" --prop8:\"val8\";\n" +
" --prop9:'val9';\n" +
Copy link
Member

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?

Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, definitely.

Copy link
Member

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.

Copy link
Member

@mgol mgol left a 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.


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();
Copy link
Member

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.

Copy link
Contributor Author

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" +
Copy link
Member

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.

Copy link
Member

@mgol mgol left a 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";
Copy link
Member

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" ),
Copy link
Member

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!

@mgol mgol removed the Needs review label Sep 23, 2021
@mgol mgol changed the title CSS: workaround trim whitespace #4926 CSS: Trim whitespace surrounding CSS Custom Properties values Sep 23, 2021
@mgol mgol merged commit efadfe9 into jquery:main Sep 23, 2021
mgol pushed a commit to mgol/jquery that referenced this pull request Sep 23, 2021
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)
@mgol
Copy link
Member

mgol commented Sep 23, 2021

Ouch, I was too quick with merging that last iteration. The rtrim regex from the selector module is quite selector-specific and it's more complex than what we need for CSS custom properties values. I'll submit a followup PR with a fix.

mgol added a commit to mgol/jquery that referenced this pull request Sep 24, 2021
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
@mgol
Copy link
Member

mgol commented Sep 24, 2021

A followup PR: #4938.

mgol pushed a commit to mgol/jquery that referenced this pull request Sep 24, 2021
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)
mgol pushed a commit that referenced this pull request Oct 18, 2021
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" );
Copy link
Contributor

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.

mgol added a commit to mgol/jquery that referenced this pull request Oct 4, 2022
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
mgol added a commit to mgol/jquery that referenced this pull request Jul 10, 2023
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
mgol added a commit to mgol/jquery that referenced this pull request Jul 10, 2023
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

The compatibility of different browsers should be considered about test case css(--customProperty)
5 participants