about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-05-22 07:19:01 +0200
committerGitHub <noreply@github.com>2025-05-22 07:19:01 +0200
commit981bbf41974a0f1aa4e2dfed1ea0955906d592a2 (patch)
tree415c5cac7def21b2c72bc45e1ef97ff961ba4db8
parentcc87ae85dd689abeba40efa1a7761300e14908f4 (diff)
parent4516a5b96bbf8ba39dd74913aac7297c0c0f2ab6 (diff)
downloadrust-981bbf41974a0f1aa4e2dfed1ea0955906d592a2.tar.gz
rust-981bbf41974a0f1aa4e2dfed1ea0955906d592a2.zip
Rollup merge of #140868 - SpecificProtagonist:rustdoc-trait-impl-code-link, r=notriddle
rustdoc: Fix links with inline code in trait impl docs

Fixes #140857
-rw-r--r--src/librustdoc/html/static/js/main.js4
-rw-r--r--tests/rustdoc-gui/collapse-trait-impl.goml26
-rw-r--r--tests/rustdoc-gui/src/test_docs/lib.rs1
3 files changed, 30 insertions, 1 deletions
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js
index a7ce2bf9048..7b1a61a3ffa 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 00000000000..4db02e5239b
--- /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 bb0015b8f9c..e8afe8b5687 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 href="#implementations"><code id="trait-impl-link-in-summary">A collapsible trait impl with a link</code></a>
 impl AsRef<str> for Foo {
     fn as_ref(&self) -> &str {
         "hello"