diffToDelta and patchToText can throw if diff split surrogate-pair
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 70/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- javascript, typescript
- Domain
- tooling
Research direction
Start with the diff, diffToDelta, patch, and patchToText entry points shown in the reproduction, then run the provided surrogate-pair example. Trace how the split characters reach the URI encoding step; done means the example completes without URIError and preserves the expected diff and patch text.
Written by the indexing model from the issue text.
Description
Describe the bug
The current diff implementation operates on UTF-16 characters directly, which can cause it to split surrogate-pairs. When this happens, diffToDelta and patchToText can throw URIError.
Reproducible on the latest v2.0.1.
Reproduction:
import { diff, diffToDelta, patch, patchToText } from 'diff-match-patch-es'
const oldText = '😀'
const newText = '😃'
const diffs = diff(oldText, newText)
console.log('diff:', JSON.stringify(diffs))
for (const [label, reproduce] of [
['diffToDelta', () => diffToDelta(diffs)],
['patchToText', () => patchToText(patch(oldText, newText))],
]) {
try {
console.log(`${label}:`, reproduce())
} catch (error) {
console.log(`${label}:`, `${error.name}: ${error.message}`)
}
}
Output:
diff: [[0,"\ud83d"],[-1,"\ude00"],[1,"\ude03"]]
diffToDelta: URIError: URI malformed
patchToText: URIError: URI malformed
Reproduction
https://github.com/aforemendude/bugs-reproduction/tree/main/diff-match-patch-es-urierror
System Info
System:
OS: Linux 7.0 Ubuntu 24.04.4 LTS 24.04.4 LTS (Noble Numbat)
CPU: (4) x64 Intel(R) N95
Memory: 12.10 GB / 15.40 GB
Container: Yes
Shell: 5.2.21 - /bin/bash
Binaries:
Node: 24.18.0 - /usr/local/nodejs/bin/node
npm: 11.16.0 - /usr/local/nodejs/bin/npm
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guide.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
- 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!)
- Dominant language
- TypeScript
- Stars
- 201
- Forks
- 7
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from antfu/diff-match-patch-es
-
Difficulty 3/5 1-2 days Newbie friendliness 74/100
All issues in antfu/diff-match-patch-es
Similar issues
-
triage
Difficulty 1/5 Under an hour Newbie friendliness 64/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
ranxianglei/billion-context#7860 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
agent-oc-find-bugs api complexity → simple payment-methods recurring contributions reviewed stripe
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
opencollective/opencollective#9177 ·