Skip to content

rustdoc: Fix links with inline code in trait impl docs #140868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Expand Down
26 changes: 26 additions & 0 deletions tests/rustdoc-gui/collapse-trait-impl.goml
Original file line number Diff line number Diff line change
@@ -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": ""})
1 change: 1 addition & 0 deletions tests/rustdoc-gui/src/test_docs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading