Skip to content

Commit

Permalink
fix: dont trim pre elements
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Sep 25, 2023
1 parent 82593c6 commit 0ea773d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

// @ts-ignore sax will be replaced with something else later
const SAX = require('@trysound/sax');
const { textElems } = require('../plugins/_collections.js');
const { spaceSensitiveElems } = require('./svgo/tools');

class SvgoParserError extends Error {
/**
Expand Down Expand Up @@ -213,7 +213,7 @@ const parseSvg = (data, from) => {
sax.ontext = (text) => {
if (current.type === 'element') {
// prevent trimming of meaningful whitespace inside textual tags
if (textElems.includes(current.name)) {
if (spaceSensitiveElems.includes(current.name)) {
/**
* @type {XastText}
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @typedef {import('./types').StringifyOptions} StringifyOptions
*/

const { textElems } = require('../plugins/_collections.js');
const { spaceSensitiveElems } = require('./svgo/tools');

/**
* @typedef {{
Expand Down Expand Up @@ -239,7 +239,7 @@ const stringifyElement = (node, config, state) => {
tagCloseStart = defaults.tagCloseStart;
tagCloseEnd = defaults.tagCloseEnd;
openIndent = '';
} else if (textElems.includes(node.name)) {
} else if (spaceSensitiveElems.includes(node.name)) {
tagOpenEnd = defaults.tagOpenEnd;
tagCloseStart = defaults.tagCloseStart;
closeIndent = '';
Expand Down
10 changes: 10 additions & 0 deletions lib/svgo/tools.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
'use strict';

const { textElems } = require('../../plugins/_collections');

/**
* @typedef {import('../types').PathDataCommand} PathDataCommand
* @typedef {import('../types').DataUri} DataUri
*/

/**
* Elements where adding or removing spaces from either end of the content
* may effect semantic meaning or rendering.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
*/
exports.spaceSensitiveElems = [...textElems, 'pre'];

/**
* Encode plain SVG data string into Data URI string.
*
Expand Down
16 changes: 11 additions & 5 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ exports.textElems = exports.elemsGroups.textContent.concat('title');

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

// https://www.w3.org/TR/SVG11/intro.html#Definitions
/**
* @type {Record<string, Array<string>>}
* @see https://www.w3.org/TR/SVG11/intro.html#Definitions
*/
exports.attrsGroups = {
animationAddition: ['additive', 'accumulate'],
Expand Down Expand Up @@ -304,7 +304,6 @@ exports.attrsGroupsDefaults = {
},
};

// https://www.w3.org/TR/SVG11/eltindex.html
/**
* @type {Record<string, {
* attrsGroups: Array<string>,
Expand All @@ -313,6 +312,7 @@ exports.attrsGroupsDefaults = {
* contentGroups?: Array<string>,
* content?: Array<string>,
* }>}
* @see https://www.w3.org/TR/SVG11/eltindex.html
*/
exports.elems = {
a: {
Expand Down Expand Up @@ -1889,7 +1889,9 @@ exports.editorNamespaces = [
'http://www.vector.evaxdesign.sk',
];

// https://www.w3.org/TR/SVG11/linking.html#processingIRI
/**
* @see https://www.w3.org/TR/SVG11/linking.html#processingIRI
*/
exports.referencesProps = [
'clip-path',
'color-profile',
Expand All @@ -1903,7 +1905,9 @@ exports.referencesProps = [
'style',
];

// https://www.w3.org/TR/SVG11/propidx.html
/**
* @see https://www.w3.org/TR/SVG11/propidx.html
*/
exports.inheritableAttrs = [
'clip-rule',
'color',
Expand Down Expand Up @@ -2157,7 +2161,9 @@ exports.colorsShortNames = {
'#f5deb3': 'wheat',
};

// https://www.w3.org/TR/SVG11/single-page.html#types-DataTypeColor
/**
* @see https://www.w3.org/TR/SVG11/single-page.html#types-DataTypeColor
*/
exports.colorsProps = [
'color',
'fill',
Expand Down
15 changes: 15 additions & 0 deletions test/svgo/_index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,19 @@ describe('svgo', () => {
});
expect(normalize(result.data)).toEqual(expected);
});
it('should not trim whitespace at start and end of pre element', async () => {
const [original, expected] = await parseFixture('pre-element.svg');
const result = optimize(original, {
path: 'input.svg',
});
expect(normalize(result.data)).toEqual(expected);
});
it('should not add whitespace in pre element', async () => {
const [original, expected] = await parseFixture('pre-element-pretty.svg');
const result = optimize(original, {
path: 'input.svg',
js2svg: { pretty: true },
});
expect(normalize(result.data)).toEqual(expected);
});
});
37 changes: 37 additions & 0 deletions test/svgo/pre-element-pretty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions test/svgo/pre-element.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 0ea773d

Please sign in to comment.