Skip to content

Commit 6ac83e1

Browse files
Rollup merge of rust-lang#85722 - GuillaumeGomez:trait-toggle, r=jsha
Fix trait methods' toggle A `<details>` tag wasn't closed on trait methods, which created broken DOM. I also used this occasion to only generate the toggle in case there is documentation on the method. r? `@jsha`
2 parents ea78d1e + 3bed0be commit 6ac83e1

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,23 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
578578
info!("Documenting {} on {:?}", name, t.name);
579579
let item_type = m.type_();
580580
let id = cx.derive_id(format!("{}.{}", item_type, name));
581-
write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
582-
write!(w, "<h3 id=\"{id}\" class=\"method\"><code>", id = id,);
581+
let mut content = Buffer::empty_from(w);
582+
document(&mut content, cx, m, Some(t));
583+
let toggled = !content.is_empty();
584+
if toggled {
585+
write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
586+
}
587+
write!(w, "<h3 id=\"{id}\" class=\"method\"><code>", id = id);
583588
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx);
584589
w.write_str("</code>");
585590
render_stability_since(w, m, t, cx.tcx());
586591
write_srclink(cx, m, w);
587-
w.write_str("</h3></summary>");
588-
document(w, cx, m, Some(t));
592+
w.write_str("</h3>");
593+
if toggled {
594+
write!(w, "</summary>");
595+
w.push_buffer(content);
596+
write!(w, "</details>");
597+
}
589598
}
590599

591600
if !types.is_empty() {

src/test/rustdoc/toggle-trait-fn.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#![crate_name = "foo"]
22

33
// @has foo/trait.Foo.html
4-
// @has - '//details[@class="rustdoc-toggle"]//code' 'bar'
4+
// @!has - '//details[@class="rustdoc-toggle"]//code' 'bar'
5+
// @has - '//code' 'bar'
6+
// @has - '//details[@class="rustdoc-toggle"]//code' 'foo'
57
pub trait Foo {
68
fn bar() -> ();
9+
/// hello
10+
fn foo();
711
}

0 commit comments

Comments
 (0)