Skip to content

Handle nested comments with other nodes after it. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ 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 isComment = tag.indexOf('<!--') === 0;
var start = index + tag.length;
var nextChar = html.charAt(start);
var parent;

if (isOpen) {
if (isOpen && !isComment) {
level++;

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

if (!isOpen || current.voidElement) {
level--;
if (isComment || !isOpen || current.voidElement) {
if (!isComment) {
level--;
}
if (!inComponent && nextChar !== '<' && nextChar) {
// trailing text node
// if we're at the root, push a base text node. otherwise add as
Expand Down
12 changes: 10 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,23 @@ test('parse', function (t) {
]
}], 'should ignore HTML comments');

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