Skip to content

Commit

Permalink
Bail on escape key press if focus is on anything (other than a link)
Browse files Browse the repository at this point in the history
Fixes #12811.
  • Loading branch information
gabalafou committed Aug 22, 2024
1 parent 1852d0f commit 62ada32
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
12 changes: 3 additions & 9 deletions sphinx/themes/basic/static/doctools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions sphinx/themes/basic/static/sphinx_highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down

0 comments on commit 62ada32

Please sign in to comment.