File tree Expand file tree Collapse file tree 4 files changed +29
-1
lines changed Expand file tree Collapse file tree 4 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -8,9 +8,18 @@ module.exports = serializeComment
88// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
99var breakout = / ^ > | ^ - > | < ! - - | - - > | - - ! > | < ! - $ / g
1010var subset = [ '<' , '>' ]
11+ var bogusSubset = [ '>' ]
1112
1213function 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 } ) )
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change @@ -152,6 +152,12 @@ Drop unneeded spaces in doctypes: `<!doctypehtml>` instead of `<!doctype html>`
152152to 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
157163Do 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) {
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>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
You can’t perform that action at this time.
0 commit comments