From a75863887774b52e2d121ea80abf42763aa4e9ae Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sun, 18 Apr 2021 11:20:05 +0800 Subject: [PATCH 1/2] Automatically jump to the first result when entering directly in the search form --- src/librustdoc/html/static/search.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/librustdoc/html/static/search.js b/src/librustdoc/html/static/search.js index 538c811c71097..55a9a981ca539 100644 --- a/src/librustdoc/html/static/search.js +++ b/src/librustdoc/html/static/search.js @@ -969,6 +969,15 @@ window.initSearch = function(rawSearchIndex) { if (actives[currentTab].length) { document.location.href = actives[currentTab][0].getElementsByTagName("a")[0].href; + } else { + var results = document.getElementById("results").childNodes; + if (results.length > 0) { + var res = results[currentTab].getElementsByClassName("result"); + if (res.length > 0) { + document.location.href = + res[0].getElementsByTagName("a")[0].href; + } + } } } else if (e.which === 16) { // shift // Does nothing, it's just to avoid losing "focus" on the highlighted element. From 4f7b8cb1099260e36d137b424bf527587ccb0bf2 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sun, 18 Apr 2021 12:12:01 +0800 Subject: [PATCH 2/2] Always use first res --- src/librustdoc/html/static/search.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/search.js b/src/librustdoc/html/static/search.js index 55a9a981ca539..af6c09ef0e879 100644 --- a/src/librustdoc/html/static/search.js +++ b/src/librustdoc/html/static/search.js @@ -929,7 +929,8 @@ window.initSearch = function(rawSearchIndex) { var actives = [[], [], []]; // "current" is used to know which tab we're looking into. var current = 0; - onEachLazy(document.getElementById("results").childNodes, function(e) { + var results = document.getElementById("results").childNodes; + onEachLazy(results, function(e) { onEachLazy(e.getElementsByClassName("highlighted"), function(h_e) { actives[current].push(h_e); }); @@ -953,7 +954,6 @@ window.initSearch = function(rawSearchIndex) { if (e.ctrlKey) { // Going through result tabs. printTab(currentTab > 1 ? 0 : currentTab + 1); } else if (!actives[currentTab].length) { - var results = document.getElementById("results").childNodes; if (results.length > 0) { var res = results[currentTab].getElementsByClassName("result"); if (res.length > 0) { @@ -970,12 +970,11 @@ window.initSearch = function(rawSearchIndex) { document.location.href = actives[currentTab][0].getElementsByTagName("a")[0].href; } else { - var results = document.getElementById("results").childNodes; if (results.length > 0) { - var res = results[currentTab].getElementsByClassName("result"); - if (res.length > 0) { + var firstRes = results[0].getElementsByClassName("result"); + if (firstRes.length > 0) { document.location.href = - res[0].getElementsByTagName("a")[0].href; + firstRes[0].getElementsByTagName("a")[0].href; } } }