Skip to content

Fix template parts inside comments #75

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
Feb 11, 2019
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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ module.exports = function (h, opts) {
} else {
p.push([ OPEN, arg ])
}
} else {
} else if (xstate === COMMENT && opts.comments) {
reg += String(arg)
} else if (xstate !== COMMENT) {
p.push([ VAR, xstate, arg ])
}
parts.push.apply(parts, p)
Expand Down Expand Up @@ -170,7 +172,7 @@ module.exports = function (h, opts) {
state = TEXT
} else if (state === COMMENT && /-$/.test(reg) && c === '-') {
if (opts.comments) {
res.push([ATTR_VALUE,reg.substr(0, reg.length - 1)],[CLOSE])
res.push([ATTR_VALUE,reg.substr(0, reg.length - 1)])
}
reg = ''
state = TEXT
Expand Down
26 changes: 26 additions & 0 deletions test/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,29 @@ test('excluded by default', function (t) {
t.equal(tree, '<div></div>')
t.end()
})

test('template parts in comment, discard comments', function (t) {
var child = 'something'
var objectChild = {
type: 'div',
children: ['something']
}
var tree = hx`<div><!-- abc ${child} def --></div>`
t.equal(tree, '<div></div>')
tree = hx`<div><!-- abc ${objectChild} def --></div>`
t.equal(tree, '<div></div>')
t.end()
})

test('template parts in comment, keep comments', function (t) {
var child = 'something'
var objectChild = {
type: 'div',
children: ['something']
}
var tree = hxc`<div><!-- abc ${child} def --></div>`
t.equal(tree, '<div><!-- abc something def --></div>')
tree = hxc`<div><!-- abc ${objectChild} def --></div>`
t.equal(tree, '<div><!-- abc [object Object] def --></div>', 'stringifies comment contents')
t.end()
})