Skip to content

Commit 32188d7

Browse files
Wrap <table> elements into <div> to prevent breaking layout and width
1 parent 1c858ba commit 32188d7

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,42 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
441441
}
442442
}
443443

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

446482
/// Make headings links with anchor IDs and build up TOC.
@@ -983,6 +1019,7 @@ impl Markdown<'_> {
9831019
let p = HeadingLinks::new(p, None, &mut ids);
9841020
let p = Footnotes::new(p);
9851021
let p = LinkReplacer::new(p.map(|(ev, _)| ev), links);
1022+
let p = TableWrapper::new(p);
9861023
let p = CodeBlocks::new(p, codes, edition, playground);
9871024
html::push_html(&mut s, p);
9881025

@@ -1003,7 +1040,8 @@ impl MarkdownWithToc<'_> {
10031040
{
10041041
let p = HeadingLinks::new(p, Some(&mut toc), &mut ids);
10051042
let p = Footnotes::new(p);
1006-
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
1043+
let p = TableWrapper::new(p.map(|(ev, _)| ev));
1044+
let p = CodeBlocks::new(p, codes, edition, playground);
10071045
html::push_html(&mut s, p);
10081046
}
10091047

@@ -1031,7 +1069,8 @@ impl MarkdownHtml<'_> {
10311069

10321070
let p = HeadingLinks::new(p, None, &mut ids);
10331071
let p = Footnotes::new(p);
1034-
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
1072+
let p = TableWrapper::new(p.map(|(ev, _)| ev));
1073+
let p = CodeBlocks::new(p, codes, edition, playground);
10351074
html::push_html(&mut s, p);
10361075

10371076
s

src/librustdoc/html/render/print_item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
273273
write!(
274274
w,
275275
"<h2 id=\"{id}\" class=\"section-header\">\
276-
<a href=\"#{id}\">{name}</a></h2>\n{}",
276+
<a href=\"#{id}\">{name}</a>\
277+
</h2>\n{}",
277278
ITEM_TABLE_OPEN,
278279
id = cx.derive_id(short.to_owned()),
279280
name = name

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

Lines changed: 5 additions & 0 deletions
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;

0 commit comments

Comments
 (0)