From eb25ea6fc04b90cd8d4612c1bee092fb225cd92a Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Fri, 21 Aug 2026 21:57:43 +0200 Subject: [PATCH] fix: pass existing backslash escapes through escape_content escape_content had no case for backslash, so it escaped the backslash of an escape sequence that was already in the source: `\]` became `\\]`, a literal backslash followed by a bare `]` that closes the caller's content block early. Copy escape sequences through whole instead. --- src/render.rs | 21 ++++++++++++++++++++- tests/integration.rs | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/render.rs b/src/render.rs index 37a775a..a89d431 100644 --- a/src/render.rs +++ b/src/render.rs @@ -239,6 +239,8 @@ fn write_enum_prefix(number: Option, out: &mut String) { /// /// - Tracks bracket depth so that balanced `[...]` pairs are left untouched. /// - Unbalanced `]` is escaped as `\]`. +/// - Backslash escapes already in the source are passed through as-is, so an +/// escaped character is not escaped a second time. /// - If the content ends with an odd number of backslashes, a trailing space is /// appended to prevent the closing `]` from being interpreted as `\]`. /// - When `escape_refs` is true, `@` is escaped as `\@` and `<` is escaped @@ -249,8 +251,17 @@ fn write_enum_prefix(number: Option, out: &mut String) { fn escape_content(s: &str, escape_refs: bool) -> String { let mut result = String::with_capacity(s.len()); let mut depth: i32 = 0; - for (i, ch) in s.char_indices() { + let mut chars = s.char_indices(); + while let Some((i, ch)) = chars.next() { match ch { + // An escape sequence is already literal; copy it through whole so + // the escaped character is not treated as markup below. + '\\' => { + result.push('\\'); + if let Some((_, escaped)) = chars.next() { + result.push(escaped); + } + } '[' => { depth += 1; result.push(ch); @@ -317,6 +328,14 @@ mod tests { assert_eq!(escape_content("a ] b", false), "a \\] b"); } + #[test] + fn test_escape_content_preserves_existing_escapes() { + // `\]` is already literal; escaping the backslash would hand the `]` + // back to Typst and close the caller's content block early. + assert_eq!(escape_content("a \\] b", false), "a \\] b"); + assert_eq!(escape_content("\\[x", false), "\\[x"); + } + #[test] fn test_escape_content_trailing_backslash() { assert_eq!(escape_content("text\\", false), "text\\ "); diff --git a/tests/integration.rs b/tests/integration.rs index 7562e1a..19f0d27 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -218,6 +218,24 @@ after the field log was reconciled. assert!(!output.contains("#ref(\\")); } +#[test] +fn test_existing_escaped_bracket_is_not_double_escaped() { + // `\]` is already literal. Escaping the backslash would hand the `]` + // back to Typst and close the `#diff-added[...]` block early. + let output = run_diff("Foo bar here.\n", "Foo \\] bar here.\n"); + assert!(output.contains("#diff-added[\\] ]"), "{output}"); + assert!(!output.contains("\\\\]"), "{output}"); +} + +#[test] +fn test_existing_escaped_at_is_not_double_escaped_on_deleted_side() { + // The deleted side escapes `@`, so an existing `\@` must not become `\\@`, + // which would turn it back into a reference. + let output = run_diff("A \\@b c d.\n", "A x c d.\n"); + assert!(output.contains("#diff-deleted[\\@b]"), "{output}"); + assert!(!output.contains("\\\\@"), "{output}"); +} + #[test] fn test_indented_line_comment_does_not_split_paragraph() { let old = "First sentence.\n // indented comment\nSecond sentence.\n";