Skip to content

Commit dcd4ac8

Browse files
committed
Add support for bogus comments
Closes GH-16.
1 parent 1b34d25 commit dcd4ac8

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

lib/comment.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ module.exports = serializeComment
88
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
99
var breakout = /^>|^->|<!--|-->|--!>|<!-$/g
1010
var subset = ['<', '>']
11+
var bogusSubset = ['>']
1112

1213
function serializeComment(ctx, node) {
13-
return '<!--' + node.value.replace(breakout, encode) + '-->'
14+
var value = node.value
15+
16+
if (ctx.bogusComments) {
17+
return (
18+
'<?' + entities(value, xtend(ctx.entities, {subset: bogusSubset})) + '>'
19+
)
20+
}
21+
22+
return '<!--' + value.replace(breakout, encode) + '-->'
1423

1524
function encode($0) {
1625
return entities($0, xtend(ctx.entities, {subset: subset}))

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function toHtml(node, options) {
5757
tight: settings.tightAttributes,
5858
upperDoctype: Boolean(settings.upperDoctype),
5959
tightDoctype: Boolean(settings.tightDoctype),
60+
bogusComments: Boolean(settings.bogusComments),
6061
tightLists: settings.tightCommaSeparatedLists,
6162
tightClose: settings.tightSelfClosing,
6263
collapseEmpty: settings.collapseEmptyAttributes,

readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ Drop unneeded spaces in doctypes: `<!doctypehtml>` instead of `<!doctype html>`
152152
to save bytes (`boolean`, default: `false`).
153153
**Note**: creates invalid (but working) markup.
154154

155+
###### `options.bogusComments`
156+
157+
Use “bogus comments” instead of comments to save byes: `<?charlie>` instead of
158+
`<!--charlie-->` (`boolean`, default: `false`).
159+
**Note**: creates invalid (but working) markup.
160+
155161
###### `options.allowParseErrors`
156162

157163
Do not encode characters which cause parse errors (even though they work), to

test/comment.js

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ test('`comment`', function(t) {
1717
'should not encode `comment`s'
1818
)
1919

20+
t.deepEqual(
21+
to(u('comment', 'asd'), {bogusComments: true}),
22+
'<?asd>',
23+
'`bogusComments`: should serialize bogus comments'
24+
)
25+
26+
t.deepEqual(
27+
to(u('comment', 'a<s>d'), {bogusComments: true}),
28+
'<?a<s&#x3E;d>',
29+
'`bogusComments`: should prevent breaking out of bogus comments'
30+
)
31+
2032
// https://html.spec.whatwg.org/multipage/syntax.html#comments
2133
// Optionally, text, with the additional restriction that the text must not
2234
// start with the string `>`, nor start with the string `->`, nor contain the

0 commit comments

Comments
 (0)