Skip to content

Commit 5bf18ad

Browse files
authored
Rollup merge of #103296 - GuillaumeGomez:collapse-expand-shortcuts, r=notriddle
+/- shortcut now only expand/collapse, not both Fixes #102772. r? ```@notriddle```
2 parents c6a680e + 8e3b891 commit 5bf18ad

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

src/librustdoc/html/static/js/main.js

+30-29
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,12 @@ function loadCss(cssFileName) {
409409
break;
410410

411411
case "+":
412+
ev.preventDefault();
413+
expandAllDocs();
414+
break;
412415
case "-":
413416
ev.preventDefault();
414-
toggleAllDocs();
417+
collapseAllDocs();
415418
break;
416419

417420
case "?":
@@ -614,45 +617,43 @@ function loadCss(cssFileName) {
614617
sidebarElems.appendChild(ul);
615618
}
616619

620+
function expandAllDocs() {
621+
const innerToggle = document.getElementById(toggleAllDocsId);
622+
removeClass(innerToggle, "will-expand");
623+
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
624+
if (!hasClass(e, "type-contents-toggle")) {
625+
e.open = true;
626+
}
627+
});
628+
innerToggle.title = "collapse all docs";
629+
innerToggle.children[0].innerText = "\u2212"; // "\u2212" is "−" minus sign
630+
}
617631

618-
function labelForToggleButton(sectionIsCollapsed) {
619-
if (sectionIsCollapsed) {
620-
// button will expand the section
621-
return "+";
622-
}
623-
// button will collapse the section
624-
// note that this text is also set in the HTML template in ../render/mod.rs
625-
return "\u2212"; // "\u2212" is "−" minus sign
632+
function collapseAllDocs() {
633+
const innerToggle = document.getElementById(toggleAllDocsId);
634+
addClass(innerToggle, "will-expand");
635+
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
636+
if (e.parentNode.id !== "implementations-list" ||
637+
(!hasClass(e, "implementors-toggle") &&
638+
!hasClass(e, "type-contents-toggle"))
639+
) {
640+
e.open = false;
641+
}
642+
});
643+
innerToggle.title = "expand all docs";
644+
innerToggle.children[0].innerText = "+";
626645
}
627646

628647
function toggleAllDocs() {
629648
const innerToggle = document.getElementById(toggleAllDocsId);
630649
if (!innerToggle) {
631650
return;
632651
}
633-
let sectionIsCollapsed = false;
634652
if (hasClass(innerToggle, "will-expand")) {
635-
removeClass(innerToggle, "will-expand");
636-
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
637-
if (!hasClass(e, "type-contents-toggle")) {
638-
e.open = true;
639-
}
640-
});
641-
innerToggle.title = "collapse all docs";
653+
expandAllDocs();
642654
} else {
643-
addClass(innerToggle, "will-expand");
644-
onEachLazy(document.getElementsByClassName("rustdoc-toggle"), e => {
645-
if (e.parentNode.id !== "implementations-list" ||
646-
(!hasClass(e, "implementors-toggle") &&
647-
!hasClass(e, "type-contents-toggle"))
648-
) {
649-
e.open = false;
650-
}
651-
});
652-
sectionIsCollapsed = true;
653-
innerToggle.title = "expand all docs";
655+
collapseAllDocs();
654656
}
655-
innerToggle.children[0].innerText = labelForToggleButton(sectionIsCollapsed);
656657
}
657658

658659
(function() {

src/test/rustdoc-gui/shortcuts.goml

+18
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,21 @@ press-key: "?"
1111
assert-css: ("#help-button .popover", {"display": "block"})
1212
press-key: "Escape"
1313
assert-css: ("#help-button .popover", {"display": "none"})
14+
// Checking doc collapse and expand.
15+
// It should be displaying a "-":
16+
assert-text: ("#toggle-all-docs", "[\u2212]")
17+
press-key: "-"
18+
wait-for-text: ("#toggle-all-docs", "[+]")
19+
assert-attribute: ("#toggle-all-docs", {"class": "will-expand"})
20+
// Pressing it again shouldn't do anything.
21+
press-key: "-"
22+
assert-text: ("#toggle-all-docs", "[+]")
23+
assert-attribute: ("#toggle-all-docs", {"class": "will-expand"})
24+
// Expanding now.
25+
press-key: "+"
26+
wait-for-text: ("#toggle-all-docs", "[\u2212]")
27+
assert-attribute: ("#toggle-all-docs", {"class": ""})
28+
// Pressing it again shouldn't do anything.
29+
press-key: "+"
30+
assert-text: ("#toggle-all-docs", "[\u2212]")
31+
assert-attribute: ("#toggle-all-docs", {"class": ""})

0 commit comments

Comments
 (0)