Skip to content

Commit 8c805fa

Browse files
committed
auto merge of #11760 : dmac/rust/addressable-search, r=alexcrichton
This change adds two improvements to docs searching functionality. First, search results will immediately be displayed when a ?search=searchterm query string parameter is provided to any docs url. Second, search results are now inserted into the browser history, allowing for easier navigation between search results and docs pages.
2 parents 1ea0947 + b869f36 commit 8c805fa

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

src/librustdoc/html/static/main.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@
1717

1818
$('.js-only').removeClass('js-only');
1919

20+
function getQueryStringParams() {
21+
var params = {};
22+
window.location.search.substring(1).split("&").
23+
map(function(s) {
24+
var pair = s.split("=");
25+
params[decodeURIComponent(pair[0])] =
26+
typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]);
27+
});
28+
return params;
29+
}
30+
31+
function browserSupportsHistoryApi() {
32+
return window.history && typeof window.history.pushState === "function";
33+
}
34+
2035
function resizeShortBlocks() {
2136
if (resizeTimeout) {
2237
clearTimeout(resizeTimeout);
@@ -97,10 +112,10 @@
97112
});
98113

99114
function initSearch(searchIndex) {
100-
var currentResults, index;
115+
var currentResults, index, params = getQueryStringParams();
101116

102-
// clear cached values from the search bar
103-
$(".search-input")[0].value = '';
117+
// Populate search bar with query string search term when provided.
118+
$(".search-input")[0].value = params.search || '';
104119

105120
/**
106121
* Executes the query and builds an index of results
@@ -418,6 +433,7 @@
418433
results = [],
419434
maxResults = 200,
420435
resultIndex;
436+
var params = getQueryStringParams();
421437

422438
query = getQuery();
423439
if (e) {
@@ -428,6 +444,16 @@
428444
return;
429445
}
430446

447+
// Because searching is incremental by character, only the most recent search query
448+
// is added to the browser history.
449+
if (browserSupportsHistoryApi()) {
450+
if (!history.state && !params.search) {
451+
history.pushState(query, "", "?search=" + encodeURIComponent(query.query));
452+
} else {
453+
history.replaceState(query, "", "?search=" + encodeURIComponent(query.query));
454+
}
455+
}
456+
431457
resultIndex = execQuery(query, 20000, index);
432458
len = resultIndex.length;
433459
for (i = 0; i < len; i += 1) {
@@ -536,6 +562,27 @@
536562
clearTimeout(keyUpTimeout);
537563
keyUpTimeout = setTimeout(search, 100);
538564
});
565+
// Push and pop states are used to add search results to the browser history.
566+
if (browserSupportsHistoryApi()) {
567+
$(window).on('popstate', function(e) {
568+
var params = getQueryStringParams();
569+
// When browsing back from search results the main page visibility must be reset.
570+
if (!params.search) {
571+
$('#main.content').removeClass('hidden');
572+
$('#search.content').addClass('hidden');
573+
}
574+
// When browsing forward to search results the previous search will be repeated,
575+
// so the currentResults are cleared to ensure the search is successful.
576+
currentResults = null;
577+
// Synchronize search bar with query string state and perform the search.
578+
$('.search-input').val(params.search);
579+
// Some browsers fire 'onpopstate' for every page load (Chrome), while others fire the
580+
// event only when actually popping a state (Firefox), which is why search() is called
581+
// both here and at the end of the startSearch() function.
582+
search();
583+
});
584+
}
585+
search();
539586
}
540587

541588
index = buildIndex(searchIndex);

0 commit comments

Comments
 (0)