From 44ce1fae16778d95c5043c0cdbea7670491e9344 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 2 Jan 2019 00:59:18 +0100 Subject: [PATCH] doxygen: hide the search by going back only when it makes sense. If we landed directly on an URL with #search, going back would mean the browser is redirected back to the referrer page, the Speed Dial or whatever. Do not want. In this case we simply replace the history state without the #search hash -- there definitely wasn't any other hash we could return to. --- doxygen/search.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/doxygen/search.js b/doxygen/search.js index 05bda57f..330351a7 100644 --- a/doxygen/search.js +++ b/doxygen/search.js @@ -40,6 +40,10 @@ var Search = { only after mouse moves */ mouseMovedSinceLastRender: false, + /* Whether we can go back in history in order to hide the search box or + not. We can't do that if we arrived directly on #search from outside. */ + canGoBackToHideSearch: false, + init: function(buffer, maxResults) { let view = new DataView(buffer); @@ -512,6 +516,7 @@ function updateForSearchVisible() { /* istanbul ignore next */ function showSearch() { window.location.hash = '#search'; + Search.canGoBackToHideSearch = true; updateForSearchVisible(); return false; @@ -519,8 +524,17 @@ function showSearch() { /* istanbul ignore next */ function hideSearch() { - /* Go back to the previous state (that removes the #search hash) */ - window.history.back(); + /* If the search box was opened using showSearch(), we can go back in the + history. Otherwise (for example when we landed to #search from a + bookmark or another server), going back would not do the right thing and + in that case we simply replace the current history state. */ + if(Search.canGoBackToHideSearch) { + Search.canGoBackToHideSearch = false; + window.history.back(); + } else { + window.location.hash = '#!'; + window.history.replaceState('', '', window.location.pathname); + } /* Restore scrollbar, prevent page layout jumps */ document.body.style.overflow = 'auto'; -- 2.30.2