From bca82254137a161094377b2d8189c2d9d5906a0f Mon Sep 17 00:00:00 2001 From: Ariel Flesler Date: Mon, 14 Sep 2009 22:04:22 +0000 Subject: [PATCH] jquery event: fixes #4989. blur and focus events now bubble and can be handled using live(). --- src/event.js | 35 +++++++++++++++++++++++++++++++++-- test/unit/event.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/event.js b/src/event.js index 9d77314..380d3d4 100644 --- a/src/event.js +++ b/src/event.js @@ -518,9 +518,10 @@ var withinElement = function( event ) { } }; +// Create mouseenter and mouseleave events jQuery.each({ - mouseover: 'mouseenter', - mouseout: 'mouseleave' + mouseover: "mouseenter", + mouseout: "mouseleave" }, function( orig, fix ) { jQuery.event.special[ fix ] = { setup: function(){ @@ -532,6 +533,36 @@ jQuery.each({ }; }); +// Create "bubbling" focus and blur events +jQuery.each({ + focus: "focusin", + blur: "focusout" +}, function( orig, fix ){ + var event = jQuery.event, + special = event.special, + handle = event.handle; + + function ieHandler() { + arguments[0].type = orig; + return handle.apply(this, arguments); + } + + special[orig] = { + setup:function() { + if ( this.addEventListener ) + this.addEventListener( orig, handle, true ); + else + jQuery.event.add( this, fix, ieHandler ); + }, + teardown:function() { + if ( this.removeEventListener ) + this.removeEventListener( orig, handle, true ); + else + jQuery.event.remove( this, fix, ieHandler ); + } + }; +}); + jQuery.fn.extend({ bind: function( type, data, fn, thisObject ) { if ( jQuery.isFunction( data ) ) { diff --git a/test/unit/event.js b/test/unit/event.js index cc84786..d10b3d3 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -745,6 +745,36 @@ test(".live()/.die()", function() { jQuery('span#liveSpan1').die('click'); }); +test("live with focus/blur", function(){ + expect(2); + + // Setup + jQuery("").appendTo("body"); + + var $child = jQuery("#livefb"), + child = $child[0], + counter = 0; + + function count(){ + counter++; + } + + // Test + $child.live("focus", count); + $child.live("blur", count); + + child.focus(); + equals(counter, 1, "Test live() with focus event"); + + child.blur(); + equals(counter, 2, "Test live() with blur event"); + + // Teardown + $child.die("focus", count); + $child.die("blur", count); + $child.remove(); +}); + test("Non DOM element events", function() { expect(3); -- 1.7.10.4