From 85fc5878b3c6af73f42d61eedf73013e7faae408 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Mon, 8 Apr 2013 21:33:25 -0400 Subject: [PATCH] Fix #13393. Avoid IE9 activeElement of death. --- src/event.js | 10 ++++++++-- test/data/event/focusElem.html | 16 ++++++++++++++++ test/unit/event.js | 5 +++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/data/event/focusElem.html diff --git a/src/event.js b/src/event.js index 2c9b555144..97a408839b 100644 --- a/src/event.js +++ b/src/event.js @@ -11,6 +11,12 @@ function returnFalse() { return false; } +function safeActiveElement() { + try { + return document.activeElement; + } catch ( err ) { } +} + /* * Helper functions for managing events -- not part of the public interface. * Props to Dean Edwards' addEvent library for many of the ideas. @@ -522,7 +528,7 @@ jQuery.event = { focus: { // Fire native event if possible so blur/focus sequence is correct trigger: function() { - if ( this !== document.activeElement && this.focus ) { + if ( this !== safeActiveElement() && this.focus ) { this.focus(); return false; } @@ -531,7 +537,7 @@ jQuery.event = { }, blur: { trigger: function() { - if ( this === document.activeElement && this.blur ) { + if ( this === safeActiveElement() && this.blur ) { this.blur(); return false; } diff --git a/test/data/event/focusElem.html b/test/data/event/focusElem.html new file mode 100644 index 0000000000..eed082c1ad --- /dev/null +++ b/test/data/event/focusElem.html @@ -0,0 +1,16 @@ + + + + + .focus() (activeElement access #13393) + + + + + + + + \ No newline at end of file diff --git a/test/unit/event.js b/test/unit/event.js index 60f2c7a0a7..1a1a5652e8 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -2416,6 +2416,11 @@ testIframeWithCallback( "jQuery.ready promise", "event/promiseReady.html", funct ok( isOk, "$.when( $.ready ) works" ); }); +testIframeWithCallback( "Focusing iframe element", "event/focusElem.html", function( isOk ) { + expect(1); + ok( isOk, "Focused an element in an iframe" ); +}); + // need PHP here to make the incepted IFRAME hang if ( hasPHP ) { testIframeWithCallback( "jQuery.ready synchronous load with long loading subresources", "event/syncReady.html", function( isOk ) {