Skip to content

Commit a3f3f91

Browse files
Rollup merge of rust-lang#87056 - GuillaumeGomez:fix-codeblocks-overflow, 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`
2 parents 13ee7ea + 09270ed commit a3f3f91

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
207207
let should_panic;
208208
let ignore;
209209
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;
210+
let kind = if let Some(Event::Start(Tag::CodeBlock(kind))) = event {
211+
kind
224212
} else {
225213
return event;
226-
}
227-
228-
let explicit_edition = edition.is_some();
229-
let edition = edition.unwrap_or(self.edition);
214+
};
230215

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

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

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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 8 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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)