Skip to content

Commit 57e021c

Browse files
Rollup merge of #87056 - GuillaumeGomez:fix-codeblocks-overflow, r=notriddle
Fix codeblocks overflow Fixes #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`
2 parents 4d141f5 + 7593bfe commit 57e021c

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

src/librustdoc/html/markdown.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use std::str;
3333

3434
use crate::clean::RenderedLink;
3535
use crate::doctest;
36+
use crate::html::escape::Escape;
3637
use crate::html::highlight;
3738
use crate::html::toc::TocBuilder;
3839

@@ -207,26 +208,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
207208
let should_panic;
208209
let ignore;
209210
let edition;
210-
if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
211-
let parse_result = match kind {
212-
CodeBlockKind::Fenced(ref lang) => {
213-
LangString::parse_without_check(&lang, self.check_error_codes, false)
214-
}
215-
CodeBlockKind::Indented => Default::default(),
216-
};
217-
if !parse_result.rust {
218-
return Some(Event::Start(Tag::CodeBlock(kind)));
219-
}
220-
compile_fail = parse_result.compile_fail;
221-
should_panic = parse_result.should_panic;
222-
ignore = parse_result.ignore;
223-
edition = parse_result.edition;
211+
let kind = if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
212+
kind
224213
} else {
225214
return event;
226-
}
227-
228-
let explicit_edition = edition.is_some();
229-
let edition = edition.unwrap_or(self.edition);
215+
};
230216

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

230+
let parse_result = match kind {
231+
CodeBlockKind::Fenced(ref lang) => {
232+
let parse_result =
233+
LangString::parse_without_check(&lang, self.check_error_codes, false);
234+
if !parse_result.rust {
235+
return Some(Event::Html(
236+
format!(
237+
"<div class=\"example-wrap\">\
238+
<pre{}>{}</pre>\
239+
</div>",
240+
format!(" class=\"language-{}\"", lang),
241+
Escape(&text),
242+
)
243+
.into(),
244+
));
245+
}
246+
parse_result
247+
}
248+
CodeBlockKind::Indented => Default::default(),
249+
};
250+
251+
compile_fail = parse_result.compile_fail;
252+
should_panic = parse_result.should_panic;
253+
ignore = parse_result.ignore;
254+
edition = parse_result.edition;
255+
256+
let explicit_edition = edition.is_some();
257+
let edition = edition.unwrap_or(self.edition);
258+
244259
let playground_button = self.playground.as_ref().and_then(|playground| {
245260
let krate = &playground.crate_name;
246261
let url = &playground.url;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ nav.sub {
435435
border-bottom-left-radius: 5px;
436436
}
437437

438-
.rustdoc:not(.source) .example-wrap > pre.rust {
438+
.rustdoc:not(.source) .example-wrap > pre:not(.line-number) {
439439
width: 100%;
440440
overflow-x: auto;
441441
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This test ensures that codeblocks content don't overflow.
2+
goto: file://|DOC_PATH|/lib2/sub_mod/struct.Foo.html
3+
size: (1080, 600)
4+
// There should be two codeblocks: a rust one and a non-rust one.
5+
assert-count: (".docblock > .example-wrap", 2)
6+
assert: ".docblock > .example-wrap > .language-txt"
7+
assert: ".docblock > .example-wrap > .rust-example-rendered"
8+
assert-css: (".docblock > .example-wrap > pre", {"width": "796px", "overflow-x": "auto"}, ALL)

src/test/rustdoc-gui/src/lib2/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-tidy-linelength
2+
13
pub mod module {
24
pub mod sub_module {
35
pub mod sub_sub_module {
@@ -32,4 +34,16 @@ impl Trait for Foo {
3234
const Y: u32 = 0;
3335
}
3436

37+
3538
impl implementors::Whatever for Foo {}
39+
40+
pub mod sub_mod {
41+
/// ```txt
42+
/// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
43+
/// ```
44+
///
45+
/// ```
46+
/// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
47+
/// ```
48+
pub struct Foo;
49+
}

0 commit comments

Comments
 (0)