Skip to content

Ensure that the source code display is working with DOS backline #77939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add test to ensure that no DOS backline (\r\n) doesn't create extra b…
…ackline in source rendering
  • Loading branch information
GuillaumeGomez committed Nov 15, 2020
commit 0c52044528580fb5b02eb0dca7bbbe3e584d6b13
6 changes: 3 additions & 3 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/html/highlight/fixtures/dos_line.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">foo</span>() {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;foo&quot;</span>);
}
32 changes: 21 additions & 11 deletions src/librustdoc/html/highlight/tests.rs
Original file line number Diff line number Diff line change
@@ -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!("{}<pre><code>{}</code></pre>\n", STYLE, out)
};
expect_file!["fixtures/sample.html"].assert_eq(&html);
}

const STYLE: &str = r#"
<style>
.kw { color: #8959A8; }
Expand All @@ -23,3 +12,24 @@ const STYLE: &str = r#"
.question-mark { color: #ff9011; }
</style>
"#;

#[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!("{}<pre><code>{}</code></pre>\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);
}