Skip to content

Commit 0b6c7fb

Browse files
authored
Rollup merge of #93673 - jsha:linkify-sidebar-headings, r=GuillaumeGomez
Linkify sidebar headings for sibling items Also adjust CSS so this doesn't produce excess padding/margin. Note: I tried and failed to write a test with browser-UI-test. First I tried to `assert-property: (".block.mod h3 a", {"href": "index.html#macros"})`. But the `href` that gets read out is the fully-quallified URL, starting with `file:///`. That URL will differ depending on what path the test is run from, so that doesn't work. Next I tried clicking on the appropriate sidebar link, and verifying that the appropriate heading on the next page is highlighted with the right background color. However, that also didn't work: according to browser-UI-test, the targeted heading was plain white. However, running with no-headless, I could see that it actually was yellow. I suspect this is a bug in the older version of Chromium used with browser-UI-test's bundled puppeteer, since it doesn't reproduce on latest Chrome. Fixes #92957 Demo: https://rustdoc.crud.net/jsha/linkify-sidebar-headings/std/string/trait.ToString.html r? ``@GuillaumeGomez``
2 parents f611de0 + e27ebb5 commit 0b6c7fb

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

src/librustdoc/html/static/css/rustdoc.css

+1-2
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ nav.sub {
472472
}
473473
.block ul, .block li {
474474
padding: 0;
475+
margin: 0;
475476
list-style: none;
476477
}
477478

@@ -502,8 +503,6 @@ nav.sub {
502503
font-weight: 500;
503504
padding: 0;
504505
margin: 0;
505-
margin-top: 0.5rem;
506-
margin-bottom: 0.25rem;
507506
}
508507

509508
.sidebar-links,

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

+24-16
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,15 @@ function hideThemeButtonState() {
559559
others.appendChild(div);
560560
}
561561

562-
function block(shortty, longty) {
562+
/**
563+
* Append to the sidebar a "block" of links - a heading along with a list (`<ul>`) of items.
564+
*
565+
* @param {string} shortty - A short type name, like "primitive", "mod", or "macro"
566+
* @param {string} id - The HTML id of the corresponding section on the module page.
567+
* @param {string} longty - A long, capitalized, plural name, like "Primitive Types",
568+
* "Modules", or "Macros".
569+
*/
570+
function block(shortty, id, longty) {
563571
var filtered = items[shortty];
564572
if (!filtered) {
565573
return;
@@ -568,7 +576,7 @@ function hideThemeButtonState() {
568576
var div = document.createElement("div");
569577
div.className = "block " + shortty;
570578
var h3 = document.createElement("h3");
571-
h3.textContent = longty;
579+
h3.innerHTML = `<a href="index.html#${id}">${longty}</a>`;
572580
div.appendChild(h3);
573581
var ul = document.createElement("ul");
574582

@@ -607,20 +615,20 @@ function hideThemeButtonState() {
607615

608616
var isModule = hasClass(document.body, "mod");
609617
if (!isModule) {
610-
block("primitive", "Primitive Types");
611-
block("mod", "Modules");
612-
block("macro", "Macros");
613-
block("struct", "Structs");
614-
block("enum", "Enums");
615-
block("union", "Unions");
616-
block("constant", "Constants");
617-
block("static", "Statics");
618-
block("trait", "Traits");
619-
block("fn", "Functions");
620-
block("type", "Type Definitions");
621-
block("foreigntype", "Foreign Types");
622-
block("keyword", "Keywords");
623-
block("traitalias", "Trait Aliases");
618+
block("primitive", "primitives", "Primitive Types");
619+
block("mod", "modules", "Modules");
620+
block("macro", "macros", "Macros");
621+
block("struct", "structs", "Structs");
622+
block("enum", "enums", "Enums");
623+
block("union", "unions", "Unions");
624+
block("constant", "constants", "Constants");
625+
block("static", "static", "Statics");
626+
block("trait", "traits", "Traits");
627+
block("fn", "functions", "Functions");
628+
block("type", "types", "Type Definitions");
629+
block("foreigntype", "foreign-types", "Foreign Types");
630+
block("keyword", "keywords", "Keywords");
631+
block("traitalias", "trait-aliases", "Trait Aliases");
624632
}
625633

626634
// `crates{version}.js` should always be loaded before this script, so we can use

src/test/rustdoc-gui/sidebar-mobile.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ assert-position: ("#method\.must_use", {"y": 45})
3939
// Check that the bottom-most item on the sidebar menu can be scrolled fully into view.
4040
click: ".sidebar-menu-toggle"
4141
scroll-to: ".block.keyword li:nth-child(1)"
42-
assert-position: (".block.keyword li:nth-child(1)", {"y": 542.96875})
42+
assert-position: (".block.keyword li:nth-child(1)", {"y": 542.234375})

src/test/rustdoc-gui/sidebar.goml

+7
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ assert-text: ("#functions + .item-table .item-left > a", "foo")
7878
// Links to trait implementations in the sidebar should not wrap even if they are long.
7979
goto: file://|DOC_PATH|/lib2/struct.HasALongTraitWithParams.html
8080
assert-property: (".sidebar-links a", {"offsetHeight": 29})
81+
82+
// Test that clicking on of the "In <module>" headings in the sidebar links to the
83+
// appropriate anchor in index.html.
84+
goto: file://|DOC_PATH|/test_docs/struct.Foo.html
85+
click: ".block.mod h3 a"
86+
// PAGE: index.html
87+
assert-css: ("#modules", {"background-color": "rgb(253, 255, 211)"})

0 commit comments

Comments
 (0)