Skip to content

Commit 069c645

Browse files
committed
Tests (Simulate): Added focus and blur support.
(cherry picked from commit e31adf0) Conflicts: tests/unit/autocomplete/autocomplete_events.js tests/unit/tooltip/tooltip_events.js
1 parent ead7e92 commit 069c645

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/jquery.simulate.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ $.simulate = function( el, type, options ) {
2424

2525
if ( type === "drag" ) {
2626
this[ type ].apply( this, [ this.target, options ] );
27+
} else if ( type === "focus" || type === "blur" ) {
28+
this[ type ]();
2729
} else {
2830
this.simulateEvent( el, type, options );
2931
}
@@ -157,6 +159,53 @@ $.extend( $.simulate.prototype, {
157159
x: o.left + el.outerWidth() / 2 - d.scrollLeft(),
158160
y: o.top + el.outerHeight() / 2 - d.scrollTop()
159161
};
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 );
160209
}
161210
});
162211

0 commit comments

Comments
 (0)