From 2223605e91c52dc4f8a551df21a10f45935a1507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?= Date: Mon, 14 Apr 2025 22:55:35 +0200 Subject: [PATCH] Event: Patch jQuery.event.global The API has been write-only since 1.9.0 and is going to be removed in jQuery 4.0.0. Also: * make it more explicit why certain patches are tested in the context of `jQuery.migrateDisablePatches` & related APIs * fix an erroneous future tense in a few warnings --- src/jquery/event.js | 4 ++++ test/unit/jquery/event.js | 9 +++++++++ test/unit/migrate.js | 30 ++++++++++++++++++++++++++---- warnings.md | 10 ++++++++-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/jquery/event.js b/src/jquery/event.js index e13a5085..ce56591f 100644 --- a/src/jquery/event.js +++ b/src/jquery/event.js @@ -1,5 +1,6 @@ import { migrateWarn, + migrateWarnProp, migratePatchAndInfoFunc, migratePatchFunc } from "../main.js"; @@ -41,3 +42,6 @@ migratePatchAndInfoFunc( jQuery.fn, "undelegate", jQuery.fn.undelegate, migratePatchAndInfoFunc( jQuery.fn, "hover", jQuery.fn.hover, "hover", "jQuery.fn.hover() is deprecated" ); + +migrateWarnProp( jQuery.event, "global", {}, "event-global", + "jQuery.event.global is removed" ); diff --git a/test/unit/jquery/event.js b/test/unit/jquery/event.js index 519c1e01..e23383df 100644 --- a/test/unit/jquery/event.js +++ b/test/unit/jquery/event.js @@ -84,3 +84,12 @@ TestManager.runIframeTest( "Load within a ready handler", "event-lateload.html", JSON.stringify( jQuery.migrateMessages ) ); assert.ok( /load/.test( jQuery.migrateMessages[ 0 ] ), "message ok" ); } ); + +QUnit.test( "jQuery.event.global", function( assert ) { + assert.expect( 3 ); + + expectMessage( assert, "jQuery.event.global", 2, function() { + assert.ok( jQuery.isPlainObject( jQuery.event.global ), "is a plain object" ); + assert.deepEqual( jQuery.event.global, {}, "is an empty object" ); + } ); +} ); diff --git a/test/unit/migrate.js b/test/unit/migrate.js index a70ba9a2..27a4865d 100644 --- a/test/unit/migrate.js +++ b/test/unit/migrate.js @@ -46,7 +46,7 @@ QUnit.test( "jQuery.migrateDeduplicateMessages", function( assert ) { } ); QUnit.test( "disabling/enabling patches", function( assert ) { - assert.expect( 27 ); + assert.expect( 32 ); var elem = jQuery( "
" ); @@ -56,17 +56,28 @@ QUnit.test( "disabling/enabling patches", function( assert ) { // existing warnings. If the ones we rely on here get removed, // replace them with ones that still exist. + // A few APIs that are not slated for removal to make these tests more stable: assert.strictEqual( jQuery.migrateIsPatchEnabled( "pre-on-methods" ), true, "patch enabled by default (pre-on-methods)" ); assert.strictEqual( jQuery.migrateIsPatchEnabled( "proxy" ), true, "patch enabled by default (proxy)" ); assert.strictEqual( jQuery.migrateIsPatchEnabled( "shorthand-deprecated-v3" ), true, "patch enabled by default (shorthand-deprecated-v3)" ); + + // APIs patched via `migratePatchAndWarnFunc` or `migratePatchAndInfoFunc`; + // we're testing that: + // * they don't warn on access but only when called + // * they don't exist (access evaluates to `undefined`) if patch is disabled assert.strictEqual( jQuery.migrateIsPatchEnabled( "push" ), true, "patch enabled by default (push)" ); assert.strictEqual( jQuery.migrateIsPatchEnabled( "isArray" ), true, "patch enabled by default (isArray)" ); + // APIs patched via `migrateWarnProp` or `migrateInfoProp`; we're testing that: + // * they don't exist (access evaluates to `undefined`) if patch is disabled + assert.strictEqual( jQuery.migrateIsPatchEnabled( "event-global" ), + true, "patch enabled by default (event-global)" ); + expectMessage( assert, "pre-on-methods (default)", function() { jQuery().bind(); } ); @@ -82,6 +93,11 @@ QUnit.test( "disabling/enabling patches", function( assert ) { expectMessage( assert, "isArray (default)", function() { jQuery.isArray(); } ); + expectMessage( assert, "event-global (default)", function() { + + // eslint-disable-next-line no-unused-expressions + jQuery.event.global; + } ); expectNoMessage( assert, "push access without calling (default)", function() { assert.strictEqual( typeof jQuery().push, "function", @@ -92,7 +108,7 @@ QUnit.test( "disabling/enabling patches", function( assert ) { "access check doesn't trigger a message (isArray)" ); } ); - jQuery.migrateDisablePatches( "pre-on-methods", "proxy", "push", "isArray" ); + jQuery.migrateDisablePatches( "pre-on-methods", "proxy", "push", "isArray", "event-global" ); assert.strictEqual( jQuery.migrateIsPatchEnabled( "pre-on-methods" ), false, "patch disabled (pre-on-methods)" ); assert.strictEqual( jQuery.migrateIsPatchEnabled( "proxy" ), @@ -101,6 +117,8 @@ QUnit.test( "disabling/enabling patches", function( assert ) { true, "patch still enabled (shorthand-deprecated-v3)" ); assert.strictEqual( jQuery.migrateIsPatchEnabled( "push" ), false, "patch disabled (push)" ); + assert.strictEqual( jQuery.migrateIsPatchEnabled( "event-global" ), + false, "patch disabled (event-global)" ); expectNoMessage( assert, "pre-on-methods (disabled)", function() { jQuery().bind(); @@ -112,10 +130,14 @@ QUnit.test( "disabling/enabling patches", function( assert ) { jQuery().click(); } ); expectNoMessage( assert, "push (disabled)", function() { - assert.strictEqual( jQuery().push, undefined, "`push` patch no longer defined" ); + assert.strictEqual( jQuery().push, undefined, "`jQuery.fn.push` no longer defined" ); } ); expectNoMessage( assert, "isArray (disabled)", function() { - assert.strictEqual( jQuery.isArray, undefined, "`jQuery.isArray` patch no longer defined" ); + assert.strictEqual( jQuery.isArray, undefined, "`jQuery.isArray` no longer defined" ); + } ); + expectNoMessage( assert, "event-global (disabled)", function() { + assert.strictEqual( jQuery.event.global, undefined, + "`jQuery.event.global` no longer defined" ); } ); jQuery.migrateDisablePatches( "shorthand-deprecated-v3" ); diff --git a/warnings.md b/warnings.md index b41ebe9c..ef3683e3 100644 --- a/warnings.md +++ b/warnings.md @@ -161,16 +161,22 @@ This is _not_ a warning, but a console log message the plugin shows when it firs ### \[jsonp-promotion\] JQMIGRATE: JSON-to-JSONP auto-promotion is removed -**Cause:** `jQuery.ajax` calls with `dataType: 'json'` with a provided callback are automatically converted by jQuery to JSONP requests unless the options also specify `jsonp: false`. Auto-promoting JSON requests to JSONP introduces a security risk as the developer may be unaware they're not just downloading data but executing code from a remote domain. This auto-promoting behavior is deprecated and will be removed in jQuery 4.0.0. +**Cause:** `jQuery.ajax` calls with `dataType: 'json'` with a provided callback are automatically converted by jQuery to JSONP requests unless the options also specify `jsonp: false`. Auto-promoting JSON requests to JSONP introduces a security risk as the developer may be unaware they're not just downloading data but executing code from a remote domain. This auto-promoting behavior is removed in jQuery 4.0.0. **Solution:** To trigger a JSONP request, specify the `dataType: "jsonp"` option. ### \[deferred-getStackHook\] JQMIGRATE: jQuery.Deferred.getStackHook is removed; use jQuery.Deferred.getErrorHook -**Cause:** `jQuery.Deferred.getStackHook` was originally created to pass the stack trace from before an async barrier to report when a user error (like calling a non-existing function) causes a promise to be rejected. However, passing a stack trace doesn't take source maps into account, so we started advising to pass the whole error object. To make it clearer, we also renamed the API to `jQuery.Deferred.getErrorHook`. The legacy alias will be removed in jQuery 4.0.0 +**Cause:** `jQuery.Deferred.getStackHook` was originally created to pass the stack trace from before an async barrier to report when a user error (like calling a non-existing function) causes a promise to be rejected. However, passing a stack trace doesn't take source maps into account, so we started advising to pass the whole error object. To make it clearer, we also renamed the API to `jQuery.Deferred.getErrorHook`. The legacy alias is removed in jQuery 4.0.0. **Solution:** Rename all usage of `jQuery.Deferred.getStackHook` to `jQuery.Deferred.getErrorHook`. If you previously assigned a function returning an error stack to `jQuery.Deferred.getStackHook` or `jQuery.Deferred.getErrorHook`, change it to return a full error object. +### \[event-global\] JQMIGRATE: jQuery.Deferred.getStackHook is removed; use jQuery.Deferred.getErrorHook + +**Cause:** `jQuery.event.global` was an object with keys being event names for which event listeners have ever been added. Originally, it was needed for performance reasons and to fix memory leaks in old IE, but since jQuery 1.9.0, the library has only been recording the events, but it was not using that information anywhere. jQuery 4.0.0 removes the API. + +**Solution:** Remove all usage of `jQuery.event.global`; it's unlikely any existing usage is needed. + ## Deprecated APIs