Skip to content

fix: pass existing backslash escapes through escape_content - #27

Open
imphil wants to merge 1 commit into
sou1118:mainfrom
imphil:pass-through-backslash
Open

imphil wants to merge 1 commit into
sou1118:mainfrom
imphil:pass-through-backslash

Conversation

@imphil

@imphil imphil commented Sep 12, 2026 •

Copy link
Copy Markdown

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.

Summary by CodeRabbit

  • Bug Fixes
    • Existing backslash escapes are preserved in diff output instead of being escaped a second time.
    • Escaped brackets and other escaped characters, including \] and \@, now display without extra backslashes.

@coderabbitai

coderabbitai Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The renderer now preserves existing backslash escape sequences during content escaping. Unit and integration tests cover escaped brackets and an escaped at sign in diff output.

Changes: Escape Preservation

Layer / File(s) Summary
Content escaping and regression coverage
src/render.rs, tests/integration.rs
escape_content preserves existing backslash escape sequences. Unit tests cover escaped brackets. Integration tests cover escaped brackets and an escaped at sign in added and deleted text.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: sou1118

Merge Risk: Minimal · up to eb25e

The change preserves existing backslash escapes in rendered diff content and no concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: pass existing backslash escapes through escape_content" matches the main change. The raw summary says escape_content now copies each backslash and the following character together, so …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix: pass existing backslash escapes through escape_content" matches the main change. The raw summary says escape_content now copies each backslash and the following character together, so existing escapes such as ] are no longer double-escaped. The PR objectives describe the same fix. The title is specific, short, and names the affected function.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: adb772a6-dd46-4b01-866f-e9283a6159f2
Commits

Reviewing files that changed from the base of the PR and between 949079b and eb25ea6.

Files selected for processing: 1

  • tests/integration.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@sou1118 sou1118 left a comment

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.

Sorry for the slow review. I've had a lot going on personally and couldn't get to this sooner. Thanks for your patience, and for the fix!

The change to escape_content looks good to me. I checked it against typst compile, and these cases go from failing on main to compiling with this PR:

change main this PR
add \] unexpected closing bracket OK
delete \] unexpected closing bracket OK
delete \@b label `<b>` does not exist OK

I also didn't see any regressions with \[x], \\, a trailing \, or \] inside raw text.

The only thing I'd like changed is the integration test. It currently passes on main without the fix, so it doesn't guard against this bug. Details and a suggested replacement are in the inline comment.

Comment thread tests/integration.rs
Comment on lines +202 to +214
#[test]
fn test_existing_backslash_escape_is_not_double_escaped() {
// `\*` and `\[` are already literal. Escaping the backslash instead would
// hand the `*`/`[` back to Typst as markup and reopen the delimiter.
let old = "Foo bar.\n";
let new = "Foo \\*baz* and \\[qux bar.\n";
let output = run_diff(old, new);

assert!(
!output.contains("\\\\*") && !output.contains("\\\\["),
"an existing escape must not be escaped a second time: {output}"
);
}

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.

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.
@imphil
imphil force-pushed the pass-through-backslash branch from 949079b to eb25ea6 Compare November 17, 2026 11:02
@imphil
imphil requested a review from sou1118 November 17, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants