Skip to content

Commit 4c77fd3

Browse files
Rollup merge of #97555 - GuillaumeGomez:line-number-click, r=notriddle
Source code page: line number click adds `NaN` When you click on the parent element of the line numbers in the source code pages, it'll add `NaN` (like in https://doc.rust-lang.org/nightly/src/alloc/lib.rs.html#NaN). This PR fixes this bug. cc ``@jsha`` r? ``@notriddle``
2 parents 6718723 + d286df1 commit 4c77fd3

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/librustdoc/html/static/js/source-script.js

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ const handleSourceHighlight = (function() {
205205

206206
return ev => {
207207
let cur_line_id = parseInt(ev.target.id, 10);
208+
// It can happen when clicking not on a line number span.
209+
if (isNaN(cur_line_id)) {
210+
return;
211+
}
208212
ev.preventDefault();
209213

210214
if (ev.shiftKey && prev_line_id) {

src/test/rustdoc-gui/source-code-page.goml

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html
33
// Check that we can click on the line number.
44
click: ".line-numbers > span:nth-child(4)" // This is the span for line 4.
5-
// Unfortunately, "#4" isn't a valid query selector, so we have to go around that limitation
6-
// by instead getting the nth span.
7-
assert-attribute: (".line-numbers > span:nth-child(4)", {"class": "line-highlighted"})
5+
// Ensure that the page URL was updated.
6+
assert-document-property: ({"URL": "lib.rs.html#4"}, ENDS_WITH)
7+
assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
88
// We now check that the good spans are highlighted
99
goto: file://|DOC_PATH|/src/test_docs/lib.rs.html#4-6
1010
assert-attribute-false: (".line-numbers > span:nth-child(3)", {"class": "line-highlighted"})
@@ -17,3 +17,13 @@ compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))
1717

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

0 commit comments

Comments
 (0)