From 62ada32808f23a78f2af2c1f87f54812e24fdab1 Mon Sep 17 00:00:00 2001 From: gabalafou Date: Thu, 22 Aug 2024 18:42:33 +0200 Subject: [PATCH 1/4] 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")) { From b7065b4912faad31c95e96577d38ed8d2cf8af22 Mon Sep 17 00:00:00 2001 From: gabalafou Date: Mon, 21 Oct 2024 05:52:19 -0500 Subject: [PATCH 2/4] Apply suggestions from code review --- sphinx/themes/basic/static/doctools.js | 2 +- sphinx/themes/basic/static/sphinx_highlight.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx/themes/basic/static/doctools.js b/sphinx/themes/basic/static/doctools.js index 4aa49883646..63fd01f616d 100644 --- a/sphinx/themes/basic/static/doctools.js +++ b/sphinx/themes/basic/static/doctools.js @@ -104,7 +104,7 @@ const Documentation = { return; document.addEventListener("keydown", (event) => { - // bail if browser focus is on anything but a link + // bail if browser focus is on anything but a link (or the body) const focusEl = document.activeElement; if (focusEl && (focusEl !== document.body || focusEl.tagName !== "A")) return; // bail with special keys diff --git a/sphinx/themes/basic/static/sphinx_highlight.js b/sphinx/themes/basic/static/sphinx_highlight.js index 10cc66fcf27..1bd7b34d71e 100644 --- a/sphinx/themes/basic/static/sphinx_highlight.js +++ b/sphinx/themes/basic/static/sphinx_highlight.js @@ -133,7 +133,7 @@ const SphinxHighlight = { if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) return; document.addEventListener("keydown", (event) => { - // bail if browser focus is on anything but a link + // bail if browser focus is on anything but a link (or the body) const focusEl = document.activeElement; if (focusEl && (focusEl !== document.body || focusEl.tagName !== "A")) return; // bail with special keys From 6f04db0074b79caea11d980cdd5c9293dfdd7876 Mon Sep 17 00:00:00 2001 From: gabalafou Date: Mon, 21 Oct 2024 06:07:42 -0500 Subject: [PATCH 3/4] Update CHANGES.rst --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 758423e176f..311eb22cd49 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -87,7 +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) +* #12811: Stop gobbling escape key Testing ------- From 78715821e11f8075b6d4a05e1f7f6bb6e983112d Mon Sep 17 00:00:00 2001 From: gabalafou Date: Mon, 21 Oct 2024 06:08:59 -0500 Subject: [PATCH 4/4] Update CHANGES.rst --- CHANGES.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 311eb22cd49..8141f8356ca 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -87,7 +87,8 @@ 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: Stop gobbling escape key +* #12813: Stop gobbling escape key + Patch by Gabriel Fouasnon. Testing -------