Skip to content

Commit 88068f8

Browse files
justinbmeyerjeresig
authored andcommitted
Make sure that focusin/focusout bubbles in non-IE browsers.
1 parent 39addc8 commit 88068f8

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/event.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var rnamespaces = /\.(.*)$/,
77
rescape = /[^\w\s.|`]/g,
88
fcleanup = function( nm ) {
99
return nm.replace(rescape, "\\$&");
10-
};
10+
},
11+
focusCounts = { focusin: 0, focusout: 0 };
1112

1213
/*
1314
* A number of helper functions used for managing events.
@@ -855,17 +856,21 @@ if ( document.addEventListener ) {
855856
jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) {
856857
jQuery.event.special[ fix ] = {
857858
setup: function() {
858-
this.addEventListener( orig, handler, true );
859+
if ( focusCounts[fix]++ === 0 ) {
860+
document.addEventListener( orig, handler, true );
861+
}
859862
},
860863
teardown: function() {
861-
this.removeEventListener( orig, handler, true );
864+
if ( --focusCounts[fix] === 0 ) {
865+
document.removeEventListener( orig, handler, true );
866+
}
862867
}
863868
};
864869

865870
function handler( e ) {
866871
e = jQuery.event.fix( e );
867872
e.type = fix;
868-
return jQuery.event.handle.call( this, e );
873+
return jQuery.event.trigger( e, null, e.target );
869874
}
870875
});
871876
}

test/unit/event.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,21 @@ test("window resize", function() {
18431843

18441844
ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
18451845
});
1846-
1846+
test("focusin bubbles", function(){
1847+
//create an input and focusin on it
1848+
var input = jQuery("<input/>"),
1849+
order = 0;
1850+
input.appendTo(document.body);
1851+
jQuery(document.body).bind("focusin.focusinBubblesTest",function(){
1852+
equals(1,order++,"focusin on the body second")
1853+
})
1854+
input.bind("focusin.focusinBubblesTest",function(){
1855+
equals(0,order++,"focusin on the element first")
1856+
})
1857+
input[0].focus();
1858+
input.remove();
1859+
jQuery(document.body).unbind("focusin.focusinBubblesTest");
1860+
})
18471861
/*
18481862
test("jQuery(function($) {})", function() {
18491863
stop();

0 commit comments

Comments
 (0)