Skip to content

Commit b77f47c

Browse files
committed
Fix breaking out of doctypes
1 parent 3a6a6d6 commit b77f47c

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lib/doctype.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict'
22

3+
var xtend = require('xtend')
4+
var ccount = require('ccount')
5+
var entities = require('stringify-entities')
6+
37
module.exports = serializeDoctype
48

59
var 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
}

lib/element.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ function serializeAttributeValue(ctx, key, value, info) {
197197
var options = ctx.entities
198198
var quote = ctx.quote
199199
var alternative = ctx.alternative
200+
var smart = ctx.smart
200201
var unquoted
201202
var subset
202203

@@ -225,7 +226,7 @@ function serializeAttributeValue(ctx, key, value, info) {
225226
// If `value` contains entities when unquoted…
226227
if (!ctx.unquoted || unquoted !== value) {
227228
// If the alternative is less common than `quote`, switch.
228-
if (alternative && ccount(value, quote) > ccount(value, alternative)) {
229+
if (smart && ccount(value, quote) > ccount(value, alternative)) {
229230
quote = alternative
230231
}
231232

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ function toHtml(node, options) {
4747
schema: settings.space === 'svg' ? svg : html,
4848
omit: settings.omitOptionalTags && omission,
4949
quote: quote,
50-
alternative: smart ? alternative : null,
50+
alternative: alternative,
51+
smart: smart,
5152
unquoted: Boolean(settings.preferUnquoted),
5253
tight: settings.tightAttributes,
5354
upperDoctype: Boolean(settings.upperDoctype),

0 commit comments

Comments
 (0)