Skip to content

Commit 0b1870e

Browse files
authored
Rollup merge of #81379 - GuillaumeGomez:improve-urls, r=Nemo157
Improve URLs handling Fixes #81330. Explanations: before this PR, when emptying the search input, we still had `?search=` in the URL, which wasn't very nice. Now, if the search is empty, we drop the `?search=` part. Also, I realized while working on this PR that when we clicked on a menu link when we were on the search results, the search parameters would look like: `?search=#the-anchor`, which was super weird. Now, it looks like this: `?search=the-search#the-anchor`. Also, I didn't use the `Url` very nice API because it's not available in any IE version (sadness...). cc `````@lzutao````` r? `````@Nemo157`````
2 parents bb6d1d3 + 09518db commit 0b1870e

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/librustdoc/html/static/main.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ function getThemePickerElement() {
9191
return document.getElementById("theme-picker");
9292
}
9393

94+
// Returns the current URL without any query parameter or hash.
95+
function getNakedUrl() {
96+
return window.location.href.split("?")[0].split("#")[0];
97+
}
98+
9499
// Sets the focus on the search bar at the top of the page
95100
function focusSearchBar() {
96101
getSearchInput().focus();
@@ -252,7 +257,9 @@ function defocusSearchBar() {
252257
hideSearchResults(search);
253258
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
254259
if (browserSupportsHistoryApi()) {
255-
history.replaceState(hash, "", "?search=#" + hash);
260+
// `window.location.search`` contains all the query parameters, not just `search`.
261+
history.replaceState(hash, "",
262+
getNakedUrl() + window.location.search + "#" + hash);
256263
}
257264
elem = document.getElementById(hash);
258265
if (elem) {
@@ -1810,10 +1817,12 @@ function defocusSearchBar() {
18101817
// Because searching is incremental by character, only the most
18111818
// recent search query is added to the browser history.
18121819
if (browserSupportsHistoryApi()) {
1820+
var newURL = getNakedUrl() + "?search=" + encodeURIComponent(query.raw) +
1821+
window.location.hash;
18131822
if (!history.state && !params.search) {
1814-
history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
1823+
history.pushState(query, "", newURL);
18151824
} else {
1816-
history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
1825+
history.replaceState(query, "", newURL);
18171826
}
18181827
}
18191828

@@ -1922,7 +1931,8 @@ function defocusSearchBar() {
19221931
clearInputTimeout();
19231932
if (search_input.value.length === 0) {
19241933
if (browserSupportsHistoryApi()) {
1925-
history.replaceState("", window.currentCrate + " - Rust", "?search=");
1934+
history.replaceState("", window.currentCrate + " - Rust",
1935+
getNakedUrl() + window.location.hash);
19261936
}
19271937
hideSearchResults();
19281938
} else {
@@ -2779,9 +2789,9 @@ function defocusSearchBar() {
27792789
if (search_input.value !== "" && hasClass(search, "hidden")) {
27802790
showSearchResults(search);
27812791
if (browserSupportsHistoryApi()) {
2782-
history.replaceState(search_input.value,
2783-
"",
2784-
"?search=" + encodeURIComponent(search_input.value));
2792+
var extra = "?search=" + encodeURIComponent(search_input.value);
2793+
history.replaceState(search_input.value, "",
2794+
getNakedUrl() + extra + window.location.hash);
27852795
}
27862796
document.title = searchTitle;
27872797
}

0 commit comments

Comments
 (0)