diff --git a/crates/mdbook-html/front-end/js/book.js b/crates/mdbook-html/front-end/js/book.js
index 62d7c4cc..ed1dfca4 100644
--- a/crates/mdbook-html/front-end/js/book.js
+++ b/crates/mdbook-html/front-end/js/book.js
@@ -19,6 +19,14 @@ function playground_text(playground, hidden = true) {
}
}
+/**
+ * Helper for global keypress handlers so they don't trigger when certain elements are active.
+ * @returns {boolean} True if the keypress handler should be skipped.
+ */
+function mdbook_something_else_has_focus(e) {
+ return /^(?:input|select|textarea)$/i.test(e.target.nodeName);
+}
+
(function codeSnippets() {
function fetch_with_timeout(url, options, timeout = 6000) {
return Promise.race([
@@ -648,12 +656,15 @@ aria-label="Show hidden lines">';
(function chapterNavigation() {
document.addEventListener('keydown', function(e) {
- if (e.altKey || e.ctrlKey || e.metaKey) {
- return;
- }
- if (window.search && window.search.hasFocus()) {
+ if (e.altKey ||
+ e.ctrlKey ||
+ e.metaKey ||
+ window.search && window.search.hasFocus() ||
+ mdbook_something_else_has_focus(e)
+ ) {
return;
}
+
const html = document.querySelector('html');
function next() {
diff --git a/crates/mdbook-html/front-end/searcher/searcher.js b/crates/mdbook-html/front-end/searcher/searcher.js
index 5c4bab65..ea17ae38 100644
--- a/crates/mdbook-html/front-end/searcher/searcher.js
+++ b/crates/mdbook-html/front-end/searcher/searcher.js
@@ -357,7 +357,7 @@ window.search = window.search || {};
e.shiftKey ||
e.target.type === 'textarea' ||
e.target.type === 'text' ||
- !hasFocus() && /^(?:input|select|textarea)$/i.test(e.target.nodeName)
+ !hasFocus() && mdbook_something_else_has_focus(e)
) {
return;
}
diff --git a/tests/gui/editor-keypress.goml b/tests/gui/editor-keypress.goml
index 6b1a96fc..f2cfe6f2 100644
--- a/tests/gui/editor-keypress.goml
+++ b/tests/gui/editor-keypress.goml
@@ -10,9 +10,10 @@ press-key: "s"
wait-for: 200
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"})
+// ? inside ACE editor shouldn't show global help popup.
press-key: "?"
wait-for: 200
-wait-for-css: ("#mdbook-help-container", {"display": "flex"})
+wait-for-css: ("#mdbook-help-container", {"display": "none"})
// Make sure arrow keys don"t navigate.
press-key: "ArrowRight"