Selector: Wrap activeElement access in try-catch

In IE 9 accessing `document.activeElement` may throw; see
https://bugs.jquery.com/ticket/13393. We've already guarded
against this in event code but not in selector.

Closes gh-5229
This commit is contained in:
Michał Gołębiowski-Owczarek 2023-03-27 21:48:38 +02:00 committed by GitHub
parent 754108fbbf
commit 3936cf3ef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -163,6 +163,17 @@ var i,
{ dir: "parentNode", next: "legend" } { dir: "parentNode", next: "legend" }
); );
// Support: IE <=9 only
// Accessing document.activeElement can throw unexpectedly
// https://bugs.jquery.com/ticket/13393
// An identical function exists in `src/event.js` but they use different
// `documents` so it cannot be easily extracted.
function safeActiveElement() {
try {
return document.activeElement;
} catch ( err ) { }
}
// Optimize for push.apply( _, NodeList ) // Optimize for push.apply( _, NodeList )
try { try {
push.apply( push.apply(
@ -1316,7 +1327,7 @@ Expr = jQuery.expr = {
}, },
focus: function( elem ) { focus: function( elem ) {
return elem === document.activeElement && return elem === safeActiveElement() &&
document.hasFocus() && document.hasFocus() &&
!!( elem.type || elem.href || ~elem.tabIndex ); !!( elem.type || elem.href || ~elem.tabIndex );
}, },