Skip to content

Commit d25ecd6

Browse files
authored
CSS: Don't warn for number values when prop in jQuery.cssNumber
Relying on numerical values for most CSS properties having `"px"` auto-appended when set via `.css( prop, value )` is deprecated. So far, Migrate was warning for number values for all CSS properties with the exception of the ones to which jQuery 4.x will still auto-append `"px"`. However, if the used jQuery version contains the property as a key in `jQuery.cssNumber`, it will already skip the `"px"` auto-appending behavior, exactly as jQuery 4.x will. This commit removes a warning in such cases. This change will help jQuery UI from not triggering Migrate warnings. Closes jquerygh-348
1 parent 4b1f6ab commit d25ecd6

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

src/jquery/css.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,19 @@ function isAutoPx( prop ) {
9999
oldFnCss = jQuery.fn.css;
100100

101101
jQuery.fn.css = function( name, value ) {
102-
var origThis = this;
102+
var camelName,
103+
origThis = this;
103104
if ( typeof name !== "string" ) {
104105
jQuery.each( name, function( n, v ) {
105106
jQuery.fn.css.call( origThis, n, v );
106107
} );
107108
}
108-
if ( typeof value === "number" && !isAutoPx( camelCase( name ) ) ) {
109-
migrateWarn( "Use of number-typed values is deprecated in jQuery.fn.css" );
109+
if ( typeof value === "number" ) {
110+
camelName = camelCase( name );
111+
if ( !isAutoPx( camelName ) && !jQuery.cssNumber[ camelName ] ) {
112+
migrateWarn( "Number-typed values are deprecated for jQuery.fn.css( \"" +
113+
name + "\", value )" );
114+
}
110115
}
111116

112117
return oldFnCss.apply( this, arguments );

test/css.js

+52-38
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,41 @@ QUnit[ ( jQueryVersionSince( "3.4.0" ) && typeof Proxy !== "undefined" ) ? "test
4545
} );
4646

4747
QUnit.test( "jQuery.css with numbers", function( assert ) {
48-
assert.expect( 5 );
49-
50-
var whitelist = [
51-
"margin",
52-
"marginTop",
53-
"marginRight",
54-
"marginBottom",
55-
"marginLeft",
56-
"padding",
57-
"paddingTop",
58-
"paddingRight",
59-
"paddingBottom",
60-
"paddingLeft",
61-
"top",
62-
"right",
63-
"bottom",
64-
"left",
65-
"width",
66-
"height",
67-
"minWidth",
68-
"minHeight",
69-
"maxWidth",
70-
"maxHeight",
71-
"border",
72-
"borderWidth",
73-
"borderTop",
74-
"borderTopWidth",
75-
"borderRight",
76-
"borderRightWidth",
77-
"borderBottom",
78-
"borderBottomWidth",
79-
"borderLeft",
80-
"borderLeftWidth"
81-
];
48+
var jQuery3OrOlder = compareVersions( jQuery.fn.jquery, "4.0.0" ) < 0,
49+
whitelist = [
50+
"margin",
51+
"marginTop",
52+
"marginRight",
53+
"marginBottom",
54+
"marginLeft",
55+
"padding",
56+
"paddingTop",
57+
"paddingRight",
58+
"paddingBottom",
59+
"paddingLeft",
60+
"top",
61+
"right",
62+
"bottom",
63+
"left",
64+
"width",
65+
"height",
66+
"minWidth",
67+
"minHeight",
68+
"maxWidth",
69+
"maxHeight",
70+
"border",
71+
"borderWidth",
72+
"borderTop",
73+
"borderTopWidth",
74+
"borderRight",
75+
"borderRightWidth",
76+
"borderBottom",
77+
"borderBottomWidth",
78+
"borderLeft",
79+
"borderLeftWidth"
80+
];
81+
82+
assert.expect( jQuery3OrOlder ? 7 : 6 );
8283

8384
function kebabCase( string ) {
8485
return string.replace( /[A-Z]/g, function( match ) {
@@ -87,26 +88,26 @@ QUnit.test( "jQuery.css with numbers", function( assert ) {
8788
}
8889

8990
expectWarning( assert, "Number value direct", function() {
90-
jQuery( "<div />" ).css( "line-height", 10 );
91+
jQuery( "<div />" ).css( "fake-property", 10 );
9192
} );
9293

9394
expectWarning( assert, "Number in an object", 1, function() {
9495
jQuery( "<div />" ).css( {
9596
"width": 14,
9697
"height": "10px",
97-
"line-height": 2
98+
"fake-property": 2
9899
} );
99100
} );
100101

101102
expectNoWarning( assert, "String value direct", function() {
102-
jQuery( "<div />" ).css( "line-height", "10px" );
103+
jQuery( "<div />" ).css( "fake-property", "10px" );
103104
} );
104105

105106
expectNoWarning( assert, "String in an object", function() {
106107
jQuery( "<div />" ).css( {
107108
"width": "14em",
108109
"height": "10px",
109-
"line-height": "2"
110+
"fake-property": "2"
110111
} );
111112
} );
112113

@@ -117,6 +118,19 @@ QUnit.test( "jQuery.css with numbers", function( assert ) {
117118
} );
118119
} );
119120

121+
expectNoWarning( assert, "Props from jQuery.cssNumber", function() {
122+
var prop,
123+
assertionFired = false;
124+
for ( prop in jQuery.cssNumber ) {
125+
assertionFired = true;
126+
jQuery( "<div />" ).css( prop, 1 );
127+
jQuery( "<div />" ).css( kebabCase( prop ), 1 );
128+
}
129+
if ( jQuery3OrOlder ) {
130+
assert.strictEqual( assertionFired, true, "jQuery.cssNumber property was accessed" );
131+
}
132+
} );
133+
120134
} );
121135

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

0 commit comments

Comments
 (0)