Skip to content

Commit

Permalink
Merge pull request #41 from madcapsoftware/collapse-spaces-normalize-…
Browse files Browse the repository at this point in the history
…newlines

Normalize newlines
  • Loading branch information
Olegas committed Sep 21, 2019
2 parents 35ecb28 + fbbaa5b commit 582347c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Comparison function can take a third argument with options like this:
var options = {
stripSpaces: true,
compareComments: true,
collapseSpaces: true
collapseSpaces: true,
normalizeNewlines: true
};

result = compare(expected, actual, options);
Expand All @@ -113,6 +114,8 @@ doesn't change the way CDATA sections is compared, they are always compared with
Set `collapseSpaces` option to `true` to automatically collapse all spaces in text and comment nodes.
This option doesn't change the way CDATA sections is compared, they are always compared with respect to
whitespaces.
Set `normalizeNewlines` option to `true` to automatically normalize new line characters in text,
comment, and CDATA nodes.

### Cli utility

Expand Down
7 changes: 7 additions & 0 deletions bin/domcompare
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ argparser.addArgument([ '-s', '--collapsespaces' ], {
help: "Collapse spaces when comparing strings (exclude CDATA nodes)"
});

argparser.addArgument([ '-s', '--normalizenewlines' ], {
defaultValue: false,
dest: 'normalizeNewlines',
action: 'storeTrue',
help: "Normalize newlines when comparing strings (include CDATA nodes)"
});

argparser.addArgument([ '-c', '--comments' ], {
defaultValue: false,
dest: 'compareComments',
Expand Down
8 changes: 8 additions & 0 deletions lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var type = require('./node_types');
var revxpath = require('./revxpath.js');
var collapseSpaces = require('./collapse_spaces');
var normalizeNewlines = require('./normalize_newlines');

var typeMap = {},
comparatorTypeMap = {};
Expand Down Expand Up @@ -38,6 +39,9 @@
if(this._options.collapseSpaces) {
nodeValue = collapseSpaces(nodeValue);
}
if(this._options.normalizeNewlines) {
nodeValue = normalizeNewlines(nodeValue);
}

return "'" + nodeValue + "'";
}
Expand Down Expand Up @@ -107,6 +111,10 @@
vExpected = collapseSpaces(vExpected);
vActual = collapseSpaces(vActual);
}
if(this._options.normalizeNewlines) {
vExpected = normalizeNewlines(vExpected);
vActual = normalizeNewlines(vActual);
}
if(vExpected == vActual)
throw new Error("Nodes are considered equal but shouldn't");
else {
Expand Down
9 changes: 9 additions & 0 deletions lib/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var type = require('./node_types');
var Collector = require('./collector');
var collapseSpaces = require('./collapse_spaces');
var normalizeNewlines = require('./normalize_newlines');

function Comparator(options, collector) {
this._options = options || {};
Expand Down Expand Up @@ -77,6 +78,10 @@
vExpected = collapseSpaces(vExpected);
vActual = collapseSpaces(vActual);
}
if(this._options.normalizeNewlines) {
vExpected = normalizeNewlines(vExpected);
vActual = normalizeNewlines(vActual);
}
if(vExpected !== vActual) {
if(!this._collector.collectFailure(aExpected[i], aActual[i]))
return false;
Expand Down Expand Up @@ -137,6 +142,10 @@
vLeft = collapseSpaces(vLeft);
vRight = collapseSpaces(vRight);
}
if (this._options.normalizeNewlines) {
vLeft = normalizeNewlines(vLeft);
vRight = normalizeNewlines(vRight);
}
r = vLeft === vRight;
return !r ? this._collector.collectFailure(left, right) : r;
default:
Expand Down
10 changes: 10 additions & 0 deletions lib/normalize_newlines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function(){

"use strict";

module.exports = function normalizeNewlines(str) {
// First replace all CR+LF newlines then replace CR newlines
return str.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
};

})();
39 changes: 39 additions & 0 deletions test/test-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ describe('Compare', function () {
doc2 = parser.parseFromString("<doc><node a=' 1 \r\n\t 2 ' /></doc>");
assert.equal(true, compare(doc1, doc2, { collapseSpaces: true }).getResult());
});

