Skip to content

Commit 7751a1c

Browse files
committed
fix : fix broken prefix and suffix diffs generation inside _addContext_
1 parent e3cdf5d commit 7751a1c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

javascript/diff_match_patch_uncompressed.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,13 +1775,19 @@ diff_match_patch.prototype.patch_addContext_ = function(patch, text) {
17751775
padding += this.Patch_Margin;
17761776

17771777
// Add the prefix.
1778-
var prefix = text.substring(patch.start2 - padding, patch.start2);
1778+
var prefix = this.isLowSurrogate(prefix[0]) // Avoid splitting on non-character boundaries
1779+
? text.substring(patch.start2 - padding - 1 , patch.start2)
1780+
: text.substring(patch.start2 - padding , patch.start2);
1781+
17791782
if (prefix) {
17801783
patch.diffs.unshift(new diff_match_patch.Diff(DIFF_EQUAL, prefix));
17811784
}
1785+
17821786
// Add the suffix.
1783-
var suffix = text.substring(patch.start2 + patch.length1,
1784-
patch.start2 + patch.length1 + padding);
1787+
var suffix = this.isHighSurrogate(suffix[suffix.length-1]) // Avoid splitting on non-character boundaries
1788+
? text.substring(patch.start2 + patch.length1, patch.start2 + patch.length1 + padding + 1)
1789+
: text.substring(patch.start2 + patch.length1, patch.start2 + patch.length1 + padding);
1790+
17851791
if (suffix) {
17861792
patch.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL, suffix));
17871793
}

0 commit comments

Comments
 (0)