From 62ada32808f23a78f2af2c1f87f54812e24fdab1 Mon Sep 17 00:00:00 2001 From: gabalafou Date: Thu, 22 Aug 2024 18:42:33 +0200 Subject: [PATCH] Bail on escape key press if focus is on anything (other than a link) Fixes #12811. --- CHANGES.rst | 1 + sphinx/themes/basic/static/doctools.js | 12 +++--------- sphinx/themes/basic/static/sphinx_highlight.js | 5 +++-- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 5785d62ef95..758423e176f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -87,6 +87,7 @@ Bugs fixed file URL (user-defined base URL of an intersphinx project are left untouched even if they end with double forward slashes). Patch by Bénédikt Tran. +* #12811: Bail on escape key press if focus is on anything (other than a link) Testing ------- diff --git a/sphinx/themes/basic/static/doctools.js b/sphinx/themes/basic/static/doctools.js index 4d67807d17d..4aa49883646 100644 --- a/sphinx/themes/basic/static/doctools.js +++ b/sphinx/themes/basic/static/doctools.js @@ -10,13 +10,6 @@ */ "use strict"; -const BLACKLISTED_KEY_CONTROL_ELEMENTS = new Set([ - "TEXTAREA", - "INPUT", - "SELECT", - "BUTTON", -]); - const _ready = (callback) => { if (document.readyState !== "loading") { callback(); @@ -111,8 +104,9 @@ const Documentation = { return; document.addEventListener("keydown", (event) => { - // bail for input elements - if (BLACKLISTED_KEY_CONTROL_ELEMENTS.has(document.activeElement.tagName)) return; + // bail if browser focus is on anything but a link + const focusEl = document.activeElement; + if (focusEl && (focusEl !== document.body || focusEl.tagName !== "A")) return; // bail with special keys if (event.altKey || event.ctrlKey || event.metaKey) return; diff --git a/sphinx/themes/basic/static/sphinx_highlight.js b/sphinx/themes/basic/static/sphinx_highlight.js index 8a96c69a194..10cc66fcf27 100644 --- a/sphinx/themes/basic/static/sphinx_highlight.js +++ b/sphinx/themes/basic/static/sphinx_highlight.js @@ -133,8 +133,9 @@ const SphinxHighlight = { if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) return; document.addEventListener("keydown", (event) => { - // bail for input elements - if (BLACKLISTED_KEY_CONTROL_ELEMENTS.has(document.activeElement.tagName)) return; + // bail if browser focus is on anything but a link + const focusEl = document.activeElement; + if (focusEl && (focusEl !== document.body || focusEl.tagName !== "A")) return; // bail with special keys if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return; if (DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS && (event.key === "Escape")) {