Skip to content

Commit

Permalink
fix: dont trim pre elements (#1796)
Browse files Browse the repository at this point in the history
Includes pre in the array of text elements in _collections.js so that we don't trim whitespace on them, which effects rendering.
  • Loading branch information
SethFalco committed Nov 12, 2023
1 parent 4c2cc1b commit a9df915
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/03-plugins/convert-one-stop-gradients.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Converts the [`<linearGradient>`](https://developer.mozilla.org/docs/Web/SVG/Ele

These nodes contain [`<stop>`](https://developer.mozilla.org/docs/Web/SVG/Element/stop) elements, which represent various colors to transition between. However, if a gradient only contains a single `<stop>`, then it's effecitively a solid fill.

Definitions of the gradients are removed, and the parent [`<defs>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs) node is removed if it has no children after optimization. The `xlink:href` namespace is also removed if there are no remaining elements using this attribute.
Definitions of the gradients are removed, and the parent [`<defs>`](https://developer.mozilla.org/docs/Web/SVG/Element/defs) node is removed if it has no children after optimization. The `xlink:href` namespace is also removed if there are no remaining elements using this attribute.

## Usage

Expand Down
2 changes: 1 addition & 1 deletion docs/03-plugins/merge-paths.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ svgo:
description: Number of decimal places to round to, using conventional rounding rules.
default: null
noSpaceAfterFlags:
description: If to omit spaces after flags. Flags are values that can only be <code>0</code> or <code>1</code> and are used by some path commands, namely <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#elliptical_arc_curve" target="_blank"><code>A</code> and <code>a</code></a>.
description: If to omit spaces after flags. Flags are values that can only be <code>0</code> or <code>1</code> and are used by some path commands, namely <a href="https://developer.mozilla.org/docs/Web/SVG/Attribute/d#elliptical_arc_curve" target="_blank"><code>A</code> and <code>a</code></a>.
default: false
---

Expand Down
2 changes: 1 addition & 1 deletion 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 { textElems } = require('../plugins/_collections');

class SvgoParserError extends Error {
/**
Expand Down
2 changes: 1 addition & 1 deletion 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 { textElems } = require('../plugins/_collections');

/**
* @typedef {{
Expand Down
24 changes: 18 additions & 6 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,19 @@ exports.elemsGroups = {
],
};

exports.textElems = exports.elemsGroups.textContent.concat('title');
/**
* Elements where adding or removing whitespace may effect rendering, metadata,
* or semantic meaning.
*
* @see https://developer.mozilla.org/docs/Web/HTML/Element/pre
*/
exports.textElems = [...exports.elemsGroups.textContent, 'title', 'pre'];

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 @@ -363,7 +369,6 @@ exports.attrsGroupsDefaults = {
},
};

// https://www.w3.org/TR/SVG11/eltindex.html
/**
* @type {Record<string, {
* attrsGroups: Array<string>,
Expand All @@ -372,6 +377,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 @@ -1948,7 +1954,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 @@ -1962,7 +1970,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 @@ -2216,7 +2226,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
4 changes: 2 additions & 2 deletions plugins/convertOneStopGradients.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ exports.description =
*
* @author Seth Falco <seth@falco.fun>
* @type {import('./plugins-types').Plugin<'convertOneStopGradients'>}
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/radialGradient
* @see https://developer.mozilla.org/docs/Web/SVG/Element/linearGradient
* @see https://developer.mozilla.org/docs/Web/SVG/Element/radialGradient
*/
exports.fn = (root) => {
const stylesheet = collectStylesheet(root);
Expand Down
2 changes: 1 addition & 1 deletion plugins/removeAttributesBySelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ exports.description =
* ↓
* <rect x="0" y="0" width="100" height="100"/>
*
* @link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors|MDN CSS Selectors
* @link https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors|MDN CSS Selectors
*
* @author Bradley Mease
*
Expand Down
2 changes: 1 addition & 1 deletion plugins/removeDesc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const standardDescs = /^(Created with|Created using)/;
* Removes only standard editors content or empty elements 'cause it can be used for accessibility.
* Enable parameter 'removeAny' to remove any description.
*
* https://developer.mozilla.org/en-US/docs/Web/SVG/Element/desc
* https://developer.mozilla.org/docs/Web/SVG/Element/desc
*
* @author Daniel Wabyick
*
Expand Down
2 changes: 1 addition & 1 deletion plugins/removeTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports.description = 'removes <title>';
/**
* Remove <title>.
*
* https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title
* https://developer.mozilla.org/docs/Web/SVG/Element/title
*
* @author Igor Kalashnikov
*
Expand Down
2 changes: 1 addition & 1 deletion plugins/reusePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports.fn = (root) => {
* element if one exists.
*
* @type {XastElement}
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs
* @see https://developer.mozilla.org/docs/Web/SVG/Element/defs
*/
let svgDefs;

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 a9df915

Please sign in to comment.