Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 20 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ fn write_enum_prefix(number: Option<usize>, 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
Expand All @@ -249,8 +251,17 @@ fn write_enum_prefix(number: Option<usize>, 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);
Expand Down Expand Up @@ -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\\ ");
Expand Down
18 changes: 18 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ after the field log was reconciled.
assert!(!output.contains("#ref(\\<sec-bridge-ledger>"));
}

#[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}");
}
Comment on lines +221 to +237

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test catches the bug: it passes on main without the fix. * and [ were never escaped by escape_content, so there was nothing to double-escape. The characters that actually broke are ] (and @ / < on the deleted side, where escape_refs is on).

Also, the input itself isn't valid Typst: in \*baz*, the second * opens strong emphasis that never closes, so typst compile fails with unclosed delimiter even on the new document.

Could you replace it with cases that fail on main? Something like this works for me (passes with your fix, fails without it):

#[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}");
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks! I updated the code to use your tests. They now fail on main, but pass after the changes.


#[test]
fn test_indented_line_comment_does_not_split_paragraph() {
let old = "First sentence.\n // indented comment\nSecond sentence.\n";
Expand Down