Skip to content

Commit

Permalink
Preserve whitespace in elements containing text
Browse files Browse the repository at this point in the history
  • Loading branch information
chromakode committed Feb 17, 2020
1 parent 07ca976 commit 959ac4c
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 27 deletions.
16 changes: 7 additions & 9 deletions lib/svgo/js2svg.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var EOL = require('os').EOL,
textElem = require('../../plugins/_collections.js').elemsGroups.textContent.concat('title');
textElems = require('../../plugins/_collections.js').textElems;

var defaults = {
doctypeStart: '<!DOCTYPE',
Expand Down Expand Up @@ -258,7 +258,7 @@ JS2SVG.prototype.createElem = function(data) {
tagCloseStart = this.config.tagCloseStart,
tagCloseEnd = this.config.tagCloseEnd,
openIndent = this.createIndent(),
textIndent = '',
closeIndent = this.createIndent(),
processedData = '',
dataEnd = '';

Expand All @@ -268,29 +268,27 @@ JS2SVG.prototype.createElem = function(data) {
tagCloseStart = defaults.tagCloseStart;
tagCloseEnd = defaults.tagCloseEnd;
openIndent = '';
} else if (data.isElem(textElem)) {
if (this.config.pretty) {
textIndent += openIndent + this.config.indent;
}
} else if (data.isElem(textElems)) {
tagOpenEnd = defaults.tagOpenEnd;
tagCloseStart = defaults.tagCloseStart;
closeIndent = '';
this.textContext = data;
}

processedData += this.convert(data).data;

if (this.textContext == data) {
this.textContext = null;
if (this.config.pretty) dataEnd = EOL;
}

return openIndent +
tagOpenStart +
data.elem +
this.createAttrs(data) +
tagOpenEnd +
textIndent +
processedData +
dataEnd +
this.createIndent() +
closeIndent +
tagCloseStart +
data.elem +
tagCloseEnd;
Expand Down
11 changes: 5 additions & 6 deletions lib/svgo/svg2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ var SAX = require('sax'),
JSAPI = require('./jsAPI.js'),
CSSClassList = require('./css-class-list'),
CSSStyleDeclaration = require('./css-style-declaration'),
entityDeclaration = /<!ENTITY\s+(\S+)\s+(?:'([^\']+)'|"([^\"]+)")\s*>/g;
entityDeclaration = /<!ENTITY\s+(\S+)\s+(?:'([^\']+)'|"([^\"]+)")\s*>/g,
textElems = require('../../plugins/_collections.js').textElems;

var config = {
strict: true,
trim: false,
normalize: true,
normalize: false,
lowercase: true,
xmlns: true,
position: true
Expand Down Expand Up @@ -117,8 +118,8 @@ module.exports = function(data, callback) {
elem = pushToContent(elem);
current = elem;

// Save info about <text> tag to prevent trimming of meaningful whitespace
if (data.name == 'text' && !data.prefix) {
// Save info about tags containing text to prevent trimming of meaningful whitespace
if (textElems.indexOf(data.name) !== -1 && !data.prefix) {
textContext = current;
}

Expand All @@ -145,9 +146,7 @@ module.exports = function(data, callback) {

var last = stack.pop();

// Trim text inside <text> tag.
if (last == textContext) {
trim(textContext);
textContext = null;
}
current = stack[stack.length - 1];
Expand Down
2 changes: 2 additions & 0 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ exports.elemsGroups = {
filterPrimitive: ['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feFlood', 'feGaussianBlur', 'feImage', 'feMerge', 'feMorphology', 'feOffset', 'feSpecularLighting', 'feTile', 'feTurbulence']
};

exports.textElems = exports.elemsGroups.textContent.concat('title');

exports.pathElems = ['path', 'glyph', 'missing-glyph'];

// http://www.w3.org/TR/SVG11/intro.html#Definitions
Expand Down
3 changes: 2 additions & 1 deletion test/plugins/cleanupIDs.09.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/plugins/cleanupIDs.10.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/plugins/cleanupIDs.13.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/plugins/cleanupIDs.14.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/plugins/collapseGroups.13.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions test/plugins/inlineStyles.13.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions test/plugins/inlineStyles.16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/svg2js/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ describe('svg2js', function() {

});

describe('text nodes', function() {

it('should contain preserved whitespace', function() {
const textNode = root.content[3].content[1].content[0].content[1]
return textNode.content[0].text.should.equal(' test ')
});

});

describe('API', function() {

describe('clone()', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/svg2js/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 959ac4c

Please sign in to comment.