Skip to content

Commit

Permalink
Rustdoc accessibility: use real headers for doc items
Browse files Browse the repository at this point in the history
This is a pretty annoying workaround: the specifications literally
contradict each other on the matter of whether headers may be nested
inside of summaries. We need the headers for people who use outline
mode to browse rustdoc, mostly in screen readers, and the headers
need to be inside the summary tag so that they're visible when the
details tag is collapsed.

HTML5 says it's okay, but the ordinary button semantics in ARIA say
that headers nested in buttons are ignored, and summary is a button.
Which means some screen readers, like NVDA, work, while others,
like JAWS, don't.
  • Loading branch information
notriddle committed Jul 22, 2021
1 parent e742158 commit 458e721
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,15 +1350,15 @@ fn render_impl(
);
render_rightside(w, cx, item, containing_item);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
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>");
w.write_str("</div>");
}
}
Expand All @@ -1371,7 +1371,7 @@ fn render_impl(
id, item_type, in_trait_class
);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("<code>");
w.write_str("<h4 class=\"code-header\">");
assoc_type(
w,
item,
Expand All @@ -1381,7 +1381,7 @@ fn render_impl(
"",
cx,
);
w.write_str("</code>");
w.write_str("</h4>");
w.write_str("</div>");
}
clean::AssocConstItem(ref ty, ref default) => {
Expand All @@ -1394,7 +1394,7 @@ fn render_impl(
);
render_rightside(w, cx, item, containing_item);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("<code>");
w.write_str("<h4 class=\"code-header\">");
assoc_const(
w,
item,
Expand All @@ -1404,15 +1404,15 @@ fn render_impl(
"",
cx,
);
w.write_str("</code>");
w.write_str("</h4>");
w.write_str("</div>");
}
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=\"{}{}\">", id, item_type, in_trait_class,);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
w.write_str("<code>");
w.write_str("<h4 class=\"code-header\">");
assoc_type(
w,
item,
Expand All @@ -1422,7 +1422,7 @@ fn render_impl(
"",
cx,
);
w.write_str("</code>");
w.write_str("</h4>");
w.write_str("</div>");
}
clean::StrippedItem(..) => return,
Expand Down Expand Up @@ -1613,7 +1613,7 @@ pub(crate) fn render_impl_summary(
write!(w, "<div id=\"{}\" class=\"impl has-srclink\"{}>", id, aliases);
render_rightside(w, cx, &i.impl_item, containing_item);
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
write!(w, "<code class=\"in-band\">");
write!(w, "<h3 class=\"code-header in-band\">");

if let Some(use_absolute) = use_absolute {
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
Expand All @@ -1629,7 +1629,7 @@ pub(crate) fn render_impl_summary(
} else {
write!(w, "{}", i.inner_impl().print(false, cx));
}
write!(w, "</code>");
write!(w, "</h3>");

let is_trait = i.inner_impl().trait_.is_some();
if is_trait {
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
render_stability_since(w, m, t, cx.tcx());
write_srclink(cx, m, w);
write!(w, "</div>");
write!(w, "<code>");
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>");
w.write_str("</div>");
if toggled {
write!(w, "</summary>");
Expand Down
17 changes: 12 additions & 5 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ h1.fqn > .in-band > a:hover {
h2, h3, h4 {
border-bottom: 1px solid;
}
h3.code-header, h4.code-header {
font-size: 1em;
font-weight: 600;
border: none;
padding: 0;
margin: 0;
}
.impl,
.impl-items .method,
.methods .method,
Expand Down Expand Up @@ -233,7 +240,7 @@ details:not(.rustdoc-toggle) summary {
margin-bottom: .6em;
}

code, pre, a.test-arrow {
code, pre, a.test-arrow, .code-header {
font-family: "Source Code Pro", monospace;
}
.docblock code, .docblock-short code {
Expand Down Expand Up @@ -520,7 +527,7 @@ 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;
}
Expand All @@ -536,7 +543,7 @@ nav.sub {
padding: 0px;
}

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

Expand Down Expand Up @@ -742,7 +749,7 @@ a {
}

.invisible > .srclink,
.method > code + .srclink {
.method > .code-header + .srclink {
position: absolute;
top: 0;
right: 0;
Expand Down Expand Up @@ -1103,7 +1110,7 @@ a.test-arrow:hover{
left: -10px;
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,9 @@ 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) {
Expand Down

0 comments on commit 458e721

Please sign in to comment.