1
Fork 0

Rollup merge of #67527 - GuillaumeGomez:results-show-too-much, r=kinnison

Results show too much

Fixes #67461.

To reproduce the current issue: search anything, then once the results appears, press escape. They should disappear then re-appear right away. This is because blurring an element triggers the "change" event.

r? @kinnison
This commit is contained in:
Mazdak Farrokhzad 2019-12-23 15:16:26 +01:00 committed by GitHub
commit 6f38a15c7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -138,6 +138,22 @@ function getSearchElement() {
} }
} }
function showSearchResults(search) {
if (search === null || typeof search === 'undefined') {
search = getSearchElement();
}
addClass(main, "hidden");
removeClass(search, "hidden");
}
function hideSearchResults(search) {
if (search === null || typeof search === 'undefined') {
search = getSearchElement();
}
addClass(search, "hidden");
removeClass(main, "hidden");
}
// used for special search precedence // used for special search precedence
var TY_PRIMITIVE = itemTypes.indexOf("primitive"); var TY_PRIMITIVE = itemTypes.indexOf("primitive");
var TY_KEYWORD = itemTypes.indexOf("keyword"); var TY_KEYWORD = itemTypes.indexOf("keyword");
@ -169,8 +185,7 @@ function getSearchElement() {
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) { if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
// This block occurs when clicking on an element in the navbar while // This block occurs when clicking on an element in the navbar while
// in a search. // in a search.
addClass(search, "hidden"); hideSearchResults(search);
removeClass(main, "hidden");
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1); var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
if (browserSupportsHistoryApi()) { if (browserSupportsHistoryApi()) {
history.replaceState(hash, "", "?search=#" + hash); history.replaceState(hash, "", "?search=#" + hash);
@ -331,8 +346,7 @@ function getSearchElement() {
displayHelp(false, ev, help); displayHelp(false, ev, help);
} else if (hasClass(search, "hidden") === false) { } else if (hasClass(search, "hidden") === false) {
ev.preventDefault(); ev.preventDefault();
addClass(search, "hidden"); hideSearchResults(search);
removeClass(main, "hidden");
document.title = titleBeforeSearch; document.title = titleBeforeSearch;
} }
defocusSearchBar(); defocusSearchBar();
@ -390,8 +404,8 @@ function getSearchElement() {
return null; return null;
} }
document.onkeypress = handleShortcut; document.addEventListener("keypress", handleShortcut);
document.onkeydown = handleShortcut; document.addEventListener("keydown", handleShortcut);
var handleSourceHighlight = (function() { var handleSourceHighlight = (function() {
var prev_line_id = 0; var prev_line_id = 0;
@ -430,7 +444,7 @@ function getSearchElement() {
} }
})(); })();
document.onclick = function(ev) { document.addEventListener("click", function(ev) {
if (hasClass(ev.target, "collapse-toggle")) { if (hasClass(ev.target, "collapse-toggle")) {
collapseDocs(ev.target, "toggle"); collapseDocs(ev.target, "toggle");
} else if (hasClass(ev.target.parentNode, "collapse-toggle")) { } else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
@ -452,7 +466,7 @@ function getSearchElement() {
expandSection(a.hash.replace(/^#/, "")); expandSection(a.hash.replace(/^#/, ""));
} }
} }
}; });
var x = document.getElementsByClassName("version-selector"); var x = document.getElementsByClassName("version-selector");
if (x.length > 0) { if (x.length > 0) {
@ -1264,8 +1278,7 @@ function getSearchElement() {
} }
dst = dst[0]; dst = dst[0];
if (window.location.pathname === dst.pathname) { if (window.location.pathname === dst.pathname) {
addClass(getSearchElement(), "hidden"); hideSearchResults();
removeClass(main, "hidden");
document.location.href = dst.href; document.location.href = dst.href;
} }
}; };
@ -1340,8 +1353,6 @@ function getSearchElement() {
e.preventDefault(); e.preventDefault();
} else if (e.which === 16) { // shift } else if (e.which === 16) { // shift
// Does nothing, it's just to avoid losing "focus" on the highlighted element. // Does nothing, it's just to avoid losing "focus" on the highlighted element.
} else if (e.which === 27) { // escape
handleEscape(e);
} else if (actives[currentTab].length > 0) { } else if (actives[currentTab].length > 0) {
removeClass(actives[currentTab][0], "highlighted"); removeClass(actives[currentTab][0], "highlighted");
} }
@ -1491,10 +1502,9 @@ function getSearchElement() {
"</div><div id=\"results\">" + "</div><div id=\"results\">" +
ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>"; ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
addClass(main, "hidden");
var search = getSearchElement(); var search = getSearchElement();
removeClass(search, "hidden");
search.innerHTML = output; search.innerHTML = output;
showSearchResults(search);
var tds = search.getElementsByTagName("td"); var tds = search.getElementsByTagName("td");
var td_width = 0; var td_width = 0;
if (tds.length > 0) { if (tds.length > 0) {
@ -1699,13 +1709,7 @@ function getSearchElement() {
if (browserSupportsHistoryApi()) { if (browserSupportsHistoryApi()) {
history.replaceState("", window.currentCrate + " - Rust", "?search="); history.replaceState("", window.currentCrate + " - Rust", "?search=");
} }
if (hasClass(main, "content")) { hideSearchResults();
removeClass(main, "hidden");
}
var search_c = getSearchElement();
if (hasClass(search_c, "content")) {
addClass(search_c, "hidden");
}
} else { } else {
searchTimeout = setTimeout(search, 500); searchTimeout = setTimeout(search, 500);
} }
@ -1718,6 +1722,10 @@ function getSearchElement() {
search(); search();
}; };
search_input.onchange = function(e) { search_input.onchange = function(e) {
if (e.target !== document.activeElement) {
// To prevent doing anything when it's from a blur event.
return;
}
// Do NOT e.preventDefault() here. It will prevent pasting. // Do NOT e.preventDefault() here. It will prevent pasting.
clearTimeout(searchTimeout); clearTimeout(searchTimeout);
// zero-timeout necessary here because at the time of event handler execution the // zero-timeout necessary here because at the time of event handler execution the
@ -1741,19 +1749,8 @@ function getSearchElement() {
// Store the previous <title> so we can revert back to it later. // Store the previous <title> so we can revert back to it later.
var previousTitle = document.title; var previousTitle = document.title;
window.onpopstate = function(e) { window.addEventListener("popstate", function(e) {
var params = getQueryStringParams(); var params = getQueryStringParams();
// When browsing back from search results the main page
// visibility must be reset.
if (!params.search) {
if (hasClass(main, "content")) {
removeClass(main, "hidden");
}
var search_c = getSearchElement();
if (hasClass(search_c, "content")) {
addClass(search_c, "hidden");
}
}
// Revert to the previous title manually since the History // Revert to the previous title manually since the History
// API ignores the title parameter. // API ignores the title parameter.
document.title = previousTitle; document.title = previousTitle;
@ -1765,18 +1762,21 @@ function getSearchElement() {
// perform the search. This will empty the bar if there's // perform the search. This will empty the bar if there's
// nothing there, which lets you really go back to a // nothing there, which lets you really go back to a
// previous state with nothing in the bar. // previous state with nothing in the bar.
if (params.search) { if (params.search && params.search.length > 0) {
search_input.value = params.search; search_input.value = params.search;
// Some browsers fire "onpopstate" for every page load
// (Chrome), while others fire the event only when actually
// popping a state (Firefox), which is why search() is
// called both here and at the end of the startSearch()
// function.
search(e);
} else { } else {
search_input.value = ""; search_input.value = "";
// When browsing back from search results the main page
// visibility must be reset.
hideSearchResults();
} }
// Some browsers fire "onpopstate" for every page load });
// (Chrome), while others fire the event only when actually
// popping a state (Firefox), which is why search() is
// called both here and at the end of the startSearch()
// function.
search();
};
} }
search(); search();
} }
@ -2522,9 +2522,9 @@ function getSearchElement() {
} }
function putBackSearch(search_input) { function putBackSearch(search_input) {
if (search_input.value !== "") { var search = getSearchElement();
addClass(main, "hidden"); if (search_input.value !== "" && hasClass(search, "hidden")) {
removeClass(getSearchElement(), "hidden"); showSearchResults(search);
if (browserSupportsHistoryApi()) { if (browserSupportsHistoryApi()) {
history.replaceState(search_input.value, history.replaceState(search_input.value,
"", "",
@ -2541,10 +2541,9 @@ function getSearchElement() {
var params = getQueryStringParams(); var params = getQueryStringParams();
if (params && params.search) { if (params && params.search) {
addClass(main, "hidden");
var search = getSearchElement(); var search = getSearchElement();
removeClass(search, "hidden");
search.innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>"; search.innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>";
showSearchResults(search);
} }
var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0]; var sidebar_menu = document.getElementsByClassName("sidebar-menu")[0];