From 1cecf64e5aa415367a7dae0b55c2dd17b591442d Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Tue, 17 Sep 2013 18:51:54 -0400 Subject: [PATCH] Fix #14180. Allow cross-frame use of focusin/out. Close gh-1369. --- src/event.js | 24 +++++++++++++------ test/data/event/focusinCrossFrame.html | 18 ++++++++++++++ test/unit/event.js | 33 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 test/data/event/focusinCrossFrame.html diff --git a/src/event.js b/src/event.js index ec905f2d6..fc924ec36 100644 --- a/src/event.js +++ b/src/event.js @@ -726,21 +726,31 @@ jQuery.each({ if ( !support.focusinBubbles ) { jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) { - // Attach a single capturing handler while someone wants focusin/focusout - var attaches = 0, - handler = function( event ) { + // Attach a single capturing handler on the document while someone wants focusin/focusout + var handler = function( event ) { jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true ); }; jQuery.event.special[ fix ] = { setup: function() { - if ( attaches++ === 0 ) { - document.addEventListener( orig, handler, true ); + var doc = this.ownerDocument, + attaches = data_priv.access( doc, fix ); + + if ( !attaches ) { + doc.addEventListener( orig, handler, true ); } + data_priv.access( doc, fix, ( attaches || 0 ) + 1 ); }, teardown: function() { - if ( --attaches === 0 ) { - document.removeEventListener( orig, handler, true ); + var doc = this.ownerDocument, + 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 ); } } }; diff --git a/test/data/event/focusinCrossFrame.html b/test/data/event/focusinCrossFrame.html new file mode 100644 index 000000000..487f8de8f --- /dev/null +++ b/test/data/event/focusinCrossFrame.html @@ -0,0 +1,18 @@ + + + + + focusin event cross-frame (#14180) + + + + + + + + diff --git a/test/unit/event.js b/test/unit/event.js index 91ac98007..a18f2f673 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -2374,6 +2374,39 @@ test("fixHooks extensions", function() { 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 ) { expect(1); ok( isOk, "$.when( $.ready ) works" ); -- 2.39.5