@@ -138,6 +138,22 @@ function getSearchElement() {
138138 }
139139 }
140140
141+ function showSearchResults ( search ) {
142+ if ( search === null || typeof search === 'undefined' ) {
143+ search = getSearchElement ( ) ;
144+ }
145+ addClass ( main , "hidden" ) ;
146+ removeClass ( search , "hidden" ) ;
147+ }
148+
149+ function hideSearchResults ( search ) {
150+ if ( search === null || typeof search === 'undefined' ) {
151+ search = getSearchElement ( ) ;
152+ }
153+ addClass ( search , "hidden" ) ;
154+ removeClass ( main , "hidden" ) ;
155+ }
156+
141157 // used for special search precedence
142158 var TY_PRIMITIVE = itemTypes . indexOf ( "primitive" ) ;
143159 var TY_KEYWORD = itemTypes . indexOf ( "keyword" ) ;
@@ -169,8 +185,7 @@ function getSearchElement() {
169185 if ( ev !== null && search && ! hasClass ( search , "hidden" ) && ev . newURL ) {
170186 // This block occurs when clicking on an element in the navbar while
171187 // in a search.
172- addClass ( search , "hidden" ) ;
173- removeClass ( main , "hidden" ) ;
188+ hideSearchResults ( search ) ;
174189 var hash = ev . newURL . slice ( ev . newURL . indexOf ( "#" ) + 1 ) ;
175190 if ( browserSupportsHistoryApi ( ) ) {
176191 history . replaceState ( hash , "" , "?search=#" + hash ) ;
@@ -331,8 +346,7 @@ function getSearchElement() {
331346 displayHelp ( false , ev , help ) ;
332347 } else if ( hasClass ( search , "hidden" ) === false ) {
333348 ev . preventDefault ( ) ;
334- addClass ( search , "hidden" ) ;
335- removeClass ( main , "hidden" ) ;
349+ hideSearchResults ( search ) ;
336350 document . title = titleBeforeSearch ;
337351 }
338352 defocusSearchBar ( ) ;
@@ -390,8 +404,8 @@ function getSearchElement() {
390404 return null ;
391405 }
392406
393- document . onkeypress = handleShortcut ;
394- document . onkeydown = handleShortcut ;
407+ document . addEventListener ( "keypress" , handleShortcut ) ;
408+ document . addEventListener ( "keydown" , handleShortcut ) ;
395409
396410 var handleSourceHighlight = ( function ( ) {
397411 var prev_line_id = 0 ;
@@ -430,7 +444,7 @@ function getSearchElement() {
430444 }
431445 } ) ( ) ;
432446
433- document . onclick = function ( ev ) {
447+ document . addEventListener ( "click" , function ( ev ) {
434448 if ( hasClass ( ev . target , "collapse-toggle" ) ) {
435449 collapseDocs ( ev . target , "toggle" ) ;
436450 } else if ( hasClass ( ev . target . parentNode , "collapse-toggle" ) ) {
@@ -452,7 +466,7 @@ function getSearchElement() {
452466 expandSection ( a . hash . replace ( / ^ # / , "" ) ) ;
453467 }
454468 }
455- } ;
469+ } ) ;
456470
457471 var x = document . getElementsByClassName ( "version-selector" ) ;
458472 if ( x . length > 0 ) {
@@ -1264,8 +1278,7 @@ function getSearchElement() {
12641278 }
12651279 dst = dst [ 0 ] ;
12661280 if ( window . location . pathname === dst . pathname ) {
1267- addClass ( getSearchElement ( ) , "hidden" ) ;
1268- removeClass ( main , "hidden" ) ;
1281+ hideSearchResults ( ) ;
12691282 document . location . href = dst . href ;
12701283 }
12711284 } ;
@@ -1340,8 +1353,6 @@ function getSearchElement() {
13401353 e . preventDefault ( ) ;
13411354 } else if ( e . which === 16 ) { // shift
13421355 // Does nothing, it's just to avoid losing "focus" on the highlighted element.
1343- } else if ( e . which === 27 ) { // escape
1344- handleEscape ( e ) ;
13451356 } else if ( actives [ currentTab ] . length > 0 ) {
13461357 removeClass ( actives [ currentTab ] [ 0 ] , "highlighted" ) ;
13471358 }
@@ -1491,10 +1502,9 @@ function getSearchElement() {
14911502 "</div><div id=\"results\">" +
14921503 ret_others [ 0 ] + ret_in_args [ 0 ] + ret_returned [ 0 ] + "</div>" ;
14931504
1494- addClass ( main , "hidden" ) ;
14951505 var search = getSearchElement ( ) ;
1496- removeClass ( search , "hidden" ) ;
14971506 search . innerHTML = output ;
1507+ showSearchResults ( search ) ;
14981508 var tds = search . getElementsByTagName ( "td" ) ;
14991509 var td_width = 0 ;
15001510 if ( tds . length > 0 ) {
@@ -1699,13 +1709,7 @@ function getSearchElement() {
16991709 if ( browserSupportsHistoryApi ( ) ) {
17001710 history . replaceState ( "" , window . currentCrate + " - Rust" , "?search=" ) ;
17011711 }
1702- if ( hasClass ( main , "content" ) ) {
1703- removeClass ( main , "hidden" ) ;
1704- }
1705- var search_c = getSearchElement ( ) ;
1706- if ( hasClass ( search_c , "content" ) ) {
1707- addClass ( search_c , "hidden" ) ;
1708- }
1712+ hideSearchResults ( ) ;
17091713 } else {
17101714 searchTimeout = setTimeout ( search , 500 ) ;
17111715 }
@@ -1718,6 +1722,10 @@ function getSearchElement() {
17181722 search ( ) ;
17191723 } ;
17201724 search_input . onchange = function ( e ) {
1725+ if ( e . target !== document . activeElement ) {
1726+ // To prevent doing anything when it's from a blur event.
1727+ return ;
1728+ }
17211729 // Do NOT e.preventDefault() here. It will prevent pasting.
17221730 clearTimeout ( searchTimeout ) ;
17231731 // zero-timeout necessary here because at the time of event handler execution the
@@ -1741,19 +1749,8 @@ function getSearchElement() {
17411749 // Store the previous <title> so we can revert back to it later.
17421750 var previousTitle = document . title ;
17431751
1744- window . onpopstate = function ( e ) {
1752+ window . addEventListener ( "popstate" , function ( e ) {
17451753 var params = getQueryStringParams ( ) ;
1746- // When browsing back from search results the main page
1747- // visibility must be reset.
1748- if ( ! params . search ) {
1749- if ( hasClass ( main , "content" ) ) {
1750- removeClass ( main , "hidden" ) ;
1751- }
1752- var search_c = getSearchElement ( ) ;
1753- if ( hasClass ( search_c , "content" ) ) {
1754- addClass ( search_c , "hidden" ) ;
1755- }
1756- }
17571754 // Revert to the previous title manually since the History
17581755 // API ignores the title parameter.
17591756 document . title = previousTitle ;
@@ -1765,18 +1762,21 @@ function getSearchElement() {
17651762 // perform the search. This will empty the bar if there's
17661763 // nothing there, which lets you really go back to a
17671764 // previous state with nothing in the bar.
1768- if ( params . search ) {
1765+ if ( params . search && params . search . length > 0 ) {
17691766 search_input . value = params . search ;
1767+ // Some browsers fire "onpopstate" for every page load
1768+ // (Chrome), while others fire the event only when actually
1769+ // popping a state (Firefox), which is why search() is
1770+ // called both here and at the end of the startSearch()
1771+ // function.
1772+ search ( e ) ;
17701773 } else {
17711774 search_input . value = "" ;
1775+ // When browsing back from search results the main page
1776+ // visibility must be reset.
1777+ hideSearchResults ( ) ;
17721778 }
1773- // Some browsers fire "onpopstate" for every page load
1774- // (Chrome), while others fire the event only when actually
1775- // popping a state (Firefox), which is why search() is
1776- // called both here and at the end of the startSearch()
1777- // function.
1778- search ( ) ;
1779- } ;
1779+ } ) ;
17801780 }
17811781 search ( ) ;
17821782 }
@@ -2522,9 +2522,9 @@ function getSearchElement() {
25222522 }
25232523
25242524 function putBackSearch ( search_input ) {
2525- if ( search_input . value !== "" ) {
2526- addClass ( main , "hidden" ) ;
2527- removeClass ( getSearchElement ( ) , "hidden" ) ;
2525+ var search = getSearchElement ( ) ;
2526+ if ( search_input . value !== "" && hasClass ( search , "hidden" ) ) {
2527+ showSearchResults ( search ) ;
25282528 if ( browserSupportsHistoryApi ( ) ) {
25292529 history . replaceState ( search_input . value ,
25302530 "" ,
@@ -2541,10 +2541,9 @@ function getSearchElement() {
25412541
25422542 var params = getQueryStringParams ( ) ;
25432543 if ( params && params . search ) {
2544- addClass ( main , "hidden" ) ;
25452544 var search = getSearchElement ( ) ;
2546- removeClass ( search , "hidden" ) ;
25472545 search . innerHTML = "<h3 style=\"text-align: center;\">Loading search results...</h3>" ;
2546+ showSearchResults ( search ) ;
25482547 }
25492548
25502549 var sidebar_menu = document . getElementsByClassName ( "sidebar-menu" ) [ 0 ] ;
0 commit comments