Skip to content

Commit 3b074f4

Browse files
committed
rustdoc js: searchState is referenced as a global, not through window
1 parent 2aa33ba commit 3b074f4

File tree

2 files changed

+18
-41
lines changed

2 files changed

+18
-41
lines changed

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
declare global {
77
/** Map from crate name to directory structure, for source view */
88
declare var srcIndex: Map<string, rustdoc.Dir>;
9+
/** Search engine data used by main.js and search.js */
10+
declare var searchState: rustdoc.SearchState;
911
/** Defined and documented in `storage.js` */
1012
declare function nonnull(x: T|null, msg: string|undefined);
1113
/** Defined and documented in `storage.js` */
@@ -19,8 +21,6 @@ declare global {
1921
RUSTDOC_TOOLTIP_HOVER_MS: number;
2022
/** Used by the popover tooltip code. */
2123
RUSTDOC_TOOLTIP_HOVER_EXIT_MS: number;
22-
/** Search engine data used by main.js and search.js */
23-
searchState: rustdoc.SearchState;
2424
/** Global option, with a long list of "../"'s */
2525
rootPath: string|null;
2626
/**
@@ -104,20 +104,22 @@ declare namespace rustdoc {
104104
currentTab: number;
105105
focusedByTab: [number|null, number|null, number|null];
106106
clearInputTimeout: function;
107-
outputElement: function(): HTMLElement|null;
108-
focus: function();
109-
defocus: function();
110-
showResults: function(HTMLElement|null|undefined);
111-
removeQueryParameters: function();
112-
hideResults: function();
113-
getQueryStringParams: function(): Object.<any, string>;
107+
outputElement(): HTMLElement|null;
108+
focus();
109+
defocus();
110+
// note: an optional param is not the same as
111+
// a nullable/undef-able param.
112+
showResults(elem?: HTMLElement|null);
113+
removeQueryParameters();
114+
hideResults();
115+
getQueryStringParams(): Object.<any, string>;
114116
origPlaceholder: string;
115117
setup: function();
116-
setLoadingSearch: function();
118+
setLoadingSearch();
117119
descShards: Map<string, SearchDescShard[]>;
118120
loadDesc: function({descShard: SearchDescShard, descIndex: number}): Promise<string|null>;
119-
loadedDescShard: function(string, number, string);
120-
isDisplayed: function(): boolean,
121+
loadedDescShard(string, number, string);
122+
isDisplayed(): boolean,
121123
}
122124

123125
interface SearchDescShard {

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

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4712,7 +4712,6 @@ function printTab(nb) {
47124712
iter += 1;
47134713
});
47144714
if (foundCurrentTab && foundCurrentResultSet) {
4715-
// @ts-expect-error
47164715
searchState.currentTab = nb;
47174716
// Corrections only kick in on type-based searches.
47184717
const correctionsElem = document.getElementsByClassName("search-corrections");
@@ -4765,7 +4764,6 @@ function getFilterCrates() {
47654764

47664765
// @ts-expect-error
47674766
function nextTab(direction) {
4768-
// @ts-expect-error
47694767
const next = (searchState.currentTab + direction + 3) % searchState.focusedByTab.length;
47704768
// @ts-expect-error
47714769
searchState.focusedByTab[searchState.currentTab] = document.activeElement;
@@ -4776,14 +4774,12 @@ function nextTab(direction) {
47764774
// Focus the first search result on the active tab, or the result that
47774775
// was focused last time this tab was active.
47784776
function focusSearchResult() {
4779-
// @ts-expect-error
47804777
const target = searchState.focusedByTab[searchState.currentTab] ||
47814778
document.querySelectorAll(".search-results.active a").item(0) ||
4782-
// @ts-expect-error
47834779
document.querySelectorAll("#search-tabs button").item(searchState.currentTab);
4784-
// @ts-expect-error
47854780
searchState.focusedByTab[searchState.currentTab] = null;
47864781
if (target) {
4782+
// @ts-expect-error
47874783
target.focus();
47884784
}
47894785
}
@@ -4935,7 +4931,6 @@ function makeTabHeader(tabNb, text, nbElems) {
49354931
const fmtNbElems =
49364932
nbElems < 10 ? `\u{2007}(${nbElems})\u{2007}\u{2007}` :
49374933
nbElems < 100 ? `\u{2007}(${nbElems})\u{2007}` : `\u{2007}(${nbElems})`;
4938-
// @ts-expect-error
49394934
if (searchState.currentTab === tabNb) {
49404935
return "<button class=\"selected\">" + text +
49414936
"<span class=\"count\">" + fmtNbElems + "</span></button>";
@@ -4949,7 +4944,6 @@ function makeTabHeader(tabNb, text, nbElems) {
49494944
* @param {string} filterCrates
49504945
*/
49514946
async function showResults(results, go_to_first, filterCrates) {
4952-
// @ts-expect-error
49534947
const search = searchState.outputElement();
49544948
if (go_to_first || (results.others.length === 1
49554949
&& getSettingValue("go-to-only-result") === "true")
@@ -4967,7 +4961,6 @@ async function showResults(results, go_to_first, filterCrates) {
49674961
// will be used, starting search again since the search input is not empty, leading you
49684962
// back to the previous page again.
49694963
window.onunload = () => { };
4970-
// @ts-expect-error
49714964
searchState.removeQueryParameters();
49724965
const elem = document.createElement("a");
49734966
elem.href = results.others[0].href;
@@ -4987,7 +4980,6 @@ async function showResults(results, go_to_first, filterCrates) {
49874980
// Navigate to the relevant tab if the current tab is empty, like in case users search
49884981
// for "-> String". If they had selected another tab previously, they have to click on
49894982
// it again.
4990-
// @ts-expect-error
49914983
let currentTab = searchState.currentTab;
49924984
if ((currentTab === 0 && results.others.length === 0) ||
49934985
(currentTab === 1 && results.in_args.length === 0) ||
@@ -5075,8 +5067,8 @@ async function showResults(results, go_to_first, filterCrates) {
50755067
resultsElem.appendChild(ret_in_args);
50765068
resultsElem.appendChild(ret_returned);
50775069

5078-
search.innerHTML = output;
50795070
// @ts-expect-error
5071+
search.innerHTML = output;
50805072
if (searchState.rustdocToolbar) {
50815073
// @ts-expect-error
50825074
search.querySelector(".main-heading").appendChild(searchState.rustdocToolbar);
@@ -5085,9 +5077,9 @@ async function showResults(results, go_to_first, filterCrates) {
50855077
if (crateSearch) {
50865078
crateSearch.addEventListener("input", updateCrate);
50875079
}
5080+
// @ts-expect-error
50885081
search.appendChild(resultsElem);
50895082
// Reset focused elements.
5090-
// @ts-expect-error
50915083
searchState.showResults(search);
50925084
// @ts-expect-error
50935085
const elems = document.getElementById("search-tabs").childNodes;
@@ -5098,7 +5090,6 @@ async function showResults(results, go_to_first, filterCrates) {
50985090
const j = i;
50995091
// @ts-expect-error
51005092
elem.onclick = () => printTab(j);
5101-
// @ts-expect-error
51025093
searchState.focusedByTab.push(null);
51035094
i += 1;
51045095
}
@@ -5110,7 +5101,6 @@ function updateSearchHistory(url) {
51105101
if (!browserSupportsHistoryApi()) {
51115102
return;
51125103
}
5113-
// @ts-expect-error
51145104
const params = searchState.getQueryStringParams();
51155105
if (!history.state && !params.search) {
51165106
history.pushState(null, "", url);
@@ -5137,10 +5127,8 @@ async function search(forced) {
51375127
return;
51385128
}
51395129

5140-
// @ts-expect-error
51415130
searchState.setLoadingSearch();
51425131

5143-
// @ts-expect-error
51445132
const params = searchState.getQueryStringParams();
51455133

51465134
// In case we have no information about the saved crate and there is a URL query parameter,
@@ -5150,7 +5138,6 @@ async function search(forced) {
51505138
}
51515139

51525140
// Update document title to maintain a meaningful browser history
5153-
// @ts-expect-error
51545141
searchState.title = "\"" + query.userQuery + "\" Search - Rust";
51555142

51565143
// Because searching is incremental by character, only the most
@@ -5172,33 +5159,28 @@ async function search(forced) {
51725159
function onSearchSubmit(e) {
51735160
// @ts-expect-error
51745161
e.preventDefault();
5175-
// @ts-expect-error
51765162
searchState.clearInputTimeout();
51775163
search();
51785164
}
51795165

51805166
function putBackSearch() {
5181-
// @ts-expect-error
51825167
const search_input = searchState.input;
5183-
// @ts-expect-error
51845168
if (!searchState.input) {
51855169
return;
51865170
}
51875171
// @ts-expect-error
51885172
if (search_input.value !== "" && !searchState.isDisplayed()) {
5189-
// @ts-expect-error
51905173
searchState.showResults();
51915174
if (browserSupportsHistoryApi()) {
51925175
history.replaceState(null, "",
5176+
// @ts-expect-error
51935177
buildUrl(search_input.value, getFilterCrates()));
51945178
}
5195-
// @ts-expect-error
51965179
document.title = searchState.title;
51975180
}
51985181
}
51995182

52005183
function registerSearchEvents() {
5201-
// @ts-expect-error
52025184
const params = searchState.getQueryStringParams();
52035185

52045186
// Populate search bar with query string search term when provided,
@@ -5212,11 +5194,9 @@ function registerSearchEvents() {
52125194
}
52135195

52145196
const searchAfter500ms = () => {
5215-
// @ts-expect-error
52165197
searchState.clearInputTimeout();
52175198
// @ts-expect-error
52185199
if (searchState.input.value.length === 0) {
5219-
// @ts-expect-error
52205200
searchState.hideResults();
52215201
} else {
52225202
// @ts-expect-error
@@ -5236,7 +5216,6 @@ function registerSearchEvents() {
52365216
return;
52375217
}
52385218
// Do NOT e.preventDefault() here. It will prevent pasting.
5239-
// @ts-expect-error
52405219
searchState.clearInputTimeout();
52415220
// zero-timeout necessary here because at the time of event handler execution the
52425221
// pasted content is not in the input field yet. Shouldn’t make any difference for
@@ -5262,7 +5241,6 @@ function registerSearchEvents() {
52625241
// @ts-expect-error
52635242
previous.focus();
52645243
} else {
5265-
// @ts-expect-error
52665244
searchState.focus();
52675245
}
52685246
e.preventDefault();
@@ -5315,7 +5293,6 @@ function registerSearchEvents() {
53155293
const previousTitle = document.title;
53165294

53175295
window.addEventListener("popstate", e => {
5318-
// @ts-expect-error
53195296
const params = searchState.getQueryStringParams();
53205297
// Revert to the previous title manually since the History
53215298
// API ignores the title parameter.
@@ -5343,7 +5320,6 @@ function registerSearchEvents() {
53435320
searchState.input.value = "";
53445321
// When browsing back from search results the main page
53455322
// visibility must be reset.
5346-
// @ts-expect-error
53475323
searchState.hideResults();
53485324
}
53495325
});
@@ -5356,7 +5332,6 @@ function registerSearchEvents() {
53565332
// that try to sync state between the URL and the search input. To work around it,
53575333
// do a small amount of re-init on page show.
53585334
window.onpageshow = () => {
5359-
// @ts-expect-error
53605335
const qSearch = searchState.getQueryStringParams().search;
53615336
// @ts-expect-error
53625337
if (searchState.input.value === "" && qSearch) {

0 commit comments

Comments
 (0)