Skip to content

Commit 07bb2f7

Browse files
committed
rustdoc: change .src-line-numbers > span to .src-line-numbers > a
This allows people to treat them like real links, such as right-click to copy URL, and makes the line numbers in a scraped example work at all, when before this commit was added, they had the clickable pointer cursor but did not actually do anything when clicked.
1 parent 2afca78 commit 07bb2f7

File tree

6 files changed

+72
-47
lines changed

6 files changed

+72
-47
lines changed

src/librustdoc/html/render/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2888,9 +2888,6 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
28882888
})()
28892889
.unwrap_or(rustc_span::DUMMY_SP);
28902890

2891-
// The root path is the inverse of Context::current
2892-
let root_path = vec!["../"; cx.current.len() - 1].join("");
2893-
28942891
let mut decoration_info = FxHashMap::default();
28952892
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
28962893
decoration_info.insert("highlight", byte_ranges);
@@ -2900,9 +2897,13 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
29002897
contents_subset,
29012898
file_span,
29022899
cx,
2903-
&root_path,
2900+
&cx.root_path(),
29042901
highlight::DecorationInfo(decoration_info),
2905-
sources::SourceContext::Embedded { offset: line_min, needs_expansion },
2902+
sources::SourceContext::Embedded {
2903+
url: &call_data.url,
2904+
offset: line_min,
2905+
needs_expansion,
2906+
},
29062907
);
29072908
write!(w, "</div></div>");
29082909

src/librustdoc/html/sources.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ where
256256
}
257257
}
258258

