Skip to content

Commit eb5c2d3

Browse files
authored
Rollup merge of #91103 - jsha:non-toggle-click-doesnt-toggle, r=Manishearth,GuillaumeGomez
Inhibit clicks on summary's children A byproduct of using `<details>` and `<summary>` to show/hide detailed documentation was that clicking any part of a method heading (or impl heading) would show or hide the documentation. This was not super noticeable because clicking a link inside the method heading would navigate to that link. But clicking any unlinked black text in a method heading would trigger the behavior. That behavior was somewhat unexpected, and means that if you try to click a type name in a method heading, but miss by a few pixels, you get a confusing surprise. This change inhibits that behavior by putting an event listener on most summaries that cancels the event unless the event target was the summary itself. In practice, that means it cancels the event unless the target was the "[+]" / "[-]", because the rest of the heading is wrapped inside a `<div>`, which is the target for anything that doesn't have a more specific target. r? ``@Manishearth``
2 parents 68a44c8 + 7f35556 commit eb5c2d3

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,14 @@ function hideThemeButtonState() {
886886
}
887887
});
888888

889+
onEachLazy(document.querySelectorAll(".rustdoc-toggle > summary:not(.hideme)"), function(el) {
890+
el.addEventListener("click", function(e) {
891+
if (e.target.tagName != "SUMMARY") {
892+
e.preventDefault();
893+
}
894+
});
895+
});
896+
889897
onEachLazy(document.getElementsByClassName("notable-traits"), function(e) {
890898
e.onclick = function() {
891899
this.getElementsByClassName('notable-traits-tooltiptext')[0]

src/test/rustdoc-gui/src/lib2/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct Foo {
2222
}
2323

2424
impl Foo {
25+
/// Some documentation
26+
/// # A Heading
2527
pub fn a_method(&self) {}
2628
}
2729

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This test ensures that clicking on a method summary, but not on the "[-]",
2+
// doesn't toggle the <details>.
3+
goto: file://|DOC_PATH|/test_docs/struct.Foo.html
4+
assert-attribute: (".impl-items .rustdoc-toggle", {"open": ""})
5+
click: "h4.code-header" // This is the position of "pub" in "pub fn a_method"
6+
assert-attribute: (".impl-items .rustdoc-toggle", {"open": ""})
7+
click: ".impl-items .rustdoc-toggle summary::before" // This is the position of "[-]" next to that pub fn.
8+
assert-attribute-false: (".impl-items .rustdoc-toggle", {"open": ""})

0 commit comments

Comments
 (0)