Skip to content

Commit 0456a83

Browse files
authored
Rollup merge of rust-lang#66430 - dns2utf8:fix_code_selection_click_handler, r=GuillaumeGomez
[doc] Fix the source code highlighting on source comments The code would always forget the previous selection. r? @GuillaumeGomez
2 parents 76a1e2c + 1bbb816 commit 0456a83

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

src/librustdoc/html/static/main.js

+37-24
Original file line numberDiff line numberDiff line change
@@ -396,38 +396,51 @@ 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;
415402

416-
var cur_id = parseInt(ev.target.id, 10);
403+
var set_fragment = function(name) {
404+
var x = window.scrollX,
405+
y = window.scrollY;
406+
if (browserSupportsHistoryApi()) {
407+
history.replaceState(null, null, "#" + name);
408+
highlightSourceLines();
409+
} else {
410+
location.replace("#" + name);
411+
}
412+
// Prevent jumps when selecting one or many lines
413+
window.scrollTo(x, y);
414+
};
415+
416+
return function(ev) {
417+
var cur_line_id = parseInt(ev.target.id, 10);
418+
ev.preventDefault();
417419

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;
420+
if (ev.shiftKey && prev_line_id) {
421+
// Swap selection if needed
422+
if (prev_line_id > cur_line_id) {
423+
var tmp = prev_line_id;
424+
prev_line_id = cur_line_id;
425+
cur_line_id = tmp;
423426
}
424427

425-
set_fragment(prev_id + "-" + cur_id);
428+
set_fragment(prev_line_id + "-" + cur_line_id);
426429
} else {
427-
prev_id = cur_id;
430+
prev_line_id = cur_line_id;
428431

429-
set_fragment(cur_id);
432+
set_fragment(cur_line_id);
430433
}
434+
}
435+
})();
436+
437+
document.onclick = function(ev) {
438+
if (hasClass(ev.target, "collapse-toggle")) {
439+
collapseDocs(ev.target, "toggle");
440+
} else if (hasClass(ev.target.parentNode, "collapse-toggle")) {
441+
collapseDocs(ev.target.parentNode, "toggle");
442+
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
443+
handleSourceHighlight(ev);
431444
} else if (hasClass(getHelpElement(), "hidden") === false) {
432445
var help = getHelpElement();
433446
var is_inside_help_popup = ev.target !== help && help.contains(ev.target);

0 commit comments

Comments
 (0)