Skip to content

Commit 9f50942

Browse files
Unify history interactions in search
1 parent 0966f32 commit 9f50942

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/librustdoc/html/static/js/search.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,18 @@ function initSearch(rawSearchIndex) {
21602160
printTab(currentTab);
21612161
}
21622162

2163+
function updateSearchHistory(url) {
2164+
if (!browserSupportsHistoryApi()) {
2165+
return;
2166+
}
2167+
const params = searchState.getQueryStringParams();
2168+
if (!history.state && !params.search) {
2169+
history.pushState(null, "", url);
2170+
} else {
2171+
history.replaceState(null, "", url);
2172+
}
2173+
}
2174+
21632175
/**
21642176
* Perform a search based on the current state of the search input element
21652177
* and display the results.
@@ -2196,15 +2208,7 @@ function initSearch(rawSearchIndex) {
21962208

21972209
// Because searching is incremental by character, only the most
21982210
// recent search query is added to the browser history.
2199-
if (browserSupportsHistoryApi()) {
2200-
const newURL = buildUrl(query.original, filterCrates);
2201-
2202-
if (!history.state && !params.search) {
2203-
history.pushState(null, "", newURL);
2204-
} else {
2205-
history.replaceState(null, "", newURL);
2206-
}
2207-
}
2211+
updateSearchHistory(buildUrl(query.original, filterCrates));
22082212

22092213
showResults(
22102214
execQuery(query, searchWords, filterCrates, window.currentCrate),
@@ -2670,13 +2674,8 @@ function initSearch(rawSearchIndex) {
26702674
function updateCrate(ev) {
26712675
if (ev.target.value === "all crates") {
26722676
// If we don't remove it from the URL, it'll be picked up again by the search.
2673-
const params = searchState.getQueryStringParams();
26742677
const query = searchState.input.value.trim();
2675-
if (!history.state && !params.search) {
2676-
history.pushState(null, "", buildUrl(query, null));
2677-
} else {
2678-
history.replaceState(null, "", buildUrl(query, null));
2679-
}
2678+
updateSearchHistory(buildUrl(query, null));
26802679
}
26812680
// In case you "cut" the entry from the search input, then change the crate filter
26822681
// before paste back the previous search, you get the old search results without

0 commit comments

Comments
 (0)