From 7751a1cf5d69019c00c2cac92298ac7914a495e0 Mon Sep 17 00:00:00 2001 From: Michal Kurz Date: Fri, 7 Oct 2022 15:22:49 +0200 Subject: [PATCH] fix : fix broken prefix and suffix diffs generation inside `_addContext_` --- javascript/diff_match_patch_uncompressed.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/javascript/diff_match_patch_uncompressed.js b/javascript/diff_match_patch_uncompressed.js index 82128f0e..7be2a8f3 100644 --- a/javascript/diff_match_patch_uncompressed.js +++ b/javascript/diff_match_patch_uncompressed.js @@ -1775,13 +1775,19 @@ diff_match_patch.prototype.patch_addContext_ = function(patch, text) { padding += this.Patch_Margin; // Add the prefix. - var prefix = text.substring(patch.start2 - padding, patch.start2); + var prefix = this.isLowSurrogate(prefix[0]) // Avoid splitting on non-character boundaries + ? text.substring(patch.start2 - padding - 1 , patch.start2) + : text.substring(patch.start2 - padding , patch.start2); + if (prefix) { patch.diffs.unshift(new diff_match_patch.Diff(DIFF_EQUAL, prefix)); } + // Add the suffix. - var suffix = text.substring(patch.start2 + patch.length1, - patch.start2 + patch.length1 + padding); + var suffix = this.isHighSurrogate(suffix[suffix.length-1]) // Avoid splitting on non-character boundaries + ? text.substring(patch.start2 + patch.length1, patch.start2 + patch.length1 + padding + 1) + : text.substring(patch.start2 + patch.length1, patch.start2 + patch.length1 + padding); + if (suffix) { patch.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL, suffix)); }