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`````
This commit is contained in:
commit
0b1870e7b9
@ -91,6 +91,11 @@ function getThemePickerElement() {
|
|||||||
return document.getElementById("theme-picker");
|
return document.getElementById("theme-picker");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the current URL without any query parameter or hash.
|
||||||
|
function getNakedUrl() {
|
||||||
|
return window.location.href.split("?")[0].split("#")[0];
|
||||||
|
}
|
||||||
|
|
||||||
// Sets the focus on the search bar at the top of the page
|
// Sets the focus on the search bar at the top of the page
|
||||||
function focusSearchBar() {
|
function focusSearchBar() {
|
||||||
getSearchInput().focus();
|
getSearchInput().focus();
|
||||||
@ -252,7 +257,9 @@ function defocusSearchBar() {
|
|||||||
hideSearchResults(search);
|
hideSearchResults(search);
|
||||||
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
|
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
|
||||||
if (browserSupportsHistoryApi()) {
|
if (browserSupportsHistoryApi()) {
|
||||||
history.replaceState(hash, "", "?search=#" + hash);
|
// `window.location.search`` contains all the query parameters, not just `search`.
|
||||||
|
history.replaceState(hash, "",
|
||||||
|
getNakedUrl() + window.location.search + "#" + hash);
|
||||||
}
|
}
|
||||||
elem = document.getElementById(hash);
|
elem = document.getElementById(hash);
|
||||||
if (elem) {
|
if (elem) {
|
||||||
@ -1810,10 +1817,12 @@ function defocusSearchBar() {
|
|||||||
// Because searching is incremental by character, only the most
|
// Because searching is incremental by character, only the most
|
||||||
// recent search query is added to the browser history.
|
// recent search query is added to the browser history.
|
||||||
if (browserSupportsHistoryApi()) {
|
if (browserSupportsHistoryApi()) {
|
||||||
|
var newURL = getNakedUrl() + "?search=" + encodeURIComponent(query.raw) +
|
||||||
|
window.location.hash;
|
||||||
if (!history.state && !params.search) {
|
if (!history.state && !params.search) {
|
||||||
history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
|
history.pushState(query, "", newURL);
|
||||||
} else {
|
} else {
|
||||||
history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
|
history.replaceState(query, "", newURL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1922,7 +1931,8 @@ function defocusSearchBar() {
|
|||||||
clearInputTimeout();
|
clearInputTimeout();
|
||||||
if (search_input.value.length === 0) {
|
if (search_input.value.length === 0) {
|
||||||
if (browserSupportsHistoryApi()) {
|
if (browserSupportsHistoryApi()) {
|
||||||
history.replaceState("", window.currentCrate + " - Rust", "?search=");
|
history.replaceState("", window.currentCrate + " - Rust",
|
||||||
|
getNakedUrl() + window.location.hash);
|
||||||
}
|
}
|
||||||
hideSearchResults();
|
hideSearchResults();
|
||||||
} else {
|
} else {
|
||||||
@ -2779,9 +2789,9 @@ function defocusSearchBar() {
|
|||||||
if (search_input.value !== "" && hasClass(search, "hidden")) {
|
if (search_input.value !== "" && hasClass(search, "hidden")) {
|
||||||
showSearchResults(search);
|
showSearchResults(search);
|
||||||
if (browserSupportsHistoryApi()) {
|
if (browserSupportsHistoryApi()) {
|
||||||
history.replaceState(search_input.value,
|
var extra = "?search=" + encodeURIComponent(search_input.value);
|
||||||
"",
|
history.replaceState(search_input.value, "",
|
||||||
"?search=" + encodeURIComponent(search_input.value));
|
getNakedUrl() + extra + window.location.hash);
|
||||||
}
|
}
|
||||||
document.title = searchTitle;
|
document.title = searchTitle;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user