Skip to content

Commit c3cfc51

Browse files
committed
Merge pull request #67 from bem/ignore-html-comments-option
Ignore html comments option
2 parents 0896397 + 1d9755c commit c3cfc51

7 files changed

Lines changed: 28 additions & 15 deletions

File tree

lib/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ module.exports = require('coa').Cmd()
5555
var options = utils.defaults(config),
5656
loggerOptions = {
5757
charsAroundDiff: opts.charsAroundDiff
58-
};
58+
},
59+
htmlDiffer = new HtmlDiffer(options);
5960

60-
htmlDiffer = new HtmlDiffer(options),
6161
diffLogger.log(htmlDiffer.diffHtml(html1, html2), loggerOptions);
6262
});
6363
})

lib/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var utils = require('./utils'),
2-
32
diff = require('diff'),
43
htmlParser = require('htmlparser2'),
54
AST2Html = require('htmlparser-to-html'),
@@ -32,6 +31,8 @@ function htmlToAST(HTMLDoc) {
3231
* @returns {AST}
3332
*/
3433
function modifyASTTree(tree, options) {
34+
var delComments = [];
35+
3536
_.forEach(tree, function(node) {
3637
if (options.ignoreWhitespace && node.type === 'text') {
3738

@@ -43,6 +44,12 @@ function modifyASTTree(tree, options) {
4344
return;
4445
}
4546

47+
if (options.ignoreHtmlComments && node.type === 'comment') {
48+
delComments.push(tree.indexOf(node));
49+
50+
return;
51+
}
52+
4653
if (node.hasOwnProperty('attribs')) {
4754
var attrs = utils.sortObj(node['attribs']);
4855

@@ -76,6 +83,10 @@ function modifyASTTree(tree, options) {
7683
}
7784
});
7885

86+
for (var i = 0; i < delComments.length; i++) {
87+
tree.splice(delComments[i] - i, 1);
88+
}
89+
7990
return tree;
8091
}
8192

@@ -194,7 +205,6 @@ function bemDiff(html1, html2) {
194205
loggerOptions = {
195206
showCharacters: 40
196207
},
197-
198208
htmlDiffer = new HtmlDiff(options);
199209

200210
logger.log(htmlDiffer.diff(html1, html2), loggerOptions);

lib/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ exports.defaults = function(options) {
5959
compareHtmlAttrsAsJSON: [],
6060

6161
ignoreWhitespace: true,
62+
ignoreHtmlComments: true,
6263

6364
bem: false
6465
});

test/diffHtml.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ describe('\'diffHtml\'', function () {
1414

1515
it('must set options', function () {
1616
var htmlDiffer = new HtmlDiffer({ ignoreHtmlAttrs: ['id', 'for'], ignoreWhitespace: true }),
17-
1817
files = readFiles('3.html', '_3.html'),
19-
2018
res = [ {
2119
value: '<html><head><title>Test</title></head><body><label for="">label for input</label><input id=""></body></html>',
2220
added: undefined,

test/fixtures/10.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<!DOCTYPE html>
22
<!-- comments1 -->
33
<html>
4-
<head lang="en">
4+
<!-- comments2 --><head lang="en"><!-- comments3 -->
55
<meta charset="UTF-8">
6-
<title><!-- comments2 --></title>
7-
</head>
6+
<title><!-- comments4 --></title>
7+
</head><!-- comments5 -->
88
<body>
9-
Text<!-- comments3 -->
9+
Text<!-- comments6 -->
1010
</body>
1111
</html>
12-
<!-- comments4 -->
12+
<!-- comments7 -->

test/getDiffText.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ describe('\'diffHtml\'', function () {
3838
added: undefined,
3939
removed: undefined
4040
} ],
41-
4241
out = '\n...\n' + 'texttexttexttexttext'.grey + '!'.inverse.green + 'Text'.grey + '!'.inverse.green + 'texttexttexttext'.grey;
4342

4443
diffLoger.getDiffText(inp, { charsAroundDiff: 20 }).must.be.eql(out);
@@ -67,7 +66,6 @@ describe('\'diffHtml\'', function () {
6766
added: false,
6867
removed: true
6968
} ],
70-
7169
out = '\n' + 'texttexttexttexttexttexttexttexttexttexttext'.inverse.green + 'ololoololoololoololoololoololoololoololoolol'.inverse.red;
7270

7371
diffLoger.getDiffText(inp, { charsAroundDiff: 20 }).must.be.eql(out);
@@ -88,7 +86,6 @@ describe('\'diffHtml\'', function () {
8886
added: undefined,
8987
removed: undefined
9088
} ],
91-
9289
out = '\n' + 'texttexttext'.grey + 'text'.inverse.red + 'texttexttexttext'.grey;
9390

9491
diffLoger.getDiffText(inp).must.be.eql(out);
@@ -125,7 +122,6 @@ describe('\'diffHtml\'', function () {
125122
added: undefined,
126123
removed: undefined
127124
} ],
128-
129125
out = '\n...\n' + 'texttexttexttexttext'.grey + 'text'.inverse.red + 'texttexttexttexttext'.grey + '\n...\n' + 'texttexttexttexttext'.grey + '!'.inverse.green + 'text'.grey + '!'.inverse.green + 'texttexttexttexttext'.grey;
130126

131127
diffLoger.getDiffText(inp, { charsAroundDiff: 20 }).must.be.eql(out);

test/isEqual.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,12 @@ describe('\'isEqual\'', function () {
8484
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
8585
});
8686

87+
it('must ignore html comments', function() {
88+
var htmlDiffer = new HtmlDiffer(),
89+
90+
files = readFiles('10.html', '_10.html');
91+
92+
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
93+
});
94+
8795
});

0 commit comments

Comments
 (0)