]> git.lizzy.rs Git - rust.git/commitdiff
Fix selected crate search filter
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Mon, 18 Nov 2019 13:11:21 +0000 (14:11 +0100)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Mon, 18 Nov 2019 13:15:00 +0000 (14:15 +0100)
src/librustdoc/html/render.rs
src/librustdoc/html/static/main.js

index 9a87bcc10db8e1b8fc9240b32b3076ad27d39914..f35d1c5c3e10eaa39b7a9eec2d8632e56c6390e0 100644 (file)
@@ -883,7 +883,9 @@ fn to_json_string(&self) -> String {
         v.push_str(&minify_replacer(
             &format!("{}\n{}", variables.join(""), all_indexes.join("\n")),
             options.enable_minification));
-        v.push_str("initSearch(searchIndex);addSearchOptions(searchIndex);");
+        // "addSearchOptions" has to be called first so the crate filtering can be set before the
+        // search might start (if it's set into the URL for example).
+        v.push_str("addSearchOptions(searchIndex);initSearch(searchIndex);");
         cx.shared.fs.write(&dst, &v)?;
     }
     if options.enable_index_page {
index 752b93f2ac5e35a2f8cd7a68015f0e0442e50814..38fe545bec62876b76cdee3b0fbc55e48a66172e 100644 (file)
@@ -512,21 +512,6 @@ function getSearchElement() {
         var OUTPUT_DATA = 1;
         var params = getQueryStringParams();
 
-        // Set the crate filter from saved storage, if the current page has the saved crate filter.
-        //
-        // If not, ignore the crate filter -- we want to support filtering for crates on sites like
-        // doc.rust-lang.org where the crates may differ from page to page while on the same domain.
-        var savedCrate = getCurrentValue("rustdoc-saved-filter-crate");
-        if (savedCrate !== null) {
-            onEachLazy(document.getElementById("crate-search").getElementsByTagName("option"),
-                       function(e) {
-                if (e.value === savedCrate) {
-                    document.getElementById("crate-search").value = e.value;
-                    return true;
-                }
-            });
-        }
-
         // Populate search bar with query string search term when provided,
         // but only if the input bar is empty. This avoid the obnoxious issue
         // where you start trying to do a search, and the index loads, and
@@ -2620,11 +2605,21 @@ function getSearchElement() {
             }
             return 0;
         });
+        var savedCrate = getCurrentValue("rustdoc-saved-filter-crate");
         for (var i = 0; i < crates_text.length; ++i) {
             var option = document.createElement("option");
             option.value = crates_text[i];
             option.innerText = crates_text[i];
             elem.appendChild(option);
+            // Set the crate filter from saved storage, if the current page has the saved crate
+            // filter.
+            //
+            // If not, ignore the crate filter -- we want to support filtering for crates on sites
+            // like doc.rust-lang.org where the crates may differ from page to page while on the
+            // same domain.
+            if (crates_text[i] === savedCrate) {
+                elem.value = savedCrate;
+            }
         }
     }