Skip to content

Commit

Permalink
Event: Support EventListener interface objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark M. Young committed Mar 15, 2022
1 parent 2525cff commit 9877ca1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ jQuery.event = {
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
// Evaluate against documentElement in case elem is a non-element node (e.g., document)
if ( selector ) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
assert.expect( 1 );

Expand Down

0 comments on commit 9877ca1

Please sign in to comment.