@@ -253,7 +253,7 @@ function preLoadCss(cssUrl) {
253
253
const out = nonnull ( nonnull ( window . searchState . outputElement ( ) ) . parentElement ) ;
254
254
const hdr = document . createElement ( "div" ) ;
255
255
hdr . className = "main-heading search-results-main-heading" ;
256
- const params = searchState . getQueryStringParams ( ) ;
256
+ const params = window . searchState . getQueryStringParams ( ) ;
257
257
const autofocusParam = params . search === "" ? "autofocus" : "" ;
258
258
hdr . innerHTML = `<nav class="sub">
259
259
<form class="search-form">
@@ -429,6 +429,66 @@ function preLoadCss(cssUrl) {
429
429
} ;
430
430
}
431
431
432
+ // Push and pop states are used to add search results to the browser
433
+ // history.
434
+ if ( browserSupportsHistoryApi ( ) ) {
435
+ // Store the previous <title> so we can revert back to it later.
436
+ const previousTitle = document . title ;
437
+
438
+ window . addEventListener ( "popstate" , e => {
439
+ const params = window . searchState . getQueryStringParams ( ) ;
440
+ // Revert to the previous title manually since the History
441
+ // API ignores the title parameter.
442
+ document . title = previousTitle ;
443
+ // Synchronize search bar with query string state and
444
+ // perform the search. This will empty the bar if there's
445
+ // nothing there, which lets you really go back to a
446
+ // previous state with nothing in the bar.
447
+ const inputElement = window . searchState . inputElement ( ) ;
448
+ if ( params . search !== undefined && inputElement !== null ) {
449
+ loadSearch ( ) ;
450
+ inputElement . value = params . search ;
451
+ // Some browsers fire "onpopstate" for every page load
452
+ // (Chrome), while others fire the event only when actually
453
+ // popping a state (Firefox), which is why search() is
454
+ // called both here and at the end of the startSearch()
455
+ // function.
456
+ e . preventDefault ( ) ;
457
+ window . searchState . showResults ( ) ;
458
+ if ( params . search === "" ) {
459
+ window . searchState . focus ( ) ;
460
+ }
461
+ } else {
462
+ // When browsing back from search results the main page
463
+ // visibility must be reset.
464
+ window . searchState . hideResults ( ) ;
465
+ }
466
+ } ) ;
467
+ }
468
+
469
+ // This is required in firefox to avoid this problem: Navigating to a search result
470
+ // with the keyboard, hitting enter, and then hitting back would take you back to
471
+ // the doc page, rather than the search that should overlay it.
472
+ // This was an interaction between the back-forward cache and our handlers
473
+ // that try to sync state between the URL and the search input. To work around it,
474
+ // do a small amount of re-init on page show.
475
+ window . onpageshow = ( ) => {
476
+ const inputElement = window . searchState . inputElement ( ) ;
477
+ const qSearch = window . searchState . getQueryStringParams ( ) . search ;
478
+ if ( qSearch !== undefined && inputElement !== null ) {
479
+ if ( inputElement . value === "" ) {
480
+ inputElement . value = qSearch ;
481
+ }
482
+ window . searchState . showResults ( ) ;
483
+ if ( qSearch === "" ) {
484
+ loadSearch ( ) ;
485
+ window . searchState . focus ( ) ;
486
+ }
487
+ } else {
488
+ window . searchState . hideResults ( ) ;
489
+ }
490
+ } ;
491
+
432
492
const params = window . searchState . getQueryStringParams ( ) ;
433
493
if ( params . search !== undefined ) {
434
494
window . searchState . setLoadingSearch ( ) ;
@@ -1752,12 +1812,12 @@ function preLoadCss(cssUrl) {
1752
1812
// tablets), it's fixed-position, appears in the left side margin,
1753
1813
// and it can be activated by resizing the sidebar into nothing.
1754
1814
let sidebarButton = document . getElementById ( "sidebar-button" ) ;
1755
- if ( ! sidebarButton ) {
1815
+ const body = document . querySelector ( ".main-heading" ) ;
1816
+ if ( ! sidebarButton && body ) {
1756
1817
sidebarButton = document . createElement ( "div" ) ;
1757
1818
sidebarButton . id = "sidebar-button" ;
1758
1819
const path = `${ window . rootPath } ${ window . currentCrate } /all.html` ;
1759
1820
sidebarButton . innerHTML = `<a href="${ path } " title="show sidebar"></a>` ;
1760
- const body = document . querySelector ( ".main-heading" ) ;
1761
1821
body . insertBefore ( sidebarButton , body . firstChild ) ;
1762
1822
}
1763
1823
if ( sidebarButton ) {
0 commit comments