Skip to content

Commit

Permalink
CSS: Fix the logic to trigger warnings on jQuery.cssNumber access
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgol committed Jul 6, 2022
1 parent a8365cf commit 8e26772
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/jquery/css.js
Expand Up @@ -2,7 +2,7 @@ import { jQueryVersionSince } from "../compareVersions.js";
import { migrateWarn, migratePatchFunc } from "../main.js";
import { camelCase } from "../utils.js";

var origFnCss,
var origFnCss, internalCssNumber,
internalSwapCall = false,
ralphaStart = /^[a-z]/,

Expand Down Expand Up @@ -84,8 +84,11 @@ if ( jQueryVersionSince( "3.4.0" ) && typeof Proxy !== "undefined" ) {
// https://github.com/jquery/jquery/blob/3.6.0/src/css.js#L212-L233
// This way, number values for the CSS properties below won't start triggering
// Migrate warnings when jQuery gets updated to >=4.0.0 (gh-438).
if ( jQueryVersionSince( "4.0.0" ) && typeof Proxy !== "undefined" ) {
jQuery.cssNumber = new Proxy( {
if ( jQueryVersionSince( "4.0.0" ) ) {

// We need to keep this as a local variable as we need it internally
// in a `jQuery.fn.css` patch and this usage shouldn't warn.
internalCssNumber = {
animationIterationCount: true,
columnCount: true,
fillOpacity: true,
Expand All @@ -106,16 +109,31 @@ if ( jQueryVersionSince( "4.0.0" ) && typeof Proxy !== "undefined" ) {
widows: true,
zIndex: true,
zoom: true
}, {
get: function() {
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
return Reflect.get.apply( this, arguments );
},
set: function() {
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
return Reflect.set.apply( this, arguments );
}
} );
};

if ( typeof Proxy !== "undefined" ) {
jQuery.cssNumber = new Proxy( internalCssNumber, {
get: function() {
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
return Reflect.get.apply( this, arguments );
},
set: function() {
migrateWarn( "css-number", "jQuery.cssNumber is deprecated" );
return Reflect.set.apply( this, arguments );
}
} );
} else {

// Support: IE 9-11+
// IE doesn't support proxies, but we still want to restore the legacy
// jQuery.cssNumber there.
jQuery.cssNumber = internalCssNumber;
}
} else {

// Make `internalCssNumber` defined for jQuery <4 as well as it's needed
// in the `jQuery.fn.css` patch below.
internalCssNumber = jQuery.cssNumber;
}

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

if ( typeof value === "number" ) {
camelName = camelCase( name );
if ( !isAutoPx( camelName ) && !jQuery.cssNumber[ camelName ] ) {

// Use `internalCssNumber` to avoid triggering our warnings in this
// internal check.
if ( !isAutoPx( camelName ) && !internalCssNumber[ camelName ] ) {
migrateWarn( "css-number",
"Number-typed values are deprecated for jQuery.fn.css( \"" +
name + "\", value )" );
Expand Down

0 comments on commit 8e26772

Please sign in to comment.