diff --git a/javascript/diff_match_patch.css b/javascript/diff_match_patch.css new file mode 100644 index 0000000..3468699 --- /dev/null +++ b/javascript/diff_match_patch.css @@ -0,0 +1,7 @@ +.ins_style { + background: #e6ffe6; +} + +.del_style { + background: #ffe6e6; +} diff --git a/javascript/diff_match_patch.js b/javascript/diff_match_patch.js index 2fe320a..a92db59 100644 --- a/javascript/diff_match_patch.js +++ b/javascript/diff_match_patch.js @@ -1,55 +1 @@ -var diff_match_patch=function(){this.Diff_Timeout=1;this.Diff_EditCost=4;this.Match_Threshold=.5;this.Match_Distance=1E3;this.Patch_DeleteThreshold=.5;this.Patch_Margin=4;this.Match_MaxBits=32},DIFF_DELETE=-1,DIFF_INSERT=1,DIFF_EQUAL=0;diff_match_patch.Diff=function(a,b){this[0]=a;this[1]=b};diff_match_patch.Diff.prototype.length=2;diff_match_patch.Diff.prototype.toString=function(){return this[0]+","+this[1]}; -diff_match_patch.prototype.diff_main=function(a,b,c,d){"undefined"==typeof d&&(d=0>=this.Diff_Timeout?Number.MAX_VALUE:(new Date).getTime()+1E3*this.Diff_Timeout);if(null==a||null==b)throw Error("Null input. (diff_main)");if(a==b)return a?[new diff_match_patch.Diff(DIFF_EQUAL,a)]:[];"undefined"==typeof c&&(c=!0);var e=c,f=this.diff_commonPrefix(a,b);c=a.substring(0,f);a=a.substring(f);b=b.substring(f);f=this.diff_commonSuffix(a,b);var g=a.substring(a.length-f);a=a.substring(0,a.length-f);b=b.substring(0, -b.length-f);a=this.diff_compute_(a,b,e,d);c&&a.unshift(new diff_match_patch.Diff(DIFF_EQUAL,c));g&&a.push(new diff_match_patch.Diff(DIFF_EQUAL,g));this.diff_cleanupMerge(a);return a}; -diff_match_patch.prototype.diff_compute_=function(a,b,c,d){if(!a)return[new diff_match_patch.Diff(DIFF_INSERT,b)];if(!b)return[new diff_match_patch.Diff(DIFF_DELETE,a)];var e=a.length>b.length?a:b,f=a.length>b.length?b:a,g=e.indexOf(f);return-1!=g?(c=[new diff_match_patch.Diff(DIFF_INSERT,e.substring(0,g)),new diff_match_patch.Diff(DIFF_EQUAL,f),new diff_match_patch.Diff(DIFF_INSERT,e.substring(g+f.length))],a.length>b.length&&(c[0][0]=c[2][0]=DIFF_DELETE),c):1==f.length?[new diff_match_patch.Diff(DIFF_DELETE, -a),new diff_match_patch.Diff(DIFF_INSERT,b)]:(e=this.diff_halfMatch_(a,b))?(b=e[1],f=e[3],a=e[4],e=this.diff_main(e[0],e[2],c,d),c=this.diff_main(b,f,c,d),e.concat([new diff_match_patch.Diff(DIFF_EQUAL,a)],c)):c&&100c);t++){for(var v=-t+p;v<=t-x;v+=2){var n=f+v;var r=v==-t||v!=t&&h[n-1]d)x+=2;else if(y>e)p+=2;else if(m&&(n=f+k-v,0<=n&&n= -u)return this.diff_bisectSplit_(a,b,r,y,c)}}for(v=-t+w;v<=t-q;v+=2){n=f+v;u=v==-t||v!=t&&l[n-1]d)q+=2;else if(r>e)w+=2;else if(!m&&(n=f+k-v,0<=n&&n=u)))return this.diff_bisectSplit_(a,b,r,y,c)}}return[new diff_match_patch.Diff(DIFF_DELETE,a),new diff_match_patch.Diff(DIFF_INSERT,b)]}; -diff_match_patch.prototype.diff_bisectSplit_=function(a,b,c,d,e){var f=a.substring(0,c),g=b.substring(0,d);a=a.substring(c);b=b.substring(d);f=this.diff_main(f,g,!1,e);e=this.diff_main(a,b,!1,e);return f.concat(e)}; -diff_match_patch.prototype.diff_linesToChars_=function(a,b){function c(a){for(var b="",c=0,g=-1,h=d.length;gd?a=a.substring(c-d):c=a.length?[h,k,l,m,g]:null}if(0>=this.Diff_Timeout)return null; -var d=a.length>b.length?a:b,e=a.length>b.length?b:a;if(4>d.length||2*e.lengthd[4].length?g:d:d:g;else return null;if(a.length>b.length){d=g[0];e=g[1];var h=g[2];var l=g[3]}else h=g[0],l=g[1],d=g[2],e=g[3];return[d,e,h,l,g[4]]}; -diff_match_patch.prototype.diff_cleanupSemantic=function(a){for(var b=!1,c=[],d=0,e=null,f=0,g=0,h=0,l=0,k=0;f=e){if(d>=b.length/2||d>=c.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,c.substring(0,d))),a[f-1][1]=b.substring(0,b.length-d),a[f+1][1]=c.substring(d),f++}else if(e>=b.length/2||e>=c.length/2)a.splice(f,0,new diff_match_patch.Diff(DIFF_EQUAL,b.substring(0,e))),a[f-1][0]=DIFF_INSERT,a[f-1][1]=c.substring(0,c.length-e),a[f+1][0]=DIFF_DELETE, -a[f+1][1]=b.substring(e),f++;f++}f++}}; -diff_match_patch.prototype.diff_cleanupSemanticLossless=function(a){function b(a,b){if(!a||!b)return 6;var c=a.charAt(a.length-1),d=b.charAt(0),e=c.match(diff_match_patch.nonAlphaNumericRegex_),f=d.match(diff_match_patch.nonAlphaNumericRegex_),g=e&&c.match(diff_match_patch.whitespaceRegex_),h=f&&d.match(diff_match_patch.whitespaceRegex_);c=g&&c.match(diff_match_patch.linebreakRegex_);d=h&&d.match(diff_match_patch.linebreakRegex_);var k=c&&a.match(diff_match_patch.blanklineEndRegex_),l=d&&b.match(diff_match_patch.blanklineStartRegex_); -return k||l?5:c||d?4:e&&!g&&h?3:g||h?2:e||f?1:0}for(var c=1;c=k&&(k=m,g=d,h=e,l=f)}a[c-1][1]!=g&&(g?a[c-1][1]=g:(a.splice(c- -1,1),c--),a[c][1]=h,l?a[c+1][1]=l:(a.splice(c+1,1),c--))}c++}};diff_match_patch.nonAlphaNumericRegex_=/[^a-zA-Z0-9]/;diff_match_patch.whitespaceRegex_=/\s/;diff_match_patch.linebreakRegex_=/[\r\n]/;diff_match_patch.blanklineEndRegex_=/\n\r?\n$/;diff_match_patch.blanklineStartRegex_=/^\r?\n\r?\n/; -diff_match_patch.prototype.diff_cleanupEfficiency=function(a){for(var b=!1,c=[],d=0,e=null,f=0,g=!1,h=!1,l=!1,k=!1;fb)break;e=c;f=d}return a.length!=g&&a[g][0]===DIFF_DELETE?f:f+(b-e)}; -diff_match_patch.prototype.diff_prettyHtml=function(a){for(var b=[],c=/&/g,d=//g,f=/\n/g,g=0;g");switch(h){case DIFF_INSERT:b[g]=''+l+"";break;case DIFF_DELETE:b[g]=''+l+"";break;case DIFF_EQUAL:b[g]=""+l+""}}return b.join("")}; -diff_match_patch.prototype.diff_text1=function(a){for(var b=[],c=0;cthis.Match_MaxBits)throw Error("Pattern too long for this browser.");var e=this.match_alphabet_(b),f=this,g=this.Match_Threshold,h=a.indexOf(b,c);-1!=h&&(g=Math.min(d(0,h),g),h=a.lastIndexOf(b,c+b.length),-1!=h&&(g=Math.min(d(0,h),g)));var l=1<=k;q--){var t=e[a.charAt(q-1)];m[q]=0===w?(m[q+1]<<1|1)&t:(m[q+1]<<1|1)&t|(x[q+1]|x[q])<<1|1|x[q+1];if(m[q]&l&&(t=d(w,q-1),t<=g))if(g=t,h=q-1,h>c)k=Math.max(1,2*c-h);else break}if(d(w+1,c)>g)break;x=m}return h}; -diff_match_patch.prototype.match_alphabet_=function(a){for(var b={},c=0;c=2*this.Patch_Margin&&e&&(this.patch_addContext_(a,h),c.push(a),a=new diff_match_patch.patch_obj,e=0,h=d,f=g)}k!==DIFF_INSERT&&(f+=m.length);k!==DIFF_DELETE&&(g+=m.length)}e&&(this.patch_addContext_(a,h),c.push(a));return c}; -diff_match_patch.prototype.patch_deepCopy=function(a){for(var b=[],c=0;cthis.Match_MaxBits){var k=this.match_main(b,h.substring(0,this.Match_MaxBits),g);-1!=k&&(l=this.match_main(b,h.substring(h.length-this.Match_MaxBits),g+h.length-this.Match_MaxBits),-1==l||k>=l)&&(k=-1)}else k=this.match_main(b,h, -g);if(-1==k)e[f]=!1,d-=a[f].length2-a[f].length1;else if(e[f]=!0,d=k-g,g=-1==l?b.substring(k,k+h.length):b.substring(k,l+this.Match_MaxBits),h==g)b=b.substring(0,k)+this.diff_text2(a[f].diffs)+b.substring(k+h.length);else if(g=this.diff_main(h,g,!1),h.length>this.Match_MaxBits&&this.diff_levenshtein(g)/h.length>this.Patch_DeleteThreshold)e[f]=!1;else{this.diff_cleanupSemanticLossless(g);h=0;var m;for(l=0;le[0][1].length){var f=b-e[0][1].length;e[0][1]=c.substring(e[0][1].length)+e[0][1];d.start1-=f;d.start2-=f;d.length1+=f;d.length2+=f}d=a[a.length-1];e=d.diffs; -0==e.length||e[e.length-1][0]!=DIFF_EQUAL?(e.push(new diff_match_patch.Diff(DIFF_EQUAL,c)),d.length1+=b,d.length2+=b):b>e[e.length-1][1].length&&(f=b-e[e.length-1][1].length,e[e.length-1][1]+=c.substring(0,f),d.length1+=f,d.length2+=f);return c}; -diff_match_patch.prototype.patch_splitMax=function(a){for(var b=this.Match_MaxBits,c=0;c2*b?(h.length1+=k.length,e+=k.length,l=!1,h.diffs.push(new diff_match_patch.Diff(g,k)),d.diffs.shift()):(k=k.substring(0,b-h.length1-this.Patch_Margin),h.length1+=k.length,e+=k.length,g===DIFF_EQUAL?(h.length2+=k.length,f+=k.length):l=!1,h.diffs.push(new diff_match_patch.Diff(g,k)),k==d.diffs[0][1]?d.diffs.shift():d.diffs[0][1]=d.diffs[0][1].substring(k.length))}g=this.diff_text2(h.diffs); -g=g.substring(g.length-this.Patch_Margin);k=this.diff_text1(d.diffs).substring(0,this.Patch_Margin);""!==k&&(h.length1+=k.length,h.length2+=k.length,0!==h.diffs.length&&h.diffs[h.diffs.length-1][0]===DIFF_EQUAL?h.diffs[h.diffs.length-1][1]+=k:h.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL,k)));l||a.splice(++c,0,h)}}};diff_match_patch.prototype.patch_toText=function(a){for(var b=[],c=0;ci.length?t:i,a=t.length>i.length?i:t,r=n.indexOf(a);if(-1!=r)return f=[new diff_match_patch.Diff(DIFF_INSERT,n.substring(0,r)),new diff_match_patch.Diff(DIFF_EQUAL,a),new diff_match_patch.Diff(DIFF_INSERT,n.substring(r+a.length))],t.length>i.length&&(f[0][0]=f[2][0]=DIFF_DELETE),f;if(1==a.length)return[new diff_match_patch.Diff(DIFF_DELETE,t),new diff_match_patch.Diff(DIFF_INSERT,i)];var s=this.diff_halfMatch_(t,i);if(s){var _=s[0],c=s[1],l=s[2],g=s[3],o=s[4],p=this.diff_main(_,l,h,e),d=this.diff_main(c,g,h,e);return p.concat([new diff_match_patch.Diff(DIFF_EQUAL,o)],d)}return h&&100h);m++){for(var F=-m+o;F<=m-p;F+=2){for(var D=a+F,E=(L=F==-m||F!=m&&s[D-1]i.length?t:i,e=t.length>i.length?i:t;if(h.length<4||2*e.length=t.length?[e,f,n,a,_]:null}var n,a,r,s,_,c=f(h,e,Math.ceil(h.length/4)),l=f(h,e,Math.ceil(h.length/2));return c||l?(n=l?c&&c[4].length>l[4].length?c:l:c,t.length>i.length?(a=n[0],r=n[1],s=n[2],_=n[3]):(s=n[0],_=n[1],a=n[2],r=n[3]),[a,r,s,_,n[4]]):null},diff_match_patch.prototype.diff_cleanupSemantic=function(t){for(var i=!1,h=[],e=0,f=null,n=0,a=0,r=0,s=0,_=0;n=c.length/2||g>=l.length/2)&&(t.splice(n,0,new diff_match_patch.Diff(DIFF_EQUAL,l.substring(0,g))),t[n-1][1]=c.substring(0,c.length-g),t[n+1][1]=l.substring(g),n++):(o>=c.length/2||o>=l.length/2)&&(t.splice(n,0,new diff_match_patch.Diff(DIFF_EQUAL,c.substring(0,o))),t[n-1][0]=DIFF_INSERT,t[n-1][1]=l.substring(0,l.length-o),t[n+1][0]=DIFF_DELETE,t[n+1][1]=c.substring(o),n++),n++}n++}},diff_match_patch.prototype.diff_cleanupSemanticLossless=function(t){function i(t,i){if(!t||!i)return 6;var h=t.charAt(t.length-1),e=i.charAt(0),f=h.match(diff_match_patch.nonAlphaNumericRegex_),n=e.match(diff_match_patch.nonAlphaNumericRegex_),a=f&&h.match(diff_match_patch.whitespaceRegex_),r=n&&e.match(diff_match_patch.whitespaceRegex_),s=a&&h.match(diff_match_patch.linebreakRegex_),_=r&&e.match(diff_match_patch.linebreakRegex_),c=s&&t.match(diff_match_patch.blanklineEndRegex_),l=_&&i.match(diff_match_patch.blanklineStartRegex_);return c||l?5:s||_?4:f&&!a&&r?3:a||r?2:f||n?1:0}for(var h=1;h/g,n=/\n/g;if(this.Style_Injection)var a='style="background:#e6ffe6;"',r='style="background:#ffe6e6;"';else a='class="ins_style"',r='class="del_style"';for(var s=0;s");switch(_){case DIFF_INSERT:i[s]=""+c+"";break;case DIFF_DELETE:i[s]=""+c+"";break;case DIFF_EQUAL:i[s]=""+c+""}}return i.join("")},diff_match_patch.prototype.diff_text1=function(t){for(var i=[],h=0;hthis.Match_MaxBits)throw new Error("Pattern too long for this browser.");var i=this.match_alphabet_(f),a=this;function h(t,i){var h=t/f.length,e=Math.abs(n-i);return a.Match_Distance?h+e/a.Match_Distance:e?1:h}var e=this.Match_Threshold,r=t.indexOf(f,n);-1!=r&&(e=Math.min(h(0,r),e),-1!=(r=t.lastIndexOf(f,n+f.length))&&(e=Math.min(h(0,r),e)));var s,_,c=1<e)break;l=u}return r},diff_match_patch.prototype.match_alphabet_=function(t){for(var i={},h=0;h=2*this.Patch_Margin&&r&&(this.patch_addContext_(a,c),n.push(a),a=new diff_match_patch.patch_obj,r=0,c=l,s=_)}o!==DIFF_INSERT&&(s+=p.length),o!==DIFF_DELETE&&(_+=p.length)}return r&&(this.patch_addContext_(a,c),n.push(a)),n},diff_match_patch.prototype.patch_deepCopy=function(t){for(var i=[],h=0;hthis.Match_MaxBits?-1!=(a=this.match_main(i,_.substring(0,this.Match_MaxBits),s))&&(-1==(c=this.match_main(i,_.substring(_.length-this.Match_MaxBits),s+_.length-this.Match_MaxBits))||c<=a)&&(a=-1):a=this.match_main(i,_,s),-1==a)f[n]=!1,e-=t[n].length2-t[n].length1;else if(f[n]=!0,e=a-s,_==(r=-1==c?i.substring(a,a+_.length):i.substring(a,c+this.Match_MaxBits)))i=i.substring(0,a)+this.diff_text2(t[n].diffs)+i.substring(a+_.length);else{var l=this.diff_main(_,r,!1);if(_.length>this.Match_MaxBits&&this.diff_levenshtein(l)/_.length>this.Patch_DeleteThreshold)f[n]=!1;else{this.diff_cleanupSemanticLossless(l);for(var g,o=0,p=0;pn[0][1].length){var a=i-n[0][1].length;n[0][1]=h.substring(n[0][1].length)+n[0][1],f.start1-=a,f.start2-=a,f.length1+=a,f.length2+=a}if(0==(n=(f=t[t.length-1]).diffs).length||n[n.length-1][0]!=DIFF_EQUAL)n.push(new diff_match_patch.Diff(DIFF_EQUAL,h)),f.length1+=i,f.length2+=i;else if(i>n[n.length-1][1].length){a=i-n[n.length-1][1].length;n[n.length-1][1]+=h.substring(0,a),f.length1+=a,f.length2+=a}return h},diff_match_patch.prototype.patch_splitMax=function(t){for(var i=this.Match_MaxBits,h=0;h2*i?(r.length1+=c.length,f+=c.length,s=!1,r.diffs.push(new diff_match_patch.Diff(_,c)),e.diffs.shift()):(c=c.substring(0,i-r.length1-this.Patch_Margin),r.length1+=c.length,f+=c.length,_===DIFF_EQUAL?(r.length2+=c.length,n+=c.length):s=!1,r.diffs.push(new diff_match_patch.Diff(_,c)),c==e.diffs[0][1]?e.diffs.shift():e.diffs[0][1]=e.diffs[0][1].substring(c.length))}a=(a=this.diff_text2(r.diffs)).substring(a.length-this.Patch_Margin);var l=this.diff_text1(e.diffs).substring(0,this.Patch_Margin);""!==l&&(r.length1+=l.length,r.length2+=l.length,0!==r.diffs.length&&r.diffs[r.diffs.length-1][0]===DIFF_EQUAL?r.diffs[r.diffs.length-1][1]+=l:r.diffs.push(new diff_match_patch.Diff(DIFF_EQUAL,l))),s||t.splice(++h,0,r)}}},diff_match_patch.prototype.patch_toText=function(t){for(var i=[],h=0;h/g; var pattern_para = /\n/g; + if (this.Style_Injection) { + var ins_style = 'style="background:#e6ffe6;"' + var del_style = 'style="background:#ffe6e6;"' + } else { + var ins_style = 'class="ins_style"' + var del_style = 'class="del_style"' + } + for (var x = 0; x < diffs.length; x++) { var op = diffs[x][0]; // Operation (insert, delete, equal) var data = diffs[x][1]; // Text of change. @@ -1261,10 +1273,10 @@ diff_match_patch.prototype.diff_prettyHtml = function(diffs) { .replace(pattern_gt, '>').replace(pattern_para, '¶
'); switch (op) { case DIFF_INSERT: - html[x] = '' + text + ''; + html[x] = '' + text + ''; break; case DIFF_DELETE: - html[x] = '' + text + ''; + html[x] = '' + text + ''; break; case DIFF_EQUAL: html[x] = '' + text + '';