Skip to content

Commit

Permalink
Rollup merge of #94642 - GuillaumeGomez:source-code-scroll, r=Urgau
Browse files Browse the repository at this point in the history
Fix source code pages scroll

To reproduce the bug, go to https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_ast/ast.rs.html#537-541 and click on the `Path` link. The page won't scroll to the content.

r? `@Urgau`
  • Loading branch information
RalfJung authored Mar 5, 2022
2 parents e887e66 + 40e3b6e commit 49d20c4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/librustdoc/html/static/js/source-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function createSourceSidebar() {

var lineNumbersRegex = /^#?(\d+)(?:-(\d+))?$/;

function highlightSourceLines(scrollTo, match) {
function highlightSourceLines(match) {
if (typeof match === "undefined") {
match = window.location.hash.match(lineNumbersRegex);
}
Expand All @@ -170,11 +170,9 @@ function highlightSourceLines(scrollTo, match) {
if (!elem) {
return;
}
if (scrollTo) {
var x = document.getElementById(from);
if (x) {
x.scrollIntoView();
}
var x = document.getElementById(from);
if (x) {
x.scrollIntoView();
}
onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
onEachLazy(e.getElementsByTagName("span"), function(i_e) {
Expand All @@ -198,7 +196,7 @@ var handleSourceHighlight = (function() {
y = window.scrollY;
if (searchState.browserSupportsHistoryApi()) {
history.replaceState(null, null, "#" + name);
highlightSourceLines(true);
highlightSourceLines();
} else {
location.replace("#" + name);
}
Expand Down Expand Up @@ -230,15 +228,15 @@ var handleSourceHighlight = (function() {
window.addEventListener("hashchange", function() {
var match = window.location.hash.match(lineNumbersRegex);
if (match) {
return highlightSourceLines(false, match);
return highlightSourceLines(match);
}
});

onEachLazy(document.getElementsByClassName("line-numbers"), function(el) {
el.addEventListener("click", handleSourceHighlight);
});

highlightSourceLines(true);
highlightSourceLines();

window.createSourceSidebar = createSourceSidebar;
})();
20 changes: 20 additions & 0 deletions src/test/rustdoc-gui/source-anchor-scroll.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// We check that when the anchor changes and is output of the displayed content,
// the page is scrolled to it.
goto: file://|DOC_PATH|/src/link_to_definition/lib.rs.html

// We reduce the window size to make it easier to make an element "out of the page".
size: (600, 800)
// We check that the scroll is at the top first.
assert-property: ("html", {"scrollTop": "0"})

click: '//a[text() = "barbar"]'
assert-property: ("html", {"scrollTop": "125"})
click: '//a[text() = "bar"]'
assert-property: ("html", {"scrollTop": "166"})
click: '//a[text() = "sub_fn"]'
assert-property: ("html", {"scrollTop": "53"})

// We now check that clicking on lines doesn't change the scroll
// Extra information: the "sub_fn" function header is on line 1.
click: '//*[@id="6"]'
assert-property: ("html", {"scrollTop": "53"})
29 changes: 29 additions & 0 deletions src/test/rustdoc-gui/src/link_to_definition/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
pub fn sub_fn() {
barbar();
}
fn barbar() {
bar(vec![], vec![], vec![], vec![], Bar { a: "a".into(), b: 0 });
}

pub struct Bar {
pub a: String,
pub b: u32,
}

pub fn foo(_b: &Bar) {}

// The goal now is to add
// a lot of lines so
// that the next content
// will be out of the screen
// to allow us to test that
// if the anchor changes to
// something outside of the
// current view, it'll
// scroll to it as expected.

// More filling content.

pub fn bar(
_a: Vec<String>,
_b: Vec<String>,
_c: Vec<String>,
_d: Vec<String>,
_e: Bar,
) {
sub_fn();
}

0 comments on commit 49d20c4

Please sign in to comment.