From 1861a38ca9040c36741fb51f9b22f65228722b13 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 14 Oct 2020 17:02:04 +0200 Subject: [PATCH 1/2] Ensure that the source code display is working with DOS backline --- src/librustdoc/html/highlight.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 4769edc50ff07..986337336540a 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -21,6 +21,8 @@ pub fn render_with_highlighting( playground_button: Option<&str>, tooltip: Option<(&str, &str)>, ) -> String { + // This replace allows to fix how the code source with DOS backline characters is displayed. + let src = src.replace("\r\n", "\n"); debug!("highlighting: ================\n{}\n==============", src); let mut out = String::with_capacity(src.len()); if let Some((tooltip, class)) = tooltip { From 0c52044528580fb5b02eb0dca7bbbe3e584d6b13 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 15 Nov 2020 20:51:25 +0100 Subject: [PATCH 2/2] Add test to ensure that no DOS backline (\r\n) doesn't create extra backline in source rendering --- src/librustdoc/html/highlight.rs | 6 ++-- .../html/highlight/fixtures/dos_line.html | 3 ++ src/librustdoc/html/highlight/tests.rs | 32 ++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 src/librustdoc/html/highlight/fixtures/dos_line.html diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 986337336540a..b5fe593dc0105 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -21,8 +21,6 @@ pub fn render_with_highlighting( playground_button: Option<&str>, tooltip: Option<(&str, &str)>, ) -> String { - // This replace allows to fix how the code source with DOS backline characters is displayed. - let src = src.replace("\r\n", "\n"); debug!("highlighting: ================\n{}\n==============", src); let mut out = String::with_capacity(src.len()); if let Some((tooltip, class)) = tooltip { @@ -48,7 +46,9 @@ fn write_header(out: &mut String, class: Option<&str>) { } fn write_code(out: &mut String, src: &str) { - Classifier::new(src).highlight(&mut |highlight| { + // This replace allows to fix how the code source with DOS backline characters is displayed. + let src = src.replace("\r\n", "\n"); + Classifier::new(&src).highlight(&mut |highlight| { match highlight { Highlight::Token { text, class } => string(out, Escape(text), class), Highlight::EnterSpan { class } => enter_span(out, class), diff --git a/src/librustdoc/html/highlight/fixtures/dos_line.html b/src/librustdoc/html/highlight/fixtures/dos_line.html new file mode 100644 index 0000000000000..4400f85681d8a --- /dev/null +++ b/src/librustdoc/html/highlight/fixtures/dos_line.html @@ -0,0 +1,3 @@ +pub fn foo() { +println!("foo"); +} diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs index c79471b1fae6b..f57f52d6f0875 100644 --- a/src/librustdoc/html/highlight/tests.rs +++ b/src/librustdoc/html/highlight/tests.rs @@ -1,17 +1,6 @@ use super::write_code; use expect_test::expect_file; -#[test] -fn test_html_highlighting() { - let src = include_str!("fixtures/sample.rs"); - let html = { - let mut out = String::new(); - write_code(&mut out, src); - format!("{}
{}
\n", STYLE, out) - }; - expect_file!["fixtures/sample.html"].assert_eq(&html); -} - const STYLE: &str = r#" "#; + +#[test] +fn test_html_highlighting() { + let src = include_str!("fixtures/sample.rs"); + let html = { + let mut out = String::new(); + write_code(&mut out, src); + format!("{}
{}
\n", STYLE, out) + }; + expect_file!["fixtures/sample.html"].assert_eq(&html); +} + +#[test] +fn test_dos_backline() { + let src = "pub fn foo() {\r\n\ + println!(\"foo\");\r\n\ +}\r\n"; + let mut html = String::new(); + write_code(&mut html, src); + expect_file!["fixtures/dos_line.html"].assert_eq(&html); +}