From 2e1e6274306bed67291c1eefc7332d0bf972e6ac Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 18 Jul 2024 12:06:21 -0700 Subject: [PATCH 1/2] rustdoc: fix `current` class on sidebar modnav --- src/librustdoc/html/static/js/main.js | 8 +++++--- tests/rustdoc-gui/sidebar.goml | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 64c356607788c..116ce615d8c55 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -529,13 +529,15 @@ function preLoadCss(cssUrl) { } const link = document.createElement("a"); link.href = path; - if (path === current_page) { - link.className = "current"; - } link.textContent = name; const li = document.createElement("li"); li.appendChild(link); ul.appendChild(li); + // Don't "optimize" this to just use `path`. + // We want the browser to normalize this into an absolute URL. + if (link.href === current_page) { + li.classList.add("current"); + } } sidebar.appendChild(h3); sidebar.appendChild(ul); diff --git a/tests/rustdoc-gui/sidebar.goml b/tests/rustdoc-gui/sidebar.goml index 56453517a55a2..e499c159c6c76 100644 --- a/tests/rustdoc-gui/sidebar.goml +++ b/tests/rustdoc-gui/sidebar.goml @@ -72,6 +72,7 @@ click: "#structs + .item-table .item-name > a" assert-count: (".sidebar .sidebar-crate", 1) assert-count: (".sidebar .location", 1) assert-count: (".sidebar h2", 3) +assert-text: (".sidebar-elems ul.block > li.current > a", "Foo") // We check that there is no crate listed outside of the top level. assert-false: ".sidebar-elems > .crate" @@ -110,6 +111,7 @@ click: "#functions + .item-table .item-name > a" assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2") assert-count: (".sidebar .location", 0) assert-count: (".sidebar h2", 1) +assert-text: (".sidebar-elems ul.block > li.current > a", "foobar") // We check that we don't have the crate list. assert-false: ".sidebar-elems > .crate" @@ -118,6 +120,7 @@ assert-property: (".sidebar", {"clientWidth": "200"}) assert-text: (".sidebar > .sidebar-crate > h2 > a", "lib2") assert-text: (".sidebar > .location", "Module module") assert-count: (".sidebar .location", 1) +assert-text: (".sidebar-elems ul.block > li.current > a", "module") // Module page requires three headings: // - Presistent crate branding (name and version) // - Module name, followed by TOC for module headings @@ -138,6 +141,7 @@ assert-text: (".sidebar > .sidebar-elems > h2", "In lib2::module::sub_module") assert-property: (".sidebar > .sidebar-elems > h2 > a", { "href": "/module/sub_module/index.html", }, ENDS_WITH) +assert-text: (".sidebar-elems ul.block > li.current > a", "sub_sub_module") // We check that we don't have the crate list. assert-false: ".sidebar-elems .crate" assert-text: (".sidebar-elems > section ul > li:nth-child(1)", "Functions") From 0c932b763d24a9f57ce3285927ea54308b7be902 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 18 Jul 2024 19:49:32 -0700 Subject: [PATCH 2/2] Rearrange sidebar modnav builder to more logical order --- src/librustdoc/html/static/js/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 116ce615d8c55..9506bc9ed220b 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -531,13 +531,13 @@ function preLoadCss(cssUrl) { link.href = path; link.textContent = name; const li = document.createElement("li"); - li.appendChild(link); - ul.appendChild(li); // Don't "optimize" this to just use `path`. // We want the browser to normalize this into an absolute URL. if (link.href === current_page) { li.classList.add("current"); } + li.appendChild(link); + ul.appendChild(li); } sidebar.appendChild(h3); sidebar.appendChild(ul);