Skip to content

Source code page: line number click adds NaN #97555

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

Merged
merged 3 commits into from
May 31, 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
4 changes: 4 additions & 0 deletions src/librustdoc/html/static/js/source-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ const handleSourceHighlight = (function() {

return ev => {
let cur_line_id = parseInt(ev.target.id, 10);
// It can happen when clicking not on a line number span.
if (isNaN(cur_line_id)) {
return;
}
ev.preventDefault();

if (ev.shiftKey && prev_line_id) {
Expand Down
16 changes: 13 additions & 3 deletions src/test/rustdoc-gui/source-code-page.goml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html
// Check that we can click on the line number.
click: ".line-numbers > span:nth-child(4)" // This is the span for line 4.
// Unfortunately, "#4" isn't a valid query selector, so we have to go around that limitation
// by instead getting the nth span.
assert-attribute: (".line-numbers > span:nth-child(4)", {"class": "line-highlighted"})
// Ensure that the page URL was updated.
assert-document-property: ({"URL": "lib.rs.html#4"}, ENDS_WITH)
assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
// We now check that the good spans are highlighted
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html#4-6
assert-attribute-false: (".line-numbers > span:nth-child(3)", {"class": "line-highlighted"})
Expand All @@ -17,3 +17,13 @@ compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))

// Assert that the line numbers text is aligned to the right.
assert-css: (".line-numbers", {"text-align": "right"})

// Now let's check that clicking on something else than the line number doesn't
// do anything (and certainly not add a `#NaN` to the URL!).
show-text: true
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html
// We use this assert-position to know where we will click.
assert-position: ("//*[@id='1']", {"x": 104, "y": 103})
// We click on the left of the "1" span but still in the "line-number" `<pre>`.
click: (103, 103)
assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)