Skip to content

Commit 02bd06c

Browse files
dmethvinmgol
authored andcommitted
CSS: Warn and fill jQuery.cssNumber and .css(name, Number)
Fixes jquery#296
1 parent 534fc99 commit 02bd06c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/css.js

+22
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,25 @@ if ( jQueryVersionSince( "3.4.0" ) && typeof Proxy !== "undefined" ) {
5252
}
5353
} );
5454
}
55+
56+
if ( !jQuery.cssNumber ) {
57+
jQuery.cssNumber = {};
58+
}
59+
migrateWarnProp( jQuery, "cssNumber", jQuery.cssNumber,
60+
"jQuery.cssNumber is deprecated" );
61+
62+
var oldFnCss = jQuery.fn.css;
63+
64+
jQuery.fn.css = function( name, value ) {
65+
var origThis = this;
66+
if ( typeof name !== "string" ) {
67+
jQuery.each( name, function( n, v ) {
68+
jQuery.fn.css.call( origThis, n, v );
69+
} );
70+
}
71+
if ( typeof value === "number" ) {
72+
migrateWarn( "Use of number-typed values is deprecated in jQuery.fn.css" );
73+
}
74+
75+
return oldFnCss.apply( this, arguments );
76+
};

test/css.js

+45
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,48 @@ QUnit[ ( jQueryVersionSince( "3.4.0" ) && typeof Proxy !== "undefined" ) ? "test
4141

4242
delete jQuery.cssProps.devoHat;
4343
} );
44+
45+
QUnit.test( "jQuery.css with numbers", function( assert) {
46+
assert.expect( 4 );
47+
48+
expectWarning( assert, "Number value direct", 1, function() {
49+
jQuery( "<div />" ).css( "height", 10 );
50+
} );
51+
52+
expectWarning( assert, "Number in an object", 2, function() {
53+
jQuery( "<div />" ).css( {
54+
"width": 14,
55+
"height": "10px",
56+
"line-height": 2
57+
} );
58+
} );
59+
60+
expectNoWarning( assert, "Number value direct", function() {
61+
jQuery( "<div />" ).css( "height", 10 );
62+
} );
63+
64+
expectNoWarning( assert, "Number in an object", function() {
65+
jQuery( "<div />" ).css( {
66+
"width": "14em",
67+
"height": "10px",
68+
"line-height": "2"
69+
} );
70+
} );
71+
72+
} );
73+
74+
QUnit.test( "jQuery.cssNumber", function( assert) {
75+
assert.expect( 2 );
76+
77+
expectWarning( assert, "Setting cssNumber value", 1, function() {
78+
jQuery.cssNumber.blart = true;
79+
} );
80+
81+
82+
expectWarning( assert, "Getting cssNumber value", function() {
83+
assert.ok( jQuery.cssNumber.blart, "blart was set" );
84+
} );
85+
86+
delete jQuery.cssNumber.blart;
87+
88+
} );

warnings.md

+7
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,10 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58
253253
**Cause:** Older versions of IE & Android Browser didn't implement a method to `trim` strings so jQuery provided a cross-browser implementation. The browsers supported by jQuery 3.0 all provide a standard method for this purpose.
254254

255255
**Solution:** Replace any calls to `jQuery.trim( text )` with `text.trim()` if you know `text` is a string; otherwise, you can replace it with `text == null ? "" : text.trim()`.
256+
257+
### JQMIGRATE: Use of number-typed values is deprecated in jQuery.fn.css
258+
### JQMIGRATE: jQuery.cssNumber is deprecated
259+
260+
**Cause:** In past versions, when a number-typed value was passed to `.css()` jQuery converted it to a string and added `"px"` to the end. As the CSS standard has evolved, an increasingly large set of CSS properties now accept values that are unitless numbers, where this behavior is incorrect. It has become impractical to manage these exceptions in the `jQuery.cssNumber` object. In addition, some CSS properties like `line-height` can accept both a bare number `2` or a pixel value `2px`. jQuery cannot know the correct way to interpret `$.css("line-height", 2)` and currently treats it as `"2px"`.
261+
262+
**Solution:** Always pass string values to `.css()`, and explicitly add units where required. For example, use `$.css("line-height", "2")` to specify 200% of the current line height or `$.css("line-height", "2px")` to specify pixels. When the numeric value is in a variable, ensure the value is converted to string, e.g. `$.css("line-height", String(height))` and `$.css("line-height", height+"px")`.

0 commit comments

Comments
 (0)