Skip to content

Commit 8e26772

Browse files
authored
CSS: Fix the logic to trigger warnings on jQuery.cssNumber access
The current logic has a few faults: 1. Tests fail on jQuery 4.0.0-pre as internal jQuery.cssNumber accesses are triggering warnings. 2. Tests fail in IE because it doesn't support proxies and fallbacks haven't been set properly. We've only noticed those issues now, trying to run Migrate tests on TestSwarm after a longer break when they weren't firing. Closes gh-467
1 parent a8365cf commit 8e26772

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/jquery/css.js

+35-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { jQueryVersionSince } from "../compareVersions.js";
22
import { migrateWarn, migratePatchFunc } from "../main.js";
33
import { camelCase } from "../utils.js";
44

5-
var origFnCss,
5+
var origFnCss, internalCssNumber,
66
internalSwapCall = false,
77
ralphaStart = /^[a-z]/,
88

@@ -84,8 +84,11 @@ if ( jQueryVersionSince( "3.4.0" ) && typeof Proxy !== "undefined" ) {
8484
// https://github.com/jquery/jquery/blob/3.6.0/src/css.js#L212-L233
8585
// This way, number values for the CSS properties below won't start triggering
8686
// Migrate warnings when jQuery gets updated to >=4.0.0 (gh-438).
87-
if ( jQueryVersionSince( "4.0.0" ) && typeof Proxy !== "undefined" ) {
88-
jQuery.cssNumber = new Proxy( {
87+
if ( jQueryVersionSince( "4.0.0" ) ) {
88+
89+
// We need to keep this as a local variable as we need it internally
90+
// in a `jQuery.fn.css` patch and this usage shouldn't warn.
91+
internalCssNumber = {
8992
animationIterationCount: true,
9093
columnCount: true,
9194
fillOpacity: true,
@@ -106,16 +109,31 @@ if ( jQueryVersionSince( "4.0.0" ) && typeof Proxy !== "undefined" ) {
106109
widows: true,
107110
zIndex: true,
108111
zoom: true
109-
}, {
110-
get: function() {
111-
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
112-
return Reflect.get.apply( this, arguments );
113-
},
114-
set: function() {
115-
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
116-
return Reflect.set.apply( this, arguments );
117-
}
118-
} );
112+
};
113+
114+
if ( typeof Proxy !== "undefined" ) {
115+
jQuery.cssNumber = new Proxy( internalCssNumber, {
116+
get: function() {
117+
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
118+
return Reflect.get.apply( this, arguments );
119+
},
120+
set: function() {
121+
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
122+
return Reflect.set.apply( this, arguments );
123+
}
124+
} );
125+
} else {
126+
127+
// Support: IE 9-11+
128+
// IE doesn't support proxies, but we still want to restore the legacy
129+
// jQuery.cssNumber there.
130+
jQuery.cssNumber = internalCssNumber;
131+
}
132+
} else {
133+
134+
// Make `internalCssNumber` defined for jQuery <4 as well as it's needed
135+
// in the `jQuery.fn.css` patch below.
136+
internalCssNumber = jQuery.cssNumber;
119137
}
120138

121139
function isAutoPx( prop ) {
@@ -142,7 +160,10 @@ migratePatchFunc( jQuery.fn, "css", function( name, value ) {
142160

143161
if ( typeof value === "number" ) {
144162
camelName = camelCase( name );
145-
if ( !isAutoPx( camelName ) && !jQuery.cssNumber[ camelName ] ) {
163+
164+
// Use `internalCssNumber` to avoid triggering our warnings in this
165+
// internal check.
166+
if ( !isAutoPx( camelName ) && !internalCssNumber[ camelName ] ) {
146167
migrateWarn( "css-number",
147168
"Number-typed values are deprecated for jQuery.fn.css( \"" +
148169
name + "\", value )" );

0 commit comments

Comments
 (0)