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.
This commit is contained in:
parent
2afca78a0b
commit
07bb2f701e
6 changed files with 72 additions and 47 deletions
|
@ -2888,9 +2888,6 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
|
||||||
})()
|
})()
|
||||||
.unwrap_or(rustc_span::DUMMY_SP);
|
.unwrap_or(rustc_span::DUMMY_SP);
|
||||||
|
|
||||||
// The root path is the inverse of Context::current
|
|
||||||
let root_path = vec!["../"; cx.current.len() - 1].join("");
|
|
||||||
|
|
||||||
let mut decoration_info = FxHashMap::default();
|
let mut decoration_info = FxHashMap::default();
|
||||||
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
|
decoration_info.insert("highlight focus", vec![byte_ranges.remove(0)]);
|
||||||
decoration_info.insert("highlight", byte_ranges);
|
decoration_info.insert("highlight", byte_ranges);
|
||||||
|
@ -2900,9 +2897,13 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite
|
||||||
contents_subset,
|
contents_subset,
|
||||||
file_span,
|
file_span,
|
||||||
cx,
|
cx,
|
||||||
&root_path,
|
&cx.root_path(),
|
||||||
highlight::DecorationInfo(decoration_info),
|
highlight::DecorationInfo(decoration_info),
|
||||||
sources::SourceContext::Embedded { offset: line_min, needs_expansion },
|
sources::SourceContext::Embedded {
|
||||||
|
url: &call_data.url,
|
||||||
|
offset: line_min,
|
||||||
|
needs_expansion,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
write!(w, "</div></div>");
|
write!(w, "</div></div>");
|
||||||
|
|
||||||
|
|
|
@ -256,9 +256,9 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) enum SourceContext {
|
pub(crate) enum SourceContext<'a> {
|
||||||
Standalone,
|
Standalone,
|
||||||
Embedded { offset: usize, needs_expansion: bool },
|
Embedded { url: &'a str, offset: usize, needs_expansion: bool },
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapper struct to render the source code of a file. This will do things like
|
/// Wrapper struct to render the source code of a file. This will do things like
|
||||||
|
@ -270,31 +270,32 @@ pub(crate) fn print_src(
|
||||||
context: &Context<'_>,
|
context: &Context<'_>,
|
||||||
root_path: &str,
|
root_path: &str,
|
||||||
decoration_info: highlight::DecorationInfo,
|
decoration_info: highlight::DecorationInfo,
|
||||||
source_context: SourceContext,
|
source_context: SourceContext<'_>,
|
||||||
) {
|
) {
|
||||||
let lines = s.lines().count();
|
let lines = s.lines().count();
|
||||||
let mut line_numbers = Buffer::empty_from(buf);
|
let mut line_numbers = Buffer::empty_from(buf);
|
||||||
let extra;
|
let extra;
|
||||||
line_numbers.write_str("<pre class=\"src-line-numbers\">");
|
line_numbers.write_str("<pre class=\"src-line-numbers\">");
|
||||||
|
let current_href = &context
|
||||||
|
.href_from_span(clean::Span::new(file_span), false)
|
||||||
|
.expect("only local crates should have sources emitted");
|
||||||
match source_context {
|
match source_context {
|
||||||
SourceContext::Standalone => {
|
SourceContext::Standalone => {
|
||||||
extra = None;
|
extra = None;
|
||||||
for line in 1..=lines {
|
for line in 1..=lines {
|
||||||
writeln!(line_numbers, "<span id=\"{0}\">{0}</span>", line)
|
writeln!(line_numbers, "<a href=\"#{line}\" id=\"{line}\">{line}</a>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SourceContext::Embedded { offset, needs_expansion } => {
|
SourceContext::Embedded { url, offset, needs_expansion } => {
|
||||||
extra =
|
extra =
|
||||||
if needs_expansion { Some(r#"<span class="expand">↕</span>"#) } else { None };
|
if needs_expansion { Some(r#"<span class="expand">↕</span>"#) } else { None };
|
||||||
for line in 1..=lines {
|
for line_number in 1..=lines {
|
||||||
writeln!(line_numbers, "<span>{0}</span>", line + offset)
|
let line = line_number + offset;
|
||||||
|
writeln!(line_numbers, "<a href=\"{root_path}{url}#{line}\">{line}</a>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
line_numbers.write_str("</pre>");
|
line_numbers.write_str("</pre>");
|
||||||
let current_href = &context
|
|
||||||
.href_from_span(clean::Span::new(file_span), false)
|
|
||||||
.expect("only local crates should have sources emitted");
|
|
||||||
highlight::render_source_with_highlighting(
|
highlight::render_source_with_highlighting(
|
||||||
s,
|
s,
|
||||||
buf,
|
buf,
|
||||||
|
|
|
@ -575,8 +575,7 @@ ul.block, .block li {
|
||||||
border-color: var(--example-line-numbers-border-color);
|
border-color: var(--example-line-numbers-border-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
.src-line-numbers span {
|
.src-line-numbers a {
|
||||||
cursor: pointer;
|
|
||||||
color: var(--src-line-numbers-span-color);
|
color: var(--src-line-numbers-span-color);
|
||||||
}
|
}
|
||||||
.src-line-numbers .line-highlighted {
|
.src-line-numbers .line-highlighted {
|
||||||
|
@ -2060,7 +2059,7 @@ in storage.js
|
||||||
padding: 14px 0;
|
padding: 14px 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.scraped-example .code-wrapper .src-line-numbers span {
|
.scraped-example .code-wrapper .src-line-numbers a {
|
||||||
padding: 0 14px;
|
padding: 0 14px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -157,7 +157,7 @@ function highlightSourceLines(match) {
|
||||||
x.scrollIntoView();
|
x.scrollIntoView();
|
||||||
}
|
}
|
||||||
onEachLazy(document.getElementsByClassName("src-line-numbers"), e => {
|
onEachLazy(document.getElementsByClassName("src-line-numbers"), e => {
|
||||||
onEachLazy(e.getElementsByTagName("span"), i_e => {
|
onEachLazy(e.getElementsByTagName("a"), i_e => {
|
||||||
removeClass(i_e, "line-highlighted");
|
removeClass(i_e, "line-highlighted");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -188,8 +188,13 @@ const handleSourceHighlight = (function() {
|
||||||
|
|
||||||
return ev => {
|
return ev => {
|
||||||
let cur_line_id = parseInt(ev.target.id, 10);
|
let cur_line_id = parseInt(ev.target.id, 10);
|
||||||
// It can happen when clicking not on a line number span.
|
// This event handler is attached to the entire line number column, but it should only
|
||||||
if (isNaN(cur_line_id)) {
|
// be run if one of the anchors is clicked. It also shouldn't do anything if the anchor
|
||||||
|
// is clicked with a modifier key (to open a new browser tab).
|
||||||
|
if (isNaN(cur_line_id) ||
|
||||||
|
ev.ctrlKey ||
|
||||||
|
ev.altKey ||
|
||||||
|
ev.metaKey) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
|
|
|
@ -2,17 +2,17 @@
|
||||||
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
|
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
|
||||||
show-text: true
|
show-text: true
|
||||||
// Check that we can click on the line number.
|
// Check that we can click on the line number.
|
||||||
click: ".src-line-numbers > span:nth-child(4)" // This is the span for line 4.
|
click: ".src-line-numbers > a:nth-child(4)" // This is the anchor for line 4.
|
||||||
// Ensure that the page URL was updated.
|
// Ensure that the page URL was updated.
|
||||||
assert-document-property: ({"URL": "lib.rs.html#4"}, ENDS_WITH)
|
assert-document-property: ({"URL": "lib.rs.html#4"}, ENDS_WITH)
|
||||||
assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
|
assert-attribute: ("//*[@id='4']", {"class": "line-highlighted"})
|
||||||
// We now check that the good spans are highlighted
|
// We now check that the good anchors are highlighted
|
||||||
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#4-6"
|
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#4-6"
|
||||||
assert-attribute-false: (".src-line-numbers > span:nth-child(3)", {"class": "line-highlighted"})
|
assert-attribute-false: (".src-line-numbers > a:nth-child(3)", {"class": "line-highlighted"})
|
||||||
assert-attribute: (".src-line-numbers > span:nth-child(4)", {"class": "line-highlighted"})
|
assert-attribute: (".src-line-numbers > a:nth-child(4)", {"class": "line-highlighted"})
|
||||||
assert-attribute: (".src-line-numbers > span:nth-child(5)", {"class": "line-highlighted"})
|
assert-attribute: (".src-line-numbers > a:nth-child(5)", {"class": "line-highlighted"})
|
||||||
assert-attribute: (".src-line-numbers > span:nth-child(6)", {"class": "line-highlighted"})
|
assert-attribute: (".src-line-numbers > a:nth-child(6)", {"class": "line-highlighted"})
|
||||||
assert-attribute-false: (".src-line-numbers > span:nth-child(7)", {"class": "line-highlighted"})
|
assert-attribute-false: (".src-line-numbers > a:nth-child(7)", {"class": "line-highlighted"})
|
||||||
|
|
||||||
define-function: (
|
define-function: (
|
||||||
"check-colors",
|
"check-colors",
|
||||||
|
@ -21,12 +21,12 @@ define-function: (
|
||||||
("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}),
|
("local-storage", {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}),
|
||||||
("reload"),
|
("reload"),
|
||||||
("assert-css", (
|
("assert-css", (
|
||||||
".src-line-numbers > span:not(.line-highlighted)",
|
".src-line-numbers > a:not(.line-highlighted)",
|
||||||
{"color": |color|, "background-color": |background_color|},
|
{"color": |color|, "background-color": |background_color|},
|
||||||
ALL,
|
ALL,
|
||||||
)),
|
)),
|
||||||
("assert-css", (
|
("assert-css", (
|
||||||
".src-line-numbers > span.line-highlighted",
|
".src-line-numbers > a.line-highlighted",
|
||||||
{"color": |highlight_color|, "background-color": |highlight_background_color|},
|
{"color": |highlight_color|, "background-color": |highlight_background_color|},
|
||||||
ALL,
|
ALL,
|
||||||
)),
|
)),
|
||||||
|
@ -57,6 +57,25 @@ call-function: ("check-colors", {
|
||||||
|
|
||||||
// This is to ensure that the content is correctly align with the line numbers.
|
// This is to ensure that the content is correctly align with the line numbers.
|
||||||
compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))
|
compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))
|
||||||
|
// Check the `href` property so that users can treat anchors as links.
|
||||||
|
assert-property: (".src-line-numbers > a:nth-child(1)", {
|
||||||
|
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#1"
|
||||||
|
})
|
||||||
|
assert-property: (".src-line-numbers > a:nth-child(2)", {
|
||||||
|
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#2"
|
||||||
|
})
|
||||||
|
assert-property: (".src-line-numbers > a:nth-child(3)", {
|
||||||
|
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#3"
|
||||||
|
})
|
||||||
|
assert-property: (".src-line-numbers > a:nth-child(4)", {
|
||||||
|
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#4"
|
||||||
|
})
|
||||||
|
assert-property: (".src-line-numbers > a:nth-child(5)", {
|
||||||
|
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#5"
|
||||||
|
})
|
||||||
|
assert-property: (".src-line-numbers > a:nth-child(6)", {
|
||||||
|
"href": "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html#6"
|
||||||
|
})
|
||||||
|
|
||||||
// Assert that the line numbers text is aligned to the right.
|
// Assert that the line numbers text is aligned to the right.
|
||||||
assert-css: (".src-line-numbers", {"text-align": "right"})
|
assert-css: (".src-line-numbers", {"text-align": "right"})
|
||||||
|
@ -66,7 +85,7 @@ assert-css: (".src-line-numbers", {"text-align": "right"})
|
||||||
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
|
goto: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
|
||||||
// We use this assert-position to know where we will click.
|
// We use this assert-position to know where we will click.
|
||||||
assert-position: ("//*[@id='1']", {"x": 104, "y": 112})
|
assert-position: ("//*[@id='1']", {"x": 104, "y": 112})
|
||||||
// We click on the left of the "1" span but still in the "src-line-number" `<pre>`.
|
// We click on the left of the "1" anchor but still in the "src-line-number" `<pre>`.
|
||||||
click: (103, 103)
|
click: (103, 103)
|
||||||
assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)
|
assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)
|
||||||
|
|
||||||
|
|
|
@ -10,14 +10,14 @@ extern crate source_code;
|
||||||
|
|
||||||
// @has 'src/foo/check-source-code-urls-to-def.rs.html'
|
// @has 'src/foo/check-source-code-urls-to-def.rs.html'
|
||||||
|
|
||||||
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#1-17"]' 'bar'
|
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#1-17"]' 'bar'
|
||||||
#[path = "auxiliary/source-code-bar.rs"]
|
#[path = "auxiliary/source-code-bar.rs"]
|
||||||
pub mod bar;
|
pub mod bar;
|
||||||
|
|
||||||
// @count - '//a[@href="auxiliary/source-code-bar.rs.html#5"]' 4
|
// @count - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#5"]' 4
|
||||||
use bar::Bar;
|
use bar::Bar;
|
||||||
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#13"]' 'self'
|
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#13"]' 'self'
|
||||||
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
|
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
|
||||||
use bar::sub::{self, Trait};
|
use bar::sub::{self, Trait};
|
||||||
|
|
||||||
pub struct Foo;
|
pub struct Foo;
|
||||||
|
@ -28,29 +28,29 @@ impl Foo {
|
||||||
|
|
||||||
fn babar() {}
|
fn babar() {}
|
||||||
|
|
||||||
// @has - '//a/@href' '/struct.String.html'
|
// @has - '//pre[@class="rust"]//a/@href' '/struct.String.html'
|
||||||
// @has - '//a/@href' '/primitive.u32.html'
|
// @has - '//pre[@class="rust"]//a/@href' '/primitive.u32.html'
|
||||||
// @has - '//a/@href' '/primitive.str.html'
|
// @has - '//pre[@class="rust"]//a/@href' '/primitive.str.html'
|
||||||
// @count - '//a[@href="#23"]' 5
|
// @count - '//pre[@class="rust"]//a[@href="#23"]' 5
|
||||||
// @has - '//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
|
// @has - '//pre[@class="rust"]//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
|
||||||
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
|
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
|
||||||
let x = 12;
|
let x = 12;
|
||||||
let y: Foo = Foo;
|
let y: Foo = Foo;
|
||||||
let z: Bar = bar::Bar { field: Foo };
|
let z: Bar = bar::Bar { field: Foo };
|
||||||
babar();
|
babar();
|
||||||
// @has - '//a[@href="#26"]' 'hello'
|
// @has - '//pre[@class="rust"]//a[@href="#26"]' 'hello'
|
||||||
y.hello();
|
y.hello();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'bar::sub::Trait'
|
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'bar::sub::Trait'
|
||||||
// @has - '//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
|
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14"]' 'Trait'
|
||||||
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {}
|
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {}
|
||||||
|
|
||||||
pub trait AnotherTrait {}
|
pub trait AnotherTrait {}
|
||||||
pub trait WhyNot {}
|
pub trait WhyNot {}
|
||||||
|
|
||||||
// @has - '//a[@href="#49"]' 'AnotherTrait'
|
// @has - '//pre[@class="rust"]//a[@href="#49"]' 'AnotherTrait'
|
||||||
// @has - '//a[@href="#50"]' 'WhyNot'
|
// @has - '//pre[@class="rust"]//a[@href="#50"]' 'WhyNot'
|
||||||
pub fn foo3<T, V>(t: &T, v: &V)
|
pub fn foo3<T, V>(t: &T, v: &V)
|
||||||
where
|
where
|
||||||
T: AnotherTrait,
|
T: AnotherTrait,
|
||||||
|
@ -59,11 +59,11 @@ where
|
||||||
|
|
||||||
pub trait AnotherTrait2 {}
|
pub trait AnotherTrait2 {}
|
||||||
|
|
||||||
// @has - '//a[@href="#60"]' 'AnotherTrait2'
|
// @has - '//pre[@class="rust"]//a[@href="#60"]' 'AnotherTrait2'
|
||||||
pub fn foo4() {
|
pub fn foo4() {
|
||||||
let x: Vec<AnotherTrait2> = Vec::new();
|
let x: Vec<AnotherTrait2> = Vec::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @has - '//a[@href="../../foo/primitive.bool.html"]' 'bool'
|
// @has - '//pre[@class="rust"]//a[@href="../../foo/primitive.bool.html"]' 'bool'
|
||||||
#[doc(primitive = "bool")]
|
#[doc(primitive = "bool")]
|
||||||
mod whatever {}
|
mod whatever {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue