Skip to content

Commit

Permalink
Make sure that elements that have been removed also have their specia…
Browse files Browse the repository at this point in the history
…l events cleaned up. Fixes #6084.
  • Loading branch information
jeresig committed Feb 13, 2010
1 parent 6a82f2a commit da96657
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/event.js
Expand Up @@ -112,6 +112,10 @@ jQuery.event = {

if ( special.add ) {
special.add.call( elem, handleObj );

if ( !handleObj.handler.guid ) {
handleObj.handler.guid = handler.guid;
}
}

// Add the function to the element's handler list
Expand Down
11 changes: 8 additions & 3 deletions src/manipulation.js
Expand Up @@ -547,7 +547,7 @@ jQuery.extend({
},

cleanData: function( elems ) {
var data, id, cache = jQuery.cache;
var data, id, cache = jQuery.cache, special = jQuery.event.special;

for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
id = elem[ jQuery.expando ];
Expand All @@ -556,8 +556,13 @@ jQuery.extend({
data = cache[ id ];

if ( data.events ) {
for ( var event in data.events ) {
removeEvent( elem, event, data.handle );
for ( var type in data.events ) {
if ( special[ type ] ) {
jQuery.event.remove( elem, type );

} else {
removeEvent( elem, type, data.handle );
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions test/unit/event.js
Expand Up @@ -72,7 +72,7 @@ test("bind(), multiple events at once and namespaces", function() {
});

test("bind(), namespace with special add", function() {
expect(19);
expect(24);

var div = jQuery("<div/>").bind("test", function(e) {
ok( true, "Test event fired." );
Expand All @@ -97,7 +97,9 @@ test("bind(), namespace with special add", function() {
handler.apply( this, arguments );
};
},
remove: function() {}
remove: function() {
ok(true, "Remove called.");
}
};

div.bind("test.a", {x: 1}, function(e) {
Expand All @@ -119,7 +121,17 @@ test("bind(), namespace with special add", function() {
// Should trigger 2
div.trigger("test.b");

// Should trigger 4
div.unbind("test");

div = jQuery("<div/>").bind("test", function(e) {
ok( true, "Test event fired." );
});

// Should trigger 2
div.appendTo("#main").remove();

delete jQuery.event.special.test;
});

test("bind(), no data", function() {
Expand Down

0 comments on commit da96657

Please sign in to comment.