Skip to content

Commit f2721fa

Browse files
authored
Rollup merge of #92735 - GuillaumeGomez:crate-filter-url-param, r=jsha
Add crate filter parameter in URL Fixes #92621. r? `@jsha`
2 parents 4e8fb74 + 829a047 commit f2721fa

File tree

7 files changed

+140
-58
lines changed

7 files changed

+140
-58
lines changed

src/librustdoc/html/static/js/main.js

+3-20
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ function resourcePath(basename, extension) {
5454
return getVar("root-path") + basename + getVar("resource-suffix") + extension;
5555
}
5656

57-
5857
(function () {
5958
window.rootPath = getVar("root-path");
6059
window.currentCrate = getVar("current-crate");
@@ -232,7 +231,7 @@ function hideThemeButtonState() {
232231
document.title = searchState.titleBeforeSearch;
233232
// We also remove the query parameter from the URL.
234233
if (searchState.browserSupportsHistoryApi()) {
235-
history.replaceState("", window.currentCrate + " - Rust",
234+
history.replaceState(null, window.currentCrate + " - Rust",
236235
getNakedUrl() + window.location.hash);
237236
}
238237
},
@@ -246,18 +245,6 @@ function hideThemeButtonState() {
246245
});
247246
return params;
248247
},
249-
putBackSearch: function(search_input) {
250-
var search = searchState.outputElement();
251-
if (search_input.value !== "" && hasClass(search, "hidden")) {
252-
searchState.showResults(search);
253-
if (searchState.browserSupportsHistoryApi()) {
254-
var extra = "?search=" + encodeURIComponent(search_input.value);
255-
history.replaceState(search_input.value, "",
256-
getNakedUrl() + extra + window.location.hash);
257-
}
258-
document.title = searchState.title;
259-
}
260-
},
261248
browserSupportsHistoryApi: function() {
262249
return window.history && typeof window.history.pushState === "function";
263250
},
@@ -282,14 +269,10 @@ function hideThemeButtonState() {
282269
}
283270

284271
search_input.addEventListener("focus", function() {
285-
searchState.putBackSearch(this);
286-
search_input.origPlaceholder = searchState.input.placeholder;
272+
search_input.origPlaceholder = search_input.placeholder;
287273
search_input.placeholder = "Type your search here.";
288274
loadSearch();
289275
});
290-
search_input.addEventListener("blur", function() {
291-
search_input.placeholder = searchState.input.origPlaceholder;
292-
});
293276

294277
if (search_input.value != '') {
295278
loadSearch();
@@ -330,7 +313,7 @@ function hideThemeButtonState() {
330313
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
331314
if (searchState.browserSupportsHistoryApi()) {
332315
// `window.location.search`` contains all the query parameters, not just `search`.
333-
history.replaceState(hash, "",
316+
history.replaceState(null, "",
334317
getNakedUrl() + window.location.search + "#" + hash);
335318
}
336319
elem = document.getElementById(hash);

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

+94-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* global addClass, getNakedUrl, getSettingValue, hasOwnPropertyRustdoc, initSearch, onEach */
2-
/* global onEachLazy, removeClass, searchState, updateLocalStorage */
2+
/* global onEachLazy, removeClass, searchState, hasClass */
33

44
(function() {
55
// This mapping table should match the discriminants of
@@ -133,6 +133,39 @@ window.initSearch = function(rawSearchIndex) {
133133
searchState.input.value = params.search || "";
134134
}
135135

136+
/**
137+
* Build an URL with search parameters.
138+
*
139+
* @param {string} search - The current search being performed.
140+
* @param {string|null} filterCrates - The current filtering crate (if any).
141+
* @return {string}
142+
*/
143+
function buildUrl(search, filterCrates) {
144+
var extra = "?search=" + encodeURIComponent(search);
145+
146+
if (filterCrates !== null) {
147+
extra += "&filter-crate=" + encodeURIComponent(filterCrates);
148+
}
149+
return getNakedUrl() + extra + window.location.hash;
150+
}
151+
152+
/**
153+
* Return the filtering crate or `null` if there is none.
154+
*
155+
* @return {string|null}
156+
*/
157+
function getFilterCrates() {
158+
var elem = document.getElementById("crate-search");
159+
160+
if (elem &&
161+
elem.value !== "All crates" &&
162+
hasOwnPropertyRustdoc(rawSearchIndex, elem.value))
163+
{
164+
return elem.value;
165+
}
166+
return null;
167+
}
168+
136169
/**
137170
* Executes the query and returns a list of results for each results tab.
138171
* @param {Object} query - The user query
@@ -595,7 +628,7 @@ window.initSearch = function(rawSearchIndex) {
595628
// aliases to be before the others in the displayed results.
596629
var aliases = [];
597630
var crateAliases = [];
598-
if (filterCrates !== undefined) {
631+
if (filterCrates !== null) {
599632
if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) {
600633
var query_aliases = ALIASES[filterCrates][query.search];
601634
var len = query_aliases.length;
@@ -694,7 +727,7 @@ window.initSearch = function(rawSearchIndex) {
694727
{
695728
val = extractGenerics(val.substr(1, val.length - 2));
696729
for (i = 0; i < nSearchWords; ++i) {
697-
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
730+
if (filterCrates !== null && searchIndex[i].crate !== filterCrates) {
698731
continue;
699732
}
700733
in_args = findArg(searchIndex[i], val, true, typeFilter);
@@ -725,7 +758,7 @@ window.initSearch = function(rawSearchIndex) {
725758
var output = extractGenerics(parts[1]);
726759

727760
for (i = 0; i < nSearchWords; ++i) {
728-
if (filterCrates !== undefined && searchIndex[i].crate !== filterCrates) {
761+
if (filterCrates !== null && searchIndex[i].crate !== filterCrates) {
729762
continue;
730763
}
731764
var type = searchIndex[i].type;
@@ -781,7 +814,7 @@ window.initSearch = function(rawSearchIndex) {
781814
var lev, j;
782815
for (j = 0; j < nSearchWords; ++j) {
783816
ty = searchIndex[j];
784-
if (!ty || (filterCrates !== undefined && ty.crate !== filterCrates)) {
817+
if (!ty || (filterCrates !== null && ty.crate !== filterCrates)) {
785818
continue;
786819
}
787820
var lev_add = 0;
@@ -1279,17 +1312,6 @@ window.initSearch = function(rawSearchIndex) {
12791312
};
12801313
}
12811314

1282-
function getFilterCrates() {
1283-
var elem = document.getElementById("crate-search");
1284-
1285-
if (elem && elem.value !== "All crates" &&
1286-
hasOwnPropertyRustdoc(rawSearchIndex, elem.value))
1287-
{
1288-
return elem.value;
1289-
}
1290-
return undefined;
1291-
}
1292-
12931315
/**
12941316
* Perform a search based on the current state of the search input element
12951317
* and display the results.
@@ -1309,27 +1331,34 @@ window.initSearch = function(rawSearchIndex) {
13091331
}
13101332
if (!forced && query.id === currentResults) {
13111333
if (query.query.length > 0) {
1312-
searchState.putBackSearch(searchState.input);
1334+
putBackSearch();
13131335
}
13141336
return;
13151337
}
13161338

1339+
var filterCrates = getFilterCrates();
1340+
1341+
// In case we have no information about the saved crate and there is a URL query parameter,
1342+
// we override it with the URL query parameter.
1343+
if (filterCrates === null && params["filter-crate"] !== undefined) {
1344+
filterCrates = params["filter-crate"];
1345+
}
1346+
13171347
// Update document title to maintain a meaningful browser history
13181348
searchState.title = "Results for " + query.query + " - Rust";
13191349

13201350
// Because searching is incremental by character, only the most
13211351
// recent search query is added to the browser history.
13221352
if (searchState.browserSupportsHistoryApi()) {
1323-
var newURL = getNakedUrl() + "?search=" + encodeURIComponent(query.raw) +
1324-
window.location.hash;
1353+
var newURL = buildUrl(query.raw, filterCrates);
1354+
13251355
if (!history.state && !params.search) {
1326-
history.pushState(query, "", newURL);
1356+
history.pushState(null, "", newURL);
13271357
} else {
1328-
history.replaceState(query, "", newURL);
1358+
history.replaceState(null, "", newURL);
13291359
}
13301360
}
13311361

1332-
var filterCrates = getFilterCrates();
13331362
showResults(execSearch(query, searchWords, filterCrates),
13341363
params["go_to_first"], filterCrates);
13351364
}
@@ -1495,12 +1524,28 @@ window.initSearch = function(rawSearchIndex) {
14951524
search();
14961525
}
14971526

1527+
function putBackSearch() {
1528+
var search_input = searchState.input;
1529+
if (!searchState.input) {
1530+
return;
1531+
}
1532+
var search = searchState.outputElement();
1533+
if (search_input.value !== "" && hasClass(search, "hidden")) {
1534+
searchState.showResults(search);
1535+
if (searchState.browserSupportsHistoryApi()) {
1536+
history.replaceState(null, "",
1537+
buildUrl(search_input.value, getFilterCrates()));
1538+
}
1539+
document.title = searchState.title;
1540+
}
1541+
}
1542+
14981543
function registerSearchEvents() {
14991544
var searchAfter500ms = function() {
15001545
searchState.clearInputTimeout();
15011546
if (searchState.input.value.length === 0) {
15021547
if (searchState.browserSupportsHistoryApi()) {
1503-
history.replaceState("", window.currentCrate + " - Rust",
1548+
history.replaceState(null, window.currentCrate + " - Rust",
15041549
getNakedUrl() + window.location.hash);
15051550
}
15061551
searchState.hideResults();
@@ -1567,6 +1612,14 @@ window.initSearch = function(rawSearchIndex) {
15671612
}
15681613
});
15691614

1615+
searchState.input.addEventListener("focus", function() {
1616+
putBackSearch();
1617+
});
1618+
1619+
searchState.input.addEventListener("blur", function() {
1620+
searchState.input.placeholder = searchState.input.origPlaceholder;
1621+
});
1622+
15701623
// Push and pop states are used to add search results to the browser
15711624
// history.
15721625
if (searchState.browserSupportsHistoryApi()) {
@@ -1619,7 +1672,16 @@ window.initSearch = function(rawSearchIndex) {
16191672
}
16201673

16211674
function updateCrate(ev) {
1622-
updateLocalStorage("rustdoc-saved-filter-crate", ev.target.value);
1675+
if (ev.target.value === "All crates") {
1676+
// If we don't remove it from the URL, it'll be picked up again by the search.
1677+
var params = searchState.getQueryStringParams();
1678+
var query = searchState.input.value.trim();
1679+
if (!history.state && !params.search) {
1680+
history.pushState(null, "", buildUrl(query, null));
1681+
} else {
1682+
history.replaceState(null, "", buildUrl(query, null));
1683+
}
1684+
}
16231685
// In case you "cut" the entry from the search input, then change the crate filter
16241686
// before paste back the previous search, you get the old search results without
16251687
// the filter. To prevent this, we need to remove the previous results.
@@ -1629,10 +1691,15 @@ window.initSearch = function(rawSearchIndex) {
16291691

16301692
searchWords = buildIndex(rawSearchIndex);
16311693
registerSearchEvents();
1632-
// If there's a search term in the URL, execute the search now.
1633-
if (searchState.getQueryStringParams().search) {
1634-
search();
1694+
1695+
function runSearchIfNeeded() {
1696+
// If there's a search term in the URL, execute the search now.
1697+
if (searchState.getQueryStringParams().search) {
1698+
search();
1699+
}
16351700
}
1701+
1702+
runSearchIfNeeded();
16361703
};
16371704

16381705
if (window.searchIndex !== undefined) {

src/librustdoc/html/static/js/settings.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
(function () {
66
function changeSetting(settingName, value) {
7-
updateLocalStorage("rustdoc-" + settingName, value);
7+
updateLocalStorage(settingName, value);
88

99
switch (settingName) {
1010
case "theme":

src/librustdoc/html/static/js/source-script.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ function toggleSidebar() {
8282
if (child.innerText === ">") {
8383
sidebar.classList.add("expanded");
8484
child.innerText = "<";
85-
updateLocalStorage("rustdoc-source-sidebar-show", "true");
85+
updateLocalStorage("source-sidebar-show", "true");
8686
} else {
8787
sidebar.classList.remove("expanded");
8888
child.innerText = ">";
89-
updateLocalStorage("rustdoc-source-sidebar-show", "false");
89+
updateLocalStorage("source-sidebar-show", "false");
9090
}
9191
}
9292

@@ -97,7 +97,7 @@ function createSidebarToggle() {
9797

9898
var inner = document.createElement("div");
9999

100-
if (getCurrentValue("rustdoc-source-sidebar-show") === "true") {
100+
if (getCurrentValue("source-sidebar-show") === "true") {
101101
inner.innerText = "<";
102102
} else {
103103
inner.innerText = ">";
@@ -120,7 +120,7 @@ function createSourceSidebar() {
120120

121121
var sidebar = document.createElement("div");
122122
sidebar.id = "source-sidebar";
123-
if (getCurrentValue("rustdoc-source-sidebar-show") !== "true") {
123+
if (getCurrentValue("source-sidebar-show") !== "true") {
124124
container.classList.remove("expanded");
125125
} else {
126126
container.classList.add("expanded");

src/librustdoc/html/static/js/storage.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var settingsDataset = (function () {
1515
})();
1616

1717
function getSettingValue(settingName) {
18-
var current = getCurrentValue('rustdoc-' + settingName);
18+
var current = getCurrentValue(settingName);
1919
if (current !== null) {
2020
return current;
2121
}
@@ -106,15 +106,15 @@ function hasOwnPropertyRustdoc(obj, property) {
106106

107107
function updateLocalStorage(name, value) {
108108
try {
109-
window.localStorage.setItem(name, value);
109+
window.localStorage.setItem("rustdoc-" + name, value);
110110
} catch(e) {
111111
// localStorage is not accessible, do nothing
112112
}
113113
}
114114

115115
function getCurrentValue(name) {
116116
try {
117-
return window.localStorage.getItem(name);
117+
return window.localStorage.getItem("rustdoc-" + name);
118118
} catch(e) {
119119
return null;
120120
}
@@ -127,7 +127,7 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
127127
// If this new value comes from a system setting or from the previously
128128
// saved theme, no need to save it.
129129
if (saveTheme) {
130-
updateLocalStorage("rustdoc-theme", newTheme);
130+
updateLocalStorage("theme", newTheme);
131131
}
132132

133133
if (styleElem.href === newHref) {
@@ -158,7 +158,7 @@ function useSystemTheme(value) {
158158
value = true;
159159
}
160160

161-
updateLocalStorage("rustdoc-use-system-theme", value);
161+
updateLocalStorage("use-system-theme", value);
162162

163163
// update the toggle if we're on the settings page
164164
var toggle = document.getElementById("use-system-theme");
@@ -231,7 +231,7 @@ if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
231231
if (getSettingValue("use-system-theme") === null
232232
&& getSettingValue("preferred-dark-theme") === null
233233
&& darkThemes.indexOf(localStoredTheme) >= 0) {
234-
updateLocalStorage("rustdoc-preferred-dark-theme", localStoredTheme);
234+
updateLocalStorage("preferred-dark-theme", localStoredTheme);
235235
}
236236

237237
// call the function to initialize the theme at least once!

0 commit comments

Comments
 (0)