Skip to content

Commit f431639

Browse files
Fix invalid handling of "going back in history" when "Directly go to item in search if there is only one result" setting is set to true
1 parent 9f50942 commit f431639

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,18 @@ function preLoadCss(cssUrl) {
277277
searchState.mouseMovedAfterSearch = false;
278278
document.title = searchState.title;
279279
},
280-
hideResults: () => {
281-
switchDisplayedElement(null);
280+
removeQueryParameters: () => {
281+
// We change the document title.
282282
document.title = searchState.titleBeforeSearch;
283-
// We also remove the query parameter from the URL.
284283
if (browserSupportsHistoryApi()) {
285284
history.replaceState(null, "", getNakedUrl() + window.location.hash);
286285
}
287286
},
287+
hideResults: () => {
288+
switchDisplayedElement(null);
289+
// We also remove the query parameter from the URL.
290+
searchState.removeQueryParameters();
291+
},
288292
getQueryStringParams: () => {
289293
const params = {};
290294
window.location.search.substring(1).split("&").

src/librustdoc/html/static/js/search.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,20 @@ function initSearch(rawSearchIndex) {
20462046
if (go_to_first || (results.others.length === 1
20472047
&& getSettingValue("go-to-only-result") === "true")
20482048
) {
2049+
// Needed to force re-execution of JS when coming back to a page. Let's take this
2050+
// scenario as example:
2051+
//
2052+
// 1. You have the "Directly go to item in search if there is only one result" option
2053+
// enabled.
2054+
// 2. You make a search which results only one result, leading you automatically to
2055+
// this result.
2056+
// 3. You go back to previous page.
2057+
//
2058+
// Now, without the call below, the JS will not be re-executed and the previous state
2059+
// will be used, starting search again since the search input is not empty, leading you
2060+
// back to the previous page again.
2061+
window.onunload = () => {};
2062+
searchState.removeQueryParameters();
20492063
const elem = document.createElement("a");
20502064
elem.href = results.others[0].href;
20512065
removeClass(elem, "active");
@@ -2182,7 +2196,6 @@ function initSearch(rawSearchIndex) {
21822196
if (e) {
21832197
e.preventDefault();
21842198
}
2185-
21862199
const query = parseQuery(searchState.input.value.trim());
21872200
let filterCrates = getFilterCrates();
21882201

0 commit comments

Comments
 (0)