Skip to content
Permalink
Browse files

Catch more cases where special events were incorrectly removed.

We can't take the blow-it-all-away shortcut because something in the middle of the list may be a mapped special event. On the bright side, -22!
  • Loading branch information...
dmethvin committed Nov 14, 2011
1 parent e0a4aa4 commit 683be093a675d2b8b65d5a33c3416cbe99a71967
Showing with 39 additions and 27 deletions.
  1. +15 −20 src/event.js
  2. +24 −7 test/unit/event.js
@@ -180,28 +180,23 @@ jQuery.event = {
origCount = eventType.length;
namespaces = namespaces ? new RegExp("(^|\\.)" + namespaces.split(".").sort().join("\\.(?:.*\\.)?") + "(\\.|$)") : null;

// Only need to loop for special events or selective removal
if ( handler || namespaces || selector || special.remove || origType !== type ) {
for ( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];

if ( origType === handleObj.origType &&
( !handler || handler.guid === handleObj.guid ) &&
( !namespaces || namespaces.test( handleObj.namespace ) ) &&
( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) {
eventType.splice( j--, 1 );

if ( handleObj.selector ) {
eventType.delegateCount--;
}
if ( special.remove ) {
special.remove.call( elem, handleObj );
}
// Remove matching events
for ( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];

if ( origType === handleObj.origType &&
( !handler || handler.guid === handleObj.guid ) &&
( !namespaces || namespaces.test( handleObj.namespace ) ) &&
( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) {
eventType.splice( j--, 1 );

if ( handleObj.selector ) {
eventType.delegateCount--;
}
if ( special.remove ) {
special.remove.call( elem, handleObj );
}
}
} else {
// Removing all events
eventType.length = 0;
}

// Remove generic event handler if we removed something and no more handlers exist
@@ -2462,19 +2462,18 @@ test(".on and .off", function() {
});

test("special bind/delegate name mapping", function() {
expect( 7 );
expect( 6 );

jQuery.event.special.slap = {
bindType: "click",
delegateType: "swing",
handle: function( event ) {
equal( event.handleObj.origType, "slap", "event type is correct for " + event.type );
equal( event.target.id, "mammy", "slapped your mammy" );
equal( event.handleObj.origType, "slap", "slapped your mammy, " + event.type );
}
};

var comeback = function( event ) {
ok( true, "event " + event.type + " triggered" );
ok( true, "event " + event.type + " triggered" );
};

jQuery( '<div><button id="mammy">Are We Not Men?</button></div>' )
@@ -2489,12 +2488,30 @@ test("special bind/delegate name mapping", function() {
.off( "click" )
.trigger( "swing" ) // delegateType-slap and swing
.end()
.off( "slap swing" )
.find( "button " ) // everything should be gone
.off( "slap swing", "button" )
.find( "button" ) // everything should be gone
.trigger( "slap" )
.trigger( "click" )
.trigger( "swing" )
.end();
.end()
.remove();
delete jQuery.event.special.slap;

// Ensure a special event isn't removed by its mapped type
jQuery.event.special.gutfeeling = {
bindType: "click",
delegateType: "click",
handle: function( event ) {
equal( event.handleObj.origType, "gutfeeling", "got a gutfeeling" );
}
};
jQuery( '<p>Gut Feeling</p>' )
.on( "click", jQuery.noop )
.on( "gutfeeling", jQuery.noop )
.off( "click" )
.trigger( "gutfeeling" )
.remove();
delete jQuery.event.special.gutfeeling;
});

test(".on and .off, selective mixed removal (#10705)", function() {

0 comments on commit 683be09

Please sign in to comment.
You can’t perform that action at this time.