Skip to content

Commit

Permalink
Merge branch 'fix/ignore-comments'
Browse files Browse the repository at this point in the history
  • Loading branch information
rayd committed Jun 27, 2016
2 parents 00a838a + b56ebb5 commit 17f9a07
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*jshint -W030 */
var tagRE = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
var tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g;
var parseTag = require('./parse-tag');
// re-used obj for quick lookups of components
var empty = Object.create ? Object.create(null) : {};
Expand Down Expand Up @@ -36,6 +36,10 @@ module.exports = function parse(html, options) {
inComponent = false;
}
}
// check if this is a comment tag. if so, just return.
if (tag.indexOf('<!--') === 0) {
return;
}
var isOpen = tag.charAt(1) !== '/';
var start = index + tag.length;
var nextChar = html.charAt(start);
Expand Down
30 changes: 30 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,36 @@ test('parse', function (t) {
voidElement: false,
children: []
}], 'should remove text nodes that are nothing but whitespace');

html = '<!--\n\t<style type="text/css">\n\t\t.header {\n\t\t\tfont-size: 14px;\n\t\t}\n\t</style>\n\n-->\n<div>Hi</div>';
parsed = HTML.parse(html);
t.deepEqual(parsed, [{
type: 'tag',
name: 'div',
attrs: {},
voidElement: false,
children: [
{ type: 'text', content: 'Hi'}
]
}], 'should ignore HTML comments');

html = '<div>Hi <!-- I\'m a nested comment! with a <span></span> --></div><span><!--test--></span>';
parsed = HTML.parse(html);
t.deepEqual(parsed, [{
type: 'tag',
name: 'div',
attrs: {},
voidElement: false,
children: [
{ type: 'text', content: 'Hi '}
]
},{
type: 'tag',
name: 'span',
attrs: {},
voidElement: false,
children: []
}], 'should ignore nested HTML comments');
t.end();
});

Expand Down

0 comments on commit 17f9a07

Please sign in to comment.