11'use strict'
22
3+ var xtend = require ( 'xtend' )
4+ var ccount = require ( 'ccount' )
5+ var entities = require ( 'stringify-entities' )
6+
37module . exports = serializeDoctype
48
59var docLower = 'doctype'
@@ -17,20 +21,30 @@ function serializeDoctype(ctx, node) {
1721 val . push ( sep , name )
1822
1923 if ( pub !== null && pub !== undefined ) {
20- val . push ( ' public' , sep , smart ( pub ) )
24+ val . push ( ' public' , sep , quote ( ctx , pub ) )
2125 } else if ( sys !== null && sys !== undefined ) {
2226 val . push ( ' system' )
2327 }
2428
2529 if ( sys !== null && sys !== undefined ) {
26- val . push ( sep , smart ( sys ) )
30+ val . push ( sep , quote ( ctx , sys ) )
2731 }
2832 }
2933
3034 return val . join ( '' ) + '>'
3135}
3236
33- function smart ( value ) {
34- var quote = value . indexOf ( '"' ) === - 1 ? '"' : "'"
35- return quote + value + quote
37+ function quote ( ctx , value ) {
38+ var primary = ctx . quote
39+ var secondary = ctx . alternative
40+ var val = String ( value )
41+ var quote =
42+ ccount ( val , primary ) > ccount ( val , secondary ) ? secondary : primary
43+
44+ return (
45+ quote +
46+ // Prevent breaking out of doctype.
47+ entities ( val , xtend ( ctx . entities , { subset : [ '<' , '&' , quote ] } ) ) +
48+ quote
49+ )
3650}
0 commit comments