about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-05-27 20:08:17 +0200
committerGitHub <noreply@github.com>2021-05-27 20:08:17 +0200
commit6ac83e1808fd860c8c1279cc6547e6b248bc4b47 (patch)
tree63a7f86ff7e772e364b4b19b6bbb805557568e8a /src
parentea78d1edf364dd3a4b5ff430f76e2bdd3a713a45 (diff)
parent3bed0be9fc7cf77b56404db8291b227385e1550f (diff)
downloadrust-6ac83e1808fd860c8c1279cc6547e6b248bc4b47.tar.gz
rust-6ac83e1808fd860c8c1279cc6547e6b248bc4b47.zip
Rollup merge of #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`
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/html/render/print_item.rs17
-rw-r--r--src/test/rustdoc/toggle-trait-fn.rs6
2 files changed, 18 insertions, 5 deletions
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index 50153ac14a2..e06168c708c 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -578,14 +578,23 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
         info!("Documenting {} on {:?}", name, t.name);
         let item_type = m.type_();
         let id = cx.derive_id(format!("{}.{}", item_type, name));
-        write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
-        write!(w, "<h3 id=\"{id}\" class=\"method\"><code>", id = id,);
+        let mut content = Buffer::empty_from(w);
+        document(&mut content, cx, m, Some(t));
+        let toggled = !content.is_empty();
+        if toggled {
+            write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
+        }
+        write!(w, "<h3 id=\"{id}\" class=\"method\"><code>", id = id);
         render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl, cx);
         w.write_str("</code>");
         render_stability_since(w, m, t, cx.tcx());
         write_srclink(cx, m, w);
-        w.write_str("</h3></summary>");
-        document(w, cx, m, Some(t));
+        w.write_str("</h3>");
+        if toggled {
+            write!(w, "</summary>");
+            w.push_buffer(content);
+            write!(w, "</details>");
+        }
     }
 
     if !types.is_empty() {
diff --git a/src/test/rustdoc/toggle-trait-fn.rs b/src/test/rustdoc/toggle-trait-fn.rs
index a160809cbf9..7fcac78556b 100644
--- a/src/test/rustdoc/toggle-trait-fn.rs
+++ b/src/test/rustdoc/toggle-trait-fn.rs
@@ -1,7 +1,11 @@
 #![crate_name = "foo"]
 
 // @has foo/trait.Foo.html
-// @has - '//details[@class="rustdoc-toggle"]//code' 'bar'
+// @!has - '//details[@class="rustdoc-toggle"]//code' 'bar'
+// @has - '//code' 'bar'
+// @has - '//details[@class="rustdoc-toggle"]//code' 'foo'
 pub trait Foo {
     fn bar() -> ();
+    /// hello
+    fn foo();
 }