Skip to content

Commit f09bc51

Browse files
authored
Rollup merge of #102898 - notriddle:notriddle/sidebar-block, r=GuillaumeGomez
rustdoc: remove unneeded `<div>` wrapper from sidebar DOM When this was added, the sidebar had a bit more complex style. It can be removed, now. Preview: https://notriddle.com/notriddle-rustdoc-demos/sidebar-block/std/index.html
2 parents 7f08482 + 44f466c commit f09bc51

File tree

7 files changed

+35
-48
lines changed

7 files changed

+35
-48
lines changed

src/librustdoc/html/render/mod.rs

+7-17
Original file line numberDiff line numberDiff line change
@@ -1853,12 +1853,12 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
18531853

18541854
buffer.write_str("<div class=\"sidebar-elems\">");
18551855
if it.is_crate() {
1856-
write!(buffer, "<div class=\"block\"><ul>");
1856+
write!(buffer, "<ul class=\"block\">");
18571857
if let Some(ref version) = cx.cache().crate_version {
18581858
write!(buffer, "<li class=\"version\">Version {}</li>", Escape(version));
18591859
}
18601860
write!(buffer, "<li><a id=\"all-types\" href=\"all.html\">All Items</a></li>");
1861-
buffer.write_str("</ul></div>");
1861+
buffer.write_str("</ul>");
18621862
}
18631863

18641864
match *it.kind {
@@ -2258,8 +2258,7 @@ fn extract_for_impl_name(item: &clean::Item, cx: &Context<'_>) -> Option<(String
22582258
}
22592259
}
22602260

