Skip to content

Commit aee3d06

Browse files
committed
CSS: Warn and fill jQuery.cssNumber and .css(name, Number)
Fixes jquery#296
1 parent c076e19 commit aee3d06

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
@@ -42,3 +42,25 @@ jQuery.swap = function( elem, options, callback, args ) {
4242

4343
return ret;
4444
};
45+
46+
if ( !jQuery.cssNumber ) {
47+
jQuery.cssNumber = {};
48+
}
49+
migrateWarnProp( jQuery, "cssNumber", jQuery.cssNumber,
50+
"jQuery.cssNumber is deprecated" );
51+
52+
var oldFnCss = jQuery.fn.css;
53+
54+
jQuery.fn.css = function( name, value ) {
55+
var origThis = this;
56+
if ( typeof name !== "string" ) {
57+
jQuery.each( name, function( n, v ) {
58+
jQuery.fn.css.call( origThis, n, v );
59+
} );
60+
}
61+
if ( typeof value === "number" ) {
62+
migrateWarn( "Use of number-typed values is deprecated in jQuery.fn.css" );
63+
}
64+
65+
return oldFnCss.apply( this, arguments );
66+
};

test/css.js

+45
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,48 @@ QUnit.test( "jQuery.swap()", function( assert ) {
2525
} );
2626

2727
} );
28+
29+
QUnit.test( "jQuery.css with numbers", function( assert) {
30+
assert.expect( 4 );
31+
32+
expectWarning( assert, "Number value direct", 1, function() {
33+
jQuery( "<div />" ).css( "height", 10 );
34+
} );
35+
36+
expectWarning( assert, "Number in an object", 2, function() {
37+
jQuery( "<div />" ).css( {
38+
"width": 14,
39+
"height": "10px",
40+
"line-height": 2
41+
} );
42+
} );
43+
44+
expectNoWarning( assert, "Number value direct", function() {
45+
jQuery( "<div />" ).css( "height", 10 );
46+
} );
47+
48+
expectNoWarning( assert, "Number in an object", function() {
49+
jQuery( "<div />" ).css( {
50+
"width": "14em",
51+
"height": "10px",
52+
"line-height": "2"
53+
} );
54+
} );
55+
56+
} );
57+
58+
QUnit.test( "jQuery.cssNumber", function( assert) {
59+
assert.expect( 2 );
60+
61+
expectWarning( assert, "Setting cssNumber value", 1, function() {
62+
jQuery.cssNumber.blart = true;
63+
} );
64+
65+
66+
expectWarning( assert, "Getting cssNumber value", function() {
67+
assert.ok( jQuery.cssNumber.blart, "blart was set" );
68+
} );
69+
70+
delete jQuery.cssNumber.blart;
71+
72+
} );

warnings.md

+7
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,10 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58
229229
**Cause:** This public but never-documented method has been deprecated as of jQuery 3.2.0.
230230

231231
**Solution:** Replace calls such as `jQuery.nodeName( elem, "div" )` with a test such as `elem.nodeName.toLowerCase() === "div"`.
232+
233+
### JQMIGRATE: Use of number-typed values is deprecated in jQuery.fn.css
234+
### JQMIGRATE: jQuery.cssNumber is deprecated
235+
236+
**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"`.
237+
238+
**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)