Skip to content

Commit 79ead6e

Browse files
authored
Merge pull request #6 from rayd/fix/more-comments
Handle nested comments with other nodes after it.
2 parents e011150 + 498a62c commit 79ead6e

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

lib/parse.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,14 @@ 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-
}
39+
4340
var isOpen = tag.charAt(1) !== '/';
41+
var isComment = tag.indexOf('<!--') === 0;
4442
var start = index + tag.length;
4543
var nextChar = html.charAt(start);
4644
var parent;
4745

48-
if (isOpen) {
46+
if (isOpen && !isComment) {
4947
level++;
5048

5149
current = parseTag(tag);
@@ -74,8 +72,10 @@ module.exports = function parse(html, options) {
7472
arr[level] = current;
7573
}
7674

77-
if (!isOpen || current.voidElement) {
78-
level--;
75+
if (isComment || !isOpen || current.voidElement) {
76+
if (!isComment) {
77+
level--;
78+
}
7979
if (!inComponent && nextChar !== '<' && nextChar) {
8080
// trailing text node
8181
// if we're at the root, push a base text node. otherwise add as

test/parse.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,23 @@ test('parse', function (t) {
385385
]
386386
}], 'should ignore HTML comments');
387387

388-
html = '<div>Hi <!-- I\'m a nested comment! with a <span></span> --></div><span><!--test--></span>';
388+
html = '<div>Hi <!-- I\'m a nested comment! with a <span></span> -->there<strong>!</strong></div><span><!--test--></span>';
389389
parsed = HTML.parse(html);
390390
t.deepEqual(parsed, [{
391391
type: 'tag',
392392
name: 'div',
393393
attrs: {},
394394
voidElement: false,
395395
children: [
396-
{ type: 'text', content: 'Hi '}
396+
{ type: 'text', content: 'Hi '},
397+
{ type: 'text', content: 'there'},
398+
{
399+
type: 'tag',
400+
name: 'strong',
401+
attrs: {},
402+
voidElement: false,
403+
children: [{ type: 'text', content: '!'}]
404+
}
397405
]
398406
},{
399407
type: 'tag',

0 commit comments

Comments
 (0)