diff options
author | Scott González <scott.gonzalez@gmail.com> | 2011-10-31 17:28:00 -0400 |
---|---|---|
committer | Scott González <scott.gonzalez@gmail.com> | 2011-10-31 17:28:00 -0400 |
commit | e31adf039b4adb07b8878435beceaae1a6532d47 (patch) | |
tree | a1fbf35961c386f7a2306fe715c6988dcee7b850 /tests/jquery.simulate.js | |
parent | 79105eeb5649ad650073d5603d9d50759e979e99 (diff) | |
download | jquery-ui-e31adf039b4adb07b8878435beceaae1a6532d47.tar.gz jquery-ui-e31adf039b4adb07b8878435beceaae1a6532d47.zip |
Tests (Simulate): Added focus and blur support.
Diffstat (limited to 'tests/jquery.simulate.js')
-rw-r--r-- | tests/jquery.simulate.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/jquery.simulate.js b/tests/jquery.simulate.js index 9713a3038..c1d28f982 100644 --- a/tests/jquery.simulate.js +++ b/tests/jquery.simulate.js @@ -24,6 +24,8 @@ $.simulate = function( el, type, options ) { if ( type === "drag" ) { this[ type ].apply( this, [ this.target, options ] ); + } else if ( type === "focus" || type === "blur" ) { + this[ type ](); } else { this.simulateEvent( el, type, options ); } @@ -157,6 +159,53 @@ $.extend( $.simulate.prototype, { x: o.left + el.outerWidth() / 2 - d.scrollLeft(), y: o.top + el.outerHeight() / 2 - d.scrollTop() }; + }, + + focus: function() { + var focusinEvent, + triggered = false, + element = $( this.target ); + + function trigger() { + triggered = true; + } + + element.bind( "focus", trigger ); + element[ 0 ].focus(); + + if ( !triggered ) { + focusinEvent = $.Event( "focusin" ); + focusinEvent.preventDefault(); + element.trigger( focusinEvent ); + element.triggerHandler( "focus" ); + } + element.unbind( "focus", trigger ); + }, + + blur: function() { + var focusoutEvent, + triggered = false, + element = $( this.target ); + + function trigger() { + triggered = true; + } + + element.bind( "blur", trigger ); + element[ 0 ].blur(); + + // Some versions of IE don't actually .blur() on an element - so we focus the body + if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) { + element[ 0 ].ownerDocument.body.focus(); + } + + if ( !triggered ) { + focusoutEvent = $.Event( "focusout" ); + focusoutEvent.preventDefault(); + element.trigger( focusoutEvent ); + element.triggerHandler( "blur" ); + } + element.unbind( "blur", trigger ); } }); |