2261-
/// Don't call this function directly!!! Use `print_sidebar_title` or `print_sidebar_block` instead!
2262-
fn print_sidebar_title_inner(buf: &mut Buffer, id: &str, title: &str) {
2261+
fn print_sidebar_title(buf: &mut Buffer, id: &str, title: &str) {
22632262
write!(
22642263
buf,
22652264
"<h3 class=\"sidebar-title\">\
@@ -2269,25 +2268,18 @@ fn print_sidebar_title_inner(buf: &mut Buffer, id: &str, title: &str) {
22692268
);
22702269
}
22712270

2272-
fn print_sidebar_title(buf: &mut Buffer, id: &str, title: &str) {
2273-
buf.push_str("<div class=\"block\">");
2274-
print_sidebar_title_inner(buf, id, title);
2275-
buf.push_str("</div>");
2276-
}
2277-
22782271
fn print_sidebar_block(
22792272
buf: &mut Buffer,
22802273
id: &str,
22812274
title: &str,
22822275
items: impl Iterator<Item = impl fmt::Display>,
22832276
) {
2284-
buf.push_str("<div class=\"block\">");
2285-
print_sidebar_title_inner(buf, id, title);
2286-
buf.push_str("<ul>");
2277+
print_sidebar_title(buf, id, title);
2278+
buf.push_str("<ul class=\"block\">");
22872279
for item in items {
22882280
write!(buf, "<li>{}</li>", item);
22892281
}
2290-
buf.push_str("</ul></div>");
2282+
buf.push_str("</ul>");
22912283
}
22922284

22932285
fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean::Trait) {
@@ -2676,9 +2668,7 @@ pub(crate) fn sidebar_module_like(buf: &mut Buffer, item_sections_in_use: FxHash
26762668
write!(
26772669
buf,
26782670
"<section>\
2679-
<div class=\"block\">\
2680-
<ul>{}</ul>\
2681-
</div>\
2671+
<ul class=\"block\">{}</ul>\
26822672
</section>",
26832673
sidebar
26842674
);

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,14 @@ img {
501501
width: 100px;
502502
}
503503

504-
.block ul, .block li {
504+
ul.block, .block li {
505505
padding: 0;
506506
margin: 0;
507507
list-style: none;
508508
}
509509

510510
.block a,
511+
.sidebar h3 a,
511512
h2.location a {
512513
display: block;
513514
padding: 0.25rem;
@@ -767,7 +768,7 @@ h2.small-section-header > .anchor {
767768
text-decoration: underline;
768769
}
769770

770-
.block a.current.crate { font-weight: 500; }
771+
.crate.block a.current { font-weight: 500; }
771772

772773
/* In most contexts we use `overflow-wrap: anywhere` to ensure that we can wrap
773774
as much as needed on mobile (see

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

+11-15
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,10 @@ function loadCss(cssFileName) {
442442
return;
443443
}
444444

445-
const div = document.createElement("div");
446-
div.className = "block " + shortty;
447445
const h3 = document.createElement("h3");
448446
h3.innerHTML = `<a href="index.html#${id}">${longty}</a>`;
449-
div.appendChild(h3);
450447
const ul = document.createElement("ul");
448+
ul.className = "block " + shortty;
451449

452450
for (const item of filtered) {
453451
const name = item[0];
@@ -473,8 +471,8 @@ function loadCss(cssFileName) {
473471
li.appendChild(link);
474472
ul.appendChild(li);
475473
}
476-
div.appendChild(ul);
477-
sidebar.appendChild(div);
474+
sidebar.appendChild(h3);
475+
sidebar.appendChild(ul);
478476
}
479477

480478
if (sidebar) {
@@ -592,27 +590,25 @@ function loadCss(cssFileName) {
592590
return;
593591
}
594592
// Draw a convenient sidebar of known crates if we have a listing
595-
const div = document.createElement("div");
596-
div.className = "block crate";
597-
div.innerHTML = "<h3>Crates</h3>";
593+
const h3 = document.createElement("h3");
594+
h3.innerHTML = "Crates";
598595
const ul = document.createElement("ul");
599-
div.appendChild(ul);
596+
ul.className = "block crate";
600597

601598
for (const crate of window.ALL_CRATES) {
602-
let klass = "crate";
603-
if (window.rootPath !== "./" && crate === window.currentCrate) {
604-
klass += " current";
605-
}
606599
const link = document.createElement("a");
607600
link.href = window.rootPath + crate + "/index.html";
608-
link.className = klass;
601+
if (window.rootPath !== "./" && crate === window.currentCrate) {
602+
link.className = "current";
603+
}
609604
link.textContent = crate;
610605

611606
const li = document.createElement("li");
612607
li.appendChild(link);
613608
ul.appendChild(li);
614609
}
615-
sidebarElems.appendChild(div);
610+
sidebarElems.appendChild(h3);
611+
sidebarElems.appendChild(ul);
616612
}
617613

618614

src/test/rustdoc-gui/headings.goml

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ assert-css: ("h6#sub-heading-for-enum-impl-item-doc", {"border-bottom-width": "0
106106
assert-css: ("h6#sub-sub-heading-for-enum-impl-item-doc", {"font-size": "14px"})
107107
assert-css: ("h6#sub-sub-heading-for-enum-impl-item-doc", {"border-bottom-width": "0px"})
108108

109-
assert-text: (".sidebar .mod h3", "Modules")
110-
assert-css: (".sidebar .mod h3", {"border-bottom-width": "0px"}, ALL)
109+
assert-text: ("//ul[@class='block mod']/preceding-sibling::h3", "Modules")
110+
assert-css: ("//ul[@class='block mod']/preceding-sibling::h3", {"border-bottom-width": "0px"}, ALL)
111111

112112
goto: "file://" + |DOC_PATH| + "/test_docs/union.HeavilyDocumentedUnion.html"
113113

src/test/rustdoc-gui/sidebar.goml

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ assert-count: (".sidebar .location", 1)
1212
assert-text: ("#all-types", "All Items")
1313
assert-css: ("#all-types", {"color": "rgb(53, 109, 164)"})
1414
// We check that we have the crates list and that the "current" on is "test_docs".
15-
assert-text: (".sidebar-elems .crate > ul > li > a.current", "test_docs")
15+
assert-text: (".sidebar-elems ul.crate > li > a.current", "test_docs")
1616
// And we're also supposed to have the list of items in the current module.
1717
assert-text: (".sidebar-elems section ul > li:nth-child(1)", "Re-exports")
1818
assert-text: (".sidebar-elems section ul > li:nth-child(2)", "Modules")
@@ -41,21 +41,21 @@ assert-property: ("html", {"scrollTop": "0"})
4141
// We now go back to the crate page to click on the "lib2" crate link.
4242
goto: "file://" + |DOC_PATH| + "/test_docs/index.html"
4343
assert-property: (".sidebar", {"clientWidth": "200"})
44-
assert-css: (".sidebar-elems .crate > ul > li:first-child > a", {"color": "rgb(53, 109, 164)"})
45-
click: ".sidebar-elems .crate > ul > li:first-child > a"
44+
assert-css: (".sidebar-elems ul.crate > li:first-child > a", {"color": "rgb(53, 109, 164)"})
45+
click: ".sidebar-elems ul.crate > li:first-child > a"
4646

4747
// PAGE: lib2/index.html
4848
goto: "file://" + |DOC_PATH| + "/lib2/index.html"
4949
assert-property: (".sidebar", {"clientWidth": "200"})
5050
assert-text: (".sidebar > .location", "Crate lib2")
5151
// We check that we have the crates list and that the "current" on is now "lib2".
52-
assert-text: (".sidebar-elems .crate > ul > li > a.current", "lib2")
52+
assert-text: (".sidebar-elems ul.crate > li > a.current", "lib2")
5353
// We now go to the "foobar" function page.
54-
assert-text: (".sidebar-elems > section .block ul > li:nth-child(1)", "Modules")
55-
assert-text: (".sidebar-elems > section .block ul > li:nth-child(2)", "Structs")
56-
assert-text: (".sidebar-elems > section .block ul > li:nth-child(3)", "Traits")
57-
assert-text: (".sidebar-elems > section .block ul > li:nth-child(4)", "Functions")
58-
assert-text: (".sidebar-elems > section .block ul > li:nth-child(5)", "Type Definitions")
54+
assert-text: (".sidebar-elems > section ul.block > li:nth-child(1)", "Modules")
55+
assert-text: (".sidebar-elems > section ul.block > li:nth-child(2)", "Structs")
56+
assert-text: (".sidebar-elems > section ul.block > li:nth-child(3)", "Traits")
57+
assert-text: (".sidebar-elems > section ul.block > li:nth-child(4)", "Functions")
58+
assert-text: (".sidebar-elems > section ul.block > li:nth-child(5)", "Type Definitions")
5959
assert-text: ("#functions + .item-table .item-left > a", "foobar")
6060
click: "#functions + .item-table .item-left > a"
6161

@@ -90,7 +90,7 @@ assert-property: (".sidebar-elems section .block li > a", {"offsetHeight": 29})
9090
// appropriate anchor in index.html.
9191
goto: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
9292
assert-property: (".sidebar", {"clientWidth": "200"})
93-
click: ".block.mod h3 a"
93+
click: "//ul[@class='block mod']/preceding-sibling::h3/a"
9494
// PAGE: index.html
9595
assert-css: ("#modules", {"background-color": "rgb(253, 255, 211)"})
9696

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<ul><li><a href="#variant.Shown">Shown</a></li></ul>
1+
<ul class="block"><li><a href="#variant.Shown">Shown</a></li></ul>

src/test/rustdoc/strip-enum-variant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @!has - '//code' 'NotShown'
44
// @has - '//code' '// some variants omitted'
55
// Also check that `NotShown` isn't displayed in the sidebar.
6-
// @snapshot no-not-shown - '//*[@class="sidebar-elems"]/section/*[@class="block"][1]/ul'
6+
// @snapshot no-not-shown - '//*[@class="sidebar-elems"]/section/*[@class="block"][1]'
77
pub enum MyThing {
88
Shown,
99
#[doc(hidden)]

0 commit comments

Comments
 (0)