Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix table in docblocks #88742

Merged
merged 2 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,42 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
}
}

/// Wrap HTML tables into `<div>` to prevent having the doc blocks width being too big.
struct TableWrapper<'a, I: Iterator<Item = Event<'a>>> {
inner: I,
stored_events: VecDeque<Event<'a>>,
}

impl<'a, I: Iterator<Item = Event<'a>>> TableWrapper<'a, I> {
fn new(iter: I) -> Self {
Self { inner: iter, stored_events: VecDeque::new() }
}
}

impl<'a, I: Iterator<Item = Event<'a>>> Iterator for TableWrapper<'a, I> {
type Item = Event<'a>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(first) = self.stored_events.pop_front() {
return Some(first);
}

let event = self.inner.next()?;

Some(match event {
Event::Start(Tag::Table(t)) => {
self.stored_events.push_back(Event::Start(Tag::Table(t)));
Event::Html(CowStr::Borrowed("<div>"))
}
Event::End(Tag::Table(t)) => {
self.stored_events.push_back(Event::Html(CowStr::Borrowed("</div>")));
Event::End(Tag::Table(t))
}
e => e,
})
}
}

type SpannedEvent<'a> = (Event<'a>, Range<usize>);

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

Expand All @@ -1003,7 +1040,8 @@ impl MarkdownWithToc<'_> {
{
let p = HeadingLinks::new(p, Some(&mut toc), &mut ids);
let p = Footnotes::new(p);
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
let p = TableWrapper::new(p.map(|(ev, _)| ev));
let p = CodeBlocks::new(p, codes, edition, playground);
html::push_html(&mut s, p);
}

Expand Down Expand Up @@ -1031,7 +1069,8 @@ impl MarkdownHtml<'_> {

let p = HeadingLinks::new(p, None, &mut ids);
let p = Footnotes::new(p);
let p = CodeBlocks::new(p.map(|(ev, _)| ev), codes, edition, playground);
let p = TableWrapper::new(p.map(|(ev, _)| ev));
let p = CodeBlocks::new(p, codes, edition, playground);
html::push_html(&mut s, p);

s
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
write!(
w,
"<h2 id=\"{id}\" class=\"section-header\">\
<a href=\"#{id}\">{name}</a></h2>\n{}",
<a href=\"#{id}\">{name}</a>\
</h2>\n{}",
ITEM_TABLE_OPEN,
id = cx.derive_id(short.to_owned()),
name = name
Expand Down
5 changes: 5 additions & 0 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ nav.sub {
position: relative;
}

.docblock > * {
max-width: 100%;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is actually unnecessary. According to the MDN, overflow: auto should introduce new block formatting context by itself. So if the element this applies to is a block element, then max-width: 100% is not needed. If the element this applies to is an inline element, then max-width is not useful because it does not apply to the non-replaced inline elements.

So I think we just need

.docblock > * {
	overflow-x: auto;
}

Or maybe

.docblock > * {
	display: block;
	overflow-x: auto;
}

if we want to be absolutely sure, but this is likely not necessary since inline elements should be wrapped with <p></p> already.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it for images actually. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't images be wrapped within <p></p>?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not always, for example:

//! <img src="whatever.png">
//!
//! ![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 1")

First one isn't but the second one is. The beauty of markdown. :D

overflow-x: auto;
}

.content .out-of-band {
flex-grow: 0;
text-align: right;
Expand Down
8 changes: 8 additions & 0 deletions src/test/rustdoc-gui/docblock-table-overflow.goml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ compare-elements-property: (".top-doc .docblock", ".top-doc .docblock > p", ["sc
assert-property: (".top-doc .docblock", {"scrollWidth": "816"})
// However, since there is overflow in the <table>, its scroll width is bigger.
assert-property: (".top-doc .docblock table", {"scrollWidth": "1573"})

// Checking it works on other doc blocks as well...

// Logically, the ".docblock" and the "<p>" should have the same scroll width.
compare-elements-property: ("#implementations + details .docblock", "#implementations + details .docblock > p", ["scrollWidth"])
assert-property: ("#implementations + details .docblock", {"scrollWidth": "816"})
// However, since there is overflow in the <table>, its scroll width is bigger.
assert-property: ("#implementations + details .docblock table", {"scrollWidth": "1573"})
9 changes: 9 additions & 0 deletions src/test/rustdoc-gui/src/lib2/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ pub mod long_table {
///
/// 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
pub struct Foo;

/// | 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 |
/// | ----------- | ----------- | ----------- | ----------- |
/// | 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 |
///
/// 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
impl Foo {
pub fn foo(&self) {}
}
}

pub mod summary_table {
Expand Down
16 changes: 16 additions & 0 deletions src/test/rustdoc/table-in-docblock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![crate_name = "foo"]

// @has foo/struct.Foo.html
// @count - '//*[@class="docblock"]/div/table' 2
// @!has - '//*[@class="docblock"]/table'
/// | hello | hello2 |
/// | ----- | ------ |
/// | data | data2 |
pub struct Foo;

impl Foo {
/// | hello | hello2 |
/// | ----- | ------ |
/// | data | data2 |
pub fn foo(&self) {}
}