From 71cf364c3cf1c6b437be0e9352fa6d3ee8191404 Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Fri, 15 Nov 2019 02:02:09 +0100 Subject: [PATCH 1/2] Fix the source code highlighting on source comments --- src/librustdoc/html/static/main.js | 56 +++++++++++++++++------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 04c0a0f005b82..e992c0b62bf00 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -396,38 +396,46 @@ function getSearchElement() { document.onkeypress = handleShortcut; document.onkeydown = handleShortcut; - document.onclick = function(ev) { - if (hasClass(ev.target, "collapse-toggle")) { - collapseDocs(ev.target, "toggle"); - } else if (hasClass(ev.target.parentNode, "collapse-toggle")) { - collapseDocs(ev.target.parentNode, "toggle"); - } else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) { - var prev_id = 0; - var set_fragment = function(name) { - if (browserSupportsHistoryApi()) { - history.replaceState(null, null, "#" + name); - highlightSourceLines(); - } else { - location.replace("#" + name); - } - }; + var handleSourceHighlight = (function() { + var prev_line_id = 0; + + var set_fragment = function(name) { + if (browserSupportsHistoryApi()) { + history.replaceState(null, null, "#" + name); + highlightSourceLines(); + } else { + location.replace("#" + name); + } + }; - var cur_id = parseInt(ev.target.id, 10); + return function(ev) { + var cur_line_id = parseInt(ev.target.id, 10); - if (ev.shiftKey && prev_id) { - if (prev_id > cur_id) { - var tmp = prev_id; - prev_id = cur_id; - cur_id = tmp; + if (ev.shiftKey && prev_line_id) { + // Swap selection if needed + if (prev_line_id > cur_line_id) { + var tmp = prev_line_id; + prev_line_id = cur_line_id; + cur_line_id = tmp; } - set_fragment(prev_id + "-" + cur_id); + set_fragment(prev_line_id + "-" + cur_line_id); } else { - prev_id = cur_id; + prev_line_id = cur_line_id; - set_fragment(cur_id); + set_fragment(cur_line_id); } + } + })(); + + document.onclick = function(ev) { + if (hasClass(ev.target, "collapse-toggle")) { + collapseDocs(ev.target, "toggle"); + } else if (hasClass(ev.target.parentNode, "collapse-toggle")) { + collapseDocs(ev.target.parentNode, "toggle"); + } else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) { + handleSourceHighlight(ev); } else if (hasClass(getHelpElement(), "hidden") === false) { var help = getHelpElement(); var is_inside_help_popup = ev.target !== help && help.contains(ev.target); From 1bbb8168ec5aa2d03e1b49d5a38eda0300c67da4 Mon Sep 17 00:00:00 2001 From: Stefan Schindler Date: Fri, 15 Nov 2019 02:30:44 +0100 Subject: [PATCH 2/2] Prevent jumps when selecting one or many lines --- src/librustdoc/html/static/main.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index e992c0b62bf00..4db02d58c51ac 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -401,16 +401,21 @@ function getSearchElement() { var prev_line_id = 0; var set_fragment = function(name) { + var x = window.scrollX, + y = window.scrollY; if (browserSupportsHistoryApi()) { history.replaceState(null, null, "#" + name); highlightSourceLines(); } else { location.replace("#" + name); } + // Prevent jumps when selecting one or many lines + window.scrollTo(x, y); }; return function(ev) { var cur_line_id = parseInt(ev.target.id, 10); + ev.preventDefault(); if (ev.shiftKey && prev_line_id) { // Swap selection if needed