Skip to content

Commit 7475d5d

Browse files
Gabriel Schulhofmarkelog
authored andcommitted
Event: Remove fake originalEvent from jQuery.Event.simulate
Fixes gh-2300 Closes gh-2303
1 parent 3c92770 commit 7475d5d

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/event.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,14 @@ jQuery.event = {
610610
event,
611611
{
612612
type: type,
613-
isSimulated: true,
614-
originalEvent: {}
613+
isSimulated: true
615614
}
616615
);
616+
617+
// This prevents stopPropagation(), stopImmediatePropagation(), and preventDefault() from
618+
// preventing default on the donor event.
619+
delete e.originalEvent;
620+
617621
if ( bubble ) {
618622
jQuery.event.trigger( e, null, elem );
619623
} else {

test/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<!-- this iframe is outside the #qunit-fixture so it won't reload constantly wasting time, but it means the tests must be "safe" and clean up after themselves -->
4343
<iframe id="loadediframe" name="loadediframe" style="display:none;" src="data/iframe.html"></iframe>
4444
<dl id="dl" style="position:absolute;top:-32767px;left:-32767px;width:1px;">
45+
<div id="donor-outer">
46+
<form id="donor-form">
47+
<input id="donor-input" type="radio" />
48+
</form>
49+
</div>
4550
<div id="qunit-fixture">
4651
<p id="firstp">See <a id="simon1" href="http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector" rel="bookmark">this blog entry</a> for more information.</p>
4752
<p id="ap">

test/unit/event.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,60 @@ test( ".off() removes the expando when there's no more data", function() {
26712671
}
26722672
});
26732673

2674+
test( "preventDefault() on focusin does not throw exception", function( assert ) {
2675+
expect( 1 );
2676+
2677+
var done = assert.async(),
2678+
input = jQuery( "<input/>" ).appendTo( "#form" );
2679+
input
2680+
.on( "focusin", function( event ) {
2681+
var exceptionCaught;
2682+
2683+
try {
2684+
event.preventDefault();
2685+
} catch ( theException ) {
2686+
exceptionCaught = theException;
2687+
}
2688+
2689+
assert.strictEqual( exceptionCaught, undefined,
2690+
"Preventing default on focusin throws no exception" );
2691+
2692+
done();
2693+
} )
2694+
.focus();
2695+
} );
2696+
2697+
test( "jQuery.event.simulate() event has no originalEvent", function( assert ) {
2698+
expect( 1 );
2699+
2700+
var done = assert.async(),
2701+
input = jQuery( "<input>" )
2702+
.on( "click", function( event ) {
2703+
assert.strictEqual( "originalEvent" in event, false,
2704+
"originalEvent not present on simulated event" );
2705+
done();
2706+
} );
2707+
2708+
jQuery.event.simulate( "click", input[ 0 ], new jQuery.Event(), true );
2709+
} );
2710+
2711+
test( "Donor event interference", function( assert ) {
2712+
assert.expect( 4 );
2713+
2714+
jQuery( "#donor-outer" ).on( "click", function() {
2715+
assert.ok( true, "click bubbled to outer div" );
2716+
} );
2717+
jQuery( "#donor-input" ).on( "click", function( event ) {
2718+
assert.ok( true, "got a click event from the input" );
2719+
assert.ok( !event.isPropagationStopped(), "propagation says it's not stopped" );
2720+
} );
2721+
jQuery( "#donor-input" ).on( "change", function( event ) {
2722+
assert.ok( true, "got a change event from the input" );
2723+
event.stopPropagation();
2724+
} );
2725+
jQuery( "#donor-input" )[0].click();
2726+
} );
2727+
26742728
// This tests are unreliable in Firefox
26752729
if ( !(/firefox/i.test( window.navigator.userAgent )) ) {
26762730
test( "Check order of focusin/focusout events", 2, function() {

0 commit comments

Comments
 (0)