From c4023c6ca31f80cb7fa3cbebc9f0095d5f649d53 Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume1.gomez@gmail.com>
Date: Tue, 22 Jun 2021 12:06:09 +0200
Subject: [PATCH] Fix search filter update

---
 src/librustdoc/html/static/search.js    |  4 ++++
 src/test/rustdoc-gui/search-filter.goml | 17 +++++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 src/test/rustdoc-gui/search-filter.goml

diff --git a/src/librustdoc/html/static/search.js b/src/librustdoc/html/static/search.js
index 35be246b5bf..617c79a4579 100644
--- a/src/librustdoc/html/static/search.js
+++ b/src/librustdoc/html/static/search.js
@@ -1442,6 +1442,10 @@ window.initSearch = function(rawSearchIndex) {
         if (selectCrate) {
             selectCrate.onchange = function() {
                 updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
+                // In case you "cut" the entry from the search input, then change the crate filter
+                // before paste back the previous search, you get the old search results without
+                // the filter. To prevent this, we need to remove the previous results.
+                currentResults = null;
                 search(undefined, true);
             };
         }
diff --git a/src/test/rustdoc-gui/search-filter.goml b/src/test/rustdoc-gui/search-filter.goml
new file mode 100644
index 00000000000..a098dbd9f12
--- /dev/null
+++ b/src/test/rustdoc-gui/search-filter.goml
@@ -0,0 +1,17 @@
+goto: file://|DOC_PATH|/test_docs/index.html
+write: (".search-input", "test")
+// Waiting for the search results to appear...
+wait-for: "#titles"
+assert-text: ("#results .externcrate", "test_docs")
+text: (".search-input", "")
+// We now want to change the crate filter.
+click: "#crate-search"
+// We select "lib2" option then press enter to change the filter.
+press-key: "ArrowDown"
+press-key: "Enter"
+// We now make the search again.
+write: (".search-input", "test")
+// Waiting for the search results to appear...
+wait-for: "#titles"
+// We check that there is no more "test_docs" appearing.
+assert-false: "#results .externcrate"