Skip to content

Commit f38bc2c

Browse files
committed
Fix librustdoc search events
Previously only keyup event was looked at, which meant that pasting, cutting and otherwise changing the input without typing would not catch any updates to the search query.
1 parent 98841d4 commit f38bc2c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/librustdoc/html/static/main.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -515,23 +515,20 @@
515515
var $active = $results.filter('.highlighted');
516516

517517
if (e.which === 38) { // up
518-
e.preventDefault();
519518
if (!$active.length || !$active.prev()) {
520519
return;
521520
}
522521

523522
$active.prev().addClass('highlighted');
524523
$active.removeClass('highlighted');
525524
} else if (e.which === 40) { // down
526-
e.preventDefault();
527525
if (!$active.length) {
528526
$results.first().addClass('highlighted');
529527
} else if ($active.next().length) {
530528
$active.next().addClass('highlighted');
531529
$active.removeClass('highlighted');
532530
}
533531
} else if (e.which === 13) { // return
534-
e.preventDefault();
535532
if ($active.length) {
536533
document.location.href = $active.find('a').prop('href');
537534
}
@@ -722,20 +719,29 @@
722719
}
723720

724721
function startSearch() {
725-
726-
$(".search-input").on("keyup",function() {
722+
var searchTimeout;
723+
$(".search-input").on("keyup input",function() {
724+
clearTimeout(searchTimeout);
727725
if ($(this).val().length === 0) {
728726
window.history.replaceState("", "std - Rust", "?search=");
729727
$('#main.content').removeClass('hidden');
730728
$('#search.content').addClass('hidden');
729+
} else {
730+
searchTimeout = setTimeout(search, 500);
731731
}
732732
});
733-
734-
var keyUpTimeout;
735-
$('.do-search').on('click', search);
736-
$('.search-input').on('keyup', function() {
737-
clearTimeout(keyUpTimeout);
738-
keyUpTimeout = setTimeout(search, 500);
733+
$('.search-form').on('submit', function(e){
734+
e.preventDefault();
735+
clearTimeout(searchTimeout);
736+
search();
737+
});
738+
$('.search-input').on('change paste', function(e) {
739+
// Do NOT e.preventDefault() here. It will prevent pasting.
740+
clearTimeout(searchTimeout);
741+
// zero-timeout necessary here because at the time of event handler execution the
742+
// pasted content is not in the input field yet. Shouldn’t make any difference for
743+
// change, though.
744+
setTimeout(search, 0);
739745
});
740746

741747
// Push and pop states are used to add search results to the browser

0 commit comments

Comments
 (0)