@@ -24,6 +24,8 @@ $.simulate = function( el, type, options ) {
24
24
25
25
if ( type === "drag" ) {
26
26
this [ type ] . apply ( this , [ this . target , options ] ) ;
27
+ } else if ( type === "focus" || type === "blur" ) {
28
+ this [ type ] ( ) ;
27
29
} else {
28
30
this . simulateEvent ( el , type , options ) ;
29
31
}
@@ -157,6 +159,53 @@ $.extend( $.simulate.prototype, {
157
159
x : o . left + el . outerWidth ( ) / 2 - d . scrollLeft ( ) ,
158
160
y : o . top + el . outerHeight ( ) / 2 - d . scrollTop ( )
159
161
} ;
162
+ } ,
163
+
164
+ focus : function ( ) {
165
+ var focusinEvent ,
166
+ triggered = false ,
167
+ element = $ ( this . target ) ;
168
+
169
+ function trigger ( ) {
170
+ triggered = true ;
171
+ }
172
+
173
+ element . bind ( "focus" , trigger ) ;
174
+ element [ 0 ] . focus ( ) ;
175
+
176
+ if ( ! triggered ) {
177
+ focusinEvent = $ . Event ( "focusin" ) ;
178
+ focusinEvent . preventDefault ( ) ;
179
+ element . trigger ( focusinEvent ) ;
180
+ element . triggerHandler ( "focus" ) ;
181
+ }
182
+ element . unbind ( "focus" , trigger ) ;
183
+ } ,
184
+
185
+ blur : function ( ) {
186
+ var focusoutEvent ,
187
+ triggered = false ,
188
+ element = $ ( this . target ) ;
189
+
190
+ function trigger ( ) {
191
+ triggered = true ;
192
+ }
193
+
194
+ element . bind ( "blur" , trigger ) ;
195
+ element [ 0 ] . blur ( ) ;
196
+
197
+ // Some versions of IE don't actually .blur() on an element - so we focus the body
198
+ if ( element [ 0 ] . ownerDocument . activeElement === element [ 0 ] ) {
199
+ element [ 0 ] . ownerDocument . body . focus ( ) ;
200
+ }
201
+
202
+ if ( ! triggered ) {
203
+ focusoutEvent = $ . Event ( "focusout" ) ;
204
+ focusoutEvent . preventDefault ( ) ;
205
+ element . trigger ( focusoutEvent ) ;
206
+ element . triggerHandler ( "blur" ) ;
207
+ }
208
+ element . unbind ( "blur" , trigger ) ;
160
209
}
161
210
} ) ;
162
211
0 commit comments