|
15 | 15 | * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927
|
16 | 16 | */
|
17 | 17 | (function(global, undefined) {
|
| 18 | + |
18 | 19 | var JsDiff = (function() {
|
19 | 20 | /*jshint maxparams: 5*/
|
20 | 21 | /*istanbul ignore next*/
|
|
174 | 175 | editLength++;
|
175 | 176 | }
|
176 | 177 |
|
177 |
| - // Performs the length of edit iteration. Is a bit fugly as this has to support the |
| 178 | + // Performs the length of edit iteration. Is a bit fugly as this has to support the |
178 | 179 | // sync and async mode which is never fun. Loops over execEditLength until a value
|
179 | 180 | // is produced.
|
180 | 181 | var editLength = 1;
|
|
256 | 257 | };
|
257 | 258 |
|
258 | 259 | var LineDiff = new Diff();
|
259 |
| - LineDiff.tokenize = function(value) { |
| 260 | + |
| 261 | + var TrimmedLineDiff = new Diff(); |
| 262 | + TrimmedLineDiff.ignoreTrim = true; |
| 263 | + |
| 264 | + LineDiff.tokenize = TrimmedLineDiff.tokenize = function(value) { |
260 | 265 | var retLines = [],
|
261 | 266 | lines = value.split(/^/m);
|
262 |
| - |
263 | 267 | for(var i = 0; i < lines.length; i++) {
|
264 | 268 | var line = lines[i],
|
265 |
| - lastLine = lines[i - 1]; |
| 269 | + lastLine = lines[i - 1], |
| 270 | + lastLineLastChar = lastLine ? lastLine[lastLine.length - 1] : ''; |
266 | 271 |
|
267 | 272 | // Merge lines that may contain windows new lines
|
268 |
| - if (line === '\n' && lastLine && lastLine[lastLine.length - 1] === '\r') { |
269 |
| - retLines[retLines.length - 1] += '\n'; |
| 273 | + if (line === '\n' && (lastLineLastChar === '\r' || lastLineLastChar === '\n')) { |
| 274 | + if (this.ignoreTrim || lastLineLastChar === '\n'){ |
| 275 | + //to avoid merging to \n\n, remove \n and add \r\n. |
| 276 | + retLines[retLines.length - 1] = retLines[retLines.length - 1].slice(0,-1) + '\r\n'; |
| 277 | + } else { |
| 278 | + retLines[retLines.length - 1] += '\n'; |
| 279 | + } |
270 | 280 | } else if (line) {
|
| 281 | + if (this.ignoreTrim) { |
| 282 | + line = line.trim(); |
| 283 | + //add a newline unless this is the last line. |
| 284 | + if (i < lines.length - 1) { |
| 285 | + line += '\n'; |
| 286 | + } |
| 287 | + } |
271 | 288 | retLines.push(line);
|
272 | 289 | }
|
273 | 290 | }
|
274 | 291 |
|
275 | 292 | return retLines;
|
276 | 293 | };
|
277 | 294 |
|
| 295 | + |
278 | 296 | var SentenceDiff = new Diff();
|
279 | 297 | SentenceDiff.tokenize = function (value) {
|
280 | 298 | return removeEmpty(value.split(/(\S.+?[.!?])(?=\s+|$)/));
|
|
344 | 362 | diffWords: function(oldStr, newStr, callback) { return WordDiff.diff(oldStr, newStr, callback); },
|
345 | 363 | diffWordsWithSpace: function(oldStr, newStr, callback) { return WordWithSpaceDiff.diff(oldStr, newStr, callback); },
|
346 | 364 | diffLines: function(oldStr, newStr, callback) { return LineDiff.diff(oldStr, newStr, callback); },
|
| 365 | + diffTrimmedLines: function(oldStr, newStr, callback) { return TrimmedLineDiff.diff(oldStr, newStr, callback); }, |
| 366 | + |
347 | 367 | diffSentences: function(oldStr, newStr, callback) { return SentenceDiff.diff(oldStr, newStr, callback); },
|
348 | 368 |
|
349 | 369 | diffCss: function(oldStr, newStr, callback) { return CssDiff.diff(oldStr, newStr, callback); },
|
|
447 | 467 | addEOFNL = false;
|
448 | 468 |
|
449 | 469 | for (var i = (diffstr[0][0]==='I'?4:0); i < diffstr.length; i++) {
|
450 |
| - if(diffstr[i][0] === '@') { |
| 470 | + if (diffstr[i][0] === '@') { |
451 | 471 | var meh = diffstr[i].split(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/);
|
452 | 472 | diff.unshift({
|
453 | 473 | start:meh[3],
|
|
456 | 476 | newlength:meh[4],
|
457 | 477 | newlines:[]
|
458 | 478 | });
|
459 |
| - } else if(diffstr[i][0] === '+') { |
| 479 | + } else if (diffstr[i][0] === '+') { |
460 | 480 | diff[0].newlines.push(diffstr[i].substr(1));
|
461 |
| - } else if(diffstr[i][0] === '-') { |
| 481 | + } else if (diffstr[i][0] === '-') { |
462 | 482 | diff[0].oldlines.push(diffstr[i].substr(1));
|
463 |
| - } else if(diffstr[i][0] === ' ') { |
| 483 | + } else if (diffstr[i][0] === ' ') { |
464 | 484 | diff[0].newlines.push(diffstr[i].substr(1));
|
465 | 485 | diff[0].oldlines.push(diffstr[i].substr(1));
|
466 |
| - } else if(diffstr[i][0] === '\\') { |
| 486 | + } else if (diffstr[i][0] === '\\') { |
467 | 487 | if (diffstr[i-1][0] === '+') {
|
468 | 488 | remEOFNL = true;
|
469 |
| - } else if(diffstr[i-1][0] === '-') { |
| 489 | + } else if (diffstr[i-1][0] === '-') { |
470 | 490 | addEOFNL = true;
|
471 | 491 | }
|
472 | 492 | }
|
|
476 | 496 | for (var i = diff.length - 1; i >= 0; i--) {
|
477 | 497 | var d = diff[i];
|
478 | 498 | for (var j = 0; j < d.oldlength; j++) {
|
479 |
| - if(str[d.start-1+j] !== d.oldlines[j]) { |
| 499 | + if (str[d.start-1+j] !== d.oldlines[j]) { |
480 | 500 | return false;
|
481 | 501 | }
|
482 | 502 | }
|
|
0 commit comments