@@ -1423,17 +1423,17 @@ diff_match_patch.prototype.digit16 = function(c) {
1423
1423
1424
1424
/**
1425
1425
* Decode URI-encoded string but allow for encoded surrogate halves
1426
- *
1426
+ *
1427
1427
* diff_match_patch needs this relaxation of the requirements because
1428
1428
* not all libraries and versions produce valid URI strings in toDelta
1429
1429
* and we don't want to crash this code when the input is valid input
1430
1430
* but at the same time invalid utf-8
1431
- *
1431
+ *
1432
1432
* @example : decodeURI( 'abcd%3A %F0%9F%85%B0' ) = 'abcd: \ud83c\udd70'
1433
1433
* @example : decodeURI( 'abcd%3A %ED%A0%BC' ) = 'abcd: \ud83c'
1434
- *
1434
+ *
1435
1435
* @cite : @mathiasbynens utf8.js at https://github.com/mathiasbynens/utf8.js
1436
- *
1436
+ *
1437
1437
* @param {String } text input string encoded by encodeURI() or equivalent
1438
1438
* @return {String }
1439
1439
*/
@@ -2215,6 +2215,46 @@ diff_match_patch.prototype.patch_splitMax = function(patches) {
2215
2215
}
2216
2216
} ;
2217
2217
2218
+ diff_match_patch . prototype . diffs_joinSurrogatePairs = function ( diffs ) {
2219
+ var newDiffs = [ ] ;
2220
+
2221
+ var lastEnd ;
2222
+
2223
+ for ( var x = 0 ; x < diffs . length ; x ++ ) {
2224
+ var thisDiff = diffs [ x ] ;
2225
+ var thisTop = thisDiff [ 1 ] [ 0 ] ;
2226
+ var thisEnd = thisDiff [ 1 ] [ thisDiff [ 1 ] . length - 1 ] ;
2227
+
2228
+ if ( 0 === thisDiff [ 1 ] . length ) {
2229
+ continue ;
2230
+ }
2231
+
2232
+ // trap a trailing high-surrogate so we can
2233
+ // distribute it to the successive edits
2234
+ if ( thisEnd && this . isHighSurrogate ( thisEnd ) ) {
2235
+ lastEnd = thisEnd ;
2236
+ thisDiff [ 1 ] = thisDiff [ 1 ] . slice ( 0 , - 1 ) ;
2237
+ }
2238
+
2239
+ if ( lastEnd && thisTop && this . isHighSurrogate ( lastEnd ) && this . isLowSurrogate ( thisTop ) ) {
2240
+ thisDiff [ 1 ] = lastEnd + thisDiff [ 1 ] ;
2241
+ }
2242
+
2243
+ if ( 0 === thisDiff [ 1 ] . length ) {
2244
+ continue ;
2245
+ }
2246
+
2247
+ newDiffs . push ( thisDiff )
2248
+ }
2249
+
2250
+ return newDiffs
2251
+ }
2252
+
2253
+ diff_match_patch . prototype . patch_joinSurrogatePairs = function ( patch ) {
2254
+ patch . diffs = this . diffs_joinSurrogatePairs ( patch . diffs )
2255
+ return patch
2256
+ }
2257
+
2218
2258
2219
2259
/**
2220
2260
* Take a list of patches and return a textual representation.
@@ -2224,7 +2264,7 @@ diff_match_patch.prototype.patch_splitMax = function(patches) {
2224
2264
diff_match_patch . prototype . patch_toText = function ( patches ) {
2225
2265
var text = [ ] ;
2226
2266
for ( var x = 0 ; x < patches . length ; x ++ ) {
2227
- text [ x ] = patches [ x ] ;
2267
+ text [ x ] = this . patch_joinSurrogatePairs ( patches [ x ] ) ;
2228
2268
}
2229
2269
return text . join ( '' ) ;
2230
2270
} ;
@@ -2277,7 +2317,7 @@ diff_match_patch.prototype.patch_fromText = function(textline) {
2277
2317
while ( textPointer < text . length ) {
2278
2318
var sign = text [ textPointer ] . charAt ( 0 ) ;
2279
2319
try {
2280
- var line = decodeURI ( text [ textPointer ] . substring ( 1 ) ) ;
2320
+ var line = this . decodeURI ( text [ textPointer ] . substring ( 1 ) ) ;
2281
2321
} catch ( ex ) {
2282
2322
// Malformed URI sequence.
2283
2323
throw new Error ( 'Illegal escape in patch_fromText: ' + line ) ;
0 commit comments