Skip to content

Commit

Permalink
CSS: Don't automatically add "px" to properties with a few exceptions
Browse files Browse the repository at this point in the history
Fixes gh-2795
Ref gh-4009
  • Loading branch information
mgol committed Apr 25, 2018
1 parent 7646836 commit 91551c9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
24 changes: 4 additions & 20 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ define( [
"./var/rcssNum",
"./css/var/rnumnonpx",
"./css/var/cssExpand",
"./css/var/isAutoPx",
"./css/var/getStyles",
"./css/var/swap",
"./css/curCSS",
Expand All @@ -19,7 +20,7 @@ define( [
"./core/ready",
"./selector" // contains
], function( jQuery, pnum, access, camelCase, document, rcssNum, rnumnonpx, cssExpand,
getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, finalPropName ) {
isAutoPx, getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, finalPropName ) {

"use strict";

Expand Down Expand Up @@ -183,23 +184,6 @@ jQuery.extend( {
}
},

// Don't automatically add "px" to these possibly-unitless properties
cssNumber: {
"animationIterationCount": true,
"columnCount": true,
"fillOpacity": true,
"flexGrow": true,
"flexShrink": true,
"fontWeight": true,
"lineHeight": true,
"opacity": true,
"order": true,
"orphans": true,
"widows": true,
"zIndex": true,
"zoom": true
},

// Add in properties whose names you wish to fix before
// setting or getting the value
cssProps: {},
Expand Down Expand Up @@ -245,9 +229,9 @@ jQuery.extend( {
return;
}

// If a number was passed in, add the unit (except for certain CSS properties)
// If the value is a number, add `px` for certain CSS properties
if ( type === "number" ) {
value += ret && ret[ 3 ] || ( jQuery.cssNumber[ origName ] ? "" : "px" );
value += ret && ret[ 3 ] || ( isAutoPx( origName ) ? "px" : "" );
}

// background-* props affect original clone's values
Expand Down
8 changes: 5 additions & 3 deletions src/css/adjustCSS.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
define( [
"../core",
"./var/isAutoPx",
"../var/rcssNum"
], function( jQuery, rcssNum ) {
], function( jQuery, isAutoPx, rcssNum ) {

"use strict";

Expand All @@ -16,10 +17,11 @@ function adjustCSS( elem, prop, valueParts, tween ) {
return jQuery.css( elem, prop, "" );
},
initial = currentValue(),
unit = valueParts && valueParts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ),
unit = valueParts && valueParts[ 3 ] || ( isAutoPx( prop ) ? "px" : "" ),

// Starting value computation is required for potential unit mismatches
initialInUnit = ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) &&
initialInUnit = elem.nodeType &&
( !isAutoPx( prop ) || unit !== "px" && +initial ) &&
rcssNum.exec( jQuery.css( elem, prop ) );

if ( initialInUnit && initialInUnit[ 3 ] !== unit ) {
Expand Down
36 changes: 36 additions & 0 deletions src/css/var/isAutoPx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
define( function() {

"use strict";

return function( prop ) {

// The second regex visualized:
//
// /----------\
// /-------------\ | |
// | | | / Top \ |
// | / Margin \ | | | Right | |
// /--------+-| |-+---+-| Bottom |-+---\
// | \ Padding / \ Left / |
// | |
// | /---------\ |
// | | | |- END
// | | / Min \ | / Width \ |
// |---------------+-| |-+---| |----|
// | \ Max / \ Height / |
// BEGIN -| |
// | /----------\ |
// | | | /-------\ |
// | | / Top \ | | | |
// \--- Border ---+-| Right |-+---+- Width -+---/
// | Bottom |
// \ Left /

// The first test is used to ensure that:
// 1. The prop starts with a lowercase letter (as we uppercase it for the second regex).
// 2. The prop is not empty.
return /^[a-z]$/.test( prop[ 0 ] ) &&
/^(?:((?:Margin|Padding)?(?:Top|Right|Bottom|Left)?)|(?:Min|Max)?(?:Width|Height)|Border(?:Top|Right|Bottom|Left)?(?:Width)?)$/.test( prop[ 0 ].toUpperCase() + prop.substr( 1 ) );
};

} );
5 changes: 3 additions & 2 deletions src/effects/Tween.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
define( [
"../core",
"../css/var/isAutoPx",
"../css/finalPropName",

"../css"
], function( jQuery, finalPropName ) {
], function( jQuery, isAutoPx, finalPropName ) {

"use strict";

Expand All @@ -21,7 +22,7 @@ Tween.prototype = {
this.options = options;
this.start = this.now = this.cur();
this.end = end;
this.unit = unit || ( jQuery.cssNumber[ prop ] ? "" : "px" );
this.unit = unit || ( isAutoPx( prop ) ? "px" : "" );
},
cur: function() {
var hooks = Tween.propHooks[ this.prop ];
Expand Down

0 comments on commit 91551c9

Please sign in to comment.