Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eae7238

Browse files
committed
Don't pretty print text elements because it affects text content. Fixes svg#241
1 parent 66ca37c commit eae7238

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

lib/svgo/js2svg.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

3-
var EXTEND = require('whet.extend');
3+
var EXTEND = require('whet.extend'),
4+
textElem = require('../../plugins/_collections.js').elemsGroups.textContent.concat('title'),
5+
textChildElem = require('../../plugins/_collections.js').elemsGroups.textContentChild;
46

57
var defaults = {
68
doctypeStart: '<!DOCTYPE',
@@ -62,7 +64,6 @@ function JS2SVG(config) {
6264
this.config.tagShortEnd += '\n';
6365
this.config.tagOpenEnd += '\n';
6466
this.config.tagCloseEnd += '\n';
65-
this.config.textEnd += '\n';
6667
}
6768

6869
this.indentLevel = 0;
@@ -227,17 +228,37 @@ JS2SVG.prototype.createElem = function(data) {
227228

228229
// non-empty element
229230
} else {
231+
var tagOpenStart = this.config.tagOpenStart,
232+
tagOpenEnd = this.config.tagOpenEnd,
233+
tagCloseStart = this.config.tagCloseStart,
234+
tagCloseEnd = this.config.tagCloseEnd,
235+
openIdent = '',
236+
closeIdent = '';
237+
238+
if (data.isElem(textChildElem)) {
239+
tagOpenStart = defaults.tagOpenStart;
240+
tagCloseEnd = defaults.tagCloseEnd;
241+
} else {
242+
openIdent = this.createIndent();
243+
}
230244

231-
return this.createIndent() +
232-
this.config.tagOpenStart +
245+
if (data.isElem(textElem)) {
246+
tagOpenEnd = defaults.tagOpenEnd;
247+
tagCloseStart = defaults.tagCloseStart;
248+
} else {
249+
closeIdent = this.createIndent();
250+
}
251+
252+
return openIdent +
253+
tagOpenStart +
233254
data.elem +
234255
this.createAttrs(data) +
235-
this.config.tagOpenEnd +
256+
tagOpenEnd +
236257
this.convert(data).data +
237-
this.createIndent() +
238-
this.config.tagCloseStart +
258+
closeIdent +
259+
tagCloseStart +
239260
data.elem +
240-
this.config.tagCloseEnd;
261+
tagCloseEnd;
241262

242263
}
243264

@@ -282,8 +303,7 @@ JS2SVG.prototype.createText = function(text) {
282303
text = text.split(entity).join(this.config.entities[entity]);
283304
}
284305

285-
return this.createIndent() +
286-
this.config.textStart +
306+
return this.config.textStart +
287307
text +
288308
this.config.textEnd;
289309

plugins/_collections.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ exports.elemsGroups = {
88
structural: ['defs', 'g', 'svg', 'symbol', 'use'],
99
gradient: ['linearGradient', 'radialGradient'],
1010
container: ['a', 'defs', 'g', 'marker', 'mask', 'missing-glyph', 'pattern', 'svg', 'switch', 'symbol'],
11+
textContent: ['altGlyph', 'altGlyphDef', 'altGlyphItem', 'glyph', 'glyphRef', 'textPath', 'text', 'tref', 'tspan'],
1112
textContentChild: ['altGlyph', 'textPath', 'tref', 'tspan'],
1213
lightSource: ['feDiffuseLighting', 'feSpecularLighting', 'feDistantLight', 'fePointLight', 'feSpotLight'],
1314
filterPrimitive: ['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feFlood', 'feGaussianBlur', 'feImage', 'feMerge', 'feMorphology', 'feOffset', 'feSpecularLighting', 'feTile', 'feTurbulence']

0 commit comments

Comments
 (0)