]> source.dussan.org Git - jquery.git/commitdiff
Fix for #964
authorBrandon Aaron <brandon.aaron@gmail.com>
Sun, 25 Feb 2007 19:36:29 +0000 (19:36 +0000)
committerBrandon Aaron <brandon.aaron@gmail.com>
Sun, 25 Feb 2007 19:36:29 +0000 (19:36 +0000)
ChangeLog.txt
src/event/event.js
src/event/eventTest.js

index df48486839ebbb8d8d33471e45587862e350d5e6..f98b8bf77672d57577cb494a109e36a674b8ec13 100644 (file)
@@ -4,6 +4,7 @@
 
 === 1.1.2 ===
 
+* Event handlers (like element.onclick) are now removed when no more functions are bound to the event.
 * Fixed DOM Manipulations for form elements.
 * Fixed jQuery.isFunction to return false on nodes.
 * Fixed jQuery.className.has, escaping regex characters in className (for metadata)
index e7f920f9b885c207a1cf2d16e16178da56cc1508..fb7f820ca7d859b00d0204c7eefad669b33740c9 100644 (file)
@@ -55,18 +55,36 @@ jQuery.event = {
 
        // Detach an event or set of events from an element
        remove: function(element, type, handler) {
-               if (element.$events)
-                       if ( type && type.type )
-                               delete element.$events[ type.type ][ type.handler.guid ];
-                       else if (type && element.$events[type])
+               if (element.$events) {
+                       var i,j,k;
+                       if ( type && type.type ) { // type is actually an event object here
+                               handler = type.handler;
+                               type    = type.type;
+                       }
+                       
+                       if (type && element.$events[type])
+                               // remove the given handler for the given type
                                if ( handler )
                                        delete element.$events[type][handler.guid];
+                                       
+                               // remove all handlers for the given type
                                else
-                                       for ( var i in element.$events[type] )
+                                       for ( i in element.$events[type] )
                                                delete element.$events[type][i];
+                                               
+                       // remove all handlers          
                        else
-                               for ( var j in element.$events )
+                               for ( j in element.$events )
                                        this.remove( element, j );
+                       
+                       // remove event handler if no more handlers exist
+                       for ( k in element.$events[type] )
+                               if (k) {
+                                       k = true;
+                                       break;
+                               }
+                       if (!k) element["on" + type] = null;
+               }
        },
 
        trigger: function(type, data, element) {
index 9615e0b637792376f11f840a937da6db908937b6..a71e01b778ce1adcbb108e738c9778c07d476472 100644 (file)
@@ -15,7 +15,7 @@ test("toggle(Function, Function) - add toggle event and fake a few clicks", func
 });
 
 test("unbind(event)", function() {
-       expect(3);
+       expect(4);
        var el = $("#firstp");
        el.click(function() {
                ok( true, "Fake normal bind" );
@@ -25,6 +25,10 @@ test("unbind(event)", function() {
                ok( true, "Fake onebind" );
        });
        el.click().click();
+       
+       el.click(function() { return; });
+       el.unbind('click');
+       ok( !el[0].onclick, "Handler is removed" ); // Bug #964
 });
 
 test("trigger(event, [data]", function() {