Skip to content

Commit 5c2d087

Browse files
authored
Event: Don't crash if an element is removed on blur
In Chrome, if an element having a `focusout` handler is blurred by clicking outside of it, it invokes the handler synchronously. If that handler calls `.remove()` on the element, the data is cleared, leaving private data undefined. We're reading a property from that data so we need to guard against this. Fixes gh-4417 Closes gh-4799
1 parent a503c69 commit 5c2d087

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/event.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,13 @@ function leverageNative( el, type, expectSync ) {
558558
// Cancel the outer synthetic event
559559
event.stopImmediatePropagation();
560560
event.preventDefault();
561-
return result.value;
561+
562+
// Support: Chrome 86+
563+
// In Chrome, if an element having a focusout handler is blurred by
564+
// clicking outside of it, it invokes the handler synchronously. If
565+
// that handler calls `.remove()` on the element, the data is cleared,
566+
// leaving `result` undefined. We need to guard against this.
567+
return result && result.value;
562568
}
563569

564570
// If this is an inner synthetic event for an event with a bubbling surrogate

test/unit/event.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,33 @@ QUnit.test( "focusin on document & window", function( assert ) {
26302630
jQuery( document ).off( "focusout", increment );
26312631
} );
26322632

2633+
QUnit.test( "element removed during focusout (gh-4417)", function( assert ) {
2634+
assert.expect( 1 );
2635+
2636+
var button = jQuery( "<button>Click me</button>" );
2637+
2638+
button.appendTo( "#qunit-fixture" );
2639+
2640+
button.on( "click", function() {
2641+
button.trigger( "blur" );
2642+
assert.ok( true, "Removing the element didn't crash" );
2643+
} );
2644+
2645+
// Support: Chrome 86+
2646+
// In Chrome, if an element having a focusout handler is blurred by
2647+
// clicking outside of it, it invokes the handler synchronously. However,
2648+
// if the click happens programmatically, the invocation is asynchronous.
2649+
// As we have no way to simulate real user input in unit tests, simulate
2650+
// this behavior by calling `jQuery.cleanData` & removing the element using
2651+
// native APIs.
2652+
button[ 0 ].blur = function() {
2653+
jQuery.cleanData( [ this ] );
2654+
this.parentNode.removeChild( this );
2655+
};
2656+
2657+
button[ 0 ].click();
2658+
} );
2659+
26332660
testIframe(
26342661
"jQuery.ready promise",
26352662
"event/promiseReady.html",

0 commit comments

Comments
 (0)