From f3eae89b33fbd729fb6c401b42daa672bdd1751c Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Mon, 30 May 2022 16:53:24 +0200
Subject: [PATCH 1/3] Fix invalid line number computation when clicking on
 something else than a line number

---
 src/librustdoc/html/static/js/source-script.js | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/librustdoc/html/static/js/source-script.js b/src/librustdoc/html/static/js/source-script.js
index aaac878d3a37b..58c036e0b3ca3 100644
--- a/src/librustdoc/html/static/js/source-script.js
+++ b/src/librustdoc/html/static/js/source-script.js
@@ -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) {

From 16d5cdc570b45a17a9c85953244ef6058929f608 Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Mon, 30 May 2022 17:07:21 +0200
Subject: [PATCH 2/3] Improve source-code-page.goml GUI test code

---
 src/test/rustdoc-gui/source-code-page.goml | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml
index ad7080c39b842..f31acc80f7e55 100644
--- a/src/test/rustdoc-gui/source-code-page.goml
+++ b/src/test/rustdoc-gui/source-code-page.goml
@@ -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": "#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"})

From d286df1402f0e3a96e9ec11748e80c8be4bca92f Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume.gomez@huawei.com>
Date: Mon, 30 May 2022 17:14:46 +0200
Subject: [PATCH 3/3] Add line number click GUI test

---
 src/test/rustdoc-gui/source-code-page.goml | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/test/rustdoc-gui/source-code-page.goml b/src/test/rustdoc-gui/source-code-page.goml
index f31acc80f7e55..509739c9f2951 100644
--- a/src/test/rustdoc-gui/source-code-page.goml
+++ b/src/test/rustdoc-gui/source-code-page.goml
@@ -3,7 +3,7 @@ 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.
 // Ensure that the page URL was updated.
-assert-document-property: ({"URL": "#4"}, ENDS_WITH)
+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
@@ -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)