Skip to content

Commit 130e2e1

Browse files
authored
Rollup merge of rust-lang#88742 - GuillaumeGomez:fix-table-in-docblocks, r=nbdd0121
Fix table in docblocks "Overwrite" of rust-lang#88702. Instead of adding a z-index to the sidebar (which only hides the issue, doesn't fix it), I wrap `<table>` elements inside a `<div>` and limit all chidren of `.docblock` elements' width to prevent having the scrollbar on the whole doc block. ![Screenshot from 2021-09-08 15-11-24](https://user-images.githubusercontent.com/3050060/132515740-71796515-e74f-429f-ba98-2596bdbf781c.png) Thanks `@nbdd0121` for `overflow-x: auto;`. ;) r? `@notriddle`
2 parents 1c091e4 + 021b8ff commit 130e2e1

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed

src/librustdoc/html/markdown.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,42 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
445445
}
446446
}
447447

448+
/// Wrap HTML tables into `<div>` to prevent having the doc blocks width being too big.
449+
struct TableWrapper<'a, I: Iterator<Item = Event<'a>>> {
450+
inner: I,
451+
stored_events: VecDeque<Event<'a>>,
452+
}
453+
454+
impl<'a, I: Iterator<Item = Event<'a>>> TableWrapper<'a, I> {
455+
fn new(iter: I) -> Self {
456+
Self { inner: iter, stored_events: VecDeque::new() }
457+
}
458+
}
459+
460+
impl<'a, I: Iterator<Item = Event<'a>>> Iterator for TableWrapper<'a, I> {
461+
type Item = Event<'a>;
462+
463+
fn next(&mut self) -> Option<Self::Item> {
464+
if let Some(first) = self.stored_events.pop_front() {
465+
return Some(first);
466+
}
467+
468+
let event = self.inner.next()?;
469+
470+
Some(match event {
471+
Event::Start(Tag::Table(t)) => {
472+
self.stored_events.push_back(Event::Start(Tag::Table(t)));
473+
Event::Html(CowStr::Borrowed("<div>"))
474+
}
475+
Event::End(Tag::Table(t)) => {
476+
self.stored_events.push_back(Event::Html(CowStr::Borrowed("</div>")));
477+
Event::End(Tag::Table(t))
478+
}
479+
e => e,
480+
})
481+
}
482+
}
483+
448484
type SpannedEvent<'a> = (Event<'a>, Range<usize>);
449485

450486
/// Make headings links with anchor IDs and build up TOC.
@@ -993,6 +1029,7 @@ impl Markdown<'_> {
9931029
let p = HeadingLinks::new(p, None, &mut ids);
9941030
let p = Footnotes::new(p);
9951031
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
1032+
let p = TableWrapper::new(p);
9961033
let p = CodeBlocks::new(p, codes, edition, playground);
9971034
html::push_html(&mut s, p);
9981035

@@ -1013,7 +1050,8 @@ impl MarkdownWithToc<'_> {
10131050
{
10141051
let p = HeadingLinks::new(p, Some(&mut toc), &mut ids);
10151052
let p = Footnotes::new(p);
1016-
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
1053+
let p = TableWrapper::new(p.map(|(ev, _)| ev));
1054+
let p = CodeBlocks::new(p, codes, edition, playground);
10171055
html::push_html(&mut s, p);
10181056
}
10191057

@@ -1041,7 +1079,8 @@ impl MarkdownHtml<'_> {
10411079

10421080
let p = HeadingLinks::new(p, None, &mut ids);
10431081
let p = Footnotes::new(p);
1044-
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
1082+
let p = TableWrapper::new(p.map(|(ev, _)| ev));
1083+
let p = CodeBlocks::new(p, codes, edition, playground);
10451084
html::push_html(&mut s, p);
10461085

10471086
s

src/librustdoc/html/render/print_item.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
275275
write!(
276276
w,
277277
"<h2 id=\"{id}\" class=\"section-header\">\
278-
<a href=\"#{id}\">{name}</a></h2>\n{}",
278+
<a href=\"#{id}\">{name}</a>\
279+
</h2>\n{}",
279280
ITEM_TABLE_OPEN,
280281
id = cx.derive_id(short.to_owned()),
281282
name = name

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

+5
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,11 @@ nav.sub {
522522
position: relative;
523523
}
524524

525+
.docblock > * {
526+
max-width: 100%;
527+
overflow-x: auto;
528+
}
529+
525530
.content .out-of-band {
526531
flex-grow: 0;
527532
text-align: right;

src/test/rustdoc-gui/docblock-table-overflow.goml

+8
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ compare-elements-property: (".top-doc .docblock", ".top-doc .docblock > p", ["sc
77
assert-property: (".top-doc .docblock", {"scrollWidth": "816"})
88
// However, since there is overflow in the <table>, its scroll width is bigger.
99
assert-property: (".top-doc .docblock table", {"scrollWidth": "1573"})
10+
11+
// Checking it works on other doc blocks as well...
12+
13+
// Logically, the ".docblock" and the "<p>" should have the same scroll width.
14+
compare-elements-property: ("#implementations + details .docblock", "#implementations + details .docblock > p", ["scrollWidth"])
15+
assert-property: ("#implementations + details .docblock", {"scrollWidth": "816"})
16+
// However, since there is overflow in the <table>, its scroll width is bigger.
17+
assert-property: ("#implementations + details .docblock table", {"scrollWidth": "1573"})

src/test/rustdoc-gui/src/lib2/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ pub mod long_table {
6767
///
6868
/// I wanna sqdkfnqds f dsqf qds f dsqf dsq f dsq f qds f qds f qds f dsqq f dsf sqdf dsq fds f dsq f dq f ds fq sd fqds f dsq f sqd fsq df sd fdsqfqsd fdsq f dsq f dsqfd s dfq
6969
pub struct Foo;
70+
71+
/// | This::is::a::kinda::very::long::header::number::one | This::is::a::kinda::very::long::header::number::two | This::is::a::kinda::very::long::header::number::one | This::is::a::kinda::very::long::header::number::two |
72+
/// | ----------- | ----------- | ----------- | ----------- |
73+
/// | This::is::a::kinda::long::content::number::one | This::is::a::kinda::very::long::content::number::two | This::is::a::kinda::long::content::number::one | This::is::a::kinda::very::long::content::number::two |
74+
///
75+
/// I wanna sqdkfnqds f dsqf qds f dsqf dsq f dsq f qds f qds f qds f dsqq f dsf sqdf dsq fds f dsq f dq f ds fq sd fqds f dsq f sqd fsq df sd fdsqfqsd fdsq f dsq f dsqfd s dfq
76+
impl Foo {
77+
pub fn foo(&self) {}
78+
}
7079
}
7180

7281
pub mod summary_table {

src/test/rustdoc/table-in-docblock.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![crate_name = "foo"]
2+
3+
// @has foo/struct.Foo.html
4+
// @count - '//*[@class="docblock"]/div/table' 2
5+
// @!has - '//*[@class="docblock"]/table'
6+
/// | hello | hello2 |
7+
/// | ----- | ------ |
8+
/// | data | data2 |
9+
pub struct Foo;
10+
11+
impl Foo {
12+
/// | hello | hello2 |
13+
/// | ----- | ------ |
14+
/// | data | data2 |
15+
pub fn foo(&self) {}
16+
}

0 commit comments

Comments
 (0)