Skip to content

Commit cf8a9d0

Browse files
committed
search: avoid reacting to an empty search term
When someone currently presses the `/` key to let the search box get the focus, and then immediately presses the Enter key without typing a search term, the site currently redirects to https://git-scm.com/undefined. The same happens when the search term consists of only one character. That's because it is somewhat unexpected that anybody would want to see _all_ search results before seeing _some_ search results. Let's fix that. Incidentally, this also helps with adding a test that verifies that hitting Enter in the search box will never direct the browser to that `/undefined` URL, no matter how quickly that search term and the Enter key are provided: The `runSearch()` function triggers on key-up, which Playwright's `fill()` method (which can be used to fill in a search term) does not emit. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 00dc084 commit cf8a9d0

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

assets/js/application.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ var Search = {
371371
selectResultOption: function() {
372372
var link = $('#search-results a')[Search.selectedIndex];
373373
var url = $(link).attr('href');
374+
if (!url) {
375+
const term = $('#search-text').val();
376+
if (!term) return;
377+
const language = document.querySelector("html")?.getAttribute("lang");
378+
url = `${baseURLPrefix}search/results?search=${term}${language && `&language=${language}`}`;
379+
}
374380
window.location.href = url;
375381
selectedIndex = 0;
376382
},

0 commit comments

Comments
 (0)