Skip to content

Fix codeblocks overflow #87056

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

Merged
merged 2 commits into from
Jul 14, 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
51 changes: 33 additions & 18 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::str;

use crate::clean::RenderedLink;
use crate::doctest;
use crate::html::escape::Escape;
use crate::html::highlight;
use crate::html::toc::TocBuilder;

Expand Down Expand Up @@ -207,26 +208,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
let should_panic;
let ignore;
let edition;
if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
let parse_result = match kind {
CodeBlockKind::Fenced(ref lang) => {
LangString::parse_without_check(&lang, self.check_error_codes, false)
}
CodeBlockKind::Indented => Default::default(),
};
if !parse_result.rust {
return Some(Event::Start(Tag::CodeBlock(kind)));
}
compile_fail = parse_result.compile_fail;
should_panic = parse_result.should_panic;
ignore = parse_result.ignore;
edition = parse_result.edition;
let kind = if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
kind
} else {
return event;
}

let explicit_edition = edition.is_some();
let edition = edition.unwrap_or(self.edition);
};

let mut origtext = String::new();
for event in &mut self.inner {
Expand All @@ -241,6 +227,35 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
let text = lines.collect::<Vec<Cow<'_, str>>>().join("\n");

let parse_result = match kind {
CodeBlockKind::Fenced(ref lang) => {
let parse_result =
LangString::parse_without_check(&lang, self.check_error_codes, false);
if !parse_result.rust {
return Some(Event::Html(
format!(
"<div class=\"example-wrap\">\
<pre{}>{}</pre>\
</div>",
format!(" class=\"language-{}\"", lang),
Escape(&text),
)
.into(),
));
}
parse_result
}
CodeBlockKind::Indented => Default::default(),
};

compile_fail = parse_result.compile_fail;
should_panic = parse_result.should_panic;
ignore = parse_result.ignore;
edition = parse_result.edition;

let explicit_edition = edition.is_some();
let edition = edition.unwrap_or(self.edition);

let playground_button = self.playground.as_ref().and_then(|playground| {
let krate = &playground.crate_name;
let url = &playground.url;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ nav.sub {
border-bottom-left-radius: 5px;
}

.rustdoc:not(.source) .example-wrap > pre.rust {
.rustdoc:not(.source) .example-wrap > pre:not(.line-number) {
width: 100%;
overflow-x: auto;
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/rustdoc-gui/code-blocks-overflow.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This test ensures that codeblocks content don't overflow.
goto: file://|DOC_PATH|/lib2/sub_mod/struct.Foo.html
size: (1080, 600)
// There should be two codeblocks: a rust one and a non-rust one.
assert-count: (".docblock > .example-wrap", 2)
assert: ".docblock > .example-wrap > .language-txt"
assert: ".docblock > .example-wrap > .rust-example-rendered"
assert-css: (".docblock > .example-wrap > pre", {"width": "796px", "overflow-x": "auto"}, ALL)
14 changes: 14 additions & 0 deletions src/test/rustdoc-gui/src/lib2/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore-tidy-linelength

pub mod module {
pub mod sub_module {
pub mod sub_sub_module {
Expand Down Expand Up @@ -32,4 +34,16 @@ impl Trait for Foo {
const Y: u32 = 0;
}


impl implementors::Whatever for Foo {}

pub mod sub_mod {
/// ```txt
/// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
/// ```
///
/// ```
/// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
/// ```
pub struct Foo;
}