Skip to content
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

Linkify sidebar headings for sibling items #93673

Merged
merged 1 commit into from
Feb 7, 2022
Merged
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
3 changes: 1 addition & 2 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ nav.sub {
}
.block ul, .block li {
padding: 0;
margin: 0;
list-style: none;
}

Expand Down Expand Up @@ -502,8 +503,6 @@ nav.sub {
font-weight: 500;
padding: 0;
margin: 0;
margin-top: 0.5rem;
margin-bottom: 0.25rem;
}

.sidebar-links,
Expand Down
40 changes: 24 additions & 16 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,15 @@ function hideThemeButtonState() {
others.appendChild(div);
}

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

Expand Down Expand Up @@ -607,20 +615,20 @@ function hideThemeButtonState() {

var isModule = hasClass(document.body, "mod");
if (!isModule) {
block("primitive", "Primitive Types");
block("mod", "Modules");
block("macro", "Macros");
block("struct", "Structs");
block("enum", "Enums");
block("union", "Unions");
block("constant", "Constants");
block("static", "Statics");
block("trait", "Traits");
block("fn", "Functions");
block("type", "Type Definitions");
block("foreigntype", "Foreign Types");
block("keyword", "Keywords");
block("traitalias", "Trait Aliases");
block("primitive", "primitives", "Primitive Types");
block("mod", "modules", "Modules");
block("macro", "macros", "Macros");
block("struct", "structs", "Structs");
block("enum", "enums", "Enums");
block("union", "unions", "Unions");
block("constant", "constants", "Constants");
block("static", "static", "Statics");
block("trait", "traits", "Traits");
block("fn", "functions", "Functions");
block("type", "types", "Type Definitions");
block("foreigntype", "foreign-types", "Foreign Types");
block("keyword", "keywords", "Keywords");
block("traitalias", "trait-aliases", "Trait Aliases");
}

// `crates{version}.js` should always be loaded before this script, so we can use
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc-gui/sidebar-mobile.goml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ assert-position: ("#method\.must_use", {"y": 45})
// Check that the bottom-most item on the sidebar menu can be scrolled fully into view.
click: ".sidebar-menu-toggle"
scroll-to: ".block.keyword li:nth-child(1)"
assert-position: (".block.keyword li:nth-child(1)", {"y": 542.96875})
assert-position: (".block.keyword li:nth-child(1)", {"y": 542.234375})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With latest version, you can just check with 542 (which is much better for reading).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(from #93597)

7 changes: 7 additions & 0 deletions src/test/rustdoc-gui/sidebar.goml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@ assert-text: ("#functions + .item-table .item-left > a", "foo")
// Links to trait implementations in the sidebar should not wrap even if they are long.
goto: file://|DOC_PATH|/lib2/struct.HasALongTraitWithParams.html
assert-property: (".sidebar-links a", {"offsetHeight": 29})

// Test that clicking on of the "In <module>" headings in the sidebar links to the
// appropriate anchor in index.html.
goto: file://|DOC_PATH|/test_docs/struct.Foo.html
click: ".block.mod h3 a"
// PAGE: index.html
assert-css: ("#modules", {"background-color": "rgb(253, 255, 211)"})