Skip to content

Commit 17f9a07

Browse files
committed
Merge branch 'fix/ignore-comments'
2 parents 00a838a + b56ebb5 commit 17f9a07

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/parse.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*jshint -W030 */
2-
var tagRE = /<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g;
2+
var tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g;
33
var parseTag = require('./parse-tag');
44
// re-used obj for quick lookups of components
55
var empty = Object.create ? Object.create(null) : {};
@@ -36,6 +36,10 @@ module.exports = function parse(html, options) {
3636
inComponent = false;
3737
}
3838
}
39+
// check if this is a comment tag. if so, just return.
40+
if (tag.indexOf('<!--') === 0) {
41+
return;
42+
}
3943
var isOpen = tag.charAt(1) !== '/';
4044
var start = index + tag.length;
4145
var nextChar = html.charAt(start);

test/parse.js

+30
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,36 @@ test('parse', function (t) {
372372
voidElement: false,
373373
children: []
374374
}], 'should remove text nodes that are nothing but whitespace');
375+
376+
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>';
377+
parsed = HTML.parse(html);
378+
t.deepEqual(parsed, [{
379+
type: 'tag',
380+
name: 'div',
381+
attrs: {},
382+
voidElement: false,
383+
children: [
384+
{ type: 'text', content: 'Hi'}
385+
]
386+
}], 'should ignore HTML comments');
387+
388+
html = '<div>Hi <!-- I\'m a nested comment! with a <span></span> --></div><span><!--test--></span>';
389+
parsed = HTML.parse(html);
390+
t.deepEqual(parsed, [{
391+
type: 'tag',
392+
name: 'div',
393+
attrs: {},
394+
voidElement: false,
395+
children: [
396+
{ type: 'text', content: 'Hi '}
397+
]
398+
},{
399+
type: 'tag',
400+
name: 'span',
401+
attrs: {},
402+
voidElement: false,
403+
children: []
404+
}], 'should ignore nested HTML comments');
375405
t.end();
376406
});
377407

0 commit comments

Comments
 (0)