Skip to content

Commit

Permalink
Fix template parts inside comments (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop authored Feb 11, 2019
1 parent 2316653 commit 75d0b5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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()
})

0 comments on commit 75d0b5b

Please sign in to comment.