Skip to content

Commit

Permalink
Rollup merge of rust-lang#103296 - GuillaumeGomez:collapse-expand-sho…
Browse files Browse the repository at this point in the history
…rtcuts, r=notriddle

+/- shortcut now only expand/collapse, not both

Fixes rust-lang#102772.

r? ```@notriddle```
  • Loading branch information
matthiaskrgr committed Oct 20, 2022
2 parents c6a680e + 8e3b891 commit 5bf18ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
59 changes: 30 additions & 29 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,12 @@ function loadCss(cssFileName) {
break;

case "+":
ev.preventDefault();
expandAllDocs();
break;
case "-":
ev.preventDefault();
toggleAllDocs();
collapseAllDocs();
break;

case "?":
Expand Down Expand Up @@ -614,45 +617,43 @@ function loadCss(cssFileName) {
sidebarElems.appendChild(ul);
}

function expandAllDocs() {
const innerToggle = document.getElementById(toggleAllDocsId);
removeClass(innerToggle, "will-expand");
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
if (!hasClass(e, "type-contents-toggle")) {
e.open = true;
}
});
innerToggle.title = "collapse all docs";
innerToggle.children[0].innerText = "\u2212"; // "\u2212" is "−" minus sign
}

function labelForToggleButton(sectionIsCollapsed) {
if (sectionIsCollapsed) {
// button will expand the section
return "+";
}
// button will collapse the section
// note that this text is also set in the HTML template in ../render/mod.rs
return "\u2212"; // "\u2212" is "−" minus sign
function collapseAllDocs() {
const innerToggle = document.getElementById(toggleAllDocsId);
addClass(innerToggle, "will-expand");
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
if (e.parentNode.id !== "implementations-list" ||
(!hasClass(e, "implementors-toggle") &&
!hasClass(e, "type-contents-toggle"))
) {
e.open = false;
}
});
innerToggle.title = "expand all docs";
innerToggle.children[0].innerText = "+";
}

function toggleAllDocs() {
const innerToggle = document.getElementById(toggleAllDocsId);
if (!innerToggle) {
return;
}
let sectionIsCollapsed = false;
if (hasClass(innerToggle, "will-expand")) {
removeClass(innerToggle, "will-expand");
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
if (!hasClass(e, "type-contents-toggle")) {
e.open = true;
}
});
innerToggle.title = "collapse all docs";
expandAllDocs();
} else {
addClass(innerToggle, "will-expand");
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
if (e.parentNode.id !== "implementations-list" ||
(!hasClass(e, "implementors-toggle") &&
!hasClass(e, "type-contents-toggle"))
) {
e.open = false;
}
});
sectionIsCollapsed = true;
innerToggle.title = "expand all docs";
collapseAllDocs();
}
innerToggle.children[0].innerText = labelForToggleButton(sectionIsCollapsed);
}

(function() {
Expand Down
18 changes: 18 additions & 0 deletions src/test/rustdoc-gui/shortcuts.goml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,21 @@ press-key: "?"
assert-css: ("#help-button .popover", {"display": "block"})
press-key: "Escape"
assert-css: ("#help-button .popover", {"display": "none"})
// Checking doc collapse and expand.
// It should be displaying a "-":
assert-text: ("#toggle-all-docs", "[\u2212]")
press-key: "-"
wait-for-text: ("#toggle-all-docs", "[+]")
assert-attribute: ("#toggle-all-docs", {"class": "will-expand"})
// Pressing it again shouldn't do anything.
press-key: "-"
assert-text: ("#toggle-all-docs", "[+]")
assert-attribute: ("#toggle-all-docs", {"class": "will-expand"})
// Expanding now.
press-key: "+"
wait-for-text: ("#toggle-all-docs", "[\u2212]")
assert-attribute: ("#toggle-all-docs", {"class": ""})
// Pressing it again shouldn't do anything.
press-key: "+"
assert-text: ("#toggle-all-docs", "[\u2212]")
assert-attribute: ("#toggle-all-docs", {"class": ""})

0 comments on commit 5bf18ad

Please sign in to comment.