Skip to content

Commit

Permalink
Fix #14180. Allow cross-frame use of focusin/out. Close gh-1369.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmethvin committed Nov 13, 2013
1 parent b7f62ab commit 1cecf64
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/event.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -726,21 +726,31 @@ jQuery.each({
if ( !support.focusinBubbles ) { if ( !support.focusinBubbles ) {
jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) { jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) {


// Attach a single capturing handler while someone wants focusin/focusout // Attach a single capturing handler on the document while someone wants focusin/focusout
var attaches = 0, var handler = function( event ) {
handler = function( event ) {
jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true ); jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true );
}; };


jQuery.event.special[ fix ] = { jQuery.event.special[ fix ] = {
setup: function() { setup: function() {
if ( attaches++ === 0 ) { var doc = this.ownerDocument,
document.addEventListener( orig, handler, true ); attaches = data_priv.access( doc, fix );

if ( !attaches ) {
doc.addEventListener( orig, handler, true );
} }
data_priv.access( doc, fix, ( attaches || 0 ) + 1 );
}, },
teardown: function() { teardown: function() {
if ( --attaches === 0 ) { var doc = this.ownerDocument,
document.removeEventListener( orig, handler, true ); attaches = data_priv.access( doc, fix ) - 1;

if ( !attaches ) {
doc.removeEventListener( orig, handler, true );
data_priv.remove( doc, fix );

} else {
data_priv.access( doc, fix, attaches );
} }
} }
}; };
Expand Down
18 changes: 18 additions & 0 deletions test/data/event/focusinCrossFrame.html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>focusin event cross-frame (#14180)</title>

<script src="../../jquery.js"></script>
</head>
<body>
<input type="text" id="frame-input" />
<script>
// Call parent when this frame is fully loaded, it will mess with #frame-input
jQuery( window ).one( "load", function() {
window.parent.iframeCallback( document );
});
</script>
</body>
</html>
33 changes: 33 additions & 0 deletions test/unit/event.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2374,6 +2374,39 @@ test("fixHooks extensions", function() {
jQuery.event.fixHooks.click = saved; jQuery.event.fixHooks.click = saved;
}); });


testIframeWithCallback( "focusin from an iframe", "event/focusinCrossFrame.html", function( frameDoc ) {
expect(1);

var input = jQuery( frameDoc ).find( "#frame-input" );

if ( input.length ) {
// Create a focusin handler on the parent; shouldn't affect the iframe's fate
jQuery ( "body" ).on( "focusin.iframeTest", function() {
ok( false, "fired a focusin event in the parent document" );
});

input.on( "focusin", function() {
ok( true, "fired a focusin event in the iframe" );
});

// Avoid a native event; Chrome can't force focus to another frame
input.trigger( "focusin" );

// Must manually remove handler to avoid leaks in our data store
input.remove();

// Be sure it was removed; nothing should happen
input.trigger( "focusin" );

// Remove body handler manually since it's outside the fixture
jQuery( "body" ).off( "focusin.iframeTest" );

} else {
// Opera 12 (pre-Blink) doesn't select anything
ok( true, "SOFTFAIL: no focus event fired in the iframe" );
}
});

testIframeWithCallback( "jQuery.ready promise", "event/promiseReady.html", function( isOk ) { testIframeWithCallback( "jQuery.ready promise", "event/promiseReady.html", function( isOk ) {
expect(1); expect(1);
ok( isOk, "$.when( $.ready ) works" ); ok( isOk, "$.when( $.ready ) works" );
Expand Down

3 comments on commit 1cecf64

@Krinkle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On jQuery 1.8.3 we were having this issue at Wikimedia. I patched around it with a similar approach.

However before I finished the upgrade I wanted to verify that that monkey-patch was no longer needed and was baffled that the test was passing on plain jQuery 1.8.3 without any patch. Turns out the unit test was flawed and I fear this one is flawed too.

When the event is triggered via .trigger( "focusin" ) it always works. Presumably because that bypasses the critical path.

I fixed my unit test by calling .focus() on the reference to the iframe's input node object directly.

@mgol
Copy link
Member

@mgol mgol commented on 1cecf64 Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Krinkle I've just noticed your comment when investigating another issue. Test fixed in #4656. 😅

@Krinkle
Copy link
Member

@Krinkle Krinkle commented on 1cecf64 Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgol Awesome, thanks! (And yeah, nowadays I would've filed an issue for this. Not sure why I didn't.)

Please sign in to comment.