Skip to content

Commit d2df222

Browse files
GuillaumeGomezpietroalbini
authored andcommitted
Add test to ensure that no DOS backline (\r\n) doesn't create extra backline in source rendering
1 parent f7886a6 commit d2df222

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/librustdoc/html/highlight.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ pub fn render_with_highlighting(
2121
playground_button: Option<&str>,
2222
tooltip: Option<(&str, &str)>,
2323
) -> String {
24-
// This replace allows to fix how the code source with DOS backline characters is displayed.
25-
let src = src.replace("\r\n", "\n");
2624
debug!("highlighting: ================\n{}\n==============", src);
2725
let mut out = String::with_capacity(src.len());
2826
if let Some((tooltip, class)) = tooltip {
@@ -48,7 +46,9 @@ fn write_header(out: &mut String, class: Option<&str>) {
4846
}
4947

5048
fn write_code(out: &mut String, src: &str) {
51-
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| {
5252
match highlight {
5353
Highlight::Token { text, class } => string(out, Escape(text), class),
5454
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)