Skip to content

Commit 569096c

Browse files
committed
rustdoc: use details tag for trait implementors
This switches from JS-generated toggles to using the HTML <details> tag for expanding and collapsing entries in the "Implementors" section.
1 parent 392ba2b commit 569096c

25 files changed

+68
-66
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,7 @@ fn render_impl(
13131313
let cache = cx.cache();
13141314
let traits = &cache.traits;
13151315
let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]);
1316+
let mut close_tags = String::new();
13161317

13171318
if render_mode == RenderMode::Normal {
13181319
let id = cx.derive_id(match i.inner_impl().trait_ {
@@ -1331,7 +1332,12 @@ fn render_impl(
13311332
format!(" aliases=\"{}\"", aliases.join(","))
13321333
};
13331334
if let Some(use_absolute) = use_absolute {
1334-
write!(w, "<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">", id, aliases);
1335+
write!(
1336+
w,
1337+
"<details class=\"rustdoc-toggle implementors-toggle\"><summary><h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">",
1338+
id, aliases
1339+
);
1340+
close_tags.insert_str(0, "</details>");
13351341
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
13361342
if show_def_docs {
13371343
for it in &i.inner_impl().items {
@@ -1354,11 +1360,12 @@ fn render_impl(
13541360
} else {
13551361
write!(
13561362
w,
1357-
"<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">{}</code>",
1363+
"<details class=\"rustdoc-toggle implementors-toggle\"><summary><h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">{}</code>",
13581364
id,
13591365
aliases,
13601366
i.inner_impl().print(false, cx)
13611367
);
1368+
close_tags.insert_str(0, "</details>");
13621369
}
13631370
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
13641371
render_stability_since_raw(
@@ -1370,6 +1377,7 @@ fn render_impl(
13701377
);
13711378
write_srclink(cx, &i.impl_item, w);
13721379
w.write_str("</h3>");
1380+
w.write_str("</summary>");
13731381

13741382
if trait_.is_some() {
13751383
if let Some(portability) = portability(&i.impl_item, Some(parent)) {
@@ -1580,6 +1588,7 @@ fn render_impl(
15801588
}
15811589

15821590
w.write_str("<div class=\"impl-items\">");
1591+
close_tags.insert_str(0, "</div>");
15831592
for trait_item in &i.inner_impl().items {
15841593
doc_impl_item(
15851594
w,
@@ -1650,7 +1659,7 @@ fn render_impl(
16501659
);
16511660
}
16521661
}
1653-
w.write_str("</div>");
1662+
w.write_str(&close_tags);
16541663
}
16551664

16561665
fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {

src/librustdoc/html/static/main.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,31 +1196,18 @@ function hideThemeButtonState() {
11961196
if (!next) {
11971197
return;
11981198
}
1199-
if (hasClass(e, "impl") &&
1200-
(next.getElementsByClassName("method").length > 0 ||
1201-
next.getElementsByClassName("associatedconstant").length > 0)) {
1202-
var newToggle = toggle.cloneNode(true);
1203-
insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
1204-
// In case the option "auto-collapse implementors" is not set to false, we collapse
1205-
// all implementors.
1206-
if (hideImplementors === true && e.parentNode.id === "implementors-list") {
1207-
collapseDocs(newToggle, "hide");
1208-
}
1209-
}
12101199
};
12111200

12121201
onEachLazy(document.getElementsByClassName("method"), func);
12131202
onEachLazy(document.getElementsByClassName("associatedconstant"), func);
1214-
onEachLazy(document.getElementsByClassName("impl"), funcImpl);
12151203
var impl_call = function() {};
1216-
// Large items are hidden by default in the HTML. If the setting overrides that, show 'em.
1217-
if (!hideLargeItemContents) {
1218-
onEachLazy(document.getElementsByTagName("details"), function (e) {
1219-
if (hasClass(e, "type-contents-toggle")) {
1220-
e.open = true;
1221-
}
1222-
});
1223-
}
1204+
onEachLazy(document.getElementsByTagName("details"), function (e) {
1205+
var showLargeItem = !hideLargeItemContents && hasClass(e, "type-contents-toggle");
1206+
var showImplementor = !hideImplementors && hasClass(e, "implementors-toggle");
1207+
if (showLargeItem || showImplementor) {
1208+
e.open = true;
1209+
}
1210+
});
12241211
if (hideMethodDocs === true) {
12251212
impl_call = function(e, newToggle) {
12261213
if (e.id.match(/^impl(?:-\d+)?$/) === null) {

src/librustdoc/html/static/rustdoc.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,10 @@ h4 > .notable-traits {
15611561
left: -10px;
15621562
}
15631563

1564+
.item-list > details.rustdoc-toggle > summary:not(.hideme)::before {
1565+
left: -10px;
1566+
}
1567+
15641568
#all-types {
15651569
margin: 10px;
15661570
}
@@ -1775,14 +1779,16 @@ details.rustdoc-toggle > summary::before {
17751779
font-weight: 300;
17761780
font-size: 0.8em;
17771781
letter-spacing: 1px;
1782+
cursor: pointer;
17781783
}
17791784

17801785
details.rustdoc-toggle > summary.hideme::before {
17811786
position: relative;
17821787
}
17831788

17841789
details.rustdoc-toggle > summary:not(.hideme)::before {
1785-
float: left;
1790+
position: absolute;
1791+
left: -23px;
17861792
}
17871793

17881794
/* When a "hideme" summary is open and the "Expand description" or "Show

src/test/rustdoc/const-generics/add-impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct Simd<T, const WIDTH: usize> {
88
inner: T,
99
}
1010

11-
// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]/h3/code' 'impl Add<Simd<u8, 16_usize>> for Simd<u8, 16>'
11+
// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]//h3/code' 'impl Add<Simd<u8, 16_usize>> for Simd<u8, 16>'
1212
impl Add for Simd<u8, 16> {
1313
type Output = Self;
1414

src/test/rustdoc/duplicate_impls/issue-33054.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @has issue_33054/impls/struct.Foo.html
22
// @has - '//code' 'impl Foo'
33
// @has - '//code' 'impl Bar for Foo'
4-
// @count - '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
5-
// @count - '//*[@id="main"]/*[@class="impl"]' 1
4+
// @count - '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
5+
// @count - '//*[@id="main"]/details/summary/*[@class="impl"]' 1
66
// @has issue_33054/impls/bar/trait.Bar.html
77
// @has - '//code' 'impl Bar for Foo'
88
// @count - '//*[@class="struct"]' 1

src/test/rustdoc/issue-21474.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ mod inner {
77
pub trait Blah { }
88

99
// @count issue_21474/struct.What.html \
10-
// '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
10+
// '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
1111
pub struct What;

src/test/rustdoc/issue-29503.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub trait MyTrait {
55
fn my_string(&self) -> String;
66
}
77

8-
// @has - "//div[@id='implementors-list']/h3[@id='impl-MyTrait']//code" "impl<T> MyTrait for T where T: Debug"
8+
// @has - "//div[@id='implementors-list']//h3[@id='impl-MyTrait']//code" "impl<T> MyTrait for T where T: Debug"
99
impl<T> MyTrait for T where T: fmt::Debug {
1010
fn my_string(&self) -> String {
1111
format!("{:?}", self)

src/test/rustdoc/issue-45584.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ pub trait Bar<T, U> {}
44

55
// @has 'foo/struct.Foo1.html'
66
pub struct Foo1;
7-
// @count - '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
7+
// @count - '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
88
// @has - '//*[@class="impl"]' "impl Bar<Foo1, &'static Foo1> for Foo1"
99
impl Bar<Foo1, &'static Foo1> for Foo1 {}
1010

1111
// @has 'foo/struct.Foo2.html'
1212
pub struct Foo2;
13-
// @count - '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
13+
// @count - '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
1414
// @has - '//*[@class="impl"]' "impl Bar<&'static Foo2, Foo2> for u8"
1515
impl Bar<&'static Foo2, Foo2> for u8 {}

src/test/rustdoc/issue-50159.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ impl<B, C> Signal2 for B where B: Signal<Item = C> {
1313
// @has issue_50159/struct.Switch.html
1414
// @has - '//code' 'impl<B> Send for Switch<B> where <B as Signal>::Item: Send'
1515
// @has - '//code' 'impl<B> Sync for Switch<B> where <B as Signal>::Item: Sync'
16-
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 0
17-
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 5
16+
// @count - '//*[@id="implementations-list"]//*[@class="impl"]' 0
17+
// @count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 5
1818
pub struct Switch<B: Signal> {
1919
pub inner: <B as Signal2>::Item2,
2020
}

src/test/rustdoc/issue-51236.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod traits {
77
}
88

99
// @has issue_51236/struct.Owned.html
10-
// @has - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]//code' "impl<T> Send for \
10+
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//code' "impl<T> Send for \
1111
// Owned<T> where <T as Owned<'static>>::Reader: Send"
1212
pub struct Owned<T> where T: for<'a> ::traits::Owned<'a> {
1313
marker: PhantomData<<T as ::traits::Owned<'static>>::Reader>,

0 commit comments

Comments
 (0)