]> source.dussan.org Git - jquery.git/commitdiff
Added support for bubbling triggered events.
authorJohn Resig <jeresig@gmail.com>
Mon, 22 Dec 2008 01:57:06 +0000 (01:57 +0000)
committerJohn Resig <jeresig@gmail.com>
Mon, 22 Dec 2008 01:57:06 +0000 (01:57 +0000)
src/event.js
test/unit/event.js

index 9d0645731ed974b5d2f9956389b9223ac44cbf17..8fb77a9a244b594d99f016de4392625291993170 100644 (file)
@@ -212,6 +212,12 @@ jQuery.event = {
                        if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
                                val = false;
 
+                       if ( donative !== false && val !== false ) {
+                               var parent = elem.parentNode || elem.ownerDocument;
+                               if ( parent )
+                                       jQuery.event.trigger(type, data, parent, donative);
+                       }
+
                        // Extra functions don't get the custom event object
                        if ( event )
                                data.shift();
index 642ba5e5d0b18e598f608a865717ad1ff7cb362a..eb9af7a340b349404a9826c923bfd08842a2b208 100644 (file)
@@ -198,6 +198,40 @@ test("trigger() shortcuts", function() {
        }).load();
 });
 
+test("trigger() bubbling", function() {
+       expect(14);
+
+       var doc = 0, html = 0, body = 0, main = 0, ap = 0;
+
+       jQuery(document).bind("click", function(){ doc++; });
+       jQuery("html").bind("click", function(){ html++; });
+       jQuery("body").bind("click", function(){ body++; });
+       jQuery("#main").bind("click", function(){ main++; });
+       jQuery("#ap").bind("click", function(){ ap++; return false; });
+
+       jQuery("html").trigger("click");
+       equals( doc, 1, "HTML bubble" );
+       equals( html, 1, "HTML bubble" );
+
+       jQuery("body").trigger("click");
+       equals( doc, 2, "Body bubble" );
+       equals( html, 2, "Body bubble" );
+       equals( body, 1, "Body bubble" );
+
+       jQuery("#main").trigger("click");
+       equals( doc, 3, "Main bubble" );
+       equals( html, 3, "Main bubble" );
+       equals( body, 2, "Main bubble" );
+       equals( main, 1, "Main bubble" );
+
+       jQuery("#ap").trigger("click");
+       equals( doc, 3, "ap bubble" );
+       equals( html, 3, "ap bubble" );
+       equals( body, 2, "ap bubble" );
+       equals( main, 1, "ap bubble" );
+       equals( ap, 1, "ap bubble" );
+});
+
 test("unbind(event)", function() {
        expect(8);
        var el = jQuery("#firstp");
@@ -400,4 +434,4 @@ test("event properties", function() {
                start();
        }).click();
 });
-*/
\ No newline at end of file
+*/