Skip to content

Commit

Permalink
Fix kpdecker#180 & kpdecker#211: threat each newline as separate word…
Browse files Browse the repository at this point in the history
… in `diffWordsWithSpace`
  • Loading branch information
Mingun committed Mar 10, 2018
1 parent e9ab948 commit 810269e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/diff/word.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ wordDiff.equals = function(left, right) {
return left === right || (this.options.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right));
};
wordDiff.tokenize = function(value) {
let tokens = value.split(/(\s+|\b)/);
// All whitespace symbols except newline group into one token, each newline - in separate token
let tokens = value.split(/([^\S\r\n]+|[\r\n]|\b)/);

// Join the boundary splits that we do not consider to be boundaries. This is primarily the extended Latin character set.
for (let i = 0; i < tokens.length - 1; i++) {
Expand Down
9 changes: 9 additions & 0 deletions test/diff/word.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ describe('WordDiff', function() {
expect(convertChangesToXML(diffResult)).to.equal('New<ins> ValueMoreData</ins> <del>Value </del>');
});

it('should threat newline as separate token (issues #180, #211)', function() {
// #180
const diffResult1 = diffWordsWithSpace('foo\nbar', 'foo\n\n\nbar');
expect(convertChangesToXML(diffResult1)).to.equal('foo\n<ins>\n\n</ins>bar');
// #211
const diffResult2 = diffWordsWithSpace('A\n\nB\n', 'A\nB\n');
expect(convertChangesToXML(diffResult2)).to.equal('A\n<del>\n</del>B\n');
});

it('should perform async operations', function(done) {
diffWordsWithSpace('New Value ', 'New ValueMoreData ', function(err, diffResult) {
expect(convertChangesToXML(diffResult)).to.equal('New<ins> ValueMoreData</ins> <del>Value </del>');
Expand Down

0 comments on commit 810269e

Please sign in to comment.