🌐 AI搜索 & 代理 主页
Skip to content

Commit c834f76

Browse files
committed
CSS: Don't automatically add "px" to properties with a few exceptions
Fixes gh-2795 Ref gh-4009
1 parent f5e36bd commit c834f76

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

src/css.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ define( [
77
"./var/rcssNum",
88
"./css/var/rnumnonpx",
99
"./css/var/cssExpand",
10+
"./css/var/isAutoPx",
1011
"./css/var/getStyles",
1112
"./css/var/swap",
1213
"./css/curCSS",
@@ -19,7 +20,7 @@ define( [
1920
"./core/ready",
2021
"./selector" // contains
2122
], function( jQuery, pnum, access, camelCase, document, rcssNum, rnumnonpx, cssExpand,
22-
getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, finalPropName ) {
23+
isAutoPx, getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, finalPropName ) {
2324

2425
"use strict";
2526

@@ -183,23 +184,6 @@ jQuery.extend( {
183184
}
184185
},
185186

186-
// Don't automatically add "px" to these possibly-unitless properties
187-
cssNumber: {
188-
"animationIterationCount": true,
189-
"columnCount": true,
190-
"fillOpacity": true,
191-
"flexGrow": true,
192-
"flexShrink": true,
193-
"fontWeight": true,
194-
"lineHeight": true,
195-
"opacity": true,
196-
"order": true,
197-
"orphans": true,
198-
"widows": true,
199-
"zIndex": true,
200-
"zoom": true
201-
},
202-
203187
// Add in properties whose names you wish to fix before
204188
// setting or getting the value
205189
cssProps: {},
@@ -245,9 +229,9 @@ jQuery.extend( {
245229
return;
246230
}
247231

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

253237
// background-* props affect original clone's values

src/css/adjustCSS.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
define( [
22
"../core",
3+
"./var/isAutoPx",
34
"../var/rcssNum"
4-
], function( jQuery, rcssNum ) {
5+
], function( jQuery, isAutoPx, rcssNum ) {
56

67
"use strict";
78

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

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

2627
if ( initialInUnit && initialInUnit[ 3 ] !== unit ) {

src/css/var/isAutoPx.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
define( function() {
2+
3+
"use strict";
4+
5+
return function( prop ) {
6+
7+
// The second regex visualized:
8+
//
9+
// /----------\
10+
// /-------------\ | |
11+
// | | | / Top \ |
12+
// | / Margin \ | | | Right | |
13+
// /--------+-| |-+---+-| Bottom |-+---\
14+
// | \ Padding / \ Left / |
15+
// | |
16+
// | /---------\ |
17+
// | | | |- END
18+
// | | / Min \ | / Width \ |
19+
// |---------------+-| |-+---| |----|
20+
// | \ Max / \ Height / |
21+
// BEGIN -| |
22+
// | /----------\ |
23+
// | | | /-------\ |
24+
// | | / Top \ | | | |
25+
// \--- Border ---+-| Right |-+---+- Width -+---/
26+
// | Bottom |
27+
// \ Left /
28+
29+
// The first test is used to ensure that:
30+
// 1. The prop starts with a lowercase letter (as we uppercase it for the second regex).
31+
// 2. The prop is not empty.
32+
return /^[a-z]$/.test( prop[ 0 ] ) &&
33+
/^(?:((?:Margin|Padding)?(?:Top|Right|Bottom|Left)?)|(?:Min|Max)?(?:Width|Height)|Border(?:Top|Right|Bottom|Left)?(?:Width)?)$/.test( prop[ 0 ].toUpperCase() + prop.substr( 1 ) );
34+
};
35+
36+
} );

src/effects/Tween.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
define( [
22
"../core",
3+
"../css/var/isAutoPx",
34
"../css/finalPropName",
45

56
"../css"
6-
], function( jQuery, finalPropName ) {
7+
], function( jQuery, isAutoPx, finalPropName ) {
78

89
"use strict";
910

@@ -21,7 +22,7 @@ Tween.prototype = {
2122
this.options = options;
2223
this.start = this.now = this.cur();
2324
this.end = end;
24-
this.unit = unit || ( jQuery.cssNumber[ prop ] ? "" : "px" );
25+
this.unit = unit || ( isAutoPx( prop ) ? "px" : "" );
2526
},
2627
cur: function() {
2728
var hooks = Tween.propHooks[ this.prop ];

0 commit comments

Comments
 (0)