Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source code pages scroll #94642

Merged
merged 2 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}