From d2df22268c9eb0e900a72157170d2b320daa3099 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 15 Nov 2020 20:51:25 +0100 Subject: [PATCH] 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); +}