Skip to content

Commit a276a7c

Browse files
committed
Fix crash with 'compareAttributesAsJSON' option concerning the invalid input
1 parent 5998b9c commit a276a7c

7 files changed

Lines changed: 35 additions & 12 deletions

File tree

lib/utils/utils.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,24 @@ function sortAttrsValues(attrs, compareAttributesAsJSON) {
9595
}
9696

9797
/**
98-
* Parses the given JSON in HTML attribute
98+
* Parses the given in HTML attribute JSON
9999
* @param {String} val
100100
* @param {Boolean} [isClick]
101101
* @returns {Object}
102102
*/
103103
function _parseAttr(val, isClick) {
104-
if (isClick) {
105-
/*jshint evil: true */
106-
var fn = Function(val);
104+
try {
105+
if (isClick) {
106+
/*jshint evil: true */
107+
var fn = Function(val);
107108

108-
return fn ? fn() : {};
109-
}
109+
return fn ? fn() : {};
110+
}
110111

111-
return JSON.parse(val);
112+
return JSON.parse(val);
113+
} catch (err) {
114+
return undefined;
115+
}
112116
}
113117

114118
_.forEach(compareAttributesAsJSON, function (attr) {
@@ -125,10 +129,10 @@ function sortAttrsValues(attrs, compareAttributesAsJSON) {
125129

126130
_.forEach(attrIndexes, function (index) {
127131
attrValue = _parseAttr(attrs[index].value, isFunction);
128-
attrValue = _sortObj(attrValue);
129-
attrValue = JSON.stringify(attrValue);
130132

131-
attrs[index].value = (isFunction ? 'return ' : '') + attrValue;
133+
attrValue && (attrValue = JSON.stringify(_sortObj(attrValue)));
134+
135+
attrs[index].value = (isFunction && attrValue ? 'return ' : '') + attrValue;
132136
});
133137
});
134138

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button onclick="{&quot;button&quot;:{}};"></button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button data-bem="{&quot;button&quot;:{invalid}}"></button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button onclick="undefined"></button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button data-bem="undefined"></button>

test/diff/getDiffText.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var diffLoger = require('../../lib/logger');
22

33
require('colors');
44

5-
describe('\'diffHtml\'', function () {
5+
describe('\'getDiffText\'', function () {
66
it('must return an empty string', function () {
77
var inp = [ {
88
value: 'text',

test/diff/isEqual.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,19 @@ describe('\'isEqual\'', function () {
4747
});
4848

4949
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 }] }),
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 handle invalid JSON', function () {
57+
var htmlDiffer = new HtmlDiffer({ compareAttributesAsJSON: ['data-bem'] }),
58+
files = readFiles('invalid-json');
59+
60+
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
61+
});
62+
5663
it('must sort values of attributes as JSON when the content is a function', function () {
5764
var options = {
5865
compareAttributesAsJSON: [
@@ -66,6 +73,14 @@ describe('\'isEqual\'', function () {
6673
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
6774
});
6875

76+
it('must handle invalid function', function () {
77+
var options = { compareAttributesAsJSON: [{ name: 'onclick', isFunction: true }] },
78+
htmlDiffer = new HtmlDiffer(options),
79+
files = readFiles('invalid-function');
80+
81+
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
82+
});
83+
6984
it('must work option \'ignoreAttributes\'', function () {
7085
var htmlDiffer = new HtmlDiffer({ ignoreAttributes: ['id', 'for'] }),
7186
files = readFiles('ignore-attributes');

0 commit comments

Comments
 (0)