UNPKG

automizy-js-api

Version:

JavaScript API library for Automizy Marketing Automation software

1,112 lines (1,034 loc) 37 kB
/* * This file is a modified version of google-diff-match-patch's JavaScript implementation * (https://code.google.com/p/google-diff-match-patch/source/browse/trunk/javascript/diff_match_patch_uncompressed.js), * modifications are licensed as more fully set forth in LICENSE.txt. * * The original source of google-diff-match-patch is attributable and licensed as follows: * * Copyright 2006 Google Inc. * http://code.google.com/p/google-diff-match-patch/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * More Info: * https://code.google.com/p/google-diff-match-patch/ * * Usage: QUnit.diff(expected, actual) * */ QUnit.diff = ( function() { function DiffMatchPatch() { } // DIFF FUNCTIONS /** * The data structure representing a diff is an array of tuples: * [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']] * which means: delete 'Hello', add 'Goodbye' and keep ' world.' */ var DIFF_DELETE = -1, DIFF_INSERT = 1, DIFF_EQUAL = 0; /** * Find the differences between two texts. Simplifies the problem by stripping * any common prefix or suffix off the texts before diffing. * @param {string} text1 Old string to be diffed. * @param {string} text2 New string to be diffed. * @param {boolean=} optChecklines Optional speedup flag. If present and false, * then don't run a line-level diff first to identify the changed areas. * Defaults to true, which does a faster, slightly less optimal diff. * @return {!Array.<!DiffMatchPatch.Diff>} Array of diff tuples. */ DiffMatchPatch.prototype.DiffMain = function( text1, text2, optChecklines ) { var deadline, checklines, commonlength, commonprefix, commonsuffix, diffs; // The diff must be complete in up to 1 second. deadline = ( new Date() ).getTime() + 1000; // Check for null inputs. if ( text1 === null || text2 === null ) { throw new Error( "Null input. (DiffMain)" ); } // Check for equality (speedup). if ( text1 === text2 ) { if ( text1 ) { return [ [ DIFF_EQUAL, text1 ] ]; } return []; } if ( typeof optChecklines === "undefined" ) { optChecklines = true; } checklines = optChecklines; // Trim off common prefix (speedup). commonlength = this.diffCommonPrefix( text1, text2 ); commonprefix = text1.substring( 0, commonlength ); text1 = text1.substring( commonlength ); text2 = text2.substring( commonlength ); // Trim off common suffix (speedup). commonlength = this.diffCommonSuffix( text1, text2 ); commonsuffix = text1.substring( text1.length - commonlength ); text1 = text1.substring( 0, text1.length - commonlength ); text2 = text2.substring( 0, text2.length - commonlength ); // Compute the diff on the middle block. diffs = this.diffCompute( text1, text2, checklines, deadline ); // Restore the prefix and suffix. if ( commonprefix ) { diffs.unshift( [ DIFF_EQUAL, commonprefix ] ); } if ( commonsuffix ) { diffs.push( [ DIFF_EQUAL, commonsuffix ] ); } this.diffCleanupMerge( diffs ); return diffs; }; /** * Reduce the number of edits by eliminating operationally trivial equalities. * @param {!Array.<!DiffMatchPatch.Diff>} diffs Array of diff tuples. */ DiffMatchPatch.prototype.diffCleanupEfficiency = function( diffs ) { var changes, equalities, equalitiesLength, lastequality, pointer, preIns, preDel, postIns, postDel; changes = false; equalities = []; // Stack of indices where equalities are found. equalitiesLength = 0; // Keeping our own length var is faster in JS. /** @type {?string} */ lastequality = null; // Always equal to diffs[equalities[equalitiesLength - 1]][1] pointer = 0; // Index of current position. // Is there an insertion operation before the last equality. preIns = false; // Is there a deletion operation before the last equality. preDel = false; // Is there an insertion operation after the last equality. postIns = false; // Is there a deletion operation after the last equality. postDel = false; while ( pointer < diffs.length ) { // Equality found. if ( diffs[ pointer ][ 0 ] === DIFF_EQUAL ) { if ( diffs[ pointer ][ 1 ].length < 4 && ( postIns || postDel ) ) { // Candidate found. equalities[ equalitiesLength++ ] = pointer; preIns = postIns; preDel = postDel; lastequality = diffs[ pointer ][ 1 ]; } else { // Not a candidate, and can never become one. equalitiesLength = 0; lastequality = null; } postIns = postDel = false; // An insertion or deletion. } else { if ( diffs[ pointer ][ 0 ] === DIFF_DELETE ) { postDel = true; } else { postIns = true; } /* * Five types to be split: * <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del> * <ins>A</ins>X<ins>C</ins><del>D</del> * <ins>A</ins><del>B</del>X<ins>C</ins> * <ins>A</del>X<ins>C</ins><del>D</del> * <ins>A</ins><del>B</del>X<del>C</del> */ if ( lastequality && ( ( preIns && preDel && postIns && postDel ) || ( ( lastequality.length < 2 ) && ( preIns + preDel + postIns + postDel ) === 3 ) ) ) { // Duplicate record. diffs.splice( equalities[ equalitiesLength - 1 ], 0, [ DIFF_DELETE, lastequality ] ); // Change second copy to insert. diffs[ equalities[ equalitiesLength - 1 ] + 1 ][ 0 ] = DIFF_INSERT; equalitiesLength--; // Throw away the equality we just deleted; lastequality = null; if ( preIns && preDel ) { // No changes made which could affect previous entry, keep going. postIns = postDel = true; equalitiesLength = 0; } else { equalitiesLength--; // Throw away the previous equality. pointer = equalitiesLength > 0 ? equalities[ equalitiesLength - 1 ] : -1; postIns = postDel = false; } changes = true; } } pointer++; } if ( changes ) { this.diffCleanupMerge( diffs ); } }; /** * Convert a diff array into a pretty HTML report. * @param {!Array.<!DiffMatchPatch.Diff>} diffs Array of diff tuples. * @param {integer} string to be beautified. * @return {string} HTML representation. */ DiffMatchPatch.prototype.diffPrettyHtml = function( diffs ) { var op, data, x, html = []; for ( x = 0; x < diffs.length; x++ ) { op = diffs[ x ][ 0 ]; // Operation (insert, delete, equal) data = diffs[ x ][ 1 ]; // Text of change. switch ( op ) { case DIFF_INSERT: html[ x ] = "<ins>" + data + "</ins>"; break; case DIFF_DELETE: html[ x ] = "<del>" + data + "</del>"; break; case DIFF_EQUAL: html[ x ] = "<span>" + data + "</span>"; break; } } return html.join( "" ); }; /** * Determine the common prefix of two strings. * @param {string} text1 First string. * @param {string} text2 Second string. * @return {number} The number of characters common to the start of each * string. */ DiffMatchPatch.prototype.diffCommonPrefix = function( text1, text2 ) { var pointermid, pointermax, pointermin, pointerstart; // Quick check for common null cases. if ( !text1 || !text2 || text1.charAt( 0 ) !== text2.charAt( 0 ) ) { return 0; } // Binary search. // Performance analysis: http://neil.fraser.name/news/2007/10/09/ pointermin = 0; pointermax = Math.min( text1.length, text2.length ); pointermid = pointermax; pointerstart = 0; while ( pointermin < pointermid ) { if ( text1.substring( pointerstart, pointermid ) === text2.substring( pointerstart, pointermid ) ) { pointermin = pointermid; pointerstart = pointermin; } else { pointermax = pointermid; } pointermid = Math.floor( ( pointermax - pointermin ) / 2 + pointermin ); } return pointermid; }; /** * Determine the common suffix of two strings. * @param {string} text1 First string. * @param {string} text2 Second string. * @return {number} The number of characters common to the end of each string. */ DiffMatchPatch.prototype.diffCommonSuffix = function( text1, text2 ) { var pointermid, pointermax, pointermin, pointerend; // Quick check for common null cases. if ( !text1 || !text2 || text1.charAt( text1.length - 1 ) !== text2.charAt( text2.length - 1 ) ) { return 0; } // Binary search. // Performance analysis: http://neil.fraser.name/news/2007/10/09/ pointermin = 0; pointermax = Math.min( text1.length, text2.length ); pointermid = pointermax; pointerend = 0; while ( pointermin < pointermid ) { if ( text1.substring( text1.length - pointermid, text1.length - pointerend ) === text2.substring( text2.length - pointermid, text2.length - pointerend ) ) { pointermin = pointermid; pointerend = pointermin; } else { pointermax = pointermid; } pointermid = Math.floor( ( pointermax - pointermin ) / 2 + pointermin ); } return pointermid; }; /** * Find the differences between two texts. Assumes that the texts do not * have any common prefix or suffix. * @param {string} text1 Old string to be diffed. * @param {string} text2 New string to be diffed. * @param {boolean} checklines Speedup flag. If false, then don't run a * line-level diff first to identify the changed areas. * If true, then run a faster, slightly less optimal diff. * @param {number} deadline Time when the diff should be complete by. * @return {!Array.<!DiffMatchPatch.Diff>} Array of diff tuples. * @private */ DiffMatchPatch.prototype.diffCompute = function( text1, text2, checklines, deadline ) { var diffs, longtext, shorttext, i, hm, text1A, text2A, text1B, text2B, midCommon, diffsA, diffsB; if ( !text1 ) { // Just add some text (speedup). return [ [ DIFF_INSERT, text2 ] ]; } if ( !text2 ) { // Just delete some text (speedup). return [ [ DIFF_DELETE, text1 ] ]; } longtext = text1.length > text2.length ? text1 : text2; shorttext = text1.length > text2.length ? text2 : text1; i = longtext.indexOf( shorttext ); if ( i !== -1 ) { // Shorter text is inside the longer text (speedup). diffs = [ [ DIFF_INSERT, longtext.substring( 0, i ) ], [ DIFF_EQUAL, shorttext ], [ DIFF_INSERT, longtext.substring( i + shorttext.length ) ] ]; // Swap insertions for deletions if diff is reversed. if ( text1.length > text2.length ) { diffs[ 0 ][ 0 ] = diffs[ 2 ][ 0 ] = DIFF_DELETE; } return diffs; } if ( shorttext.length === 1 ) { // Single character string. // After the previous speedup, the character can't be an equality. return [ [ DIFF_DELETE, text1 ], [ DIFF_INSERT, text2 ] ]; } // Check to see if the problem can be split in two. hm = this.diffHalfMatch( text1, text2 ); if ( hm ) { // A half-match was found, sort out the return data. text1A = hm[ 0 ]; text1B = hm[ 1 ]; text2A = hm[ 2 ]; text2B = hm[ 3 ]; midCommon = hm[ 4 ]; // Send both pairs off for separate processing. diffsA = this.DiffMain( text1A, text2A, checklines, deadline ); diffsB = this.DiffMain( text1B, text2B, checklines, deadline ); // Merge the results. return diffsA.concat( [ [ DIFF_EQUAL, midCommon ] ], diffsB ); } if ( checklines && text1.length > 100 && text2.length > 100 ) { return this.diffLineMode( text1, text2, deadline ); } return this.diffBisect( text1, text2, deadline ); }; /** * Do the two texts share a substring which is at least half the length of the * longer text? * This speedup can produce non-minimal diffs. * @param {string} text1 First string. * @param {string} text2 Second string. * @return {Array.<string>} Five element Array, containing the prefix of * text1, the suffix of text1, the prefix of text2, the suffix of * text2 and the common middle. Or null if there was no match. * @private */ DiffMatchPatch.prototype.diffHalfMatch = function( text1, text2 ) { var longtext, shorttext, dmp, text1A, text2B, text2A, text1B, midCommon, hm1, hm2, hm; longtext = text1.length > text2.length ? text1 : text2; shorttext = text1.length > text2.length ? text2 : text1; if ( longtext.length < 4 || shorttext.length * 2 < longtext.length ) { return null; // Pointless. } dmp = this; // 'this' becomes 'window' in a closure. /** * Does a substring of shorttext exist within longtext such that the substring * is at least half the length of longtext? * Closure, but does not reference any external variables. * @param {string} longtext Longer string. * @param {string} shorttext Shorter string. * @param {number} i Start index of quarter length substring within longtext. * @return {Array.<string>} Five element Array, containing the prefix of * longtext, the suffix of longtext, the prefix of shorttext, the suffix * of shorttext and the common middle. Or null if there was no match. * @private */ function diffHalfMatchI( longtext, shorttext, i ) { var seed, j, bestCommon, prefixLength, suffixLength, bestLongtextA, bestLongtextB, bestShorttextA, bestShorttextB; // Start with a 1/4 length substring at position i as a seed. seed = longtext.substring( i, i + Math.floor( longtext.length / 4 ) ); j = -1; bestCommon = ""; while ( ( j = shorttext.indexOf( seed, j + 1 ) ) !== -1 ) { prefixLength = dmp.diffCommonPrefix( longtext.substring( i ), shorttext.substring( j ) ); suffixLength = dmp.diffCommonSuffix( longtext.substring( 0, i ), shorttext.substring( 0, j ) ); if ( bestCommon.length < suffixLength + prefixLength ) { bestCommon = shorttext.substring( j - suffixLength, j ) + shorttext.substring( j, j + prefixLength ); bestLongtextA = longtext.substring( 0, i - suffixLength ); bestLongtextB = longtext.substring( i + prefixLength ); bestShorttextA = shorttext.substring( 0, j - suffixLength ); bestShorttextB = shorttext.substring( j + prefixLength ); } } if ( bestCommon.length * 2 >= longtext.length ) { return [ bestLongtextA, bestLongtextB, bestShorttextA, bestShorttextB, bestCommon ]; } else { return null; } } // First check if the second quarter is the seed for a half-match. hm1 = diffHalfMatchI( longtext, shorttext, Math.ceil( longtext.length / 4 ) ); // Check again based on the third quarter. hm2 = diffHalfMatchI( longtext, shorttext, Math.ceil( longtext.length / 2 ) ); if ( !hm1 && !hm2 ) { return null; } else if ( !hm2 ) { hm = hm1; } else if ( !hm1 ) { hm = hm2; } else { // Both matched. Select the longest. hm = hm1[ 4 ].length > hm2[ 4 ].length ? hm1 : hm2; } // A half-match was found, sort out the return data. text1A, text1B, text2A, text2B; if ( text1.length > text2.length ) { text1A = hm[ 0 ]; text1B = hm[ 1 ]; text2A = hm[ 2 ]; text2B = hm[ 3 ]; } else { text2A = hm[ 0 ]; text2B = hm[ 1 ]; text1A = hm[ 2 ]; text1B = hm[ 3 ]; } midCommon = hm[ 4 ]; return [ text1A, text1B, text2A, text2B, midCommon ]; }; /** * Do a quick line-level diff on both strings, then rediff the parts for * greater accuracy. * This speedup can produce non-minimal diffs. * @param {string} text1 Old string to be diffed. * @param {string} text2 New string to be diffed. * @param {number} deadline Time when the diff should be complete by. * @return {!Array.<!DiffMatchPatch.Diff>} Array of diff tuples. * @private */ DiffMatchPatch.prototype.diffLineMode = function( text1, text2, deadline ) { var a, diffs, linearray, pointer, countInsert, countDelete, textInsert, textDelete, j; // Scan the text on a line-by-line basis first. a = this.diffLinesToChars( text1, text2 ); text1 = a.chars1; text2 = a.chars2; linearray = a.lineArray; diffs = this.DiffMain( text1, text2, false, deadline ); // Convert the diff back to original text. this.diffCharsToLines( diffs, linearray ); // Eliminate freak matches (e.g. blank lines) this.diffCleanupSemantic( diffs ); // Rediff any replacement blocks, this time character-by-character. // Add a dummy entry at the end. diffs.push( [ DIFF_EQUAL, "" ] ); pointer = 0; countDelete = 0; countInsert = 0; textDelete = ""; textInsert = ""; while ( pointer < diffs.length ) { switch ( diffs[ pointer ][ 0 ] ) { case DIFF_INSERT: countInsert++; textInsert += diffs[ pointer ][ 1 ]; break; case DIFF_DELETE: countDelete++; textDelete += diffs[ pointer ][ 1 ]; break; case DIFF_EQUAL: // Upon reaching an equality, check for prior redundancies. if ( countDelete >= 1 && countInsert >= 1 ) { // Delete the offending records and add the merged ones. diffs.splice( pointer - countDelete - countInsert, countDelete + countInsert ); pointer = pointer - countDelete - countInsert; a = this.DiffMain( textDelete, textInsert, false, deadline ); for ( j = a.length - 1; j >= 0; j-- ) { diffs.splice( pointer, 0, a[ j ] ); } pointer = pointer + a.length; } countInsert = 0; countDelete = 0; textDelete = ""; textInsert = ""; break; } pointer++; } diffs.pop(); // Remove the dummy entry at the end. return diffs; }; /** * Find the 'middle snake' of a diff, split the problem in two * and return the recursively constructed diff. * See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations. * @param {string} text1 Old string to be diffed. * @param {string} text2 New string to be diffed. * @param {number} deadline Time at which to bail if not yet complete. * @return {!Array.<!DiffMatchPatch.Diff>} Array of diff tuples. * @private */ DiffMatchPatch.prototype.diffBisect = function( text1, text2, deadline ) { var text1Length, text2Length, maxD, vOffset, vLength, v1, v2, x, delta, front, k1start, k1end, k2start, k2end, k2Offset, k1Offset, x1, x2, y1, y2, d, k1, k2; // Cache the text lengths to prevent multiple calls. text1Length = text1.length; text2Length = text2.length; maxD = Math.ceil( ( text1Length + text2Length ) / 2 ); vOffset = maxD; vLength = 2 * maxD; v1 = new Array( vLength ); v2 = new Array( vLength ); // Setting all elements to -1 is faster in Chrome & Firefox than mixing // integers and undefined. for ( x = 0; x < vLength; x++ ) { v1[ x ] = -1; v2[ x ] = -1; } v1[ vOffset + 1 ] = 0; v2[ vOffset + 1 ] = 0; delta = text1Length - text2Length; // If the total number of characters is odd, then the front path will collide // with the reverse path. front = ( delta % 2 !== 0 ); // Offsets for start and end of k loop. // Prevents mapping of space beyond the grid. k1start = 0; k1end = 0; k2start = 0; k2end = 0; for ( d = 0; d < maxD; d++ ) { // Bail out if deadline is reached. if ( ( new Date() ).getTime() > deadline ) { break; } // Walk the front path one step. for ( k1 = -d + k1start; k1 <= d - k1end; k1 += 2 ) { k1Offset = vOffset + k1; if ( k1 === -d || ( k1 !== d && v1[ k1Offset - 1 ] < v1[ k1Offset + 1 ] ) ) { x1 = v1[ k1Offset + 1 ]; } else { x1 = v1[ k1Offset - 1 ] + 1; } y1 = x1 - k1; while ( x1 < text1Length && y1 < text2Length && text1.charAt( x1 ) === text2.charAt( y1 ) ) { x1++; y1++; } v1[ k1Offset ] = x1; if ( x1 > text1Length ) { // Ran off the right of the graph. k1end += 2; } else if ( y1 > text2Length ) { // Ran off the bottom of the graph. k1start += 2; } else if ( front ) { k2Offset = vOffset + delta - k1; if ( k2Offset >= 0 && k2Offset < vLength && v2[ k2Offset ] !== -1 ) { // Mirror x2 onto top-left coordinate system. x2 = text1Length - v2[ k2Offset ]; if ( x1 >= x2 ) { // Overlap detected. return this.diffBisectSplit( text1, text2, x1, y1, deadline ); } } } } // Walk the reverse path one step. for ( k2 = -d + k2start; k2 <= d - k2end; k2 += 2 ) { k2Offset = vOffset + k2; if ( k2 === -d || ( k2 !== d && v2[ k2Offset - 1 ] < v2[ k2Offset + 1 ] ) ) { x2 = v2[ k2Offset + 1 ]; } else { x2 = v2[ k2Offset - 1 ] + 1; } y2 = x2 - k2; while ( x2 < text1Length && y2 < text2Length && text1.charAt( text1Length - x2 - 1 ) === text2.charAt( text2Length - y2 - 1 ) ) { x2++; y2++; } v2[ k2Offset ] = x2; if ( x2 > text1Length ) { // Ran off the left of the graph. k2end += 2; } else if ( y2 > text2Length ) { // Ran off the top of the graph. k2start += 2; } else if ( !front ) { k1Offset = vOffset + delta - k2; if ( k1Offset >= 0 && k1Offset < vLength && v1[ k1Offset ] !== -1 ) { x1 = v1[ k1Offset ]; y1 = vOffset + x1 - k1Offset; // Mirror x2 onto top-left coordinate system. x2 = text1Length - x2; if ( x1 >= x2 ) { // Overlap detected. return this.diffBisectSplit( text1, text2, x1, y1, deadline ); } } } } } // Diff took too long and hit the deadline or // number of diffs equals number of characters, no commonality at all. return [ [ DIFF_DELETE, text1 ], [ DIFF_INSERT, text2 ] ]; }; /** * Given the location of the 'middle snake', split the diff in two parts * and recurse. * @param {string} text1 Old string to be diffed. * @param {string} text2 New string to be diffed. * @param {number} x Index of split point in text1. * @param {number} y Index of split point in text2. * @param {number} deadline Time at which to bail if not yet complete. * @return {!Array.<!DiffMatchPatch.Diff>} Array of diff tuples. * @private */ DiffMatchPatch.prototype.diffBisectSplit = function( text1, text2, x, y, deadline ) { var text1a, text1b, text2a, text2b, diffs, diffsb; text1a = text1.substring( 0, x ); text2a = text2.substring( 0, y ); text1b = text1.substring( x ); text2b = text2.substring( y ); // Compute both diffs serially. diffs = this.DiffMain( text1a, text2a, false, deadline ); diffsb = this.DiffMain( text1b, text2b, false, deadline ); return diffs.concat( diffsb ); }; /** * Reduce the number of edits by eliminating semantically trivial equalities. * @param {!Array.<!DiffMatchPatch.Diff>} diffs Array of diff tuples. */ DiffMatchPatch.prototype.diffCleanupSemantic = function( diffs ) { var changes, equalities, equalitiesLength, lastequality, pointer, lengthInsertions2, lengthDeletions2, lengthInsertions1, lengthDeletions1, deletion, insertion, overlapLength1, overlapLength2; changes = false; equalities = []; // Stack of indices where equalities are found. equalitiesLength = 0; // Keeping our own length var is faster in JS. /** @type {?string} */ lastequality = null; // Always equal to diffs[equalities[equalitiesLength - 1]][1] pointer = 0; // Index of current position. // Number of characters that changed prior to the equality. lengthInsertions1 = 0; lengthDeletions1 = 0; // Number of characters that changed after the equality. lengthInsertions2 = 0; lengthDeletions2 = 0; while ( pointer < diffs.length ) { if ( diffs[ pointer ][ 0 ] === DIFF_EQUAL ) { // Equality found. equalities[ equalitiesLength++ ] = pointer; lengthInsertions1 = lengthInsertions2; lengthDeletions1 = lengthDeletions2; lengthInsertions2 = 0; lengthDeletions2 = 0; lastequality = diffs[ pointer ][ 1 ]; } else { // An insertion or deletion. if ( diffs[ pointer ][ 0 ] === DIFF_INSERT ) { lengthInsertions2 += diffs[ pointer ][ 1 ].length; } else { lengthDeletions2 += diffs[ pointer ][ 1 ].length; } // Eliminate an equality that is smaller or equal to the edits on both // sides of it. if ( lastequality && ( lastequality.length <= Math.max( lengthInsertions1, lengthDeletions1 ) ) && ( lastequality.length <= Math.max( lengthInsertions2, lengthDeletions2 ) ) ) { // Duplicate record. diffs.splice( equalities[ equalitiesLength - 1 ], 0, [ DIFF_DELETE, lastequality ] ); // Change second copy to insert. diffs[ equalities[ equalitiesLength - 1 ] + 1 ][ 0 ] = DIFF_INSERT; // Throw away the equality we just deleted. equalitiesLength--; // Throw away the previous equality (it needs to be reevaluated). equalitiesLength--; pointer = equalitiesLength > 0 ? equalities[ equalitiesLength - 1 ] : -1; // Reset the counters. lengthInsertions1 = 0; lengthDeletions1 = 0; lengthInsertions2 = 0; lengthDeletions2 = 0; lastequality = null; changes = true; } } pointer++; } // Normalize the diff. if ( changes ) { this.diffCleanupMerge( diffs ); } // Find any overlaps between deletions and insertions. // e.g: <del>abcxxx</del><ins>xxxdef</ins> // -> <del>abc</del>xxx<ins>def</ins> // e.g: <del>xxxabc</del><ins>defxxx</ins> // -> <ins>def</ins>xxx<del>abc</del> // Only extract an overlap if it is as big as the edit ahead or behind it. pointer = 1; while ( pointer < diffs.length ) { if ( diffs[ pointer - 1 ][ 0 ] === DIFF_DELETE && diffs[ pointer ][ 0 ] === DIFF_INSERT ) { deletion = diffs[ pointer - 1 ][ 1 ]; insertion = diffs[ pointer ][ 1 ]; overlapLength1 = this.diffCommonOverlap( deletion, insertion ); overlapLength2 = this.diffCommonOverlap( insertion, deletion ); if ( overlapLength1 >= overlapLength2 ) { if ( overlapLength1 >= deletion.length / 2 || overlapLength1 >= insertion.length / 2 ) { // Overlap found. Insert an equality and trim the surrounding edits. diffs.splice( pointer, 0, [ DIFF_EQUAL, insertion.substring( 0, overlapLength1 ) ] ); diffs[ pointer - 1 ][ 1 ] = deletion.substring( 0, deletion.length - overlapLength1 ); diffs[ pointer + 1 ][ 1 ] = insertion.substring( overlapLength1 ); pointer++; } } else { if ( overlapLength2 >= deletion.length / 2 || overlapLength2 >= insertion.length / 2 ) { // Reverse overlap found. // Insert an equality and swap and trim the surrounding edits. diffs.splice( pointer, 0, [ DIFF_EQUAL, deletion.substring( 0, overlapLength2 ) ] ); diffs[ pointer - 1 ][ 0 ] = DIFF_INSERT; diffs[ pointer - 1 ][ 1 ] = insertion.substring( 0, insertion.length - overlapLength2 ); diffs[ pointer + 1 ][ 0 ] = DIFF_DELETE; diffs[ pointer + 1 ][ 1 ] = deletion.substring( overlapLength2 ); pointer++; } } pointer++; } pointer++; } }; /** * Determine if the suffix of one string is the prefix of another. * @param {string} text1 First string. * @param {string} text2 Second string. * @return {number} The number of characters common to the end of the first * string and the start of the second string. * @private */ DiffMatchPatch.prototype.diffCommonOverlap = function( text1, text2 ) { var text1Length, text2Length, textLength, best, length, pattern, found; // Cache the text lengths to prevent multiple calls. text1Length = text1.length; text2Length = text2.length; // Eliminate the null case. if ( text1Length === 0 || text2Length === 0 ) { return 0; } // Truncate the longer string. if ( text1Length > text2Length ) { text1 = text1.substring( text1Length - text2Length ); } else if ( text1Length < text2Length ) { text2 = text2.substring( 0, text1Length ); } textLength = Math.min( text1Length, text2Length ); // Quick check for the worst case. if ( text1 === text2 ) { return textLength; } // Start by looking for a single character match // and increase length until no match is found. // Performance analysis: http://neil.fraser.name/news/2010/11/04/ best = 0; length = 1; while ( true ) { pattern = text1.substring( textLength - length ); found = text2.indexOf( pattern ); if ( found === -1 ) { return best; } length += found; if ( found === 0 || text1.substring( textLength - length ) === text2.substring( 0, length ) ) { best = length; length++; } } }; /** * Split two texts into an array of strings. Reduce the texts to a string of * hashes where each Unicode character represents one line. * @param {string} text1 First string. * @param {string} text2 Second string. * @return {{chars1: string, chars2: string, lineArray: !Array.<string>}} * An object containing the encoded text1, the encoded text2 and * the array of unique strings. * The zeroth element of the array of unique strings is intentionally blank. * @private */ DiffMatchPatch.prototype.diffLinesToChars = function( text1, text2 ) { var lineArray, lineHash, chars1, chars2; lineArray = []; // e.g. lineArray[4] === 'Hello\n' lineHash = {}; // e.g. lineHash['Hello\n'] === 4 // '\x00' is a valid character, but various debuggers don't like it. // So we'll insert a junk entry to avoid generating a null character. lineArray[ 0 ] = ""; /** * Split a text into an array of strings. Reduce the texts to a string of * hashes where each Unicode character represents one line. * Modifies linearray and linehash through being a closure. * @param {string} text String to encode. * @return {string} Encoded string. * @private */ function diffLinesToCharsMunge( text ) { var chars, lineStart, lineEnd, lineArrayLength, line; chars = ""; // Walk the text, pulling out a substring for each line. // text.split('\n') would would temporarily double our memory footprint. // Modifying text would create many large strings to garbage collect. lineStart = 0; lineEnd = -1; // Keeping our own length variable is faster than looking it up. lineArrayLength = lineArray.length; while ( lineEnd < text.length - 1 ) { lineEnd = text.indexOf( "\n", lineStart ); if ( lineEnd === -1 ) { lineEnd = text.length - 1; } line = text.substring( lineStart, lineEnd + 1 ); lineStart = lineEnd + 1; if ( lineHash.hasOwnProperty ? lineHash.hasOwnProperty( line ) : ( lineHash[ line ] !== undefined ) ) { chars += String.fromCharCode( lineHash[ line ] ); } else { chars += String.fromCharCode( lineArrayLength ); lineHash[ line ] = lineArrayLength; lineArray[ lineArrayLength++ ] = line; } } return chars; } chars1 = diffLinesToCharsMunge( text1 ); chars2 = diffLinesToCharsMunge( text2 ); return { chars1: chars1, chars2: chars2, lineArray: lineArray }; }; /** * Rehydrate the text in a diff from a string of line hashes to real lines of * text. * @param {!Array.<!DiffMatchPatch.Diff>} diffs Array of diff tuples. * @param {!Array.<string>} lineArray Array of unique strings. * @private */ DiffMatchPatch.prototype.diffCharsToLines = function( diffs, lineArray ) { var x, chars, text, y; for ( x = 0; x < diffs.length; x++ ) { chars = diffs[ x ][ 1 ]; text = []; for ( y = 0; y < chars.length; y++ ) { text[ y ] = lineArray[ chars.charCodeAt( y ) ]; } diffs[ x ][ 1 ] = text.join( "" ); } }; /** * Reorder and merge like edit sections. Merge equalities. * Any edit section can move as long as it doesn't cross an equality. * @param {!Array.<!DiffMatchPatch.Diff>} diffs Array of diff tuples. */ DiffMatchPatch.prototype.diffCleanupMerge = function( diffs ) { var pointer, countDelete, countInsert, textInsert, textDelete, commonlength, changes, diffPointer, position; diffs.push( [ DIFF_EQUAL, "" ] ); // Add a dummy entry at the end. pointer = 0; countDelete = 0; countInsert = 0; textDelete = ""; textInsert = ""; commonlength; while ( pointer < diffs.length ) { switch ( diffs[ pointer ][ 0 ] ) { case DIFF_INSERT: countInsert++; textInsert += diffs[ pointer ][ 1 ]; pointer++; break; case DIFF_DELETE: countDelete++; textDelete += diffs[ pointer ][ 1 ]; pointer++; break; case DIFF_EQUAL: // Upon reaching an equality, check for prior redundancies. if ( countDelete + countInsert > 1 ) { if ( countDelete !== 0 && countInsert !== 0 ) { // Factor out any common prefixies. commonlength = this.diffCommonPrefix( textInsert, textDelete ); if ( commonlength !== 0 ) { if ( ( pointer - countDelete - countInsert ) > 0 && diffs[ pointer - countDelete - countInsert - 1 ][ 0 ] === DIFF_EQUAL ) { diffs[ pointer - countDelete - countInsert - 1 ][ 1 ] += textInsert.substring( 0, commonlength ); } else { diffs.splice( 0, 0, [ DIFF_EQUAL, textInsert.substring( 0, commonlength ) ] ); pointer++; } textInsert = textInsert.substring( commonlength ); textDelete = textDelete.substring( commonlength ); } // Factor out any common suffixies. commonlength = this.diffCommonSuffix( textInsert, textDelete ); if ( commonlength !== 0 ) { diffs[ pointer ][ 1 ] = textInsert.substring( textInsert.length - commonlength ) + diffs[ pointer ][ 1 ]; textInsert = textInsert.substring( 0, textInsert.length - commonlength ); textDelete = textDelete.substring( 0, textDelete.length - commonlength ); } } // Delete the offending records and add the merged ones. if ( countDelete === 0 ) { diffs.splice( pointer - countInsert, countDelete + countInsert, [ DIFF_INSERT, textInsert ] ); } else if ( countInsert === 0 ) { diffs.splice( pointer - countDelete, countDelete + countInsert, [ DIFF_DELETE, textDelete ] ); } else { diffs.splice( pointer - countDelete - countInsert, countDelete + countInsert, [ DIFF_DELETE, textDelete ], [ DIFF_INSERT, textInsert ] ); } pointer = pointer - countDelete - countInsert + ( countDelete ? 1 : 0 ) + ( countInsert ? 1 : 0 ) + 1; } else if ( pointer !== 0 && diffs[ pointer - 1 ][ 0 ] === DIFF_EQUAL ) { // Merge this equality with the previous one. diffs[ pointer - 1 ][ 1 ] += diffs[ pointer ][ 1 ]; diffs.splice( pointer, 1 ); } else { pointer++; } countInsert = 0; countDelete = 0; textDelete = ""; textInsert = ""; break; } } if ( diffs[ diffs.length - 1 ][ 1 ] === "" ) { diffs.pop(); // Remove the dummy entry at the end. } // Second pass: look for single edits surrounded on both sides by equalities // which can be shifted sideways to eliminate an equality. // e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC changes = false; pointer = 1; // Intentionally ignore the first and last element (don't need checking). while ( pointer < diffs.length - 1 ) { if ( diffs[ pointer - 1 ][ 0 ] === DIFF_EQUAL && diffs[ pointer + 1 ][ 0 ] === DIFF_EQUAL ) { diffPointer = diffs[ pointer ][ 1 ]; position = diffPointer.substring( diffPointer.length - diffs[ pointer - 1 ][ 1 ].length ); // This is a single edit surrounded by equalities. if ( position === diffs[ pointer - 1 ][ 1 ] ) { // Shift the edit over the previous equality. diffs[ pointer ][ 1 ] = diffs[ pointer - 1 ][ 1 ] + diffs[ pointer ][ 1 ].substring( 0, diffs[ pointer ][ 1 ].length - diffs[ pointer - 1 ][ 1 ].length ); diffs[ pointer + 1 ][ 1 ] = diffs[ pointer - 1 ][ 1 ] + diffs[ pointer + 1 ][ 1 ]; diffs.splice( pointer - 1, 1 ); changes = true; } else if ( diffPointer.substring( 0, diffs[ pointer + 1 ][ 1 ].length ) === diffs[ pointer + 1 ][ 1 ] ) { // Shift the edit over the next equality. diffs[ pointer - 1 ][ 1 ] += diffs[ pointer + 1 ][ 1 ]; diffs[ pointer ][ 1 ] = diffs[ pointer ][ 1 ].substring( diffs[ pointer + 1 ][ 1 ].length ) + diffs[ pointer + 1 ][ 1 ]; diffs.splice( pointer + 1, 1 ); changes = true; } } pointer++; } // If shifts were made, the diff needs reordering and another shift sweep. if ( changes ) { this.diffCleanupMerge( diffs ); } }; return function( o, n ) { var diff, output, text; diff = new DiffMatchPatch(); output = diff.DiffMain( o, n ); diff.diffCleanupEfficiency( output ); text = diff.diffPrettyHtml( output ); return text; }; }() );