2015-07-11 22:38:08 +00:00
|
|
|
function require(){return global}var global=this;!function(global,undefined){var JsDiff=function(){function map(arr,mapper,that){if(Array.prototype.map)return Array.prototype.map.call(arr,mapper,that);for(var other=new Array(arr.length),i=0,n=arr.length;n>i;i++)other[i]=mapper.call(that,arr[i],i,arr);return other}function clonePath(path){return{newPos:path.newPos,components:path.components.slice(0)}}function removeEmpty(array){for(var ret=[],i=0;i<array.length;i++)array[i]&&ret.push(array[i]);return ret}function escapeHTML(s){var n=s;return n=n.replace(/&/g,"&"),n=n.replace(/</g,"<"),n=n.replace(/>/g,">"),n=n.replace(/"/g,""")}function buildValues(components,newString,oldString,useLongestToken){for(var componentPos=0,componentLen=components.length,newPos=0,oldPos=0;componentLen>componentPos;componentPos++){var component=components[componentPos];if(component.removed)component.value=oldString.slice(oldPos,oldPos+component.count).join(""),oldPos+=component.count;else{if(!component.added&&useLongestToken){var value=newString.slice(newPos,newPos+component.count);value=map(value,function(value,i){var oldValue=oldString[oldPos+i];return oldValue.length>value.length?oldValue:value}),component.value=value.join("")}else component.value=newString.slice(newPos,newPos+component.count).join("");newPos+=component.count,component.added||(oldPos+=component.count)}}return components}function canonicalize(obj,stack,replacementStack){stack=stack||[],replacementStack=replacementStack||[];for(var i,i=0;i<stack.length;i+=1)if(stack[i]===obj)return replacementStack[i];var canonicalizedObj;if("[object Array]"===objectPrototypeToString.call(obj)){for(stack.push(obj),canonicalizedObj=new Array(obj.length),replacementStack.push(canonicalizedObj),i=0;i<obj.length;i+=1)canonicalizedObj[i]=canonicalize(obj[i],stack,replacementStack);stack.pop(),replacementStack.pop()}else if("object"==typeof obj&&null!==obj){stack.push(obj),canonicalizedObj={},replacementStack.push(canonicalizedObj);var sortedKeys=[];for(var key in obj)sortedKeys.push(key);for(sortedKeys.sort(),i=0;i<sortedKeys.length;i+=1){var key=sortedKeys[i];canonicalizedObj[key]=canonicalize(obj[key],stack,replacementStack)}stack.pop(),replacementStack.pop()}else canonicalizedObj=obj;return canonicalizedObj}var Diff=function(ignoreWhitespace){this.ignoreWhitespace=ignoreWhitespace};Diff.prototype={diff:function(oldString,newString,callback){function done(value){return callback?(setTimeout(function(){callback(undefined,value)},0),!0):value}function execEditLength(){for(var diagonalPath=-1*editLength;editLength>=diagonalPath;diagonalPath+=2){var basePath,addPath=bestPath[diagonalPath-1],removePath=bestPath[diagonalPath+1];oldPos=(removePath?removePath.newPos:0)-diagonalPath,addPath&&(bestPath[diagonalPath-1]=undefined);var canAdd=addPath&&addPath.newPos+1<newLen,canRemove=removePath&&oldPos>=0&&oldLen>oldPos;if(canAdd||canRemove){!canAdd||canRemove&&addPath.newPos<removePath.newPos?(basePath=clonePath(removePath),self.pushComponent(basePath.components,undefined,!0)):(basePath=addPath,basePath.newPos++,self.pushComponent(basePath.components,!0,undefined));var oldPos=self.extractCommon(basePath,newString,oldString,diagonalPath);if(basePath.newPos+1>=newLen&&oldPos+1>=oldLen)return done(buildValues(basePath.components,newString,oldString,self.useLongestToken));bestPath[diagonalPath]=basePath}else bestPath[diagonalPath]=undefined}editLength++}var self=this;if(newString===oldString)return done([{value:newString}]);if(!newString)return done([{value:oldString,removed:!0}]);if(!oldString)return done([{value:newString,added:!0}]);newString=this.tokenize(newString),oldString=this.tokenize(oldString);var newLen=newString.length,oldLen=oldString.length,maxEditLength=newLen+oldLen,bestPath=[{newPos:-1,components:[]}],oldPos=this.extractCommon(bestPath[0],newString,oldString,0);if(bestPath[0].newPos+1>=newLen&&oldPos+1>=oldLen)return done([{value:newString.join("")}]);var editLength=1;if(callback)!function exec(){setTimeout(function(){return editLength>maxEditLength?callback():void(execEditLength
|