Event: Support EventListener interface objects.

http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventListener

Fixes gh-1735
This commit is contained in:
Mark M. Young 2022-03-15 13:43:26 -05:00
parent 2525cffc42
commit 9877ca1a39
2 changed files with 27 additions and 0 deletions

View File

@ -117,6 +117,11 @@ jQuery.event = {
selector = handleObjIn.selector; selector = handleObjIn.selector;
} }
// Support objects implementing the EventListener interface.
if ( handler && typeof handler.handleEvent === "function" ) {
handler = handler.handleEvent.bind( handler );
}
// Ensure that invalid selectors throw exceptions at attach time // Ensure that invalid selectors throw exceptions at attach time
// Evaluate against documentElement in case elem is a non-element node (e.g., document) // Evaluate against documentElement in case elem is a non-element node (e.g., document)
if ( selector ) { if ( selector ) {

View File

@ -45,6 +45,28 @@ QUnit.test( "on() with non-null,defined data", function( assert ) {
} ); } );
QUnit.test( "on() with EventListener interface object", function( assert ) {
assert.expect( 4 );
var handler = {
eventDelegateData: "can be found",
handleEvent: function( event, data ) {
assert.equal( data, 0, "non-null, defined data (zero) is correctly passed" );
assert.equal( this.eventDelegateData, "can be found", "event delegate is accessible via this" );
}
};
jQuery( "#foo" ).on( "foo.on", handler );
jQuery( "div" ).on( "foo.delegate", "#foo", handler );
jQuery( "#foo" ).trigger( "foo", 0 );
jQuery( "#foo" ).off( "foo.on", handler );
jQuery( "div" ).off( "foo.delegate", "#foo" );
} );
QUnit.test( "Handler changes and .trigger() order", function( assert ) { QUnit.test( "Handler changes and .trigger() order", function( assert ) {
assert.expect( 1 ); assert.expect( 1 );