]> source.dussan.org Git - jquery.git/commitdiff
Landed a fix for when a DOM element gets accidentally removed by another live event...
authorJohn Resig <jeresig@gmail.com>
Sat, 10 Jan 2009 19:57:07 +0000 (19:57 +0000)
committerJohn Resig <jeresig@gmail.com>
Sat, 10 Jan 2009 19:57:07 +0000 (19:57 +0000)
src/event.js
test/unit/event.js

index 1ba0357d2252760091d6310bcade548de394a9d9..08deb8b2985e13579a55be98e8097c2f68199576 100644 (file)
@@ -560,15 +560,23 @@ jQuery.fn.extend({
 
 function liveHandler( event ){
        var check = RegExp("(^|\\.)" + event.type + "(\\.|$)"),
-               stop = true;
+               stop = true,
+               elems = [];
 
        jQuery.each(jQuery.data(this, "events").live || [], function(i, fn){
-               if ( !event.isImmediatePropagationStopped() && check.test(fn.type) ) {
+               if ( check.test(fn.type) ) {
                        var elem = jQuery(event.target).closest(fn.data)[0];
-                       if ( elem && fn.call(elem, event, fn.data) === false )
-                               stop = false;
+                       if ( elem )
+                               elems.push({ elem: elem, fn: fn });
                }
        });
+
+       jQuery.each(elems, function(){
+               if ( !event.isImmediatePropagationStopped() &&
+                       this.fn.call(this.elem, event, this.fn.data) === false )
+                               stop = false;
+       });
+
        return stop;
 }
 
index e12f3b753c9e8be5cec65e5b2abfd861b1a0c51a..83a0797c88a3069184de018249515f8dfdf53e85 100644 (file)
@@ -474,7 +474,7 @@ test("toggle(Function, Function, ...)", function() {
 });
 
 test(".live()/.die()", function() {
-       expect(36);
+       expect(38);
 
        var submit = 0, div = 0, livea = 0, liveb = 0;
 
@@ -588,6 +588,20 @@ test(".live()/.die()", function() {
 
        // Cleanup
        jQuery("#nothiddendiv").die("foo", callback);
+       
+       // Make sure we don't loose the target by DOM modifications
+       // after the bubble already reached the liveHandler
+       var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
+       
+       jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
+       jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
+       
+       jQuery("#nothiddendiv span").click();
+       equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
+       equals( livec, 1, "Verify that second handler occurred even with nuked target." );
+       
+       // Cleanup
+       jQuery("#nothiddendivchild").die("click");
 });
 
 /*