Make sure that focusin/focusout bubbles in non-IE browsers.
authorJustin Meyer <justinbmeyer@gmail.com>
Wed, 13 Oct 2010 14:35:28 +0000 (10:35 -0400)
committerjeresig <jeresig@gmail.com>
Wed, 13 Oct 2010 14:35:28 +0000 (10:35 -0400)
src/event.js
test/unit/event.js

index ab75670..d830c4d 100644 (file)
@@ -7,7 +7,8 @@ var rnamespaces = /\.(.*)$/,
        rescape = /[^\w\s.|`]/g,
        fcleanup = function( nm ) {
                return nm.replace(rescape, "\\$&");
-       };
+       },
+       focusCounts = { focusin: 0, focusout: 0 };
 
 /*
  * A number of helper functions used for managing events.
@@ -855,17 +856,21 @@ if ( document.addEventListener ) {
        jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) {
                jQuery.event.special[ fix ] = {
                        setup: function() {
-                               this.addEventListener( orig, handler, true );
+                               if ( focusCounts[fix]++ === 0 ) {
+                                       document.addEventListener( orig, handler, true );
+                               }
                        }, 
                        teardown: function() { 
-                               this.removeEventListener( orig, handler, true );
+                               if ( --focusCounts[fix] === 0 ) {
+                                       document.removeEventListener( orig, handler, true );
+                               }
                        }
                };
 
                function handler( e ) { 
                        e = jQuery.event.fix( e );
                        e.type = fix;
-                       return jQuery.event.handle.call( this, e );
+                       return jQuery.event.trigger( e, null, e.target );
                }
        });
 }
index a51659c..829ef7b 100644 (file)
@@ -1843,7 +1843,21 @@ test("window resize", function() {
 
        ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
 });
-
+test("focusin bubbles", function(){
+       //create an input and focusin on it
+       var input = jQuery("<input/>"),
+               order = 0;
+       input.appendTo(document.body);
+       jQuery(document.body).bind("focusin.focusinBubblesTest",function(){
+               equals(1,order++,"focusin on the body second")
+       })
+       input.bind("focusin.focusinBubblesTest",function(){
+               equals(0,order++,"focusin on the element first")
+       })
+       input[0].focus();
+       input.remove();
+       jQuery(document.body).unbind("focusin.focusinBubblesTest");
+})
 /*
 test("jQuery(function($) {})", function() {
        stop();