Skip to content

Commit 09518db

Browse files
Improve URL handling when clicking on a menu link while being on the search results and overall
1 parent ef3127d commit 09518db

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/librustdoc/html/static/main.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ function getThemePickerElement() {
9494
return document.getElementById("theme-picker");
9595
}
9696

97+
// Returns the current URL without any query parameter or hash.
98+
function getNakedUrl() {
99+
return window.location.href.split("?")[0].split("#")[0];
100+
}
101+
97102
// Sets the focus on the search bar at the top of the page
98103
function focusSearchBar() {
99104
getSearchInput().focus();
@@ -255,7 +260,9 @@ function defocusSearchBar() {
255260
hideSearchResults(search);
256261
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
257262
if (browserSupportsHistoryApi()) {
258-
history.replaceState(hash, "", "?search=#" + hash);
263+
// `window.location.search`` contains all the query parameters, not just `search`.
264+
history.replaceState(hash, "",
265+
getNakedUrl() + window.location.search + "#" + hash);
259266
}
260267
elem = document.getElementById(hash);
261268
if (elem) {
@@ -1813,10 +1820,12 @@ function defocusSearchBar() {
18131820
// Because searching is incremental by character, only the most
18141821
// recent search query is added to the browser history.
18151822
if (browserSupportsHistoryApi()) {
1823+
var newURL = getNakedUrl() + "?search=" + encodeURIComponent(query.raw) +
1824+
window.location.hash;
18161825
if (!history.state && !params.search) {
1817-
history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
1826+
history.pushState(query, "", newURL);
18181827
} else {
1819-
history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
1828+
history.replaceState(query, "", newURL);
18201829
}
18211830
}
18221831

@@ -1926,7 +1935,7 @@ function defocusSearchBar() {
19261935
if (search_input.value.length === 0) {
19271936
if (browserSupportsHistoryApi()) {
19281937
history.replaceState("", window.currentCrate + " - Rust",
1929-
window.location.href.split("?")[0]);
1938+
getNakedUrl() + window.location.hash);
19301939
}
19311940
hideSearchResults();
19321941
} else {
@@ -2786,9 +2795,9 @@ function defocusSearchBar() {
27862795
if (search_input.value !== "" && hasClass(search, "hidden")) {
27872796
showSearchResults(search);
27882797
if (browserSupportsHistoryApi()) {
2789-
history.replaceState(search_input.value,
2790-
"",
2791-
"?search=" + encodeURIComponent(search_input.value));
2798+
var extra = "?search=" + encodeURIComponent(search_input.value);
2799+
history.replaceState(search_input.value, "",
2800+
getNakedUrl() + extra + window.location.hash);
27922801
}
27932802
document.title = searchTitle;
27942803
}

0 commit comments

Comments
 (0)