From 3936cf3ef355a556d7990c77bbcacb69208fa4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Mon, 27 Mar 2023 21:48:38 +0200 Subject: [PATCH] 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 --- src/selector.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/selector.js b/src/selector.js index 147a2e25c..2c8f04556 100644 --- a/src/selector.js +++ b/src/selector.js @@ -163,6 +163,17 @@ var i, { 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 ) try { push.apply( @@ -1316,7 +1327,7 @@ Expr = jQuery.expr = { }, focus: function( elem ) { - return elem === document.activeElement && + return elem === safeActiveElement() && document.hasFocus() && !!( elem.type || elem.href || ~elem.tabIndex ); },