Skip to content

Commit

Permalink
Rollup merge of rust-lang#87056 - GuillaumeGomez:fix-codeblocks-overf…
Browse files Browse the repository at this point in the history
…low, r=notriddle

Fix codeblocks overflow

Fixes rust-lang#87043.

Instead of completely relying on `pulldown-cmark` (and its potential changes), I decided to move the generation of codeblocks HTML directly in rustdoc so we can unify the DOM and the CSS classes.

r? `@Nemo157`
  • Loading branch information
GuillaumeGomez authored Jul 13, 2021
2 parents 13ee7ea + 09270ed commit a3f3f91
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
50 changes: 32 additions & 18 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,26 +207,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 +226,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),
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;
}

0 comments on commit a3f3f91

Please sign in to comment.