about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2022-05-02 15:50:01 -0700
committerMichael Howell <michael@notriddle.com>2022-05-05 09:39:45 -0700
commit8b2147b4970a06377defd142fa932d2264d2aa5d (patch)
treedafd833773ffb75da947250f3d61a1507e4bb82e
parent21a121332b4c5e2302c46956564879c447d555b2 (diff)
downloadrust-8b2147b4970a06377defd142fa932d2264d2aa5d.tar.gz
rust-8b2147b4970a06377defd142fa932d2264d2aa5d.zip
rustdoc: fix keyboard shortcuts and console log on search page
-rw-r--r--src/librustdoc/html/static/js/search.js36
-rw-r--r--src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml20
2 files changed, 44 insertions, 12 deletions
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js
index 2468d39ebc7..2bb850f994d 100644
--- a/src/librustdoc/html/static/js/search.js
+++ b/src/librustdoc/html/static/js/search.js
@@ -43,26 +43,33 @@ const TY_KEYWORD = itemTypes.indexOf("keyword");
 
 // In the search display, allows to switch between tabs.
 function printTab(nb) {
-    if (nb === 0 || nb === 1 || nb === 2) {
-        searchState.currentTab = nb;
-    }
-    let nb_copy = nb;
+    let iter = 0;
+    let foundCurrentTab = false;
+    let foundCurrentResultSet = false;
     onEachLazy(document.getElementById("titles").childNodes, elem => {
-        if (nb_copy === 0) {
+        if (nb === iter) {
             addClass(elem, "selected");
+            foundCurrentTab = true;
         } else {
             removeClass(elem, "selected");
         }
-        nb_copy -= 1;
+        iter += 1;
     });
+    iter = 0;
     onEachLazy(document.getElementById("results").childNodes, elem => {
-        if (nb === 0) {
+        if (nb === iter) {
             addClass(elem, "active");
+            foundCurrentResultSet = true;
         } else {
             removeClass(elem, "active");
         }
-        nb -= 1;
+        iter += 1;
     });
+    if (foundCurrentTab && foundCurrentResultSet) {
+        searchState.currentTab = nb;
+    } else if (nb != 0) {
+        printTab(0);
+    }
 }
 
 /**
@@ -1731,6 +1738,7 @@ window.initSearch = rawSearchIndex => {
             output += '<div id="titles">' +
                 makeTabHeader(0, signatureTabTitle, ret_others[1]) +
                 "</div>";
+            currentTab = 0;
         }
 
         const resultsElem = document.createElement("div");
@@ -1746,12 +1754,16 @@ window.initSearch = rawSearchIndex => {
         }
         search.appendChild(resultsElem);
         // Reset focused elements.
-        searchState.focusedByTab = [null, null, null];
         searchState.showResults(search);
         const elems = document.getElementById("titles").childNodes;
-        elems[0].onclick = () => { printTab(0); };
-        elems[1].onclick = () => { printTab(1); };
-        elems[2].onclick = () => { printTab(2); };
+        searchState.focusedByTab = [];
+        let i = 0;
+        for (const elem of elems) {
+            const j = i;
+            elem.onclick = () => { printTab(j); };
+            searchState.focusedByTab.push(null);
+            i += 1;
+        }
         printTab(currentTab);
     }
 
diff --git a/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml b/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml
index fc6b0696bf0..7bc837bfaeb 100644
--- a/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml
+++ b/src/test/rustdoc-gui/search-tab-change-title-fn-sig.goml
@@ -6,6 +6,16 @@ write: (".search-input", "Foo")
 wait-for: "#titles"
 assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
 assert-text: ("#titles > button:nth-of-type(1)", "In Names", STARTS_WITH)
+// Use left-right keys
+press-key: "ArrowDown"
+press-key: "ArrowRight"
+wait-for-attribute: ("#titles > button:nth-of-type(2)", {"class": "selected"})
+press-key: "ArrowRight"
+wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"})
+press-key: "ArrowRight"
+wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
+press-key: "ArrowLeft"
+wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"})
 
 // Now try search-by-return
 goto: file://|DOC_PATH|/test_docs/index.html
@@ -14,6 +24,16 @@ write: (".search-input", "-> String")
 wait-for: "#titles"
 assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
 assert-text: ("#titles > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH)
+// Use left-right keys
+press-key: "ArrowDown"
+press-key: "ArrowRight"
+wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
+press-key: "ArrowRight"
+wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
+press-key: "ArrowRight"
+wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
+press-key: "ArrowLeft"
+wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
 
 // Try with a search-by-return with no results
 goto: file://|DOC_PATH|/test_docs/index.html