Skip to content

Commit 8b2147b

Browse files
committed
rustdoc: fix keyboard shortcuts and console log on search page
1 parent 21a1213 commit 8b2147b

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

Diff for: src/librustdoc/html/static/js/search.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,33 @@ const TY_KEYWORD = itemTypes.indexOf("keyword");
4343

4444
// In the search display, allows to switch between tabs.
4545
function printTab(nb) {
46-
if (nb === 0 || nb === 1 || nb === 2) {
47-
searchState.currentTab = nb;
48-
}
49-
let nb_copy = nb;
46+
let iter = 0;
47+
let foundCurrentTab = false;
48+
let foundCurrentResultSet = false;
5049
onEachLazy(document.getElementById("titles").childNodes, elem => {
51-
if (nb_copy === 0) {
50+
if (nb === iter) {
5251
addClass(elem, "selected");
52+
foundCurrentTab = true;
5353
} else {
5454
removeClass(elem, "selected");
5555
}
56-
nb_copy -= 1;
56+
iter += 1;
5757
});
58+
iter = 0;
5859
onEachLazy(document.getElementById("results").childNodes, elem => {
59-
if (nb === 0) {
60+
if (nb === iter) {
6061
addClass(elem, "active");
62+
foundCurrentResultSet = true;
6163
} else {
6264
removeClass(elem, "active");
6365
}
64-
nb -= 1;
66+
iter += 1;
6567
});
68+
if (foundCurrentTab && foundCurrentResultSet) {
69+
searchState.currentTab = nb;
70+
} else if (nb != 0) {
71+
printTab(0);
72+
}
6673
}
6774

6875
/**
@@ -1731,6 +1738,7 @@ window.initSearch = rawSearchIndex => {
17311738
output += '<div id="titles">' +
17321739
makeTabHeader(0, signatureTabTitle, ret_others[1]) +
17331740
"</div>";
1741+
currentTab = 0;
17341742
}
17351743

17361744
const resultsElem = document.createElement("div");
@@ -1746,12 +1754,16 @@ window.initSearch = rawSearchIndex => {
17461754
}
17471755
search.appendChild(resultsElem);
17481756
// Reset focused elements.
1749-
searchState.focusedByTab = [null, null, null];
17501757
searchState.showResults(search);
17511758
const elems = document.getElementById("titles").childNodes;
1752-
elems[0].onclick = () => { printTab(0); };
1753-
elems[1].onclick = () => { printTab(1); };
1754-
elems[2].onclick = () => { printTab(2); };
1759+
searchState.focusedByTab = [];
1760+
let i = 0;
1761+
for (const elem of elems) {
1762+
const j = i;
1763+
elem.onclick = () => { printTab(j); };
1764+
searchState.focusedByTab.push(null);
1765+
i += 1;
1766+
}
17551767
printTab(currentTab);
17561768
}
17571769

Diff for: src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ write: (".search-input", "Foo")
66
wait-for: "#titles"
77
assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
88
assert-text: ("#titles > button:nth-of-type(1)", "In Names", STARTS_WITH)
9+
// Use left-right keys
10+
press-key: "ArrowDown"
11+
press-key: "ArrowRight"
12+
wait-for-attribute: ("#titles > button:nth-of-type(2)", {"class": "selected"})
13+
press-key: "ArrowRight"
14+
wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"})
15+
press-key: "ArrowRight"
16+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
17+
press-key: "ArrowLeft"
18+
wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"})
919

1020
// Now try search-by-return
1121
goto: file://|DOC_PATH|/test_docs/index.html
@@ -14,6 +24,16 @@ write: (".search-input", "-> String")
1424
wait-for: "#titles"
1525
assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
1626
assert-text: ("#titles > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH)
27+
// Use left-right keys
28+
press-key: "ArrowDown"
29+
press-key: "ArrowRight"
30+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
31+
press-key: "ArrowRight"
32+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
33+
press-key: "ArrowRight"
34+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
35+
press-key: "ArrowLeft"
36+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
1737

1838
// Try with a search-by-return with no results
1939
goto: file://|DOC_PATH|/test_docs/index.html

0 commit comments

Comments
 (0)