Skip to content

Commit 9d69baa

Browse files
committed
Fix #180 & #211: threat each newline as separate word in diffWordsWithSpace
1 parent 305a2b3 commit 9d69baa

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/diff/word.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ wordDiff.equals = function(left, right) {
3232
return left === right || (this.options.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right));
3333
};
3434
wordDiff.tokenize = function(value) {
35-
let tokens = value.split(/(\s+|[()[\]{}'"]|\b)/);
35+
// All whitespace symbols except newline group into one token, each newline - in separate token
36+
let tokens = value.split(/([^\S\r\n]+|[()[\]{}'"\r\n]|\b)/);
3637

3738
// Join the boundary splits that we do not consider to be boundaries. This is primarily the extended Latin character set.
3839
for (let i = 0; i < tokens.length - 1; i++) {

test/diff/word.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ describe('WordDiff', function() {
209209
expect(convertChangesToXML(diffResult)).to.equal('&quot;<ins>word</ins>&quot;');
210210
});
211211

212+
it('should threat newline as separate token (issues #180, #211)', function() {
213+
// #180
214+
const diffResult1 = diffWordsWithSpace('foo\nbar', 'foo\n\n\nbar');
215+
expect(convertChangesToXML(diffResult1)).to.equal('foo\n<ins>\n\n</ins>bar');
216+
// #211
217+
const diffResult2 = diffWordsWithSpace('A\n\nB\n', 'A\nB\n');
218+
expect(convertChangesToXML(diffResult2)).to.equal('A\n<del>\n</del>B\n');
219+
});
220+
212221
it('should perform async operations', function(done) {
213222
diffWordsWithSpace('New Value ', 'New ValueMoreData ', function(err, diffResult) {
214223
expect(convertChangesToXML(diffResult)).to.equal('New<ins> ValueMoreData</ins> <del>Value </del>');

0 commit comments

Comments
 (0)