Skip to content

Commit 96753eb

Browse files
author
Yuki Okushi
authoredNov 13, 2022
Rollup merge of #103650 - notriddle:notriddle/line-anchors, r=GuillaumeGomez
rustdoc: change `.src-line-numbers > span` to `.src-line-numbers > a` Example: https://notriddle.com/notriddle-rustdoc-demos/line-anchors/test_dingus/fn.test.html 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.
2 parents 229e875 + cb3a04b commit 96753eb

File tree

6 files changed

+73
-44
lines changed

6 files changed

+73
-44
lines changed
 

‎src/librustdoc/html/render/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2939,9 +2939,6 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
29392939
})()
29402940
.unwrap_or(rustc_span::DUMMY_SP);
29412941

2942-
// The root path is the inverse of Context::current
2943-
let root_path = vec!["../"; cx.current.len() - 1].join("");
2944-
29452942
let mut decoration_info = FxHashMap::default();
29462943
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
29472944
decoration_info.insert("highlight", byte_ranges);
@@ -2951,7 +2948,7 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
29512948
contents_subset,
29522949
file_span,
29532950
cx,
2954-
&root_path,
2951+
&cx.root_path(),
29552952
highlight::DecorationInfo(decoration_info),
29562953
sources::SourceContext::Embedded { offset: line_min, needs_expansion },
29572954
);

‎src/librustdoc/html/sources.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -276,25 +276,26 @@ pub(crate) fn print_src(
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
}
286289
SourceContext::Embedded { 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, "<span>{line}</span>")
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

+7-5
Original file line numberDiff line numberDiff line change
@@ -579,15 +579,16 @@ ul.block, .block li {
579579
border-color: var(--example-line-numbers-border-color);
580580
}
581581

582-
.src-line-numbers span {
583-
cursor: pointer;
582+
.src-line-numbers a, .src-line-numbers span {
584583
color: var(--src-line-numbers-span-color);
585584
}
586-
.src-line-numbers .line-highlighted {
587-
background-color: var(--src-line-number-highlighted-background-color);
588-
}
589585
.src-line-numbers :target {
590586
background-color: transparent;
587+
border-right: none;
588+
padding-right: 0;
589+
}
590+
.src-line-numbers .line-highlighted {
591+
background-color: var(--src-line-number-highlighted-background-color);
591592
}
592593

593594
.search-loading {
@@ -2044,6 +2045,7 @@ in storage.js
20442045
padding: 14px 0;
20452046
}
20462047

2048+
.scraped-example .code-wrapper .src-line-numbers a,
20472049
.scraped-example .code-wrapper .src-line-numbers span {
20482050
padding: 0 14px;
20492051
}

‎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

+34-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
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+
// Ensure that the default style, with the right border, isn't used.
10+
assert-css: ("//*[@id='4']", {"border-right-width": "0px"})
11+
reload:
12+
assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
13+
assert-css: ("//*[@id='4']", {"border-right-width": "0px"})
14+
// We now check that the good anchors are highlighted
1015
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"})
16+
assert-attribute-false: (".src-line-numbers > a:nth-child(3)", {"class": "line-highlighted"})
17+
assert-attribute: (".src-line-numbers > a:nth-child(4)", {"class": "line-highlighted"})
18+
assert-attribute: (".src-line-numbers > a:nth-child(5)", {"class": "line-highlighted"})
19+
assert-attribute: (".src-line-numbers > a:nth-child(6)", {"class": "line-highlighted"})
20+
assert-attribute-false: (".src-line-numbers > a:nth-child(7)", {"class": "line-highlighted"})
1621

1722
define-function: (
1823
"check-colors",
@@ -21,12 +26,12 @@ define-function: (
2126
("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}),
2227
("reload"),
2328
("assert-css", (
24-
".src-line-numbers > span:not(.line-highlighted)",
29+
".src-line-numbers > a:not(.line-highlighted)",
2530
{"color": |color|, "background-color": |background_color|},
2631
ALL,
2732
)),
2833
("assert-css", (
29-
".src-line-numbers > span.line-highlighted",
34+
".src-line-numbers > a.line-highlighted",
3035
{"color": |highlight_color|, "background-color": |highlight_background_color|},
3136
ALL,
3237
)),
@@ -57,6 +62,25 @@ call-function: ("check-colors", {
5762

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

6185
// Assert that the line numbers text is aligned to the right.
6286
assert-css: (".src-line-numbers", {"text-align": "right"})
@@ -66,7 +90,7 @@ assert-css: (".src-line-numbers", {"text-align": "right"})
6690
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
6791
// We use this assert-position to know where we will click.
6892
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>`.
93+
// We click on the left of the "1" anchor but still in the "src-line-number" `<pre>`.
7094
click: (103, 103)
7195
assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)
7296

‎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)
Please sign in to comment.