Skip to content

Commit 9f8eeca

Browse files
committed
rustdoc: Convert sub-variant toggle to HTML
Instead of creating a JS toggle, this injects details/summary for sub-variants of enums. This also fixes the CSS so that the toggle button does not jump when expanding/collapsing.
1 parent 0cc00c4 commit 9f8eeca

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/librustdoc/html/render/print_item.rs

+2
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
934934

935935
use crate::clean::Variant;
936936
if let clean::VariantItem(Variant::Struct(ref s)) = *variant.kind {
937+
toggle_open(w, "fields");
937938
let variant_id = cx.derive_id(format!(
938939
"{}.{}.fields",
939940
ItemType::Variant,
@@ -967,6 +968,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
967968
}
968969
}
969970
w.write_str("</div></div>");
971+
toggle_close(w);
970972
}
971973
render_stability_since(w, variant, it, cx.tcx());
972974
}

src/librustdoc/html/static/main.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,15 @@ function hideThemeButtonState() {
428428
handleHashes(ev);
429429
}
430430

431+
function openParentDetails(elem) {
432+
while (elem) {
433+
if (elem.tagName === "DETAILS") {
434+
elem.open = true;
435+
}
436+
elem = elem.parentNode;
437+
}
438+
}
439+
431440
function expandSection(id) {
432441
var elem = document.getElementById(id);
433442
if (elem && isHidden(elem)) {
@@ -442,6 +451,8 @@ function hideThemeButtonState() {
442451
// The element is not visible, we need to make it appear!
443452
collapseDocs(collapses[0], "show");
444453
}
454+
// In case this is a sub-variant, toggle the <details> open.
455+
openParentDetails(h3.parentNode);
445456
}
446457
}
447458
}
@@ -2418,7 +2429,7 @@ function hideThemeButtonState() {
24182429
if (hasClass(relatedDoc, "item-info")) {
24192430
relatedDoc = relatedDoc.nextElementSibling;
24202431
}
2421-
if (hasClass(relatedDoc, "docblock") || hasClass(relatedDoc, "sub-variant")) {
2432+
if (hasClass(relatedDoc, "docblock")) {
24222433
if (mode === "toggle") {
24232434
if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
24242435
action = "show";
@@ -2727,8 +2738,6 @@ function hideThemeButtonState() {
27272738
if (hasClass(e, "type-decl")) {
27282739
// We do something special for these
27292740
return;
2730-
} else if (hasClass(e, "sub-variant")) {
2731-
otherMessage = "&nbsp;Show&nbsp;fields";
27322741
} else if (hasClass(e, "non-exhaustive")) {
27332742
otherMessage = "&nbsp;This&nbsp;";
27342743
if (hasClass(e, "non-exhaustive-struct")) {
@@ -2760,7 +2769,6 @@ function hideThemeButtonState() {
27602769
}
27612770

27622771
onEachLazy(document.getElementsByClassName("docblock"), buildToggleWrapper);
2763-
onEachLazy(document.getElementsByClassName("sub-variant"), buildToggleWrapper);
27642772

27652773
autoCollapse(getSettingValue("collapse") === "true");
27662774

src/librustdoc/html/static/rustdoc.css

+3-2
Original file line numberDiff line numberDiff line change
@@ -1031,10 +1031,11 @@ h3 > .collapse-toggle, h4 > .collapse-toggle {
10311031
}
10321032

10331033
.sub-variant, .sub-variant > h3 {
1034-
margin-top: 1px !important;
1034+
margin-top: 0px !important;
1035+
padding-top: 1px;
10351036
}
10361037

1037-
#main > .sub-variant > h3 {
1038+
#main > details > .sub-variant > h3 {
10381039
font-size: 15px;
10391040
margin-left: 25px;
10401041
margin-bottom: 5px;

src/test/rustdoc/item-hide-threshold.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ pub struct PrivStruct {
6262
}
6363

6464
// @has 'item_hide_threshold/enum.Enum.html'
65-
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 0
65+
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
66+
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show fields'
6667
pub enum Enum {
6768
A, B, C,
6869
D {

0 commit comments

Comments
 (0)