Skip to content

Commit

Permalink
CSS: Trim whitespace surrounding CSS Custom Properties values
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fecore1 committed Sep 23, 2021
1 parent 175db73 commit efadfe9
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import nodeName from "./core/nodeName.js";
import rcssNum from "./var/rcssNum.js";
import isIE from "./var/isIE.js";
import rnumnonpx from "./css/var/rnumnonpx.js";
import rcustomProp from "./css/var/rcustomProp.js";
import cssExpand from "./css/var/cssExpand.js";
import isAutoPx from "./css/isAutoPx.js";
import cssCamelCase from "./css/cssCamelCase.js";
Expand All @@ -24,7 +25,6 @@ var
// except "table", "table-cell", or "table-caption"
// See here for display values: https://developer.mozilla.org/en-US/docs/CSS/display
rdisplayswap = /^(none|table(?!-c[ea]).+)/,
rcustomProp = /^--/,
cssShow = { position: "absolute", visibility: "hidden", display: "block" },
cssNormalTransform = {
letterSpacing: "0",
Expand Down
10 changes: 9 additions & 1 deletion src/css/curCSS.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import jQuery from "../core.js";
import isAttached from "../core/isAttached.js";
import getStyles from "./var/getStyles.js";
import rcustomProp from "./var/rcustomProp.js";
import rtrim from "../var/rtrim.js";

function curCSS( elem, name, computed ) {
var ret;
var ret,
isCustomProp = rcustomProp.test( name );

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" );
}

if ( ret === "" && !isAttached( elem ) ) {
ret = jQuery.style( elem, name );
}
Expand Down
1 change: 1 addition & 0 deletions src/css/var/rcustomProp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ( /^--/ );
4 changes: 2 additions & 2 deletions src/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
import rbuggyQSA from "./selector/rbuggyQSA.js";
import rtrim from "./var/rtrim.js";
import isIE from "./var/isIE.js";

// The following utils are attached directly to the jQuery object.
Expand Down Expand Up @@ -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" ),

rcomma = new RegExp( "^" + whitespace + "*," + whitespace + "*" ),
rcombinators = new RegExp( "^" + whitespace + "*([>+~]|" + whitespace + ")" +
Expand Down
2 changes: 1 addition & 1 deletion src/selector/rbuggyQSA.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isIE from "../var/isIE.js";
import whitespace from "./var/whitespace.js";
import whitespace from "../var/whitespace.js";

var rbuggyQSA = isIE && new RegExp(

Expand Down
6 changes: 6 additions & 0 deletions src/var/rtrim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import whitespace from "./whitespace.js";

export default new RegExp(
"^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$",
"g"
);
File renamed without changes.
31 changes: 23 additions & 8 deletions test/unit/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -1738,9 +1738,16 @@ QUnit.testUnlessIE( "css(--customProperty)", function( assert ) {
" .test__customProperties {\n" +
" --prop1:val1;\n" +
" --prop2: val2;\n" +
" --prop3:val3 ;\n" +
" --prop4:\"val4\";\n" +
" --prop5:'val5';\n" +
" --prop3: val3;\n" +
" --prop4:val4 ;\n" +
" --prop5:val5 ;\n" +
" --prop6: val6 ;\n" +
" --prop7: val7 ;\n" +
" --prop8:\"val8\";\n" +
" --prop9:'val9';\n" +
" --prop10:\f\r\n\t val10 \f\r\n\t;\n" +
" --prop11:\u000C\u000D\u000A\u0009\u0020val11\u0020\u0009\u000A\u000D\u000C;\n" +
" --prop12:\u000Bval12\u000B;\n" +
" }\n" +
"</style>"
);
Expand All @@ -1749,7 +1756,7 @@ QUnit.testUnlessIE( "css(--customProperty)", function( assert ) {
$elem = jQuery( "<div>" ).addClass( "test__customProperties" )
.appendTo( "#qunit-fixture" ),
webkitOrBlink = /\bsafari\b/i.test( navigator.userAgent ),
expected = 10;
expected = 17;

if ( webkitOrBlink ) {
expected -= 2;
Expand Down Expand Up @@ -1777,16 +1784,24 @@ QUnit.testUnlessIE( "css(--customProperty)", function( assert ) {

assert.equal( $elem.css( "--prop1" ), "val1", "Basic CSS custom property" );

assert.equal( $elem.css( "--prop2" ), " val2", "Preceding whitespace maintained" );
assert.equal( $elem.css( "--prop3" ), "val3 ", "Following whitespace maintained" );
assert.equal( $elem.css( "--prop2" ), "val2", "Preceding whitespace trimmed" );
assert.equal( $elem.css( "--prop3" ), "val3", "Multiple preceding whitespace trimmed" );
assert.equal( $elem.css( "--prop4" ), "val4", "Following whitespace trimmed" );
assert.equal( $elem.css( "--prop5" ), "val5", "Multiple Following whitespace trimmed" );
assert.equal( $elem.css( "--prop6" ), "val6", "Preceding and Following whitespace trimmed" );
assert.equal( $elem.css( "--prop7" ), "val7", "Multiple preceding and following whitespace trimmed" );

// Support: Chrome <=49 - 73+, Safari <=9.1 - 12.1+
// Chrome treats single quotes as double ones.
// Safari treats double quotes as single ones.
if ( !webkitOrBlink ) {
assert.equal( $elem.css( "--prop4" ), "\"val4\"", "Works with double quotes" );
assert.equal( $elem.css( "--prop5" ), "'val5'", "Works with single quotes" );
assert.equal( $elem.css( "--prop8" ), "\"val8\"", "Works with double quotes" );
assert.equal( $elem.css( "--prop9" ), "'val9'", "Works with single quotes" );
}

assert.equal( $elem.css( "--prop10" ), "val10", "Multiple preceding and following escaped unicode whitespace trimmed" );
assert.equal( $elem.css( "--prop11" ), "val11", "Multiple preceding and following unicode whitespace trimmed" );
assert.equal( $elem.css( "--prop12" ), "\u000Bval12\u000B", "Multiple preceding and following non-CSS whitespace reserved" );
} );

// IE doesn't support CSS variables.
Expand Down

0 comments on commit efadfe9

Please sign in to comment.