diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2022-02-07 14:08:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-07 14:08:35 +0000 |
| commit | 0b6c7fbbba8540cf33a257022324849580316510 (patch) | |
| tree | 15165eac8e21ca6d43118edb28d4f1519b5ba9a8 | |
| parent | f611de074a9ff2dcbabc7e8e88aed65dda10378e (diff) | |
| parent | e27ebb5dff38aad9fbaa3d957820a1bd3c8d3bbf (diff) | |
| download | rust-0b6c7fbbba8540cf33a257022324849580316510.tar.gz rust-0b6c7fbbba8540cf33a257022324849580316510.zip | |
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``
| -rw-r--r-- | src/librustdoc/html/static/css/rustdoc.css | 3 | ||||
| -rw-r--r-- | src/librustdoc/html/static/js/main.js | 40 | ||||
| -rw-r--r-- | src/test/rustdoc-gui/sidebar-mobile.goml | 2 | ||||
| -rw-r--r-- | src/test/rustdoc-gui/sidebar.goml | 7 |
4 files changed, 33 insertions, 19 deletions
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 38ececf5e78..cf3c63d6a8f 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -472,6 +472,7 @@ nav.sub { } .block ul, .block li { padding: 0; + margin: 0; list-style: none; } @@ -502,8 +503,6 @@ nav.sub { font-weight: 500; padding: 0; margin: 0; - margin-top: 0.5rem; - margin-bottom: 0.25rem; } .sidebar-links, diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index cab3c28342d..8e1919f75d6 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -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; @@ -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"); @@ -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 diff --git a/src/test/rustdoc-gui/sidebar-mobile.goml b/src/test/rustdoc-gui/sidebar-mobile.goml index 60bcffe120b..ef588a69f1d 100644 --- a/src/test/rustdoc-gui/sidebar-mobile.goml +++ b/src/test/rustdoc-gui/sidebar-mobile.goml @@ -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}) diff --git a/src/test/rustdoc-gui/sidebar.goml b/src/test/rustdoc-gui/sidebar.goml index ef3a92ad7a6..9505e00512f 100644 --- a/src/test/rustdoc-gui/sidebar.goml +++ b/src/test/rustdoc-gui/sidebar.goml @@ -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)"}) |
