Skip to content

Commit 3dc9deb

Browse files
committed
Make doc search results use <a> tags instead of js for navigating
This has the primary advantage of not interfering with browser default behavior for links like being able to cmd/ctrl+click on a result to open the result in a new tab but leave the current page as-is (previous behavior both opened a new tab and changed the current tab's location to the result's)
1 parent 08176a3 commit 3dc9deb

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/librustdoc/html/static/main.css

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,10 @@ nav.sub {
218218
}
219219

220220
.content .highlighted {
221-
cursor: pointer;
222221
color: #000 !important;
223222
background-color: #ccc;
224223
}
225-
.content .highlighted a { color: #000 !important; }
224+
.content .highlighted a, .content .highlighted span { color: #000 !important; }
226225
.content .highlighted.trait { background-color: #fece7e; }
227226
.content .highlighted.mod { background-color: #afc6e4; }
228227
.content .highlighted.enum { background-color: #b4d1b9; }
@@ -335,11 +334,11 @@ a {
335334
p a { color: #4e8bca; }
336335
p a:hover { text-decoration: underline; }
337336

338-
.content a.trait, .block a.current.trait { color: #ed9603; }
339-
.content a.mod, .block a.current.mod { color: #4d76ae; }
340-
.content a.enum, .block a.current.enum { color: #5e9766; }
341-
.content a.struct, .block a.current.struct { color: #e53700; }
342-
.content a.fn, .block a.current.fn { color: #8c6067; }
337+
.content span.trait, .block a.current.trait { color: #ed9603; }
338+
.content span.mod, .block a.current.mod { color: #4d76ae; }
339+
.content span.enum, .block a.current.enum { color: #5e9766; }
340+
.content span.struct, .block a.current.struct { color: #e53700; }
341+
.content span.fn, .block a.current.fn { color: #8c6067; }
343342
.content .fnname { color: #8c6067; }
344343

345344
.search-input {
@@ -377,6 +376,13 @@ p a:hover { text-decoration: underline; }
377376
display: block;
378377
}
379378

379+
.search-results a {
380+
display: block;
381+
}
382+
383+
.content .search-results td:first-child { padding-right: 0; }
384+
.content .search-results td:first-child a { padding-right: 10px; }
385+
380386
#help {
381387
background: #e9e9e9;
382388
border-radius: 4px;

src/librustdoc/html/static/main.js

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@
395395
if (window.location.pathname == dst.pathname) {
396396
$('#search').addClass('hidden');
397397
$('#main').removeClass('hidden');
398+
document.location.href = dst.href;
398399
}
399-
document.location.href = dst.href;
400400
}).on('mouseover', function() {
401401
var $el = $(this);
402402
clearTimeout(hoverTimeout);
@@ -451,7 +451,7 @@
451451
shown = [];
452452

453453
results.forEach(function(item) {
454-
var name, type;
454+
var name, type, href, displayPath;
455455

456456
if (shown.indexOf(item) !== -1) {
457457
return;
@@ -461,43 +461,35 @@
461461
name = item.name;
462462
type = itemTypes[item.ty];
463463

464-
output += '<tr class="' + type + ' result"><td>';
465-
466464
if (type === 'mod') {
467-
output += item.path +
468-
'::<a href="' + rootPath +
469-
item.path.replace(/::/g, '/') + '/' +
470-
name + '/index.html" class="' +
471-
type + '">' + name + '</a>';
465+
displayPath = item.path + '::';
466+
href = rootPath + item.path.replace(/::/g, '/') + '/' +
467+
name + '/index.html';
472468
} else if (type === 'static' || type === 'reexport') {
473-
output += item.path +
474-
'::<a href="' + rootPath +
475-
item.path.replace(/::/g, '/') +
476-
'/index.html" class="' + type +
477-
'">' + name + '</a>';
469+
displayPath = item.path + '::';
470+
href = rootPath + item.path.replace(/::/g, '/') +
471+
'/index.html';
478472
} else if (item.parent !== undefined) {
479473
var myparent = item.parent;
480474
var anchor = '#' + type + '.' + name;
481-
output += item.path + '::' + myparent.name +
482-
'::<a href="' + rootPath +
483-
item.path.replace(/::/g, '/') +
484-
'/' + itemTypes[myparent.ty] +
485-
'.' + myparent.name +
486-
'.html' + anchor +
487-
'" class="' + type +
488-
'">' + name + '</a>';
475+
displayPath = item.path + '::' + myparent.name + '::';
476+
href = rootPath + item.path.replace(/::/g, '/') +
477+
'/' + itemTypes[myparent.ty] +
478+
'.' + myparent.name +
479+
'.html' + anchor;
489480
} else {
490-
output += item.path +
491-
'::<a href="' + rootPath +
492-
item.path.replace(/::/g, '/') +
493-
'/' + type +
494-
'.' + name +
495-
'.html" class="' + type +
496-
'">' + name + '</a>';
481+
displayPath = item.path + '::';
482+
href = rootPath + item.path.replace(/::/g, '/') +
483+
'/' + type + '.' + name + '.html';
497484
}
498485

499-
output += '</td><td><span class="desc">' + item.desc +
500-
'</span></td></tr>';
486+
output += '<tr class="' + type + ' result"><td>' +
487+
'<a href="' + href + '">' +
488+
displayPath + '<span class="' + type + '">' +
489+
name + '</span></a></td><td>' +
490+
'<a href="' + href + '">' +
491+
'<span class="desc">' + item.desc +
492+
'&nbsp;</span></a></td></tr>';
501493
});
502494
} else {
503495
output += 'No results :( <a href="https://duckduckgo.com/?q=' +

0 commit comments

Comments
 (0)