Skip to content

Commit

Permalink
Add 'ignoreDuplicateAttributes' option
Browse files Browse the repository at this point in the history
  • Loading branch information
eGavr committed Sep 9, 2014
1 parent c92cbab commit 7fdb08f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/HtmlDiff.js
Expand Up @@ -10,6 +10,8 @@ var defaults = require('./defaults'),
* @param {String[]} [options.compareAttributesAsJSON]
* @param {Boolean} [options.ignoreWhitespaces=true]
* @param {Boolean} [options.ignoreComments]
* @param {Boolean} [options.ignoreClosingTags=false]
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
*/
HtmlDiff = function (options) {
this.options = defaults(options);
Expand Down
5 changes: 4 additions & 1 deletion lib/defaults.js
Expand Up @@ -7,6 +7,8 @@ var _ = require('lodash');
* @param {String[]} [options.compareAttributesAsJSON]
* @param {Boolean} [options.ignoreWhitespaces=true]
* @param {Boolean} [options.ignoreComments=true]
* @param {Boolean} [options.ignoreClosingTags=false]
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
* returns {Object}
*/
module.exports = function (options) {
Expand All @@ -29,6 +31,7 @@ var _ = require('lodash');
ignoreWhitespaces: true,
ignoreComments: true,

ignoreClosingTags: false
ignoreClosingTags: false,
ignoreDuplicateAttributes: false
});
};
2 changes: 2 additions & 0 deletions lib/index.js
Expand Up @@ -22,6 +22,8 @@ HtmlDiff.prototype.tokenize = function (html) {
* @param {String[]} [options.compareAttributesAsJSON]
* @param {Boolean} [options.ignoreWhitespaces=true]
* @param {Boolean} [options.ignoreComments=true]
* @param {Boolean} [options.ignoreClosingTags=false]
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
*/
var HtmlDiffer = function (options) {
options = defaults(options);
Expand Down
10 changes: 8 additions & 2 deletions lib/modify.js
Expand Up @@ -10,6 +10,8 @@ var SimpleApiParser = require('parse5').SimpleApiParser,
* @param {String[]} [options.compareAttributesAsJSON]
* @param {Boolean} [options.ignoreWhitespaces=true]
* @param {Boolean} [options.ignoreComments=true]
* @param {Boolean} [options.ignoreClosingTags=false]
* @param {Boolean} [options.ignoreDuplicateAttributes=false]
* returns {String}
*/
module.exports = function (value, options) {
Expand All @@ -31,6 +33,10 @@ module.exports = function (value, options) {
* @param {Boolean} selfClosing
*/
startTag: function (tagName, attrs, selfClosing) {
if (options.ignoreDuplicateAttributes) {
attrs = utils.ignoreDuplicateAttributes(attrs);
}

attrs = utils.sortAttrs(attrs) &&
utils.sortCssClasses(attrs) &&
utils.sortAttrsValues(attrs, options.compareAttributesAsJSON) &&
Expand Down Expand Up @@ -60,8 +66,6 @@ module.exports = function (value, options) {
}
});

parser.parse(value);

// Makes 'parse5' handle duplicate attributes
parser._reset = function (html) {
SimpleApiParser.prototype._reset.call(this, html);
Expand All @@ -70,5 +74,7 @@ module.exports = function (value, options) {
};
};

parser.parse(value);

return modifiedValue;
};
23 changes: 22 additions & 1 deletion lib/utils.js
Expand Up @@ -35,6 +35,25 @@ function sortAttrs(attrs) {
});
}

/**
* Removes duplicate attributes from the given list
* @param {Array} attrs
* @returns {Array}
*/
function ignoreDuplicateAttributes(attrs) {
var attrsNames = [],
res = [];

_.forEach(attrs, function (attr) {
if (attrsNames.indexOf(attr.name) === -1) {
res.push(attr);
attrsNames.push(attr.name);
}
});

return res;
}

/**
* Sorts values of the attribute 'class'
* @param {Array} attrs
Expand Down Expand Up @@ -160,5 +179,7 @@ module.exports = {

sortAttrsValues: sortAttrsValues,
ignoreAttrsValues: ignoreAttrsValues,
ignoreWhitespaces: ignoreWhitespaces
ignoreWhitespaces: ignoreWhitespaces,

ignoreDuplicateAttributes: ignoreDuplicateAttributes
};
1 change: 1 addition & 0 deletions test/differ/fixtures/13.html
@@ -0,0 +1 @@
<div style="1" style="2"></div>
1 change: 1 addition & 0 deletions test/differ/fixtures/_13.html
@@ -0,0 +1 @@
<div style="1"></div>
14 changes: 14 additions & 0 deletions test/differ/isEqual.js
Expand Up @@ -94,4 +94,18 @@ describe('\'isEqual\'', function () {

htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
});

it('must ignore duplicate attributes', function () {
var htmlDiffer = new HtmlDiffer({ ignoreDuplicateAttributes: true }),
files = readFiles('13.html', '_13.html');

htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
});

it('must not ignore duplicate attributes', function () {
var htmlDiffer = new HtmlDiffer({ ignoreDuplicateAttributes: false }),
files = readFiles('13.html', '_13.html');

htmlDiffer.isEqual(files.html1, files.html2).must.be.false();
});
});

0 comments on commit 7fdb08f

Please sign in to comment.