Skip to content

Commit 49e88ef

Browse files
committed
Event: Patch jQuery.event.special's prototype
Allow to use common `Object.prototype` properties on `jQuery.event.special` but warn as well. Fixes gh-542
1 parent 5c1b81f commit 49e88ef

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/jquery/event.js

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
migrateWarnProp
66
} from "../main.js";
77
import "../disablePatches.js";
8+
import { patchProto } from "../utils.js";
89

910
var oldLoad = jQuery.fn.load,
1011
oldEventAdd = jQuery.event.add,
@@ -136,3 +137,11 @@ migratePatchAndWarnFunc( jQuery.fn, "undelegate", function( selector, types, fn
136137
migratePatchAndWarnFunc( jQuery.fn, "hover", function( fnOver, fnOut ) {
137138
return this.on( "mouseenter", fnOver ).on( "mouseleave", fnOut || fnOver );
138139
}, "pre-on-methods", "jQuery.fn.hover() is deprecated" );
140+
141+
// We can apply the patch unconditionally here as in the `3.x` line the API
142+
// inherits from `Object.prototype` even without a patch and `migrateWarn`
143+
// inside `patchProto` will already silence warnings if the patch gets disabled.
144+
patchProto( jQuery.event.special, {
145+
warningId: "event-special-null-proto",
146+
apiName: "jQuery.event.special"
147+
} );

test/unit/jquery/event.js

+24
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,27 @@ TestManager.runIframeTest( "Load within a ready handler", "event-lateload.html",
190190
JSON.stringify( jQuery.migrateWarnings ) );
191191
assert.ok( /load/.test( jQuery.migrateWarnings[ 0 ] ), "message ok" );
192192
} );
193+
194+
QUnit.test( "jQuery.event.special: properties from Object.prototype", function( assert ) {
195+
assert.expect( 4 );
196+
197+
try {
198+
expectNoWarning( assert, "Regular properties", function() {
199+
jQuery.event.special.fakeevent = {};
200+
201+
// eslint-disable-next-line no-unused-expressions
202+
jQuery.event.special.fakeevent;
203+
} );
204+
205+
(
206+
Object.setPrototypeOf ? expectWarning : expectNoWarning
207+
)( assert, "Properties from Object.prototype", 2, function() {
208+
assert.ok( jQuery.event.special.hasOwnProperty( "fakeevent" ),
209+
"hasOwnProperty works (property present)" );
210+
assert.ok( !jQuery.event.special.hasOwnProperty( "fakeevent2" ),
211+
"hasOwnProperty works (property missing)" );
212+
} );
213+
} finally {
214+
delete jQuery.event.special.fakeevent;
215+
}
216+
} );

0 commit comments

Comments
 (0)