259-
pub(crate) enum SourceContext {
259+
pub(crate) enum SourceContext<'a> {
260260
Standalone,
261-
Embedded { offset: usize, needs_expansion: bool },
261+
Embedded { url: &'a str, offset: usize, needs_expansion: bool },
262262
}
263263

264264
/// Wrapper struct to render the source code of a file. This will do things like
@@ -270,31 +270,32 @@ pub(crate) fn print_src(
270270
context: &Context<'_>,
271271
root_path: &str,
272272
decoration_info: highlight::DecorationInfo,
273-
source_context: SourceContext,
273+
source_context: SourceContext<'_>,
274274
) {
275275
let lines = s.lines().count();
276276
let mut line_numbers = Buffer::empty_from(buf);
277277
let extra;
278278
line_numbers.write_str("<pre class=\"src-line-numbers\">");
279+
let current_href = &context
280+
.href_from_span(clean::Span::new(file_span), false)
281+
.expect("only local crates should have sources emitted");
279282
match source_context {
280283
SourceContext::Standalone => {
281284
extra = None;
282285
for line in 1..=lines {
283-
writeln!(line_numbers, "<span id=\"{0}\">{0}</span>", line)
286+
writeln!(line_numbers, "<a href=\"#{line}\" id=\"{line}\">{line}</a>")
284287
}
285288
}
286-
SourceContext::Embedded { offset, needs_expansion } => {
289+
SourceContext::Embedded { url, offset, needs_expansion } => {
287290
extra =
288291
if needs_expansion { Some(r#"<span class="expand">&varr;</span>"#) } else { None };
289-
for line in 1..=lines {
290-
writeln!(line_numbers, "<span>{0}</span>", line + offset)
292+
for line_number in 1..=lines {
293+
let line = line_number + offset;
294+
writeln!(line_numbers, "<a href=\"{root_path}{url}#{line}\">{line}</a>")
291295
}
292296
}
293297
}
294298
line_numbers.write_str("</pre>");
295-
let current_href = &context
296-
.href_from_span(clean::Span::new(file_span), false)
297-
.expect("only local crates should have sources emitted");
298299
highlight::render_source_with_highlighting(
299300
s,
300301
buf,

src/librustdoc/html/static/css/rustdoc.css

+2-3
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ ul.block, .block li {
575575
border-color: var(--example-line-numbers-border-color);
576576
}
577577

578-
.src-line-numbers span {
579-
cursor: pointer;
578+
.src-line-numbers a {
580579
color: var(--src-line-numbers-span-color);
581580
}
582581
.src-line-numbers .line-highlighted {
@@ -2060,7 +2059,7 @@ in storage.js
20602059
padding: 14px 0;
20612060
}
20622061

2063-
.scraped-example .code-wrapper .src-line-numbers span {
2062+
.scraped-example .code-wrapper .src-line-numbers a {
20642063
padding: 0 14px;
20652064
}
20662065

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function highlightSourceLines(match) {
157157
x.scrollIntoView();
158158
}
159159
onEachLazy(document.getElementsByClassName("src-line-numbers"), e => {
160-
onEachLazy(e.getElementsByTagName("span"), i_e => {
160+
onEachLazy(e.getElementsByTagName("a"), i_e => {
161161
removeClass(i_e, "line-highlighted");
162162
});
163163
});
@@ -188,8 +188,13 @@ const handleSourceHighlight = (function() {
188188

189189
return ev => {
190190
let cur_line_id = parseInt(ev.target.id, 10);
191-
// It can happen when clicking not on a line number span.
192-
if (isNaN(cur_line_id)) {
191+
// This event handler is attached to the entire line number column, but it should only
192+
// be run if one of the anchors is clicked. It also shouldn't do anything if the anchor
193+
// is clicked with a modifier key (to open a new browser tab).
194+
if (isNaN(cur_line_id) ||
195+
ev.ctrlKey ||
196+
ev.altKey ||
197+
ev.metaKey) {
193198
return;
194199
}
195200
ev.preventDefault();

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

+29-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
33
show-text: true
44
// Check that we can click on the line number.
5-
click: ".src-line-numbers > span:nth-child(4)" // This is the span for line 4.
5+
click: ".src-line-numbers > a:nth-child(4)" // This is the anchor for line 4.
66
// Ensure that the page URL was updated.
77
assert-document-property: ({"URL": "lib.rs.html#4"}, ENDS_WITH)
88
assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
9-
// We now check that the good spans are highlighted
9+
// We now check that the good anchors are highlighted
1010
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#4-6"
11-
assert-attribute-false: (".src-line-numbers > span:nth-child(3)", {"class": "line-highlighted"})
12-
assert-attribute: (".src-line-numbers > span:nth-child(4)", {"class": "line-highlighted"})
13-
assert-attribute: (".src-line-numbers > span:nth-child(5)", {"class": "line-highlighted"})
14-
assert-attribute: (".src-line-numbers > span:nth-child(6)", {"class": "line-highlighted"})
15-
assert-attribute-false: (".src-line-numbers > span:nth-child(7)", {"class": "line-highlighted"})
11+
assert-attribute-false: (".src-line-numbers > a:nth-child(3)", {"class": "line-highlighted"})
12+
assert-attribute: (".src-line-numbers > a:nth-child(4)", {"class": "line-highlighted"})
13+
assert-attribute: (".src-line-numbers > a:nth-child(5)", {"class": "line-highlighted"})
14+
assert-attribute: (".src-line-numbers > a:nth-child(6)", {"class": "line-highlighted"})
15+
assert-attribute-false: (".src-line-numbers > a:nth-child(7)", {"class": "line-highlighted"})
1616

1717
define-function: (
1818
"check-colors",
@@ -21,12 +21,12 @@ define-function: (
2121
("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}),
2222
("reload"),
2323
("assert-css", (
24-
".src-line-numbers > span:not(.line-highlighted)",
24+
".src-line-numbers > a:not(.line-highlighted)",
2525
{"color": |color|, "background-color": |background_color|},
2626
ALL,
2727
)),
2828
("assert-css", (
29-
".src-line-numbers > span.line-highlighted",
29+
".src-line-numbers > a.line-highlighted",
3030
{"color": |highlight_color|, "background-color": |highlight_background_color|},
3131
ALL,
3232
)),
@@ -57,6 +57,25 @@ call-function: ("check-colors", {
5757

5858
// This is to ensure that the content is correctly align with the line numbers.
5959
compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))
60+
// Check the `href` property so that users can treat anchors as links.
61+
assert-property: (".src-line-numbers > a:nth-child(1)", {
62+
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#1"
63+
})
64+
assert-property: (".src-line-numbers > a:nth-child(2)", {
65+
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#2"
66+
})
67+
assert-property: (".src-line-numbers > a:nth-child(3)", {
68+
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#3"
69+
})
70+
assert-property: (".src-line-numbers > a:nth-child(4)", {
71+
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#4"
72+
})
73+
assert-property: (".src-line-numbers > a:nth-child(5)", {
74+
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#5"
75+
})
76+
assert-property: (".src-line-numbers > a:nth-child(6)", {
77+
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#6"
78+
})
6079

6180
// Assert that the line numbers text is aligned to the right.
6281
assert-css: (".src-line-numbers", {"text-align": "right"})
@@ -66,7 +85,7 @@ assert-css: (".src-line-numbers", {"text-align": "right"})
6685
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
6786
// We use this assert-position to know where we will click.
6887
assert-position: ("//*[@id='1']", {"x": 104, "y": 112})
69-
// We click on the left of the "1" span but still in the "src-line-number" `<pre>`.
88+
// We click on the left of the "1" anchor but still in the "src-line-number" `<pre>`.
7089
click: (103, 103)
7190
assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)
7291

src/test/rustdoc/check-source-code-urls-to-def.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ extern crate source_code;
1010

1111
// @has 'src/foo/check-source-code-urls-to-def.rs.html'
1212

13-
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#1-17"]' 'bar'
13+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#1-17"]' 'bar'
1414
#[path = "auxiliary/source-code-bar.rs"]
1515
pub mod bar;
1616

17-
// @count - '//a[@href="auxiliary/source-code-bar.rs.html#5"]' 4
17+
// @count - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#5"]' 4
1818
use bar::Bar;
19-
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#13"]' 'self'
20-
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
19+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#13"]' 'self'
20+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
2121
use bar::sub::{self, Trait};
2222

2323
pub struct Foo;
@@ -28,29 +28,29 @@ impl Foo {
2828

2929
fn babar() {}
3030

31-
// @has - '//a/@href' '/struct.String.html'
32-
// @has - '//a/@href' '/primitive.u32.html'
33-
// @has - '//a/@href' '/primitive.str.html'
34-
// @count - '//a[@href="#23"]' 5
35-
// @has - '//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
31+
// @has - '//pre[@class="rust"]//a/@href' '/struct.String.html'
32+
// @has - '//pre[@class="rust"]//a/@href' '/primitive.u32.html'
33+
// @has - '//pre[@class="rust"]//a/@href' '/primitive.str.html'
34+
// @count - '//pre[@class="rust"]//a[@href="#23"]' 5
35+
// @has - '//pre[@class="rust"]//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
3636
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
3737
let x = 12;
3838
let y: Foo = Foo;
3939
let z: Bar = bar::Bar { field: Foo };
4040
babar();
41-
// @has - '//a[@href="#26"]' 'hello'
41+
// @has - '//pre[@class="rust"]//a[@href="#26"]' 'hello'
4242
y.hello();
4343
}
4444

45-
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'bar::sub::Trait'
46-
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
45+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'bar::sub::Trait'
46+
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
4747
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {}
4848

4949
pub trait AnotherTrait {}
5050
pub trait WhyNot {}
5151

52-
// @has - '//a[@href="#49"]' 'AnotherTrait'
53-
// @has - '//a[@href="#50"]' 'WhyNot'
52+
// @has - '//pre[@class="rust"]//a[@href="#49"]' 'AnotherTrait'
53+
// @has - '//pre[@class="rust"]//a[@href="#50"]' 'WhyNot'
5454
pub fn foo3<T, V>(t: &T, v: &V)
5555
where
5656
T: AnotherTrait,
@@ -59,11 +59,11 @@ where
5959

6060
pub trait AnotherTrait2 {}
6161

62-
// @has - '//a[@href="#60"]' 'AnotherTrait2'
62+
// @has - '//pre[@class="rust"]//a[@href="#60"]' 'AnotherTrait2'
6363
pub fn foo4() {
6464
let x: Vec<AnotherTrait2> = Vec::new();
6565
}
6666

67-
// @has - '//a[@href="../../foo/primitive.bool.html"]' 'bool'
67+
// @has - '//pre[@class="rust"]//a[@href="../../foo/primitive.bool.html"]' 'bool'
6868
#[doc(primitive = "bool")]
6969
mod whatever {}

0 commit comments

Comments
 (0)