Skip to content

Commit

Permalink
Fix #13393. Avoid IE9 activeElement of death.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmethvin committed Apr 9, 2013
1 parent f1ba486 commit 85fc587
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/event.js
Expand Up @@ -11,6 +11,12 @@ function returnFalse() {
return false; return false;
} }


function safeActiveElement() {
try {
return document.activeElement;
} catch ( err ) { }
}

/* /*
* Helper functions for managing events -- not part of the public interface. * Helper functions for managing events -- not part of the public interface.
* Props to Dean Edwards' addEvent library for many of the ideas. * Props to Dean Edwards' addEvent library for many of the ideas.
Expand Down Expand Up @@ -522,7 +528,7 @@ jQuery.event = {
focus: { focus: {
// Fire native event if possible so blur/focus sequence is correct // Fire native event if possible so blur/focus sequence is correct
trigger: function() { trigger: function() {
if ( this !== document.activeElement && this.focus ) { if ( this !== safeActiveElement() && this.focus ) {
this.focus(); this.focus();
return false; return false;
} }
Expand All @@ -531,7 +537,7 @@ jQuery.event = {
}, },
blur: { blur: {
trigger: function() { trigger: function() {
if ( this === document.activeElement && this.blur ) { if ( this === safeActiveElement() && this.blur ) {
this.blur(); this.blur();
return false; return false;
} }
Expand Down
16 changes: 16 additions & 0 deletions test/data/event/focusElem.html
@@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>.focus() (activeElement access #13393)</title>

<script src="../../jquery.js"></script>
</head>
<body>
<a href="#" id="frame-link"></a>
<script>
jQuery("#frame-link").focus();
window.parent.iframeCallback( true );
</script>
</body>
</html>
5 changes: 5 additions & 0 deletions test/unit/event.js
Expand Up @@ -2416,6 +2416,11 @@ testIframeWithCallback( "jQuery.ready promise", "event/promiseReady.html", funct
ok( isOk, "$.when( $.ready ) works" ); 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 // need PHP here to make the incepted IFRAME hang
if ( hasPHP ) { if ( hasPHP ) {
testIframeWithCallback( "jQuery.ready synchronous load with long loading subresources", "event/syncReady.html", function( isOk ) { testIframeWithCallback( "jQuery.ready synchronous load with long loading subresources", "event/syncReady.html", function( isOk ) {
Expand Down

0 comments on commit 85fc587

Please sign in to comment.