Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle windows newlines on non windows machines. #24

Merged
merged 1 commit into from
Dec 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,20 @@ var JsDiff = (function() {

var LineDiff = new Diff();
LineDiff.tokenize = function(value) {
return value.split(/^/m);
var retLines = [];
var lines = value.split(/^/m);

for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var lastLine = lines[i - 1];

if(line == '\n' && lastLine && lastLine.indexOf('\r') == lastLine.length - 1)
retLines[retLines.length - 1] += '\n';
else if(line)
retLines.push(line);
}

return retLines;
};

return {
Expand Down
7 changes: 7 additions & 0 deletions test/diffTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ describe('#diffLines', function() {
'line\nvalue\nline');
diff.convertChangesToXML(diffResult).should.equal('line\n<ins>value\n</ins><del>value \n</del>line');
});

it('should handle windows line endings', function() {
var diffResult = diff.diffLines(
'line\r\nold value \r\nline',
'line\r\nnew value\r\nline');
diff.convertChangesToXML(diffResult).should.equal('line\r\n<ins>new value\r\n</ins><del>old value \r\n</del>line');
});
});

describe('convertToDMP', function() {
Expand Down