Skip to content

Commit 981bbf4

Browse files
authored
Rollup merge of #140868 - SpecificProtagonist:rustdoc-trait-impl-code-link, r=notriddle
rustdoc: Fix links with inline code in trait impl docs Fixes #140857
2 parents cc87ae8 + 4516a5b commit 981bbf4

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/librustdoc/html/static/js/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,8 +1179,10 @@ function preLoadCss(cssUrl) {
11791179

11801180
onEachLazy(document.querySelectorAll(".toggle > summary:not(.hideme)"), el => {
11811181
// @ts-expect-error
1182+
// Clicking on the summary's contents should not collapse it,
1183+
// but links within should still fire.
11821184
el.addEventListener("click", e => {
1183-
if (e.target.tagName !== "SUMMARY" && e.target.tagName !== "A") {
1185+
if (!e.target.matches("summary, a, a *")) {
11841186
e.preventDefault();
11851187
}
11861188
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Checks that individual trait impls can only be collapsed via clicking directly on the summary element
2+
3+
go-to: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
4+
assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
5+
6+
// Collapse the trait impl doc. The actual clickable area is outside the element, hence offset.
7+
click-with-offset: ("summary:has(#trait-impl-link-in-summary)", {"x": -15, "y": 5})
8+
assert-attribute-false: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
9+
click-with-offset: ("summary:has(#trait-impl-link-in-summary)", {"x": -15, "y": 5})
10+
assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
11+
12+
// Clicks on the text should be ignored
13+
click: "summary:has(#trait-impl-link-in-summary) > .impl"
14+
assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
15+
16+
// But links should still work
17+
click: "summary:has(#trait-impl-link-in-summary) a:has(#trait-impl-link-in-summary)"
18+
assert-window-property-false: ({"pageYOffset": "0"})
19+
assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})
20+
21+
// As well as clicks on elements within links
22+
go-to: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
23+
assert-window-property: ({"pageYOffset": "0"})
24+
click: "#trait-impl-link-in-summary"
25+
assert-window-property-false: ({"pageYOffset": "0"})
26+
assert-attribute: ("details:has(#trait-impl-link-in-summary)", {"open": ""})

tests/rustdoc-gui/src/test_docs/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl Foo {
7979
pub fn warning2() {}
8080
}
8181

82+
/// <a href="#implementations"><code id="trait-impl-link-in-summary">A collapsible trait impl with a link</code></a>
8283
impl AsRef<str> for Foo {
8384
fn as_ref(&self) -> &str {
8485
"hello"

0 commit comments

Comments
 (0)