Skip to content

Commit

Permalink
Rollup merge of rust-lang#98773 - notriddle:notriddle/source-sidebar-…
Browse files Browse the repository at this point in the history
…details, r=GuillaumeGomez

rustdoc: use <details> tag for the source code sidebar

This fixes the extremely poor accessibility of the old system, making it possible to navigate the sidebar by keyboard, and also implicitly gives the sidebar items the correct ARIA roles.

Split out separately from rust-lang#98772
  • Loading branch information
RalfJung authored Jul 3, 2022
2 parents b0d831a + e710ac1 commit 87df0f1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 86 deletions.
37 changes: 11 additions & 26 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1578,38 +1578,23 @@ kbd {
margin-bottom: 1em;
}

div.children {
padding-left: 27px;
display: none;
details.dir-entry {
padding-left: 4px;
}
div.name {

details.dir-entry > summary {
margin: 0 0 0 13px;
list-style-position: outside;
cursor: pointer;
position: relative;
margin-left: 16px;
}
div.files > a {
display: block;
padding: 0 3px;
}
div.files > a:hover, div.name:hover {
background-color: #a14b4b;

details.dir-entry div.folders, details.dir-entry div.files {
padding-left: 23px;
}
div.name.expand + .children {

details.dir-entry a {
display: block;
}
div.name::before {
content: "\25B6";
padding-left: 4px;
font-size: 0.625rem;
position: absolute;
left: -16px;
top: 4px;
}
div.name.expand::before {
transform: rotate(90deg);
left: -15px;
top: 2px;
}

/* The hideme class is used on summary tags that contain a span with
placeholder text shown only when the toggle is closed. For instance,
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/html/static/css/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,12 @@ kbd {
color: #fff;
border-bottom-color: #5c6773;
}
#source-sidebar div.files > a:hover, div.name:hover {
#source-sidebar div.files > a:hover, details.dir-entry summary:hover,
#source-sidebar div.files > a:focus, details.dir-entry summary:focus {
background-color: #14191f;
color: #ffb44c;
}
#source-sidebar div.files > .selected {
#source-sidebar div.files > a.selected {
background-color: #14191f;
color: #ffb44c;
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/html/static/css/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,11 @@ kbd {
#source-sidebar > .title {
border-bottom-color: #ccc;
}
#source-sidebar div.files > a:hover, div.name:hover {
#source-sidebar div.files > a:hover, details.dir-entry summary:hover,
#source-sidebar div.files > a:focus, details.dir-entry summary:focus {
background-color: #444;
}
#source-sidebar div.files > .selected {
#source-sidebar div.files > a.selected {
background-color: #333;
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/static/css/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,13 @@ kbd {
#source-sidebar > .title {
border-bottom-color: #ccc;
}
#source-sidebar div.files > a:hover, div.name:hover {
#source-sidebar div.files > a:hover, details.dir-entry summary:hover,
#source-sidebar div.files > a:focus, details.dir-entry summary:focus {
background-color: #E0E0E0;
}
#source-sidebar div.files > .selected {
#source-sidebar div.files > a.selected {
background-color: #fff;
}

.scraped-example-list .scrape-help {
border-color: #555;
color: #333;
Expand Down
31 changes: 12 additions & 19 deletions src/librustdoc/html/static/js/source-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* global sourcesIndex */

// Local js definitions:
/* global addClass, getCurrentValue, hasClass, onEachLazy, removeClass, browserSupportsHistoryApi */
/* global addClass, getCurrentValue, onEachLazy, removeClass, browserSupportsHistoryApi */
/* global updateLocalStorage */

"use strict";
Expand All @@ -13,33 +13,27 @@ const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-p
let oldScrollPosition = 0;

function createDirEntry(elem, parent, fullPath, hasFoundFile) {
const name = document.createElement("div");
name.className = "name";
const dirEntry = document.createElement("details");
const summary = document.createElement("summary");

dirEntry.className = "dir-entry";

fullPath += elem["name"] + "/";

name.onclick = ev => {
if (hasClass(ev.target, "expand")) {
removeClass(ev.target, "expand");
} else {
addClass(ev.target, "expand");
}
};
name.innerText = elem["name"];
summary.innerText = elem["name"];
dirEntry.appendChild(summary);

const children = document.createElement("div");
children.className = "children";
const folders = document.createElement("div");
folders.className = "folders";
if (elem.dirs) {
for (const dir of elem.dirs) {
if (createDirEntry(dir, folders, fullPath, hasFoundFile)) {
addClass(name, "expand");
dirEntry.open = true;
hasFoundFile = true;
}
}
}
children.appendChild(folders);
dirEntry.appendChild(folders);

const files = document.createElement("div");
files.className = "files";
Expand All @@ -51,15 +45,14 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
const w = window.location.href.split("#")[0];
if (!hasFoundFile && w === file.href) {
file.className = "selected";
addClass(name, "expand");
dirEntry.open = true;
hasFoundFile = true;
}
files.appendChild(file);
}
}
children.appendChild(files);
parent.appendChild(name);
parent.appendChild(children);
dirEntry.appendChild(files);
parent.appendChild(dirEntry);
return hasFoundFile;
}

Expand Down
84 changes: 63 additions & 21 deletions src/test/rustdoc-gui/sidebar-source-code-display.goml
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,43 @@ reload:
// Waiting for the sidebar to be displayed...
wait-for-css: ("#sidebar-toggle", {"visibility": "visible", "opacity": 1})
assert-css: (
"#source-sidebar .expand + .children a.selected",
"#source-sidebar details[open] > .files a.selected",
{"color": "rgb(0, 0, 0)", "background-color": "rgb(255, 255, 255)"},
)
// Without hover.
assert-css: (
"#source-sidebar .expand + .children > .files a:not(.selected)",
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(0, 0, 0)", "background-color": "rgba(0, 0, 0, 0)"},
)
// With focus.
focus: "#source-sidebar details[open] > .files a:not(.selected)"
wait-for-css: (
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(0, 0, 0)", "background-color": "rgb(224, 224, 224)"},
)
focus: ".search-input"
// With hover.
move-cursor-to: "#source-sidebar .expand + .children > .files a:not(.selected)"
move-cursor-to: "#source-sidebar details[open] > .files a:not(.selected)"
assert-css: (
"#source-sidebar .expand + .children > .files a:not(.selected)",
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(0, 0, 0)", "background-color": "rgb(224, 224, 224)"},
)
// Without hover.
assert-css: (
"#source-sidebar .expand + .children .folders .name",
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(0, 0, 0)", "background-color": "rgba(0, 0, 0, 0)"},
)
// With focus.
focus: "#source-sidebar details[open] > .folders > details > summary"
wait-for-css: (
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(0, 0, 0)", "background-color": "rgb(224, 224, 224)"},
)
focus: ".search-input"
// With hover.
move-cursor-to: "#source-sidebar .expand + .children .folders .name"
move-cursor-to: "#source-sidebar details[open] > .folders > details > summary"
assert-css: (
"#source-sidebar .expand + .children .folders .name",
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(0, 0, 0)", "background-color": "rgb(224, 224, 224)"},
)

Expand All @@ -59,29 +73,43 @@ reload:
// Waiting for the sidebar to be displayed...
wait-for-css: ("#sidebar-toggle", {"visibility": "visible", "opacity": 1})
assert-css: (
"#source-sidebar .expand + .children a.selected",
"#source-sidebar details[open] > .files > a.selected",
{"color": "rgb(221, 221, 221)", "background-color": "rgb(51, 51, 51)"},
)
// Without hover.
assert-css: (
"#source-sidebar .expand + .children > .files a:not(.selected)",
"#source-sidebar details[open] > .files > a:not(.selected)",
{"color": "rgb(221, 221, 221)", "background-color": "rgba(0, 0, 0, 0)"},
)
// With focus.
focus: "#source-sidebar details[open] > .files a:not(.selected)"
wait-for-css: (
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(221, 221, 221)", "background-color": "rgb(68, 68, 68)"},
)
focus: ".search-input"
// With hover.
move-cursor-to: "#source-sidebar .expand + .children > .files a:not(.selected)"
move-cursor-to: "#source-sidebar details[open] > .files a:not(.selected)"
assert-css: (
"#source-sidebar .expand + .children > .files a:not(.selected)",
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(221, 221, 221)", "background-color": "rgb(68, 68, 68)"},
)
// Without hover.
assert-css: (
"#source-sidebar .expand + .children .folders .name",
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(221, 221, 221)", "background-color": "rgba(0, 0, 0, 0)"},
)
// With focus.
focus: "#source-sidebar details[open] > .folders > details > summary"
wait-for-css: (
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(221, 221, 221)", "background-color": "rgb(68, 68, 68)"},
)
focus: ".search-input"
// With hover.
move-cursor-to: "#source-sidebar .expand + .children .folders .name"
move-cursor-to: "#source-sidebar details[open] > .folders > details > summary"
assert-css: (
"#source-sidebar .expand + .children .folders .name",
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(221, 221, 221)", "background-color": "rgb(68, 68, 68)"},
)

Expand All @@ -91,29 +119,43 @@ reload:
// Waiting for the sidebar to be displayed...
wait-for-css: ("#sidebar-toggle", {"visibility": "visible", "opacity": 1})
assert-css: (
"#source-sidebar .expand + .children a.selected",
"#source-sidebar details[open] > .files a.selected",
{"color": "rgb(255, 180, 76)", "background-color": "rgb(20, 25, 31)"},
)
// Without hover.
assert-css: (
"#source-sidebar .expand + .children > .files a:not(.selected)",
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(197, 197, 197)", "background-color": "rgba(0, 0, 0, 0)"},
)
// With focus.
focus: "#source-sidebar details[open] > .files a:not(.selected)"
wait-for-css: (
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(255, 180, 76)", "background-color": "rgb(20, 25, 31)"},
)
focus: ".search-input"
// With hover.
move-cursor-to: "#source-sidebar .expand + .children > .files a:not(.selected)"
move-cursor-to: "#source-sidebar details[open] > .files a:not(.selected)"
assert-css: (
"#source-sidebar .expand + .children > .files a:not(.selected)",
"#source-sidebar details[open] > .files a:not(.selected)",
{"color": "rgb(255, 180, 76)", "background-color": "rgb(20, 25, 31)"},
)
// Without hover.
assert-css: (
"#source-sidebar .expand + .children .folders .name",
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(197, 197, 197)", "background-color": "rgba(0, 0, 0, 0)"},
)
// With focus.
focus: "#source-sidebar details[open] > .folders > details > summary"
wait-for-css: (
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(255, 180, 76)", "background-color": "rgb(20, 25, 31)"},
)
focus: ".search-input"
// With hover.
move-cursor-to: "#source-sidebar .expand + .children .folders .name"
move-cursor-to: "#source-sidebar details[open] > .folders > details > summary"
assert-css: (
"#source-sidebar .expand + .children .folders .name",
"#source-sidebar details[open] > .folders > details > summary",
{"color": "rgb(255, 180, 76)", "background-color": "rgb(20, 25, 31)"},
)

Expand Down
23 changes: 10 additions & 13 deletions src/test/rustdoc-gui/source-code-page.goml
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,16 @@ assert-document-property: ({"URL": "/lib.rs.html"}, ENDS_WITH)
click: "#sidebar-toggle"
assert: ".source-sidebar-expanded"

// We check that the first entry of the sidebar is collapsed (which, for whatever reason,
// is number 2 and not 1...).
assert-attribute: ("#source-sidebar .name:nth-child(2)", {"class": "name"})
assert-text: ("#source-sidebar .name:nth-child(2)", "implementors")
// We also check its children are hidden too.
assert-css: ("#source-sidebar .name:nth-child(2) + .children", {"display": "none"})
// We check that the first entry of the sidebar is collapsed
assert-property: ("#source-sidebar details:first-of-type", {"open": "false"})
assert-text: ("#source-sidebar details:first-of-type > summary", "implementors")
// We now click on it.
click: "#source-sidebar .name:nth-child(2)"
assert-attribute: ("#source-sidebar .name:nth-child(2)", {"class": "name expand"})
// Checking that its children are displayed as well.
assert-css: ("#source-sidebar .name:nth-child(2) + .children", {"display": "block"})
click: "#source-sidebar details:first-of-type > summary"
assert-property: ("#source-sidebar details:first-of-type", {"open": "true"})

// And now we collapse it again.
click: "#source-sidebar .name:nth-child(2)"
assert-attribute: ("#source-sidebar .name:nth-child(2)", {"class": "name"})
assert-css: ("#source-sidebar .name:nth-child(2) + .children", {"display": "none"})
click: "#source-sidebar details:first-of-type > summary"
assert-property: ("#source-sidebar details:first-of-type", {"open": "false"})

// Check the spacing.
assert-css: ("#source-sidebar > details.dir-entry", {"padding-left": "4px"})

0 comments on commit 87df0f1

Please sign in to comment.