Skip to content

Commit 63799ba

Browse files
authored
Rollup merge of #112707 - GuillaumeGomez:back-in-history-fix, r=notriddle
[rustdoc] Fix invalid handling of "going back in history" when "go to only search result" setting is enabled You can test the fix [here](https://rustdoc.crud.net/imperio/back-in-history-fix/lib2/index.html). Enable "Directly go to item in search if there is only one result", then search for `HasALongTraitWithParams` and finally go back to previous page. It should be back on the `index.html` page. The reason for this bug is that the JS state is cached as is, so when we go back to the page, it resumes where it was left, somewhat (very weird), meaning the search is run again etc. The best way to handle this is to force the JS re-execution in this case so that it doesn't try to resume from where it left and then lead us back to the current page. r? ``@notriddle``
2 parents 7051c84 + ea55d25 commit 63799ba

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

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

+7-3
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

+28-16
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,20 @@ function initSearch(rawSearchIndex) {
20712071
if (go_to_first || (results.others.length === 1
20722072
&& getSettingValue("go-to-only-result") === "true")
20732073
) {
2074+
// Needed to force re-execution of JS when coming back to a page. Let's take this
2075+
// scenario as example:
2076+
//
2077+
// 1. You have the "Directly go to item in search if there is only one result" option
2078+
// enabled.
2079+
// 2. You make a search which results only one result, leading you automatically to
2080+
// this result.
2081+
// 3. You go back to previous page.
2082+
//
2083+
// Now, without the call below, the JS will not be re-executed and the previous state
2084+
// will be used, starting search again since the search input is not empty, leading you
2085+
// back to the previous page again.
2086+
window.onunload = () => {};
2087+
searchState.removeQueryParameters();
20742088
const elem = document.createElement("a");
20752089
elem.href = results.others[0].href;
20762090
removeClass(elem, "active");
@@ -2185,6 +2199,18 @@ function initSearch(rawSearchIndex) {
21852199
printTab(currentTab);
21862200
}
21872201

2202+
function updateSearchHistory(url) {
2203+
if (!browserSupportsHistoryApi()) {
2204+
return;
2205+
}
2206+
const params = searchState.getQueryStringParams();
2207+
if (!history.state && !params.search) {
2208+
history.pushState(null, "", url);
2209+
} else {
2210+
history.replaceState(null, "", url);
2211+
}
2212+
}
2213+
21882214
/**
21892215
* Perform a search based on the current state of the search input element
21902216
* and display the results.
@@ -2195,7 +2221,6 @@ function initSearch(rawSearchIndex) {
21952221
if (e) {
21962222
e.preventDefault();
21972223
}
2198-
21992224
const query = parseQuery(searchState.input.value.trim());
22002225
let filterCrates = getFilterCrates();
22012226

@@ -2221,15 +2246,7 @@ function initSearch(rawSearchIndex) {
22212246

22222247
// Because searching is incremental by character, only the most
22232248
// recent search query is added to the browser history.
2224-
if (browserSupportsHistoryApi()) {
2225-
const newURL = buildUrl(query.original, filterCrates);
2226-
2227-
if (!history.state && !params.search) {
2228-
history.pushState(null, "", newURL);
2229-
} else {
2230-
history.replaceState(null, "", newURL);
2231-
}
2232-
}
2249+
updateSearchHistory(buildUrl(query.original, filterCrates));
22332250

22342251
showResults(
22352252
execQuery(query, searchWords, filterCrates, window.currentCrate),
@@ -2695,13 +2712,8 @@ function initSearch(rawSearchIndex) {
26952712
function updateCrate(ev) {
26962713
if (ev.target.value === "all crates") {
26972714
// If we don't remove it from the URL, it'll be picked up again by the search.
2698-
const params = searchState.getQueryStringParams();
26992715
const query = searchState.input.value.trim();
2700-
if (!history.state && !params.search) {
2701-
history.pushState(null, "", buildUrl(query, null));
2702-
} else {
2703-
history.replaceState(null, "", buildUrl(query, null));
2704-
}
2716+
updateSearchHistory(buildUrl(query, null));
27052717
}
27062718
// In case you "cut" the entry from the search input, then change the crate filter
27072719
// before paste back the previous search, you get the old search results without

tests/rustdoc-gui/setting-go-to-only-result.goml

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ go-to: "file://" + |DOC_PATH| + "/lib2/index.html"
3434
// We enter it into the search.
3535
write: (".search-input", "HasALongTraitWithParams")
3636
wait-for-document-property: {"title": "HasALongTraitWithParams in lib2 - Rust"}
37-
assert-document-property: ({"URL": "/lib2/struct.HasALongTraitWithParams.html"}, ENDS_WITH)
37+
assert-window-property: ({"location": "/lib2/struct.HasALongTraitWithParams.html"}, ENDS_WITH)
38+
39+
// Regression test for <https://github.com/rust-lang/rust/issues/112676>.
40+
// If "go-to-only-result" is enabled and you go back to history, it should not lead you back to the
41+
// page result again automatically.
42+
history-go-back:
43+
wait-for-document-property: {"title": "lib2 - Rust"}
44+
assert-window-property: ({"location": "/lib2/index.html"}, ENDS_WITH)
3845

3946
// We try again to see if it goes to the only result
4047
go-to: "file://" + |DOC_PATH| + "/lib2/index.html?search=HasALongTraitWithParams"

0 commit comments

Comments
 (0)