Skip to content

Commit 6cfcbf7

Browse files
authoredMay 19, 2021
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``
2 parents e113a4f + d314b06 commit 6cfcbf7

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
 

‎src/librustdoc/html/static/main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ function hideThemeButtonState() {
425425
function handleEscape(ev) {
426426
var help = getHelpElement(false);
427427
var search = searchState.outputElement();
428-
if (!hasClass(help, "hidden")) {
428+
if (help && !hasClass(help, "hidden")) {
429429
displayHelp(false, ev, help);
430-
} else if (!hasClass(search, "hidden")) {
430+
} else if (search && !hasClass(search, "hidden")) {
431431
searchState.clearInputTimeout();
432432
ev.preventDefault();
433433
searchState.hideResults(search);

‎src/test/rustdoc-gui/escape-key.goml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
goto: file://|DOC_PATH|/test_docs/index.html
2+
// First, we check that the search results are hidden when the Escape key is pressed.
3+
write: (".search-input", "test")
4+
wait-for: "#search > h1" // The search element is empty before the first search
5+
assert: ("#search", "class", "content")
6+
assert: ("#main", "class", "content hidden")
7+
press-key: "Escape"
8+
assert: ("#search", "class", "content hidden")
9+
assert: ("#main", "class", "content")
10+
11+
// Check that focusing the search input brings back the search results
12+
focus: ".search-input"
13+
assert: ("#search", "class", "content")
14+
assert: ("#main", "class", "content hidden")
15+
16+
// Now let's check that when the help popup is displayed and we press Escape, it doesn't
17+
// hide the search results too.
18+
click: "#help-button"
19+
assert: ("#help", "class", "")
20+
press-key: "Escape"
21+
assert: ("#help", "class", "hidden")
22+
assert: ("#search", "class", "content")
23+
assert: ("#main", "class", "content hidden")
24+
25+
// FIXME: Once https://github.com/rust-lang/rust/pull/84462 is merged, add check to ensure
26+
// that Escape hides the search results when a result is focused.
27+
// press-key: "ArrowDown"

0 commit comments

Comments
 (0)
Please sign in to comment.