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 Nov 12, 2023
1 parent 4c2cc1b commit bf4c90c
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
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
4 changes: 2 additions & 2 deletions lib/stringifier.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

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

/**
* @typedef {import('./types').XastParent} XastParent
* @typedef {import('./types').XastRoot} XastRoot
Expand All @@ -12,8 +14,6 @@
* @typedef {import('./types').StringifyOptions} StringifyOptions
*/

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

/**
* @typedef {{
* indent: string,
Expand Down
28 changes: 22 additions & 6 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,23 @@ 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/en-US/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 +373,6 @@ exports.attrsGroupsDefaults = {
},
};

// https://www.w3.org/TR/SVG11/eltindex.html
/**
* @type {Record<string, {
* attrsGroups: Array<string>,
Expand All @@ -372,6 +381,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 +1958,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 +1974,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 +2230,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 bf4c90c

Please sign in to comment.