Skip to content

Commit bd8267b

Browse files
authoredJul 9, 2021
CSS: Don't warn against .css( "z-index", numberValue ) in jQuery >=4
When used against jQuery Git (future jQuery 4), there were warnings when `.css()` was used against `z-index` with a number value. This didn't make sense as `z-index` never had `px` auto-appended. Commit d25ecd6 was supposed to solve a similar problem but it only worked fine for `3.x`. To make the warning not show up when used with jQuery >=4 we need to fill in `jQuery.cssNumber` with the latest version from jQuery 3.x so that the same properties are allowed to have number values used with `.css()`. Fixes gh-438 Closes gh-439 Ref jquery/jquery-ui#1960
1 parent 9010c57 commit bd8267b

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed
 

‎src/jquery/css.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,33 @@ if ( jQueryVersionSince( "3.4.0" ) && typeof Proxy !== "undefined" ) {
8181
} );
8282
}
8383

84-
// Create a dummy jQuery.cssNumber if missing. It won't be used by jQuery but
85-
// it will prevent code adding new keys to it unconditionally from crashing.
84+
// In jQuery >=4 where jQuery.cssNumber is missing fill it with the latest 3.x version:
85+
// https://github.com/jquery/jquery/blob/3.6.0/src/css.js#L212-L233
86+
// This way, number values for the CSS properties below won't start triggering
87+
// Migrate warnings when jQuery gets updated to >=4.0.0 (gh-438).
8688
if ( !jQuery.cssNumber ) {
87-
jQuery.cssNumber = {};
89+
jQuery.cssNumber = {
90+
animationIterationCount: true,
91+
columnCount: true,
92+
fillOpacity: true,
93+
flexGrow: true,
94+
flexShrink: true,
95+
fontWeight: true,
96+
gridArea: true,
97+
gridColumn: true,
98+
gridColumnEnd: true,
99+
gridColumnStart: true,
100+
gridRow: true,
101+
gridRowEnd: true,
102+
gridRowStart: true,
103+
lineHeight: true,
104+
opacity: true,
105+
order: true,
106+
orphans: true,
107+
widows: true,
108+
zIndex: true,
109+
zoom: true
110+
};
88111
}
89112

90113
function isAutoPx( prop ) {

‎test/css.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ QUnit.test( "jQuery.css with arrays", function( assert ) {
6262

6363
QUnit.test( "jQuery.css with numbers", function( assert ) {
6464
var jQuery3OrOlder = compareVersions( jQuery.fn.jquery, "4.0.0" ) < 0,
65-
whitelist = [
65+
allowlist = [
6666
"margin",
6767
"marginTop",
6868
"marginRight",
@@ -95,7 +95,7 @@ QUnit.test( "jQuery.css with numbers", function( assert ) {
9595
"borderLeftWidth"
9696
];
9797

98-
assert.expect( jQuery3OrOlder ? 7 : 6 );
98+
assert.expect( jQuery3OrOlder ? 8 : 7 );
9999

100100
function kebabCase( string ) {
101101
return string.replace( /[A-Z]/g, function( match ) {
@@ -127,8 +127,8 @@ QUnit.test( "jQuery.css with numbers", function( assert ) {
127127
} );
128128
} );
129129

130-
expectNoWarning( assert, "Number value (whitelisted props)", function() {
131-
whitelist.forEach( function( prop ) {
130+
expectNoWarning( assert, "Number value (allowlisted props)", function() {
131+
allowlist.forEach( function( prop ) {
132132
jQuery( "<div />" ).css( prop, 1 );
133133
jQuery( "<div />" ).css( kebabCase( prop ), 1 );
134134
} );
@@ -147,6 +147,14 @@ QUnit.test( "jQuery.css with numbers", function( assert ) {
147147
}
148148
} );
149149

150+
// z-index is tested explicitly as raw jQuery 4.0 will not have `jQuery.cssNumber`
151+
// so iterating over it won't find anything and we'd like to ensure number values
152+
// are not warned against for safe CSS props like z-index (gh-438).
153+
expectNoWarning( assert, "z-index", function() {
154+
jQuery( "<div />" ).css( "z-index", 1 );
155+
jQuery( "<div />" ).css( kebabCase( "zIndex" ), 1 );
156+
} );
157+
150158
} );
151159

152160
QUnit.test( "jQuery.cssNumber", function( assert ) {

0 commit comments

Comments
 (0)
Please sign in to comment.