Skip to content

Commit 7d03eda

Browse files
committed
Modify 'compareAttributesAsJSON' option
1 parent 2ed1a11 commit 7d03eda

6 files changed

Lines changed: 33 additions & 11 deletions

File tree

lib/utils/defaults.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ var _ = require('lodash');
1616
if (options === 'bem') {
1717
options = {
1818
ignoreAttributes: ['id', 'for'],
19-
compareAttributesAsJSON: ['data-bem', 'onclick', 'ondblclick']
19+
compareAttributesAsJSON: [
20+
'data-bem',
21+
{ name: 'onclick', isFunction: true },
22+
{ name: 'ondblclick', isFunction: true }
23+
]
2024
};
2125
} else {
2226
console.error(options.bold.red + ' is an invalid preset name. Use ' + 'bem'.bold.green + ' instead.');

lib/utils/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,16 @@ function sortAttrsValues(attrs, compareAttributesAsJSON) {
112112
}
113113

114114
_.forEach(compareAttributesAsJSON, function (attr) {
115+
if (typeof attr === 'string') {
116+
var name = attr;
117+
attr = {};
118+
attr.name = name;
119+
attr.isFunction = false;
120+
}
121+
115122
var attrValue,
116-
isFunction = (attr === 'onclick' || attr === 'ondblclick'), // @FIXME: should be configurable
117-
attrIndexes = _getIndexesInArray(attrs, attr);
123+
isFunction = (attr.isFunction),
124+
attrIndexes = _getIndexesInArray(attrs, attr.name);
118125

119126
_.forEach(attrIndexes, function (index) {
120127
attrValue = _parseAttr(attrs[index].value, isFunction);

test/diff/fixtures/first/onclick-and-ondblclick.html renamed to test/diff/fixtures/first/sort-functions-in-json-format.html

File renamed without changes.

test/diff/fixtures/second/onclick-and-ondblclick.html renamed to test/diff/fixtures/second/sort-functions-in-json-format.html

File renamed without changes.

test/diff/isEqual.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,22 @@ describe('\'isEqual\'', function () {
4646
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
4747
});
4848

49-
it('must sort values of attributes as JSON', function () {
50-
var htmlDiffer = new HtmlDiffer({ compareAttributesAsJSON: [ 'a', 'b' ] }),
49+
it('must sort values of attributes as JSON when the content is not a function', function () {
50+
var htmlDiffer = new HtmlDiffer({ compareAttributesAsJSON: [ 'a', { name: 'b', isFunction: false }] }),
5151
files = readFiles('sort-values-in-json-format');
5252

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

56-
it('must sort values of attributes \'onclick\' and \'ondblclick\' as JSON', function () {
57-
var htmlDiffer = new HtmlDiffer({ compareAttributesAsJSON: [ 'onclick', 'ondblclick' ] }),
58-
files = readFiles('onclick-and-ondblclick');
56+
it('must sort values of attributes as JSON when the content is a function', function () {
57+
var options = {
58+
compareAttributesAsJSON: [
59+
{ name: 'onclick', isFunction: true },
60+
{ name: 'ondblclick', isFunction: true }
61+
]
62+
},
63+
htmlDiffer = new HtmlDiffer(options),
64+
files = readFiles('sort-functions-in-json-format');
5965

6066
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
6167
});
@@ -102,7 +108,7 @@ describe('\'isEqual\'', function () {
102108
htmlDiffer.isEqual(files.html1, files.html2).must.be.false();
103109
});
104110

105-
it('must work preset \'bem\'', function () {
111+
it('must work \'bem\' preset', function () {
106112
var htmlDiffer = new HtmlDiffer('bem'),
107113
files = readFiles('bem-preset');
108114

test/unit/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ describe('\'utils\'', function () {
4040
});
4141

4242
it('must sort attributes\' values which are in JSON format', function () {
43-
var input = [
43+
var options = [
44+
'a',
45+
{ name: 'onclick', isFunction: true },
46+
{ name: 'ondblclick', isFunction: true }
47+
],
48+
input = [
4449
{ name: 'a', value: '{"b":{"b":"b","a":"a"},"a":"a"}' },
4550
{ name: 'a', value: '{"a":"b","b":"a"}' },
4651
{ name: 'onclick', value: 'return {"b":{"b":"b","a":"a"},"a":"a"}' },
@@ -55,7 +60,7 @@ describe('\'utils\'', function () {
5560
{ name: 'c', value: '{"c":"c"}' }
5661
];
5762

58-
utils.sortAttrsValues(input, ['a', 'onclick', 'ondblclick']).must.be.eql(output);
63+
utils.sortAttrsValues(input, options).must.be.eql(output);
5964
});
6065

6166
it('must remove attributes\' values', function () {

0 commit comments

Comments
 (0)