diff-match-patch-es
ESM and TypeScript rewrite of Google's diff-match-patch for JavaScript
by made-in-commonTypeScript
Commit activity
Last 12 weeks · 0 commits
Community health
4 of 6 standards met
Recent PRs & issues
Fixes #6 operates on UTF-16 code units, so a common prefix or a bisect split can cut a surrogate pair in half (e.g. produced ). The unpaired surrogates then make and throw when they URI-encode each tuple. This adds a pass that runs in after . For each boundary where one tuple ends with a high surrogate and the next starts with a low surrogate, it shifts the boundary so both halves land inside a single tuple, compensating whichever text stream the moved character also fed so both concatenated texts stay identical. Boundaries that cannot be rejoined without changing the diff meaning are left alone. Merge is not run again afterwards because it re-factors common prefixes between change tuples and would re-split the pairs. Tests: added a surrogate-pair test (round-trips through /) and a / round-trip test, both of which threw before this change. Summary by CodeRabbit Bug Fixes Improved diff and patch handling of emoji and other characters represented by UTF-16 surrogate pairs, preventing characters from being split across changes. Patch context now expands safely around these characters, preserving text during patch creation and application. Diff results continue to reconstruct the original and updated text correctly, including for emoji with skin-tone modifiers. New Features Added a utility for safely cleaning up diff boundaries around surrogate pairs.
Summary In , stepping character by character right to find the best boundary fit uses: When a diff contains empty string operations (e.g. from delta operations or zero-length edits/equalities), both and can be . In JavaScript: evaluates to . Inside the loop, evaluates to , so neither variable ever changes and the while-loop hangs in an infinite loop (100% CPU lock). In the reference Neil Fraser implementation, this was guarded with . Changes In : Guard and in the while loop condition: . Added test in verifying that empty operations in do not trigger an infinite loop.
Describe the bug For a specifically crafted input, would produce a delta that cause and to hang (infinite loop). The documentation for says that it would throw on invalid input, but it's not doing that for this case. Or this could be an issue with and where they are mishandling specific diffs. This is happening due to the empty edit/equality tuples. Reproducible on the latest . Reproduction: Potential fix (unverified): The original Java version has some extra check: https://github.com/google/diff-match-patch/blob/62f2e689f498f9c92dbc588c58750addec9b1654/java/src/name/fraser/neil/plaintext/diff_match_patch.java#L938-L939 But missing from the JS (and TS) version: https://github.com/antfu/diff-match-patch-es/blob/4f35fb7fd57df68d69068cdee0780bb779f5497f/src/diff.ts#L880 Reproduction https://github.com/aforemendude/bugs-reproduction/tree/main/diff-match-patch-es-delta-inf-loop System Info Used Package Manager npm Validations [x] Follow our Code of Conduct [x] Read the Contributing Guide. [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate. [x] Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead. [x] The provided reproduction is a minimal reproducible of the bug. Contributions [ ] I am willing to submit a PR to fix this issue [ ] I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)
Recent fixes
Describe the bug For line diffs, when is the final line (without trailing new line), identical inputs receive different character IDs and duplicate entries in the line table. This issue is happening to the latest . Reproduction: Following the line diff example here: https://github.com/google/diff-match-patch/wiki/Line-or-Word-Diffs Output: Possible fix (unverified): https://github.com/antfu/diff-match-patch-es/blob/8c9aa5df9ba220c3742f8eab20f129ba9c04b685/src/diff.ts#L390 Change to or a . Please let me know if you want me to open a PR. Reproduction https://github.com/aforemendude/bugs-reproduction/blob/main/diff-match-patch-es-proto-edge-case/ System Info Used Package Manager npm Validations [x] Follow our Code of Conduct [x] Read the Contributing Guide. [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate. [x] Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead. [x] The provided reproduction is a minimal reproducible of the bug. Contributions [x] I am willing to submit a PR to fix this issue [ ] I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)
Repository: made-in-common/diff-match-patch-es. Description: ESM and TypeScript rewrite of Google's diff-match-patch for JavaScript Stars: 201, Forks: 8. Primary language: TypeScript. Languages: TypeScript (99.9%), JavaScript (0.1%). License: Apache-2.0. Topics: dff-match-patch, diff, esm, javascript, typescript. Latest release: v2.0.1 (3mo ago). Open PRs: 2, open issues: 2. Last activity: 18h ago. Community health: 62%. Top contributors: antfu, antfubot, anubra266, leekoho.