File tree 4 files changed +29
-1
lines changed
4 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -8,9 +8,18 @@ module.exports = serializeComment
8
8
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
9
9
var breakout = / ^ > | ^ - > | < ! - - | - - > | - - ! > | < ! - $ / g
10
10
var subset = [ '<' , '>' ]
11
+ var bogusSubset = [ '>' ]
11
12
12
13
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 ) + '-->'
14
23
15
24
function encode ( $0 ) {
16
25
return entities ( $0 , xtend ( ctx . entities , { subset : subset } ) )
Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ function toHtml(node, options) {
57
57
tight : settings . tightAttributes ,
58
58
upperDoctype : Boolean ( settings . upperDoctype ) ,
59
59
tightDoctype : Boolean ( settings . tightDoctype ) ,
60
+ bogusComments : Boolean ( settings . bogusComments ) ,
60
61
tightLists : settings . tightCommaSeparatedLists ,
61
62
tightClose : settings . tightSelfClosing ,
62
63
collapseEmpty : settings . collapseEmptyAttributes ,
Original file line number Diff line number Diff line change @@ -152,6 +152,12 @@ Drop unneeded spaces in doctypes: `<!doctypehtml>` instead of `<!doctype html>`
152
152
to save bytes (` boolean ` , default: ` false ` ).
153
153
** Note** : creates invalid (but working) markup.
154
154
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
+
155
161
###### ` options.allowParseErrors `
156
162
157
163
Do not encode characters which cause parse errors (even though they work), to
Original file line number Diff line number Diff line change @@ -17,6 +17,18 @@ test('`comment`', function(t) {
17
17
'should not encode `comment`s'
18
18
)
19
19
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>d>' ,
29
+ '`bogusComments`: should prevent breaking out of bogus comments'
30
+ )
31
+
20
32
// https://html.spec.whatwg.org/multipage/syntax.html#comments
21
33
// Optionally, text, with the additional restriction that the text must not
22
34
// start with the string `>`, nor start with the string `->`, nor contain the
You can’t perform that action at this time.
0 commit comments