Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix surrogate pairs splitting in toText()/fromText() #2

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions javascript/diff_match_patch_uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,46 @@ diff_match_patch.prototype.patch_splitMax = function(patches) {
}
};

diff_match_patch.prototype.diffs_joinSurrogatePairs = function(diffs) {
var lastEnd;
var overwrittenDiffsCounter = 0;

for (var x = 0 ; x < diffs.length ; x++) {
var thisDiff = diffs[x];
var thisTop = thisDiff[1][0];
var thisEnd = thisDiff[1][thisDiff[1].length - 1];

if (0 === thisDiff[1].length) {
continue;
}

// trap a trailing high-surrogate so we can
// distribute it to the successive edits
if (thisEnd && this.isHighSurrogate(thisEnd)) {
lastEnd = thisEnd;
thisDiff[1] = thisDiff[1].slice(0, -1);
}

if (lastEnd && thisTop && this.isHighSurrogate(lastEnd) && this.isLowSurrogate(thisTop)) {
thisDiff[1] = lastEnd + thisDiff[1];
}

if (0 === thisDiff[1].length) {
continue;
}

diffs[overwrittenDiffsCounter] = thisDiff;
overwrittenDiffsCounter ++;
}

return diffs.splice(0, overwrittenDiffsCounter)
}

diff_match_patch.prototype.patch_joinSurrogatePairs = function(patch) {
patch.diffs = this.diffs_joinSurrogatePairs(patch.diffs)
return patch
}


/**
* Take a list of patches and return a textual representation.
Expand All @@ -2224,7 +2264,7 @@ diff_match_patch.prototype.patch_splitMax = function(patches) {
diff_match_patch.prototype.patch_toText = function(patches) {
var text = [];
for (var x = 0; x < patches.length; x++) {
text[x] = patches[x];
text[x] = this.patch_joinSurrogatePairs(patches[x]);
}
return text.join('');
};
Expand Down Expand Up @@ -2277,7 +2317,7 @@ diff_match_patch.prototype.patch_fromText = function(textline) {
while (textPointer < text.length) {
var sign = text[textPointer].charAt(0);
try {
var line = decodeURI(text[textPointer].substring(1));
var line = this.decodeURI(text[textPointer].substring(1));
} catch (ex) {
// Malformed URI sequence.
throw new Error('Illegal escape in patch_fromText: ' + line);
Expand Down