diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 9fff508165a10..e488a76f2806e 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -207,26 +207,11 @@ impl<'a, I: Iterator- >> 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 {
@@ -241,6 +226,35 @@ impl<'a, I: Iterator
- >> Iterator for CodeBlocks<'_, 'a, I> {
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
let text = lines.collect::>>().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!(
+ "",
+ 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;
diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css
index 66dfd2fac84c6..208e8f723f407 100644
--- a/src/librustdoc/html/static/css/rustdoc.css
+++ b/src/librustdoc/html/static/css/rustdoc.css
@@ -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;
}
diff --git a/src/test/rustdoc-gui/code-blocks-overflow.goml b/src/test/rustdoc-gui/code-blocks-overflow.goml
new file mode 100644
index 0000000000000..ee4dad444e932
--- /dev/null
+++ b/src/test/rustdoc-gui/code-blocks-overflow.goml
@@ -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)
diff --git a/src/test/rustdoc-gui/src/lib2/lib.rs b/src/test/rustdoc-gui/src/lib2/lib.rs
index 72ef3cbd2026d..ec8ab339e2804 100644
--- a/src/test/rustdoc-gui/src/lib2/lib.rs
+++ b/src/test/rustdoc-gui/src/lib2/lib.rs
@@ -1,3 +1,5 @@
+// ignore-tidy-linelength
+
pub mod module {
pub mod sub_module {
pub mod sub_sub_module {
@@ -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;
+}