Skip to content

Commit 52fc846

Browse files
borsnotriddle
authored andcommitted
Rustdoc accessibility: use real headers for doc items
Part of rust-lang#87059 Partially reverts rust-lang#84703 Backport of rust-lang#87390 Preview at: https://notriddle.com/notriddle-rustdoc-test/real-headers/std/index.html
1 parent c8fb0b5 commit 52fc846

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+218
-210
lines changed

src/librustdoc/html/render/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
12141214
if out.is_empty() {
12151215
write!(
12161216
&mut out,
1217-
"<h3 class=\"notable\">Notable traits for {}</h3>\
1217+
"<div class=\"notable\">Notable traits for {}</div>\
12181218
<code class=\"content\">",
12191219
impl_.for_.print(cx)
12201220
);
@@ -1370,15 +1370,15 @@ fn render_impl(
13701370
"<div id=\"{}\" class=\"{}{} has-srclink\">",
13711371
id, item_type, in_trait_class,
13721372
);
1373-
w.write_str("<code>");
1373+
w.write_str("<h4 class=\"code-header\">");
13741374
render_assoc_item(
13751375
w,
13761376
item,
13771377
link.anchor(source_id.as_ref().unwrap_or(&id)),
13781378
ItemType::Impl,
13791379
cx,
13801380
);
1381-
w.write_str("</code>");
1381+
w.write_str("</h4>");
13821382
render_stability_since_raw(
13831383
w,
13841384
item.stable_since(tcx).as_deref(),
@@ -1396,7 +1396,7 @@ fn render_impl(
13961396
let id = cx.derive_id(source_id.clone());
13971397
write!(
13981398
w,
1399-
"<div id=\"{}\" class=\"{}{} has-srclink\"><code>",
1399+
"<div id=\"{}\" class=\"{}{} has-srclink\"><h4 class=\"code-header\">",
14001400
id, item_type, in_trait_class
14011401
);
14021402
assoc_type(
@@ -1408,7 +1408,7 @@ fn render_impl(
14081408
"",
14091409
cx,
14101410
);
1411-
w.write_str("</code>");
1411+
w.write_str("</h4>");
14121412
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14131413
w.write_str("</div>");
14141414
}
@@ -1417,7 +1417,7 @@ fn render_impl(
14171417
let id = cx.derive_id(source_id.clone());
14181418
write!(
14191419
w,
1420-
"<div id=\"{}\" class=\"{}{} has-srclink\"><code>",
1420+
"<div id=\"{}\" class=\"{}{} has-srclink\"><h4 class=\"code-header\">",
14211421
id, item_type, in_trait_class
14221422
);
14231423
assoc_const(
@@ -1429,7 +1429,7 @@ fn render_impl(
14291429
"",
14301430
cx,
14311431
);
1432-
w.write_str("</code>");
1432+
w.write_str("</h4>");
14331433
render_stability_since_raw(
14341434
w,
14351435
item.stable_since(tcx).as_deref(),
@@ -1444,7 +1444,7 @@ fn render_impl(
14441444
clean::AssocTypeItem(ref bounds, ref default) => {
14451445
let source_id = format!("{}.{}", item_type, name);
14461446
let id = cx.derive_id(source_id.clone());
1447-
write!(w, "<div id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class,);
1447+
write!(w, "<div id=\"{}\" class=\"{}{}\"><h4 class=\"code-header\">", id, item_type, in_trait_class,);
14481448
assoc_type(
14491449
w,
14501450
item,
@@ -1454,7 +1454,7 @@ fn render_impl(
14541454
"",
14551455
cx,
14561456
);
1457-
w.write_str("</code>");
1457+
w.write_str("</h4>");
14581458
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14591459
w.write_str("</div>");
14601460
}
@@ -1641,7 +1641,7 @@ fn render_impl_summary(
16411641
write!(
16421642
w,
16431643
"<div id=\"{}\" class=\"impl has-srclink\"{}>\
1644-
<code class=\"in-band\">",
1644+
<h3 class=\"code-header in-band\">",
16451645
id, aliases
16461646
);
16471647
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
@@ -1654,12 +1654,12 @@ fn render_impl_summary(
16541654
}
16551655
}
16561656
}
1657-
w.write_str("</code>");
1657+
w.write_str("</h3>");
16581658
} else {
16591659
write!(
16601660
w,
16611661
"<div id=\"{}\" class=\"impl has-srclink\"{}>\
1662-
<code class=\"in-band\">{}</code>",
1662+
<h3 class=\"code-header in-band\">{}</h3>",
16631663
id,
16641664
aliases,
16651665
i.inner_impl().print(false, cx)

src/librustdoc/html/render/print_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
585585
if toggled {
586586
write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
587587
}
588-
write!(w, "<div id=\"{}\" class=\"method has-srclink\"><code>", id);
588+
write!(w, "<div id=\"{}\" class=\"method has-srclink\"><h4 class=\"code-header\">", id);
589589
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx);
590-
w.write_str("</code>");
590+
w.write_str("</h4>");
591591
render_stability_since(w, m, t, cx.tcx());
592592
write_srclink(cx, m, w);
593593
w.write_str("</div>");

src/librustdoc/html/static/main.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,10 @@ function hideThemeButtonState() {
703703
}
704704
}
705705

706-
var code = document.createElement("code");
706+
var code = document.createElement("h3");
707707
code.innerHTML = struct.text;
708+
addClass(code, "code-header");
709+
addClass(code, "in-band");
708710

709711
onEachLazy(code.getElementsByTagName("a"), function(elem) {
710712
var href = elem.getAttribute("href");

src/librustdoc/html/static/rustdoc.css

+12-6
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ h1.fqn > .in-band > a:hover {
137137
h2, h3, h4 {
138138
border-bottom: 1px solid;
139139
}
140+
h3.code-header, h4.code-header {
141+
font-size: 1em;
142+
font-weight: 600;
143+
border: none;
144+
padding: 0;
145+
margin: 0;
146+
}
140147
.impl, .method,
141148
.type:not(.container-rustdoc), .associatedconstant,
142149
.associatedtype {
@@ -190,7 +197,7 @@ summary {
190197
outline: none;
191198
}
192199

193-
code, pre, a.test-arrow {
200+
.code-header, code, pre, a.test-arrow {
194201
font-family: "Source Code Pro", monospace;
195202
}
196203
.docblock code, .docblock-short code {
@@ -470,7 +477,7 @@ nav.sub {
470477
font-weight: normal;
471478
}
472479

473-
.method > code, .trait-impl > code, .invisible > code {
480+
.method > .code-header, .trait-impl > .code-header, .invisible > .code-header {
474481
max-width: calc(100% - 41px);
475482
display: block;
476483
}
@@ -486,7 +493,7 @@ nav.sub {
486493
padding: 0px;
487494
}
488495

489-
.in-band > code {
496+
.in-band > code, .in-band > .code-header {
490497
display: inline-block;
491498
}
492499

@@ -686,7 +693,7 @@ a {
686693
}
687694

688695
.invisible > .srclink,
689-
.method > code + .srclink {
696+
.method > .code-header + .srclink {
690697
position: absolute;
691698
top: 0;
692699
right: 0;
@@ -1041,8 +1048,7 @@ a.test-arrow:hover{
10411048
display: block;
10421049
}
10431050

1044-
1045-
:target > code {
1051+
:target > code, :target > .code-header {
10461052
opacity: 1;
10471053
}
10481054

src/librustdoc/html/static/themes/ayu.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ a.test-arrow:hover {
334334
color: #999;
335335
}
336336

337-
:target > code, :target > .in-band {
337+
:target, :target > * {
338338
background: rgba(255, 236, 164, 0.06);
339339
border-right: 3px solid rgba(255, 180, 76, 0.85);
340340
}

src/librustdoc/html/static/themes/dark.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ a.test-arrow:hover{
282282
color: #999;
283283
}
284284

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

src/librustdoc/html/static/themes/light.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ a.test-arrow:hover{
275275
color: #999;
276276
}
277277

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

src/test/rustdoc/assoc-consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub trait Foo {
1313
pub struct Bar;
1414

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

80-
// @has assoc_consts/struct.Bar.html '//code' 'impl Qux for Bar'
80+
// @has assoc_consts/struct.Bar.html '//h3[@class="code-header in-band"]' 'impl Qux for Bar'
8181
impl Qux for Bar {
8282
// @has - '//*[@id="associatedconstant.QUX0"]' 'const QUX0: u8'
8383
// @has - '//*[@class="docblock"]' "Docs for QUX0 in trait."

src/test/rustdoc/assoc-types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
// @has assoc_types/trait.Index.html
44
pub trait Index<I: ?Sized> {
5-
// @has - '//*[@id="associatedtype.Output"]//code' 'type Output: ?Sized'
5+
// @has - '//*[@id="associatedtype.Output"]//h4[@class="code-header"]' 'type Output: ?Sized'
66
type Output: ?Sized;
7-
// @has - '//*[@id="tymethod.index"]//code' \
7+
// @has - '//*[@id="tymethod.index"]//h4[@class="code-header"]' \
88
// "fn index<'a>(&'a self, index: I) -> &'a Self::Output"
9-
// @has - '//*[@id="tymethod.index"]//code//a[@href="trait.Index.html#associatedtype.Output"]' \
9+
// @has - '//*[@id="tymethod.index"]//h4[@class="code-header"]//a[@href="trait.Index.html#associatedtype.Output"]' \
1010
// "Output"
1111
fn index<'a>(&'a self, index: I) -> &'a Self::Output;
1212
}

src/test/rustdoc/async-fn.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ pub async fn quux() -> impl Bar {
3535
}
3636

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

4343
impl Foo {

src/test/rustdoc/blanket-reexport-item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![crate_name = "foo"]
22

3-
// @has foo/struct.S.html '//div[@id="impl-Into%3CU%3E"]//code' 'impl<T, U> Into<U> for T'
3+
// @has foo/struct.S.html '//div[@id="impl-Into%3CU%3E"]//h3[@class="code-header in-band"]' 'impl<T, U> Into<U> for T'
44
pub struct S2 {}
55
mod m {
66
pub struct S {}

src/test/rustdoc/const-display.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ pub const unsafe fn bar_not_gated() -> u32 { 42 }
3838
pub struct Foo;
3939

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

46-
// @has 'foo/struct.Foo.html' '//div[@id="method.stable_impl"]/code' 'pub const fn stable_impl() -> u32'
46+
// @has 'foo/struct.Foo.html' '//div[@id="method.stable_impl"]/h4[@class="code-header"]' 'pub const fn stable_impl() -> u32'
4747
// @has - '//span[@class="since"]' '1.0.0 (const: 1.2.0)'
4848
#[stable(feature = "rust1", since = "1.0.0")]
4949
#[rustc_const_stable(feature = "rust1", since = "1.2.0")]

src/test/rustdoc/const-generics/add-impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct Simd<T, const WIDTH: usize> {
88
inner: T,
99
}
1010

11-
// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]//div/code' 'impl Add<Simd<u8, 16_usize>> for Simd<u8, 16>'
11+
// @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>'
1212
impl Add for Simd<u8, 16> {
1313
type Output = Self;
1414

src/test/rustdoc/const-generics/const-generics-docs.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ pub use extern_crate::WTrait;
1919

2020
// @has foo/trait.Trait.html '//pre[@class="rust trait"]' \
2121
// 'pub trait Trait<const N: usize>'
22-
// @has - '//*[@id="impl-Trait%3C1_usize%3E-for-u8"]//code' 'impl Trait<1_usize> for u8'
23-
// @has - '//*[@id="impl-Trait%3C2_usize%3E-for-u8"]//code' 'impl Trait<2_usize> for u8'
24-
// @has - '//*[@id="impl-Trait%3C{1%20+%202}%3E-for-u8"]//code' 'impl Trait<{1 + 2}> for u8'
25-
// @has - '//*[@id="impl-Trait%3CN%3E-for-%5Bu8%3B%20N%5D"]//code' \
22+
// @has - '//*[@id="impl-Trait%3C1_usize%3E-for-u8"]//h3[@class="code-header in-band"]' 'impl Trait<1_usize> for u8'
23+
// @has - '//*[@id="impl-Trait%3C2_usize%3E-for-u8"]//h3[@class="code-header in-band"]' 'impl Trait<2_usize> for u8'
24+
// @has - '//*[@id="impl-Trait%3C{1%20+%202}%3E-for-u8"]//h3[@class="code-header in-band"]' 'impl Trait<{1 + 2}> for u8'
25+
// @has - '//*[@id="impl-Trait%3CN%3E-for-%5Bu8%3B%20N%5D"]//h3[@class="code-header in-band"]' \
2626
// 'impl<const N: usize> Trait<N> for [u8; N]'
2727
pub trait Trait<const N: usize> {}
2828
impl Trait<1> for u8 {}
@@ -36,7 +36,7 @@ pub struct Foo<const N: usize> where u8: Trait<N>;
3636
// @has foo/struct.Bar.html '//pre[@class="rust struct"]' 'pub struct Bar<T, const N: usize>(_)'
3737
pub struct Bar<T, const N: usize>([T; N]);
3838

39-
// @has foo/struct.Foo.html '//div[@id="impl"]/code' 'impl<const M: usize> Foo<M> where u8: Trait<M>'
39+
// @has foo/struct.Foo.html '//div[@id="impl"]/h3[@class="code-header in-band"]' 'impl<const M: usize> Foo<M> where u8: Trait<M>'
4040
impl<const M: usize> Foo<M> where u8: Trait<M> {
4141
// @has - '//*[@id="associatedconstant.FOO_ASSOC"]' 'pub const FOO_ASSOC: usize'
4242
pub const FOO_ASSOC: usize = M + 13;
@@ -47,7 +47,7 @@ impl<const M: usize> Foo<M> where u8: Trait<M> {
4747
}
4848
}
4949

50-
// @has foo/struct.Bar.html '//div[@id="impl"]/code' 'impl<const M: usize> Bar<u8, M>'
50+
// @has foo/struct.Bar.html '//div[@id="impl"]/h3[@class="code-header in-band"]' 'impl<const M: usize> Bar<u8, M>'
5151
impl<const M: usize> Bar<u8, M> {
5252
// @has - '//*[@id="method.hey"]' \
5353
// 'pub fn hey<const N: usize>(&self) -> Foo<N> where u8: Trait<N>'

src/test/rustdoc/const-generics/const-impl.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ pub enum Order {
99
}
1010

1111
// @has foo/struct.VSet.html '//pre[@class="rust struct"]' 'pub struct VSet<T, const ORDER: Order>'
12-
// @has foo/struct.VSet.html '//div[@id="impl-Send"]/code' 'impl<T, const ORDER: Order> Send for VSet<T, ORDER>'
13-
// @has foo/struct.VSet.html '//div[@id="impl-Sync"]/code' 'impl<T, const ORDER: Order> Sync for VSet<T, ORDER>'
12+
// @has foo/struct.VSet.html '//div[@id="impl-Send"]/h3[@class="code-header in-band"]' 'impl<T, const ORDER: Order> Send for VSet<T, ORDER>'
13+
// @has foo/struct.VSet.html '//div[@id="impl-Sync"]/h3[@class="code-header in-band"]' 'impl<T, const ORDER: Order> Sync for VSet<T, ORDER>'
1414
pub struct VSet<T, const ORDER: Order> {
1515
inner: Vec<T>,
1616
}
1717

18-
// @has foo/struct.VSet.html '//div[@id="impl"]/code' 'impl<T> VSet<T, {Order::Sorted}>'
18+
// @has foo/struct.VSet.html '//div[@id="impl"]/h3[@class="code-header in-band"]' 'impl<T> VSet<T, {Order::Sorted}>'
1919
impl <T> VSet<T, {Order::Sorted}> {
2020
pub fn new() -> Self {
2121
Self { inner: Vec::new() }
2222
}
2323
}
2424

25-
// @has foo/struct.VSet.html '//div[@id="impl-1"]/code' 'impl<T> VSet<T, {Order::Unsorted}>'
25+
// @has foo/struct.VSet.html '//div[@id="impl-1"]/h3[@class="code-header in-band"]' 'impl<T> VSet<T, {Order::Unsorted}>'
2626
impl <T> VSet<T, {Order::Unsorted}> {
2727
pub fn new() -> Self {
2828
Self { inner: Vec::new() }
@@ -31,7 +31,7 @@ impl <T> VSet<T, {Order::Unsorted}> {
3131

3232
pub struct Escape<const S: &'static str>;
3333

34-
// @has foo/struct.Escape.html '//div[@id="impl"]/code' 'impl Escape<{ r#"<script>alert("Escape");</script>"# }>'
34+
// @has foo/struct.Escape.html '//div[@id="impl"]/h3[@class="code-header in-band"]' 'impl Escape<{ r#"<script>alert("Escape");</script>"# }>'
3535
impl Escape<{ r#"<script>alert("Escape");</script>"# }> {
3636
pub fn f() {}
3737
}

src/test/rustdoc/const-generics/lazy_normalization_consts/const-equate-pred.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Hasher<T> {
1212
unsafe impl<T: Default> Send for Hasher<T> {}
1313

1414
// @has foo/struct.Foo.html
15-
// @has - '//code' 'impl Send for Foo'
15+
// @has - '//h3[@class="code-header in-band"]' 'impl Send for Foo'
1616
pub struct Foo {
1717
hasher: Hasher<[u8; 3]>,
1818
}

src/test/rustdoc/const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pub struct Foo;
44

55
impl Foo {
6-
// @has const/struct.Foo.html '//*[@id="method.new"]//code' 'const unsafe fn new'
6+
// @has const/struct.Foo.html '//*[@id="method.new"]//h4[@class="code-header"]' 'const unsafe fn new'
77
pub const unsafe fn new() -> Foo {
88
Foo
99
}

src/test/rustdoc/duplicate_impls/issue-33054.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @has issue_33054/impls/struct.Foo.html
2-
// @has - '//code' 'impl Foo'
3-
// @has - '//code' 'impl Bar for Foo'
2+
// @has - '//h3[@class="code-header in-band"]' 'impl Foo'
3+
// @has - '//h3[@class="code-header in-band"]' 'impl Bar for Foo'
44
// @count - '//*[@id="trait-implementations-list"]//*[@class="impl has-srclink"]' 1
55
// @count - '//*[@id="main"]/details/summary/*[@class="impl has-srclink"]' 1
66
// @has issue_33054/impls/bar/trait.Bar.html
7-
// @has - '//code' 'impl Bar for Foo'
7+
// @has - '//h3[@class="code-header in-band"]' 'impl Bar for Foo'
88
// @count - '//*[@class="struct"]' 1
99
pub mod impls;
1010

0 commit comments

Comments
 (0)