From ef3127dcdfedb6b3b113150f6c84171bd98ab360 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 25 Jan 2021 12:54:25 +0100 Subject: [PATCH 1/2] Completely remove search query parameter when clearing search input --- src/librustdoc/html/static/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 74bd348e9ac..5606a1d5170 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1925,7 +1925,8 @@ function defocusSearchBar() { clearInputTimeout(); if (search_input.value.length === 0) { if (browserSupportsHistoryApi()) { - history.replaceState("", window.currentCrate + " - Rust", "?search="); + history.replaceState("", window.currentCrate + " - Rust", + window.location.href.split("?")[0]); } hideSearchResults(); } else { From 09518db73e84112922af76f161b803eb90ceb445 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 25 Jan 2021 13:21:12 +0100 Subject: [PATCH 2/2] Improve URL handling when clicking on a menu link while being on the search results and overall --- src/librustdoc/html/static/main.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 5606a1d5170..51c62c28507 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -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(); @@ -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) { @@ -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); } } @@ -1926,7 +1935,7 @@ function defocusSearchBar() { if (search_input.value.length === 0) { if (browserSupportsHistoryApi()) { history.replaceState("", window.currentCrate + " - Rust", - window.location.href.split("?")[0]); + getNakedUrl() + window.location.hash); } hideSearchResults(); } else { @@ -2786,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; }