diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index a7ce2bf9048bf..7b1a61a3ffa45 100644
--- a/src/librustdoc/html/static/js/main.js
+++ b/src/librustdoc/html/static/js/main.js
@@ -1179,8 +1179,10 @@ function preLoadCss(cssUrl) {
onEachLazy(document.querySelectorAll(".toggle > summary:not(.hideme)"), el => {
// @ts-expect-error
+ // Clicking on the summary's contents should not collapse it,
+ // but links within should still fire.
el.addEventListener("click", e => {
- if (e.target.tagName !== "SUMMARY" && e.target.tagName !== "A") {
+ if (!e.target.matches("summary, a, a *")) {
e.preventDefault();
}
});
diff --git a/tests/rustdoc-gui/collapse-trait-impl.goml b/tests/rustdoc-gui/collapse-trait-impl.goml
new file mode 100644
index 0000000000000..4db02e5239b5e
--- /dev/null
+++ b/tests/rustdoc-gui/collapse-trait-impl.goml
@@ -0,0 +1,26 @@
+// Checks that individual trait impls can only be collapsed via clicking directly on the summary element
+
+go-to: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
+assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
+
+// Collapse the trait impl doc. The actual clickable area is outside the element, hence offset.
+click-with-offset: ("summary:has(#trait-impl-link-in-summary)", {"x": -15, "y": 5})
+assert-attribute-false: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
+click-with-offset: ("summary:has(#trait-impl-link-in-summary)", {"x": -15, "y": 5})
+assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
+
+// Clicks on the text should be ignored
+click: "summary:has(#trait-impl-link-in-summary) > .impl"
+assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
+
+// But links should still work
+click: "summary:has(#trait-impl-link-in-summary) a:has(#trait-impl-link-in-summary)"
+assert-window-property-false: ({"pageYOffset": "0"})
+assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
+
+// As well as clicks on elements within links
+go-to: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
+assert-window-property: ({"pageYOffset": "0"})
+click: "#trait-impl-link-in-summary"
+assert-window-property-false: ({"pageYOffset": "0"})
+assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
diff --git a/tests/rustdoc-gui/src/test_docs/lib.rs b/tests/rustdoc-gui/src/test_docs/lib.rs
index bb0015b8f9c43..e8afe8b568727 100644
--- a/tests/rustdoc-gui/src/test_docs/lib.rs
+++ b/tests/rustdoc-gui/src/test_docs/lib.rs
@@ -79,6 +79,7 @@ impl Foo {
pub fn warning2() {}
}
+/// A collapsible trait impl with a link
impl AsRef for Foo {
fn as_ref(&self) -> &str {
"hello"