Skip to content

Commit 0896397

Browse files
committed
Merge pull request #66 from bem/update-ignore-whitespace-option
Update ignore whitespace option
2 parents 9b91f3d + c955ce5 commit 0896397

7 files changed

Lines changed: 77 additions & 38 deletions

File tree

lib/index.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ var utils = require('./utils'),
1111
* @param {Object} options
1212
* @returns {AST}
1313
*/
14-
function htmlToAST(HTMLDoc, options) {
14+
function htmlToAST(HTMLDoc) {
1515
var parser,
1616
parserHandler;
1717

1818
parserHandler = new htmlParser.DomHandler(function(err) {
1919
if (err) console.log(err);
20-
}, options);
20+
});
2121

2222
parser = new htmlParser.Parser(parserHandler);
2323
parser.parseComplete(HTMLDoc);
@@ -26,45 +26,54 @@ function htmlToAST(HTMLDoc, options) {
2626
}
2727

2828
/**
29-
* Recursively sorts arttibutes' values of the AST leafs
29+
* Recursively modifies the tree depending on the given options
3030
* @param {AST} tree
3131
* @param {Object} options
3232
* @returns {AST}
3333
*/
3434
function modifyASTTree(tree, options) {
35-
_.each(tree, function(node) {
36-
Object.keys(node).forEach(function(leaf) {
37-
if (leaf === 'attribs') {
38-
var attrs = utils.sortObj(node[leaf]);
35+
_.forEach(tree, function(node) {
36+
if (options.ignoreWhitespace && node.type === 'text') {
3937

40-
if (attrs.hasOwnProperty('class')) {
41-
attrs['class'] = utils.sortCssClasses(attrs['class']);
42-
}
38+
node.data = node.data
39+
.replace(/(\n|\r|\t|\v|\f)+/g, '')
40+
.replace(/\s+/g, ' ')
41+
.trim();
4342

44-
_.each(options.compareHtmlAttrsAsJSON, function(attr) {
45-
var attrValue,
46-
isFunction = (attr === 'onclick' || attr === 'ondblclick'); // @FIXME: should be configurable
43+
return;
44+
}
4745

48-
if (attrs.hasOwnProperty(attr)) {
46+
if (node.hasOwnProperty('attribs')) {
47+
var attrs = utils.sortObj(node['attribs']);
4948

50-
attrValue = utils.parseAttr(attrs[attr].replace(/"/g, '"'), isFunction);
51-
attrValue = utils.sortObj(attrValue);
52-
attrValue = JSON.stringify(attrValue);
49+
if (attrs.hasOwnProperty('class')) {
50+
attrs['class'] = utils.sortCssClasses(attrs['class']);
51+
}
5352

54-
attrs[attr] = (isFunction ? 'return ' : '') + attrValue.replace(/"/g, '"')
55-
}
56-
});
53+
_.forEach(options.compareHtmlAttrsAsJSON, function(attr) {
54+
var attrValue,
55+
isFunction = (attr === 'onclick' || attr === 'ondblclick'); // @FIXME: should be configurable
5756

58-
_.each(options.ignoreHtmlAttrs, function(attr) {
59-
attrs.hasOwnProperty(attr) && (attrs[attr] = '');
60-
});
57+
if (attrs.hasOwnProperty(attr)) {
6158

62-
node[leaf] = attrs;
63-
}
64-
else if (leaf === 'children') {
65-
modifyASTTree(node.children, options);
66-
}
67-
});
59+
attrValue = utils.parseAttr(attrs[attr].replace(/"/g, '"'), isFunction);
60+
attrValue = utils.sortObj(attrValue);
61+
attrValue = JSON.stringify(attrValue);
62+
63+
attrs[attr] = (isFunction ? 'return ' : '') + attrValue.replace(/"/g, '"')
64+
}
65+
});
66+
67+
_.forEach(options.ignoreHtmlAttrs, function(attr) {
68+
attrs.hasOwnProperty(attr) && (attrs[attr] = '');
69+
});
70+
71+
node['attribs'] = attrs;
72+
}
73+
74+
if (node.hasOwnProperty('children')) {
75+
modifyASTTree(node.children, options);
76+
}
6877
});
6978

7079
return tree;

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports.sortObj = function sortObj(obj) {
99
var keys = _.keys(obj).sort(),
1010
sortedObj = {};
1111

12-
_.each(keys, function(key) {
12+
_.forEach(keys, function(key) {
1313
var objValue = obj[key];
1414

1515
if(_.isPlainObject(objValue)) {

test/diffHtml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('\'diffHtml\'', function () {
1818
files = readFiles('3.html', '_3.html'),
1919

2020
res = [ {
21-
value: '<html> <head> <title>Test</title> </head> <body> <label for="">label for input</label> <input id=""> </body> </html> ',
21+
value: '<html><head><title>Test</title></head><body><label for="">label for input</label><input id=""></body></html>',
2222
added: undefined,
2323
removed: undefined
2424
} ];

test/fixtures/10.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<!-- comments1 -->
3+
<html>
4+
<head lang="en">
5+
<meta charset="UTF-8">
6+
<title><!-- comments2 --></title>
7+
</head>
8+
<body>
9+
Text<!-- comments3 -->
10+
</body>
11+
</html>
12+
<!-- comments4 -->

test/fixtures/7.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html>
3+
Text Text
34
<head lang="en">
45
<meta charset="UTF-8">
56
<title></title>

test/fixtures/_10.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
Text
9+
</body>
10+
</html>

test/fixtures/_7.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html>
3+
Text Text
4+
<head lang = "en">
5+
<meta charset="UTF-8">
6+
<title > </title>
37

4-
<head lang="en">
5-
<meta charset="UTF-8">
6-
<title ></title>
7-
</head>
8+
9+
</head>
810

911
<body>
10-
Text
11-
</body>
12+
Text
13+
14+
</body>
15+
16+
17+
18+
1219
</html>
1320

1421

0 commit comments

Comments
 (0)