Skip to content

Commit 71cf364

Browse files
committed
Fix the source code highlighting on source comments
1 parent 82cf3a4 commit 71cf364

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/librustdoc/html/static/main.js

+32-24
Original file line numberDiff line numberDiff line change
@@ -396,38 +396,46 @@ function getSearchElement() {
396396

397397
document.onkeypress = handleShortcut;
398398
document.onkeydown = handleShortcut;
399-
document.onclick = function(ev) {
400-
if (hasClass(ev.target, "collapse-toggle")) {
401-
collapseDocs(ev.target, "toggle");
402-
} else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
403-
collapseDocs(ev.target.parentNode, "toggle");
404-
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
405-
var prev_id = 0;
406399

407-
var set_fragment = function(name) {
408-
if (browserSupportsHistoryApi()) {
409-
history.replaceState(null, null, "#" + name);
410-
highlightSourceLines();
411-
} else {
412-
location.replace("#" + name);
413-
}
414-
};
400+
var handleSourceHighlight = (function() {
401+
var prev_line_id = 0;
402+
403+
var set_fragment = function(name) {
404+
if (browserSupportsHistoryApi()) {
405+
history.replaceState(null, null, "#" + name);
406+
highlightSourceLines();
407+
} else {
408+
location.replace("#" + name);
409+
}
410+
};
415411

416-
var cur_id = parseInt(ev.target.id, 10);
412+
return function(ev) {
413+
var cur_line_id = parseInt(ev.target.id, 10);
417414

418-
if (ev.shiftKey && prev_id) {
419-
if (prev_id > cur_id) {
420-
var tmp = prev_id;
421-
prev_id = cur_id;
422-
cur_id = tmp;
415+
if (ev.shiftKey && prev_line_id) {
416+
// Swap selection if needed
417+
if (prev_line_id > cur_line_id) {
418+
var tmp = prev_line_id;
419+
prev_line_id = cur_line_id;
420+
cur_line_id = tmp;
423421
}
424422

425-
set_fragment(prev_id + "-" + cur_id);
423+
set_fragment(prev_line_id + "-" + cur_line_id);
426424
} else {
427-
prev_id = cur_id;
425+
prev_line_id = cur_line_id;
428426

429-
set_fragment(cur_id);
427+
set_fragment(cur_line_id);
430428
}
429+
}
430+
})();
431+
432+
document.onclick = function(ev) {
433+
if (hasClass(ev.target, "collapse-toggle")) {
434+
collapseDocs(ev.target, "toggle");
435+
} else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
436+
collapseDocs(ev.target.parentNode, "toggle");
437+
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
438+
handleSourceHighlight(ev);
431439
} else if (hasClass(getHelpElement(), "hidden") === false) {
432440
var help = getHelpElement();
433441
var is_inside_help_popup = ev.target !== help && help.contains(ev.target);

0 commit comments

Comments
 (0)