Skip to content

Commit a3eaa85

Browse files
Rollup merge of rust-lang#77939 - GuillaumeGomez:fix-source-code-dos-backline, r=jyn514
Ensure that the source code display is working with DOS backline Fixes rust-lang#76361. cc `@lzutao` r? `@jyn514`
2 parents 6a3473f + 0c52044 commit a3eaa85

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/librustdoc/html/highlight.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ fn write_header(out: &mut String, class: Option<&str>) {
4646
}
4747

4848
fn write_code(out: &mut String, src: &str) {
49-
Classifier::new(src).highlight(&mut |highlight| {
49+
// This replace allows to fix how the code source with DOS backline characters is displayed.
50+
let src = src.replace("\r\n", "\n");
51+
Classifier::new(&src).highlight(&mut |highlight| {
5052
match highlight {
5153
Highlight::Token { text, class } => string(out, Escape(text), class),
5254
Highlight::EnterSpan { class } => enter_span(out, class),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">foo</span>() {
2+
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;foo&quot;</span>);
3+
}
+21-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
use super::write_code;
22
use expect_test::expect_file;
33

4-
#[test]
5-
fn test_html_highlighting() {
6-
let src = include_str!("fixtures/sample.rs");
7-
let html = {
8-
let mut out = String::new();
9-
write_code(&mut out, src);
10-
format!("{}<pre><code>{}</code></pre>\n", STYLE, out)
11-
};
12-
expect_file!["fixtures/sample.html"].assert_eq(&html);
13-
}
14-
154
const STYLE: &str = r#"
165
<style>
176
.kw { color: #8959A8; }
@@ -23,3 +12,24 @@ const STYLE: &str = r#"
2312
.question-mark { color: #ff9011; }
2413
</style>
2514
"#;
15+
16+
#[test]
17+
fn test_html_highlighting() {
18+
let src = include_str!("fixtures/sample.rs");
19+
let html = {
20+
let mut out = String::new();
21+
write_code(&mut out, src);
22+
format!("{}<pre><code>{}</code></pre>\n", STYLE, out)
23+
};
24+
expect_file!["fixtures/sample.html"].assert_eq(&html);
25+
}
26+
27+
#[test]
28+
fn test_dos_backline() {
29+
let src = "pub fn foo() {\r\n\
30+
println!(\"foo\");\r\n\
31+
}\r\n";
32+
let mut html = String::new();
33+
write_code(&mut html, src);
34+
expect_file!["fixtures/dos_line.html"].assert_eq(&html);
35+
}

0 commit comments

Comments
 (0)