Skip to content

Commit d88ca50

Browse files
committed
Modify 'ignoreWhitespace' option
1 parent ec27713 commit d88ca50

2 files changed

Lines changed: 39 additions & 30 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)) {

0 commit comments

Comments
 (0)