From 959ac4cda1671d074b7cbc9605c9befc2ac131ef Mon Sep 17 00:00:00 2001 From: Max Goodman Date: Sun, 16 Feb 2020 19:11:05 -0800 Subject: [PATCH] Preserve whitespace in elements containing text --- lib/svgo/js2svg.js | 16 +++++++--------- lib/svgo/svg2js.js | 11 +++++------ plugins/_collections.js | 2 ++ test/plugins/cleanupIDs.09.svg | 3 ++- test/plugins/cleanupIDs.10.svg | 3 ++- test/plugins/cleanupIDs.13.svg | 3 ++- test/plugins/cleanupIDs.14.svg | 3 ++- test/plugins/collapseGroups.13.svg | 3 ++- test/plugins/inlineStyles.13.svg | 4 +--- test/plugins/inlineStyles.16.svg | 4 +--- test/svg2js/_index.js | 9 +++++++++ test/svg2js/test.svg | 2 +- 12 files changed, 36 insertions(+), 27 deletions(-) diff --git a/lib/svgo/js2svg.js b/lib/svgo/js2svg.js index 0a52c6b29..f29838c79 100644 --- a/lib/svgo/js2svg.js +++ b/lib/svgo/js2svg.js @@ -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: '/g; + entityDeclaration = //g, + textElems = require('../../plugins/_collections.js').textElems; var config = { strict: true, trim: false, - normalize: true, + normalize: false, lowercase: true, xmlns: true, position: true @@ -117,8 +118,8 @@ module.exports = function(data, callback) { elem = pushToContent(elem); current = elem; - // Save info about 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; } @@ -145,9 +146,7 @@ module.exports = function(data, callback) { var last = stack.pop(); - // Trim text inside tag. if (last == textContext) { - trim(textContext); textContext = null; } current = stack[stack.length - 1]; diff --git a/plugins/_collections.js b/plugins/_collections.js index 8179f3050..7b6a9a5c9 100644 --- a/plugins/_collections.js +++ b/plugins/_collections.js @@ -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 diff --git a/test/plugins/cleanupIDs.09.svg b/test/plugins/cleanupIDs.09.svg index fc9b93223..bf70266cd 100644 --- a/test/plugins/cleanupIDs.09.svg +++ b/test/plugins/cleanupIDs.09.svg @@ -11,7 +11,8 @@ diff --git a/test/plugins/cleanupIDs.10.svg b/test/plugins/cleanupIDs.10.svg index b733a4beb..beb8a2d62 100644 --- a/test/plugins/cleanupIDs.10.svg +++ b/test/plugins/cleanupIDs.10.svg @@ -17,7 +17,8 @@ diff --git a/test/plugins/cleanupIDs.13.svg b/test/plugins/cleanupIDs.13.svg index 434967791..0be087137 100644 --- a/test/plugins/cleanupIDs.13.svg +++ b/test/plugins/cleanupIDs.13.svg @@ -11,7 +11,8 @@ diff --git a/test/plugins/cleanupIDs.14.svg b/test/plugins/cleanupIDs.14.svg index ae8d80c98..241ed58a0 100644 --- a/test/plugins/cleanupIDs.14.svg +++ b/test/plugins/cleanupIDs.14.svg @@ -17,7 +17,8 @@ diff --git a/test/plugins/collapseGroups.13.svg b/test/plugins/collapseGroups.13.svg index 41c95a91b..896516c9c 100644 --- a/test/plugins/collapseGroups.13.svg +++ b/test/plugins/collapseGroups.13.svg @@ -15,7 +15,8 @@ diff --git a/test/plugins/inlineStyles.13.svg b/test/plugins/inlineStyles.13.svg index 6bb9b8bdd..5258f7b17 100644 --- a/test/plugins/inlineStyles.13.svg +++ b/test/plugins/inlineStyles.13.svg @@ -12,9 +12,7 @@ /* Atrules with block */ @font-face { font-family: SomeFont; - src: local("Some Font"), - local("SomeFont"), - url(SomeFont.ttf); + src: local("Some Font"), local("SomeFont"), url(SomeFont.ttf); font-weight: bold; } diff --git a/test/plugins/inlineStyles.16.svg b/test/plugins/inlineStyles.16.svg index bdcb1ad5a..1688ba8b9 100644 --- a/test/plugins/inlineStyles.16.svg +++ b/test/plugins/inlineStyles.16.svg @@ -25,9 +25,7 @@ @@@ - - button - + button diff --git a/test/svg2js/_index.js b/test/svg2js/_index.js index 7528d344a..dc3b8ad69 100644 --- a/test/svg2js/_index.js +++ b/test/svg2js/_index.js @@ -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() { diff --git a/test/svg2js/test.svg b/test/svg2js/test.svg index f8e741dbd..98746fa8d 100644 --- a/test/svg2js/test.svg +++ b/test/svg2js/test.svg @@ -12,7 +12,7 @@ - test + test