it("but newlines can be normalized", function(){
doc1 = parser.parseFromString("<doc><node a=' 1 \n 2 ' /></doc>");
doc2 = parser.parseFromString("<doc><node a=' 1 \r\n 2 ' /></doc>");
assert.equal(true, compare(doc1, doc2, { normalizeNewlines: true }).getResult());
});
});

});
Expand Down Expand Up @@ -250,6 +256,27 @@ describe('Compare', function () {
}).getResult());
});
});

describe("Newline normalizing", function () {
it("normally all whitespaces at the beginning/end are preserved", function () {
var doc1 = parser.parseFromString("<doc><!-- A \r\n \n B --></doc>");
var doc2 = parser.parseFromString("<doc><!-- A \r\n \n B --></doc>");
assert.equal(true, compare(doc1, doc2, { compareComments: true }).getResult());

doc1 = parser.parseFromString("<doc><!-- A \r\n \n B --></doc>");
doc2 = parser.parseFromString("<doc><!-- A \n \r\n B --></doc>");
assert.equal(false, compare(doc1, doc2, { compareComments: true }).getResult());
});

it("`normalizeNewlines` option normalizes them", function () {
var doc1 = parser.parseFromString("<doc><!-- \r A \r\n \n\r B \n --></doc>");
var doc2 = parser.parseFromString("<doc><!-- \r\n A \n \r\n\r B \r --></doc>");
assert.equal(true, compare(doc1, doc2, {
compareComments: true,
normalizeNewlines: true
}).getResult());
});
});
});

describe("Text", function () {
Expand Down Expand Up @@ -302,6 +329,12 @@ describe('Compare', function () {
doc2 = parser.parseFromString("<doc><node> A \t\n\r B \t\n\r </node></doc>");
assert.equal(true, compare(doc1, doc2, { collapseSpaces: true }).getResult());
});

it("set `normalizeNewlines` option to normalize newlines", function () {
var doc1 = parser.parseFromString("<doc><node> \r\n A \r B \n </node></doc>");
var doc2 = parser.parseFromString("<doc><node> \n A \r\n B \r </node></doc>");
assert.equal(true, compare(doc1, doc2, { normalizeNewlines: true }).getResult());
});
});

describe("CDATA", function () {
Expand Down Expand Up @@ -338,6 +371,12 @@ describe('Compare', function () {
doc2 = parser.parseFromString("<doc><![CDATA[ data \t\n\r data \t\n\r ]]></doc>");
assert.equal(false, compare(doc1, doc2, { collapseSpaces: true }).getResult());
});

it("set `normalizeNewlines` option to normalize newlines", function () {
var doc1 = parser.parseFromString("<doc><![CDATA[ \r\n data \r data \n ]]></doc>");
var doc2 = parser.parseFromString("<doc><![CDATA[ \n data \r\n data \r ]]></doc>");
assert.equal(true, compare(doc1, doc2, { normalizeNewlines: true }).getResult());
});
});
});

Expand Down
40 changes: 40 additions & 0 deletions test/test-normalize_newlines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var assert = require('assert');
var normalizeNewlines = require('../lib/normalize_newlines');

describe('normalize_newlines', function () {
it('should transform CR+LF to LF', function() {
var input = '\r\n';
var expected = '\n';
assert.equal(normalizeNewlines(input), expected);
});

it('should transform CR to LF', function() {
var input = '\r';
var expected = '\n';
assert.equal(normalizeNewlines(input), expected);
});

it('should transform mixed CR+LF and LF to LF', function() {
var input = '\n\r\n\n\r\n\n';
var expected = '\n\n\n\n\n';
assert.equal(normalizeNewlines(input), expected);
});

it('should transform mixed CR+LF and CR to LF', function() {
var input = '\r\n\r\n\r \r\n\r';
var expected = '\n\n\n \n\n';
assert.equal(normalizeNewlines(input), expected);
});

it('should transform mixed CR and LF to LF', function() {
var input = '\n\n\r \n\r';
var expected = '\n\n\n \n\n';
assert.equal(normalizeNewlines(input), expected);
});

it('should transform mixed CR+LF and CR and LF to LF', function() {
var input = '\n\r\n\r\n\n\r \n\r\n\r';
var expected = '\n\n\n\n\n \n\n\n';
assert.equal(normalizeNewlines(input), expected);
});
});

0 comments on commit 582347c

Please sign in to comment.