Skip to content

Commit e1b42fb

Browse files
Gabriel Schulhofmarkelog
Gabriel Schulhof
authored andcommitted
Event: Remove fake originalEvent from jQuery.Event.simulate
Fixes gh-2300 Closes gh-2303
1 parent a6ada95 commit e1b42fb

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/event.js

+6-2
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

+5
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

+54
Original file line numberDiff line numberDiff line change
@@ -2692,6 +2692,60 @@ test( ".off() removes the expando when there's no more data", function() {
26922692
}
26932693
});
26942694

2695+
test( "preventDefault() on focusin does not throw exception", function( assert ) {
2696+
expect( 1 );
2697+
2698+
var done = assert.async(),
2699+
input = jQuery( "<input/>" ).appendTo( "#form" );
2700+
input
2701+
.on( "focusin", function( event ) {
2702+
var exceptionCaught;
2703+
2704+
try {
2705+
event.preventDefault();
2706+
} catch ( theException ) {
2707+
exceptionCaught = theException;
2708+
}
2709+
2710+
assert.strictEqual( exceptionCaught, undefined,
2711+
"Preventing default on focusin throws no exception" );
2712+
2713+
done();
2714+
} )
2715+
.focus();
2716+
} );
2717+
2718+
test( "jQuery.event.simulate() event has no originalEvent", function( assert ) {
2719+
expect( 1 );
2720+
2721+
var done = assert.async(),
2722+
input = jQuery( "<input>" )
2723+
.on( "click", function( event ) {
2724+
assert.strictEqual( "originalEvent" in event, false,
2725+
"originalEvent not present on simulated event" );
2726+
done();
2727+
} );
2728+
2729+
jQuery.event.simulate( "click", input[ 0 ], new jQuery.Event(), true );
2730+
} );
2731+
2732+
test( "Donor event interference", function( assert ) {
2733+
assert.expect( 4 );
2734+
2735+
jQuery( "#donor-outer" ).on( "click", function() {
2736+
assert.ok( true, "click bubbled to outer div" );
2737+
} );
2738+
jQuery( "#donor-input" ).on( "click", function( event ) {
2739+
assert.ok( true, "got a click event from the input" );
2740+
assert.ok( !event.isPropagationStopped(), "propagation says it's not stopped" );
2741+
} );
2742+
jQuery( "#donor-input" ).on( "change", function( event ) {
2743+
assert.ok( true, "got a change event from the input" );
2744+
event.stopPropagation();
2745+
} );
2746+
jQuery( "#donor-input" )[0].click();
2747+
} );
2748+
26952749
// This tests are unreliable in Firefox
26962750
if ( !(/firefox/i.test( window.navigator.userAgent )) ) {
26972751
test( "Check order of focusin/focusout events", 2, function() {

0 commit comments

Comments
 (0)