Skip to content

Commit b93678e

Browse files
committed
rustdoc: Fix search for something on the same page
1 parent a925762 commit b93678e

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/librustdoc/html/layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub fn render<T: fmt::Default, S: fmt::Default>(
6666
</form>
6767
</nav>
6868
69-
<section class=\"content {ty}\">{content}</section>
69+
<section id='main' class=\"content {ty}\">{content}</section>
70+
<section id='search' class=\"content hidden\">{content}</section>
7071
7172
<section class=\"footer\"></section>
7273

src/librustdoc/html/static/main.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@
3939
if (e.keyCode === 188 && $('#help').hasClass('hidden')) { // question mark
4040
e.preventDefault();
4141
$('#help').removeClass('hidden');
42-
} else if (e.keyCode === 27 && !$('#help').hasClass('hidden')) { // esc
43-
e.preventDefault();
44-
$('#help').addClass('hidden');
42+
} else if (e.keyCode === 27) { // esc
43+
if (!$('#help').hasClass('hidden')) {
44+
e.preventDefault();
45+
$('#help').addClass('hidden');
46+
} else if (!$('#search').hasClass('hidden')) {
47+
e.preventDefault();
48+
$('#search').addClass('hidden');
49+
$('#main').removeClass('hidden');
50+
}
4551
} else if (e.keyCode === 83) { // S
4652
e.preventDefault();
4753
$('.search-input').focus();
@@ -202,7 +208,13 @@
202208
var hoverTimeout, $results = $('.search-results .result');
203209

204210
$results.on('click', function () {
205-
document.location.href = $(this).find('a').prop('href');
211+
var dst = $(this).find('a')[0];
212+
console.log(window.location.pathname, dst.pathname);
213+
if (window.location.pathname == dst.pathname) {
214+
$('#search').addClass('hidden');
215+
$('#main').removeClass('hidden');
216+
}
217+
document.location.href = dst.href;
206218
}).on('mouseover', function () {
207219
var $el = $(this);
208220
clearTimeout(hoverTimeout);
@@ -277,7 +289,6 @@
277289
'/index.html" class="' + type +
278290
'">' + name + '</a>';
279291
} else if (item.parent !== undefined) {
280-
console.log(item);
281292
var myparent = allPaths[item.parent];
282293
var anchor = '#' + type + '.' + name;
283294
output += item.path + '::' + myparent.name +
@@ -308,7 +319,8 @@
308319
}
309320

310321
output += "</p>";
311-
$('.content').html(output);
322+
$('#main.content').addClass('hidden');
323+
$('#search.content').removeClass('hidden').html(output);
312324
$('.search-results .desc').width($('.content').width() - 40 -
313325
$('.content td:first-child').first().width());
314326
initSearchNav();

0 commit comments

Comments
 (0)