@@ -1174,6 +1174,134 @@ diff_match_patch.prototype.diff_cleanupMerge = function(diffs) {
11741174 }
11751175} ;
11761176
1177+ /**
1178+ * Reorder and merge like edit sections. Merge equalities.
1179+ * Any edit section can move as long as it doesn't cross an equality.
1180+ * @param {!Array.<!diff_match_patch.Diff> } diffs Array of diff tuples.
1181+ */
1182+ diff_match_patch . prototype . diff_cleanupMergeNoCompression = function ( diffs ) {
1183+ diffs . push ( [ DIFF_EQUAL , '' ] ) ; // Add a dummy entry at the end.
1184+ var pointer = 0 ;
1185+ var count_delete = 0 ;
1186+ var count_insert = 0 ;
1187+ var text_delete = '' ;
1188+ var text_insert = '' ;
1189+ var commonlength ;
1190+ while ( pointer < diffs . length ) {
1191+ switch ( diffs [ pointer ] [ 0 ] ) {
1192+ case DIFF_INSERT :
1193+ count_insert ++ ;
1194+ text_insert += diffs [ pointer ] [ 1 ] ;
1195+ pointer ++ ;
1196+ break ;
1197+ case DIFF_DELETE :
1198+ count_delete ++ ;
1199+ text_delete += diffs [ pointer ] [ 1 ] ;
1200+ pointer ++ ;
1201+ break ;
1202+ case DIFF_EQUAL :
1203+ // Upon reaching an equality, check for prior redundancies.
1204+ if ( count_delete + count_insert > 1 ) {
1205+ /*
1206+ if (count_delete !== 0 && count_insert !== 0) {
1207+ // Factor out any common prefixies.
1208+ commonlength = this.diff_commonPrefix(text_insert, text_delete);
1209+ if (commonlength !== 0) {
1210+ if ((pointer - count_delete - count_insert) > 0 &&
1211+ diffs[pointer - count_delete - count_insert - 1][0] ==
1212+ DIFF_EQUAL) {
1213+ diffs[pointer - count_delete - count_insert - 1][1] +=
1214+ text_insert.substring(0, commonlength);
1215+ } else {
1216+ diffs.splice(0, 0, [DIFF_EQUAL,
1217+ text_insert.substring(0, commonlength)]);
1218+ pointer++;
1219+ }
1220+ text_insert = text_insert.substring(commonlength);
1221+ text_delete = text_delete.substring(commonlength);
1222+ }
1223+ // Factor out any common suffixies.
1224+ commonlength = this.diff_commonSuffix(text_insert, text_delete);
1225+ if (commonlength !== 0) {
1226+ diffs[pointer][1] = text_insert.substring(text_insert.length -
1227+ commonlength) + diffs[pointer][1];
1228+ text_insert = text_insert.substring(0, text_insert.length -
1229+ commonlength);
1230+ text_delete = text_delete.substring(0, text_delete.length -
1231+ commonlength);
1232+ }
1233+ }
1234+ */
1235+ // Delete the offending records and add the merged ones.
1236+ if ( count_delete === 0 ) {
1237+ diffs . splice ( pointer - count_insert ,
1238+ count_delete + count_insert , [ DIFF_INSERT , text_insert ] ) ;
1239+ } else if ( count_insert === 0 ) {
1240+ diffs . splice ( pointer - count_delete ,
1241+ count_delete + count_insert , [ DIFF_DELETE , text_delete ] ) ;
1242+ } else {
1243+ diffs . splice ( pointer - count_delete - count_insert ,
1244+ count_delete + count_insert , [ DIFF_DELETE , text_delete ] ,
1245+ [ DIFF_INSERT , text_insert ] ) ;
1246+ }
1247+ pointer = pointer - count_delete - count_insert +
1248+ ( count_delete ? 1 : 0 ) + ( count_insert ? 1 : 0 ) + 1 ;
1249+ } else if ( pointer !== 0 && diffs [ pointer - 1 ] [ 0 ] == DIFF_EQUAL ) {
1250+ // Merge this equality with the previous one.
1251+ diffs [ pointer - 1 ] [ 1 ] += diffs [ pointer ] [ 1 ] ;
1252+ diffs . splice ( pointer , 1 ) ;
1253+ } else {
1254+ pointer ++ ;
1255+ }
1256+ count_insert = 0 ;
1257+ count_delete = 0 ;
1258+ text_delete = '' ;
1259+ text_insert = '' ;
1260+ break ;
1261+ }
1262+ }
1263+ if ( diffs [ diffs . length - 1 ] [ 1 ] === '' ) {
1264+ diffs . pop ( ) ; // Remove the dummy entry at the end.
1265+ }
1266+
1267+ // Second pass: look for single edits surrounded on both sides by equalities
1268+ // which can be shifted sideways to eliminate an equality.
1269+ // e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
1270+ var changes = false ;
1271+ pointer = 1 ;
1272+ // Intentionally ignore the first and last element (don't need checking).
1273+ while ( pointer < diffs . length - 1 ) {
1274+ if ( diffs [ pointer - 1 ] [ 0 ] == DIFF_EQUAL &&
1275+ diffs [ pointer + 1 ] [ 0 ] == DIFF_EQUAL ) {
1276+ // This is a single edit surrounded by equalities.
1277+ if ( diffs [ pointer ] [ 1 ] . substring ( diffs [ pointer ] [ 1 ] . length -
1278+ diffs [ pointer - 1 ] [ 1 ] . length ) == diffs [ pointer - 1 ] [ 1 ] ) {
1279+ // Shift the edit over the previous equality.
1280+ diffs [ pointer ] [ 1 ] = diffs [ pointer - 1 ] [ 1 ] +
1281+ diffs [ pointer ] [ 1 ] . substring ( 0 , diffs [ pointer ] [ 1 ] . length -
1282+ diffs [ pointer - 1 ] [ 1 ] . length ) ;
1283+ diffs [ pointer + 1 ] [ 1 ] = diffs [ pointer - 1 ] [ 1 ] + diffs [ pointer + 1 ] [ 1 ] ;
1284+ diffs . splice ( pointer - 1 , 1 ) ;
1285+ changes = true ;
1286+ } else if ( diffs [ pointer ] [ 1 ] . substring ( 0 , diffs [ pointer + 1 ] [ 1 ] . length ) ==
1287+ diffs [ pointer + 1 ] [ 1 ] ) {
1288+ // Shift the edit over the next equality.
1289+ diffs [ pointer - 1 ] [ 1 ] += diffs [ pointer + 1 ] [ 1 ] ;
1290+ diffs [ pointer ] [ 1 ] =
1291+ diffs [ pointer ] [ 1 ] . substring ( diffs [ pointer + 1 ] [ 1 ] . length ) +
1292+ diffs [ pointer + 1 ] [ 1 ] ;
1293+ diffs . splice ( pointer + 1 , 1 ) ;
1294+ changes = true ;
1295+ }
1296+ }
1297+ pointer ++ ;
1298+ }
1299+ // If shifts were made, the diff needs reordering and another shift sweep.
1300+ if ( changes ) {
1301+ this . diff_cleanupMerge ( diffs ) ;
1302+ }
1303+ } ;
1304+
11771305
11781306/**
11791307 * loc is a location in text1, compute and return the equivalent location in
@@ -1226,13 +1354,13 @@ diff_match_patch.prototype.diff_prettyHtml = function(diffs) {
12261354 var op = diffs [ x ] [ 0 ] ; // Operation (insert, delete, equal)
12271355 var data = diffs [ x ] [ 1 ] ; // Text of change.
12281356 var text = data . replace ( pattern_amp , '&' ) . replace ( pattern_lt , '<' )
1229- . replace ( pattern_gt , '>' ) . replace ( pattern_para , '¶ <br>' ) ;
1357+ . replace ( pattern_gt , '>' ) . replace ( pattern_para , '<br>' ) ;
12301358 switch ( op ) {
12311359 case DIFF_INSERT :
1232- html [ x ] = '<ins style="background:#e6ffe6;" >' + text + '</ins>' ;
1360+ html [ x ] = '<ins>' + text + '</ins>' ;
12331361 break ;
12341362 case DIFF_DELETE :
1235- html [ x ] = '<del style="background:#ffe6e6;" >' + text + '</del>' ;
1363+ html [ x ] = '<del>' + text + '</del>' ;
12361364 break ;
12371365 case DIFF_EQUAL :
12381366 html [ x ] = '<span>' + text + '</span>' ;
0 commit comments