Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve URLs handling #81379

Merged
merged 2 commits into from
Jan 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ function getThemePickerElement() {
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
function focusSearchBar() {
getSearchInput().focus();
Expand Down Expand Up @@ -255,7 +260,9 @@ function defocusSearchBar() {
hideSearchResults(search);
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
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);
if (elem) {
Expand Down Expand Up @@ -1813,10 +1820,12 @@ function defocusSearchBar() {
// Because searching is incremental by character, only the most
// recent search query is added to the browser history.
if (browserSupportsHistoryApi()) {
var newURL = getNakedUrl() + "?search=" + encodeURIComponent(query.raw) +
window.location.hash;
if (!history.state && !params.search) {
history.pushState(query, "", "?search=" + encodeURIComponent(query.raw));
history.pushState(query, "", newURL);
} else {
history.replaceState(query, "", "?search=" + encodeURIComponent(query.raw));
history.replaceState(query, "", newURL);
}
}

Expand Down Expand Up @@ -1925,7 +1934,8 @@ function defocusSearchBar() {
clearInputTimeout();
if (search_input.value.length === 0) {
if (browserSupportsHistoryApi()) {
history.replaceState("", window.currentCrate + " - Rust", "?search=");
history.replaceState("", window.currentCrate + " - Rust",
getNakedUrl() + window.location.hash);
}
hideSearchResults();
} else {
Expand Down Expand Up @@ -2785,9 +2795,9 @@ function defocusSearchBar() {
if (search_input.value !== "" && hasClass(search, "hidden")) {
showSearchResults(search);
if (browserSupportsHistoryApi()) {
history.replaceState(search_input.value,
"",
"?search=" + encodeURIComponent(search_input.value));
var extra = "?search=" + encodeURIComponent(search_input.value);
history.replaceState(search_input.value, "",
getNakedUrl() + extra + window.location.hash);
}
document.title = searchTitle;
}
Expand Down