Skip to content

Commit 98afb01

Browse files
authored
Add support for better SVG minifying
Closes GH-10. Closes GH-13.
1 parent e8e480c commit 98afb01

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

lib/element.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function attributes(ctx, props) {
116116
result = values[index]
117117
last = null
118118

119-
if (ctx.schema.space === 'html' && ctx.tight) {
119+
if (ctx.tight) {
120120
last = result.charAt(result.length - 1)
121121
}
122122

@@ -132,7 +132,6 @@ function attributes(ctx, props) {
132132
// Stringify one attribute.
133133
function attribute(ctx, key, value) {
134134
var schema = ctx.schema
135-
var space = schema.space
136135
var info = find(schema, key)
137136
var name = info.attribute
138137

@@ -156,11 +155,23 @@ function attribute(ctx, key, value) {
156155
name = attributeName(ctx, name)
157156

158157
if (value === true) {
159-
if (space === 'html') {
160-
return name
161-
}
162-
163-
value = name
158+
// There is currently only one boolean property in SVG: `[download]` on
159+
// `<a>`.
160+
// This property does not seem to work in browsers (FF, Sa, Ch), so I can’t
161+
// test if dropping the value works.
162+
// But I assume that it should:
163+
//
164+
// ```html
165+
// <!doctype html>
166+
// <svg viewBox="0 0 100 100">
167+
// <a href=https://example.com download>
168+
// <circle cx=50 cy=40 r=35 />
169+
// </a>
170+
// </svg>
171+
// ```
172+
//
173+
// See: <https://github.com/wooorm/property-information/blob/master/lib/svg.js>
174+
return name
164175
}
165176

166177
return name + attributeValue(ctx, key, value, info)
@@ -180,7 +191,6 @@ function attributeValue(ctx, key, value, info) {
180191
var options = ctx.entities
181192
var quote = ctx.quote
182193
var alternative = ctx.alternative
183-
var space = ctx.schema.space
184194
var unquoted
185195
var subset
186196

@@ -194,11 +204,11 @@ function attributeValue(ctx, key, value, info) {
194204

195205
value = String(value)
196206

197-
if (space !== 'html' || value || !ctx.collapseEmpty) {
207+
if (value || !ctx.collapseEmpty) {
198208
unquoted = value
199209

200210
// Check unquoted value.
201-
if (space === 'html' && ctx.unquoted) {
211+
if (ctx.unquoted) {
202212
subset = constants.unquoted[ctx.valid][ctx.safe]
203213
unquoted = entities(
204214
value,
@@ -207,15 +217,15 @@ function attributeValue(ctx, key, value, info) {
207217
}
208218

209219
// If `value` contains entities when unquoted…
210-
if (space !== 'html' || !ctx.unquoted || unquoted !== value) {
220+
if (!ctx.unquoted || unquoted !== value) {
211221
// If the alternative is less common than `quote`, switch.
212222
if (alternative && ccount(value, quote) > ccount(value, alternative)) {
213223
quote = alternative
214224
}
215225

216226
subset = quote === apostrophe ? constants.single : constants.double
217227
// Always encode without parse errors in non-HTML.
218-
subset = subset[space === 'html' ? ctx.valid : 1][ctx.safe]
228+
subset = subset[ctx.schema.space === 'html' ? ctx.valid : 1][ctx.safe]
219229

220230
value = entities(value, xtend(options, {subset: subset, attribute: true}))
221231

test/svg.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ test('svg', function(t) {
5555

5656
t.deepEqual(
5757
to(s('circle', {title: ''}), {space: 'svg', collapseEmptyAttributes: true}),
58-
'<circle title=""></circle>',
59-
'should *not* collapse empty string attributes in `collapseEmptyAttributes` mode'
58+
'<circle title></circle>',
59+
'should collapse empty string attributes in `collapseEmptyAttributes` mode'
6060
)
6161

6262
t.deepEqual(
@@ -72,8 +72,8 @@ test('svg', function(t) {
7272
space: 'svg',
7373
tightAttributes: true
7474
}),
75-
'<text class="a b" title="c d">bravo</text>',
76-
'should *not* stringify multiple properties tightly in `tightAttributes` mode'
75+
'<text class="a b"title="c d">bravo</text>',
76+
'should stringify multiple properties tightly in `tightAttributes` mode'
7777
)
7878

7979
t.deepEqual(
@@ -105,7 +105,7 @@ test('svg', function(t) {
105105

106106
t.deepEqual(
107107
to(s('a', {download: true}, 'bravo'), {space: 'svg'}),
108-
'<a download="download">bravo</a>',
108+
'<a download>bravo</a>',
109109
'should stringify known boolean attributes set to `true`'
110110
)
111111

@@ -117,7 +117,7 @@ test('svg', function(t) {
117117

118118
t.deepEqual(
119119
to(s('a', {download: 1}, 'bravo'), {space: 'svg'}),
120-
'<a download="download">bravo</a>',
120+
'<a download>bravo</a>',
121121
'should stringify truthy known boolean attributes'
122122
)
123123

@@ -135,7 +135,7 @@ test('svg', function(t) {
135135

136136
t.deepEqual(
137137
to(s('a', {unknown: true}, 'bravo'), {space: 'svg'}),
138-
'<a unknown="unknown">bravo</a>',
138+
'<a unknown>bravo</a>',
139139
'should stringify unknown attributes set to `true`'
140140
)
141141

@@ -183,7 +183,7 @@ test('svg', function(t) {
183183

184184
t.deepEqual(
185185
to(s('i', {id: true}, 'bravo'), {space: 'svg'}),
186-
'<i id="id">bravo</i>',
186+
'<i id>bravo</i>',
187187
'should stringify other non-string attributes'
188188
)
189189

@@ -221,8 +221,8 @@ test('svg', function(t) {
221221

222222
t.deepEqual(
223223
to(s('circle', {cx: 2}), {space: 'svg', preferUnquoted: true}),
224-
'<circle cx="2"></circle>',
225-
'should *not* omit quotes in `preferUnquoted`'
224+
'<circle cx=2></circle>',
225+
'should omit quotes in `preferUnquoted`'
226226
)
227227

228228
t.deepEqual(

0 commit comments

Comments
 (0)