about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJack Huey <31162821+jackh726@users.noreply.github.com>2021-05-18 22:36:19 -0400
committerGitHub <noreply@github.com>2021-05-18 22:36:19 -0400
commit6cfcbf7d032bc4d990ac2121730866849602e174 (patch)
treed56ee9b9586b3c711aea1317963f3494832cd97e
parente113a4f77bc56db8511f663dd0800ed9c9a27135 (diff)
parentd314b065e71c0fa27c7f954d62537f932153f6cc (diff)
downloadrust-6cfcbf7d032bc4d990ac2121730866849602e174.tar.gz
rust-6cfcbf7d032bc4d990ac2121730866849602e174.zip
Rollup merge of #85438 - GuillaumeGomez:fix-escape-handling, r=jsha
Fix escape handling

Currently, when we press Escape while on the search results, nothing is happening, this PR fixes it.

More information: it's because in case the element doesn't exist, `hasClass` will return `null`, which coerces into `false` with the `!` comparison operator. But even if it returned `false`, it would still be an issue because if the element doesn't exist, it means it's hidden so in this case it's just as good, hence the additional check I added.

r? ``@jsha``
-rw-r--r--src/librustdoc/html/static/main.js4
-rw-r--r--src/test/rustdoc-gui/escape-key.goml27
2 files changed, 29 insertions, 2 deletions
diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js
index 307ce9ac375..7c00cf940c7 100644
--- a/src/librustdoc/html/static/main.js
+++ b/src/librustdoc/html/static/main.js
@@ -425,9 +425,9 @@ function hideThemeButtonState() {
     function handleEscape(ev) {
         var help = getHelpElement(false);
         var search = searchState.outputElement();
-        if (!hasClass(help, "hidden")) {
+        if (help && !hasClass(help, "hidden")) {
             displayHelp(false, ev, help);
-        } else if (!hasClass(search, "hidden")) {
+        } else if (search && !hasClass(search, "hidden")) {
             searchState.clearInputTimeout();
             ev.preventDefault();
             searchState.hideResults(search);
diff --git a/src/test/rustdoc-gui/escape-key.goml b/src/test/rustdoc-gui/escape-key.goml
new file mode 100644
index 00000000000..303dd000ba3
--- /dev/null
+++ b/src/test/rustdoc-gui/escape-key.goml
@@ -0,0 +1,27 @@
+goto: file://|DOC_PATH|/test_docs/index.html
+// First, we check that the search results are hidden when the Escape key is pressed.
+write: (".search-input", "test")
+wait-for: "#search > h1" // The search element is empty before the first search 
+assert: ("#search", "class", "content")
+assert: ("#main", "class", "content hidden")
+press-key: "Escape"
+assert: ("#search", "class", "content hidden")
+assert: ("#main", "class", "content")
+
+// Check that focusing the search input brings back the search results
+focus: ".search-input"
+assert: ("#search", "class", "content")
+assert: ("#main", "class", "content hidden")
+
+// Now let's check that when the help popup is displayed and we press Escape, it doesn't
+// hide the search results too.
+click: "#help-button"
+assert: ("#help", "class", "")
+press-key: "Escape"
+assert: ("#help", "class", "hidden")
+assert: ("#search", "class", "content")
+assert: ("#main", "class", "content hidden")
+
+// FIXME: Once https://github.com/rust-lang/rust/pull/84462 is merged, add check to ensure
+// that Escape hides the search results when a result is focused.
+// press-key: "ArrowDown"