Skip to content

Commit

Permalink
Rustdoc accessibility: use real headers for doc items
Browse files Browse the repository at this point in the history
Part of rust-lang#87059

Partially reverts rust-lang#84703

Heavily modified for beta backport needs.
  • Loading branch information
bors authored and GuillaumeGomez committed Jul 26, 2021
1 parent 963e820 commit 22bbbe8
Show file tree
Hide file tree
Showing 75 changed files with 271 additions and 228 deletions.
33 changes: 16 additions & 17 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
if out.is_empty() {
write!(
&mut out,
"<h3 class=\"notable\">Notable traits for {}</h3>\
"<div class=\"notable\">Notable traits for {}</div>\
<code class=\"content\">",
impl_.for_.print(cx)
);
Expand Down Expand Up @@ -1370,15 +1370,15 @@ fn render_impl(
"<div id=\"{}\" class=\"{}{} has-srclink\">",
id, item_type, in_trait_class,
);
w.write_str("<code>");
w.write_str("<h4 class=\"code-header\">");
render_assoc_item(
w,
item,
link.anchor(source_id.as_ref().unwrap_or(&id)),
ItemType::Impl,
cx,
);
w.write_str("</code>");
w.write_str("</h4>");
render_stability_since_raw(
w,
item.stable_since(tcx).as_deref(),
Expand All @@ -1396,9 +1396,10 @@ fn render_impl(
let id = cx.derive_id(source_id.clone());
write!(
w,
"<div id=\"{}\" class=\"{}{} has-srclink\"><code>",
"<div id=\"{}\" class=\"{}{} has-srclink\">",
id, item_type, in_trait_class
);
w.write_str("<h4 class=\"code-header\">");
assoc_type(
w,
item,
Expand All @@ -1408,7 +1409,7 @@ fn render_impl(
"",
cx,
);
w.write_str("</code>");
w.write_str("</h4>");
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("</div>");
}
Expand All @@ -1417,9 +1418,10 @@ fn render_impl(
let id = cx.derive_id(source_id.clone());
write!(
w,
"<div id=\"{}\" class=\"{}{} has-srclink\"><code>",
"<div id=\"{}\" class=\"{}{} has-srclink\">",
id, item_type, in_trait_class
);
w.write_str("<h4 class=\"code-header\">");
assoc_const(
w,
item,
Expand All @@ -1429,7 +1431,7 @@ fn render_impl(
"",
cx,
);
w.write_str("</code>");
w.write_str("</h4>");
render_stability_since_raw(
w,
item.stable_since(tcx).as_deref(),
Expand All @@ -1444,7 +1446,8 @@ fn render_impl(
clean::AssocTypeItem(ref bounds, ref default) => {
let source_id = format!("{}.{}", item_type, name);
let id = cx.derive_id(source_id.clone());
write!(w, "<div id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class,);
write!(w, "<div id=\"{}\" class=\"{}{}\">", id, item_type, in_trait_class,);
w.write_str("<h4 class=\"code-header\">");
assoc_type(
w,
item,
Expand All @@ -1454,7 +1457,7 @@ fn render_impl(
"",
cx,
);
w.write_str("</code>");
w.write_str("</h4>");
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("</div>");
}
Expand Down Expand Up @@ -1638,12 +1641,8 @@ fn render_impl_summary(
format!(" data-aliases=\"{}\"", aliases.join(","))
};
if let Some(use_absolute) = use_absolute {
write!(
w,
"<div id=\"{}\" class=\"impl has-srclink\"{}>\
<code class=\"in-band\">",
id, aliases
);
write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
write!(w, "<h3 class=\"code-header in-band\">");
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
if show_def_docs {
for it in &i.inner_impl().items {
Expand All @@ -1654,12 +1653,12 @@ fn render_impl_summary(
}
}
}
w.write_str("</code>");
w.write_str("</h3>");
} else {
write!(
w,
"<div id=\"{}\" class=\"impl has-srclink\"{}>\
<code class=\"in-band\">{}</code>",
<h3 class=\"code-header in-band\">{}</h3>",
id,
aliases,
i.inner_impl().print(false, cx)
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,10 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
if toggled {
write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
}
write!(w, "<div id=\"{}\" class=\"method has-srclink\"><code>", id);
write!(w, "<div id=\"{}\" class=\"method has-srclink\">", id);
write!(w, "<h4 class=\"code-header\">");
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx);
w.write_str("</code>");
w.write_str("</h4>");
render_stability_since(w, m, t, cx.tcx());
write_srclink(cx, m, w);
w.write_str("</div>");
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,10 @@ function hideThemeButtonState() {
}
}

var code = document.createElement("code");
var code = document.createElement("h3");
code.innerHTML = struct.text;
addClass(code, "code-header");
addClass(code, "in-band");

onEachLazy(code.getElementsByTagName("a"), function(elem) {
var href = elem.getAttribute("href");
Expand Down
66 changes: 53 additions & 13 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,25 @@ h1.fqn {
h1.fqn > .in-band > a:hover {
text-decoration: underline;
}
h2, h3, h4 {
#main > h2, #main > h3, #main > h4 {
border-bottom: 1px solid;
}
.impl, .method,
.type:not(.container-rustdoc), .associatedconstant,
.associatedtype {
h3.code-header, h4.code-header {
font-size: 1em;
font-weight: 600;
border: none;
padding: 0;
margin: 0;
}
.impl,
.impl-items .method,
.methods .method,
.impl-items .type,
.methods .type,
.impl-items .associatedconstant,
.methods .associatedconstant,
.impl-items .associatedtype,
.methods .associatedtype {
flex-basis: 100%;
font-weight: 600;
margin-top: 16px;
Expand Down Expand Up @@ -190,7 +203,34 @@ summary {
outline: none;
}

code, pre, a.test-arrow {
/* Fix some style changes due to normalize.css 8 */

td,
th {
padding: 0;
}

table {
border-collapse: collapse;
}

button,
input,
optgroup,
select,
textarea {
color: inherit;
font: inherit;
margin: 0;
}

/* end tweaks for normalize.css 8 */

details:not(.rustdoc-toggle) summary {
margin-bottom: .6em;
}

code, pre, a.test-arrow, .code-header {
font-family: "Source Code Pro", monospace;
}
.docblock code, .docblock-short code {
Expand Down Expand Up @@ -474,9 +514,10 @@ nav.sub {
font-weight: normal;
}

.method > code, .trait-impl > code, .invisible > code {
.method > .code-header, .trait-impl > .code-header, .invisible > .code-header {
max-width: calc(100% - 41px);
display: block;
flex-grow: 1;
}

.invisible {
Expand All @@ -490,7 +531,7 @@ nav.sub {
padding: 0px;
}

.in-band > code {
.in-band > code, .in-band > .code-header {
display: inline-block;
}

Expand Down Expand Up @@ -585,7 +626,7 @@ nav.sub {
.content .item-info {
position: relative;
margin-left: 33px;
margin-top: -13px;
margin-top: -6px;
}

.sub-variant > div > .item-info {
Expand All @@ -596,7 +637,7 @@ nav.sub {
content: '⬑';
font-size: 25px;
position: absolute;
top: -6px;
top: 0px;
left: -19px;
}

Expand Down Expand Up @@ -690,7 +731,7 @@ a {
}

.invisible > .srclink,
.method > code + .srclink {
.method > .code-header + .srclink {
position: absolute;
top: 0;
right: 0;
Expand Down Expand Up @@ -912,7 +953,7 @@ body.blur > :not(#help) {
.impl-items .since, .impl .since, .methods .since {
flex-grow: 0;
padding-left: 12px;
padding-right: 2px;
padding-right: 6px;
position: initial;
}

Expand Down Expand Up @@ -1045,8 +1086,7 @@ a.test-arrow:hover{
display: block;
}


:target > code {
:target > code, :target > .code-header {
opacity: 1;
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/static/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ pre, .rustdoc.source .example-wrap {
color: #708090;
background-color: rgba(255, 236, 164, 0.06);
padding-right: 4px;
border-right: 1px solid #ffb44c;
}

.docblock h1, .docblock h2, .docblock h3, .docblock h4, .docblock h5 {
Expand Down Expand Up @@ -334,7 +333,7 @@ a.test-arrow:hover {
color: #999;
}

:target > code, :target > .in-band {
:target, :target > * {
background: rgba(255, 236, 164, 0.06);
border-right: 3px solid rgba(255, 180, 76, 0.85);
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ a.test-arrow:hover{
color: #999;
}

:target > code, :target > .in-band {
:target, :target > * {
background-color: #494a3d;
border-right: 3px solid #bb7410;
}

pre.compile_fail {
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/static/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,8 @@ a.test-arrow:hover{
color: #999;
}

:target > code, :target > .in-band {
:target, :target > * {
background: #FDFFD3;
border-right: 3px solid #ffb44c;
}

pre.compile_fail {
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions src/test/rustdoc/assoc-consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait Foo {
pub struct Bar;

impl Foo for Bar {
// @has assoc_consts/struct.Bar.html '//code' 'impl Foo for Bar'
// @has assoc_consts/struct.Bar.html '//h3[@class="code-header in-band"]' 'impl Foo for Bar'
// @has - '//*[@id="associatedconstant.FOO"]' 'const FOO: usize'
const FOO: usize = 12;
// @has - '//*[@id="associatedconstant.FOO_NO_DEFAULT"]' 'const FOO_NO_DEFAULT: bool'
Expand Down Expand Up @@ -77,7 +77,7 @@ pub trait Qux {
const QUX_DEFAULT2: u32 = 3;
}

// @has assoc_consts/struct.Bar.html '//code' 'impl Qux for Bar'
// @has assoc_consts/struct.Bar.html '//h3[@class="code-header in-band"]' 'impl Qux for Bar'
impl Qux for Bar {
// @has - '//*[@id="associatedconstant.QUX0"]' 'const QUX0: u8'
// @has - '//*[@class="docblock"]' "Docs for QUX0 in trait."
Expand Down
6 changes: 3 additions & 3 deletions src/test/rustdoc/assoc-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

// @has assoc_types/trait.Index.html
pub trait Index<I: ?Sized> {
// @has - '//*[@id="associatedtype.Output"]//code' 'type Output: ?Sized'
// @has - '//*[@id="associatedtype.Output"]//h4[@class="code-header"]' 'type Output: ?Sized'
type Output: ?Sized;
// @has - '//*[@id="tymethod.index"]//code' \
// @has - '//*[@id="tymethod.index"]//h4[@class="code-header"]' \
// "fn index<'a>(&'a self, index: I) -> &'a Self::Output"
// @has - '//*[@id="tymethod.index"]//code//a[@href="trait.Index.html#associatedtype.Output"]' \
// @has - '//*[@id="tymethod.index"]//h4[@class="code-header"]//a[@href="trait.Index.html#associatedtype.Output"]' \
// "Output"
fn index<'a>(&'a self, index: I) -> &'a Self::Output;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/rustdoc/async-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub async fn quux() -> impl Bar {
}

// @has async_fn/struct.Foo.html
// @matches - '//code' 'pub async fn f\(\)$'
// @matches - '//code' 'pub async unsafe fn g\(\)$'
// @matches - '//code' 'pub async fn mut_self\(self, first: usize\)$'
// @matches - '//h4[@class="code-header"]' 'pub async fn f\(\)$'
// @matches - '//h4[@class="code-header"]' 'pub async unsafe fn g\(\)$'
// @matches - '//h4[@class="code-header"]' 'pub async fn mut_self\(self, first: usize\)$'
pub struct Foo;

impl Foo {
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc/blanket-reexport-item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![crate_name = "foo"]

// @has foo/struct.S.html '//div[@id="impl-Into%3CU%3E"]//code' 'impl<T, U> Into<U> for T'
// @has foo/struct.S.html '//div[@id="impl-Into%3CU%3E"]//h3[@class="code-header in-band"]' 'impl<T, U> Into<U> for T'
pub struct S2 {}
mod m {
pub struct S {}
Expand Down
8 changes: 6 additions & 2 deletions src/test/rustdoc/const-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ pub const unsafe fn bar_not_gated() -> u32 { 42 }
pub struct Foo;

impl Foo {
// @has 'foo/struct.Foo.html' '//div[@id="method.gated"]/code' 'pub unsafe fn gated() -> u32'
// @has 'foo/struct.Foo.html' '//div[@id="method.gated"]/h4[@class="code-header"]' 'pub fn gated() -> u32'
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo", issue = "none")]
pub const fn gated() -> u32 { 42 }

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo", issue = "none")]
pub const unsafe fn gated() -> u32 { 42 }

// @has 'foo/struct.Foo.html' '//div[@id="method.stable_impl"]/code' 'pub const fn stable_impl() -> u32'
// @has 'foo/struct.Foo.html' '//div[@id="method.stable_impl"]/h4[@class="code-header"]' 'pub const fn stable_impl() -> u32'
// @has - '//span[@class="since"]' '1.0.0 (const: 1.2.0)'
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "rust1", since = "1.2.0")]
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc/const-generics/add-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Simd<T, const WIDTH: usize> {
inner: T,
}

// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]//div/code' 'impl Add<Simd<u8, 16_usize>> for Simd<u8, 16>'
// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]//div/h3[@class="code-header in-band"]' 'impl Add<Simd<u8, 16_usize>> for Simd<u8, 16>'
impl Add for Simd<u8, 16> {
type Output = Self;

Expand Down
Loading

0 comments on commit 22bbbe8

Please sign in to comment.