From bf4c90c1e096f800ee4b9f47e3dc0aeb8ead2a5f Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Mon, 25 Sep 2023 16:16:23 +0100 Subject: [PATCH] fix: dont trim pre elements --- lib/parser.js | 2 +- lib/stringifier.js | 4 ++-- plugins/_collections.js | 28 ++++++++++++++++++------ test/svgo/_index.test.js | 15 +++++++++++++ test/svgo/pre-element-pretty.svg | 37 ++++++++++++++++++++++++++++++++ test/svgo/pre-element.svg | 31 ++++++++++++++++++++++++++ 6 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 test/svgo/pre-element-pretty.svg create mode 100644 test/svgo/pre-element.svg diff --git a/lib/parser.js b/lib/parser.js index 8c40eda0e..aeed3f099 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -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 { /** diff --git a/lib/stringifier.js b/lib/stringifier.js index a33814b68..8e41b8331 100644 --- a/lib/stringifier.js +++ b/lib/stringifier.js @@ -1,5 +1,7 @@ 'use strict'; +const { textElems } = require('../plugins/_collections'); + /** * @typedef {import('./types').XastParent} XastParent * @typedef {import('./types').XastRoot} XastRoot @@ -12,8 +14,6 @@ * @typedef {import('./types').StringifyOptions} StringifyOptions */ -const { textElems } = require('../plugins/_collections.js'); - /** * @typedef {{ * indent: string, diff --git a/plugins/_collections.js b/plugins/_collections.js index 5ef493e48..6ee71612e 100644 --- a/plugins/_collections.js +++ b/plugins/_collections.js @@ -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>} + * @see https://www.w3.org/TR/SVG11/intro.html#Definitions */ exports.attrsGroups = { animationAddition: ['additive', 'accumulate'], @@ -363,7 +373,6 @@ exports.attrsGroupsDefaults = { }, }; -// https://www.w3.org/TR/SVG11/eltindex.html /** * @type {Record, @@ -372,6 +381,7 @@ exports.attrsGroupsDefaults = { * contentGroups?: Array, * content?: Array, * }>} + * @see https://www.w3.org/TR/SVG11/eltindex.html */ exports.elems = { a: { @@ -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', @@ -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', @@ -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', diff --git a/test/svgo/_index.test.js b/test/svgo/_index.test.js index f21eb15c6..5ac5013b6 100644 --- a/test/svgo/_index.test.js +++ b/test/svgo/_index.test.js @@ -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); + }); }); diff --git a/test/svgo/pre-element-pretty.svg b/test/svgo/pre-element-pretty.svg new file mode 100644 index 000000000..692bbcef1 --- /dev/null +++ b/test/svgo/pre-element-pretty.svg @@ -0,0 +1,37 @@ + + +
+
 OOO   PPPP   EEEEE  N   N  SSSSS   OOO   U   U  RRRR    CCCC  EEEEE
+O   O  P   P  E      NN  N  SS     O   O  U   U  R   R  C      E    
+O   O  PPPP   EEE    N N N   SSS   O   O  U   U  RRRR   C      EEE  
+O   O  P      E      N  NN     SS  O   O  U   U  R R    C      E    
+ OOO   P      EEEEE  N   N  SSSSS   OOO    UUU   R  R    CCCC  EEEEE
+
+M   M   AAA   IIIII  N   N  TTTTT   AAA   IIIII  N   N  EEEEE  RRRR 
+MM MM  A   A    I    NN  N    T    A   A    I    NN  N  E      R   R
+M M M  AAAAA    I    N N N    T    AAAAA    I    N N N  EEE    RRRR 
+M   M  A   A    I    N  NN    T    A   A    I    N  NN  E      R R  
+M   M  A   A  IIIII  N   N    T    A   A  IIIII  N   N  EEEEE  R  R 
+
+
+
+ +@@@ + + + +
+
 OOO   PPPP   EEEEE  N   N  SSSSS   OOO   U   U  RRRR    CCCC  EEEEE
+O   O  P   P  E      NN  N  SS     O   O  U   U  R   R  C      E    
+O   O  PPPP   EEE    N N N   SSS   O   O  U   U  RRRR   C      EEE  
+O   O  P      E      N  NN     SS  O   O  U   U  R R    C      E    
+ OOO   P      EEEEE  N   N  SSSSS   OOO    UUU   R  R    CCCC  EEEEE
+
+M   M   AAA   IIIII  N   N  TTTTT   AAA   IIIII  N   N  EEEEE  RRRR 
+MM MM  A   A    I    NN  N    T    A   A    I    NN  N  E      R   R
+M M M  AAAAA    I    N N N    T    AAAAA    I    N N N  EEE    RRRR 
+M   M  A   A    I    N  NN    T    A   A    I    N  NN  E      R R  
+M   M  A   A  IIIII  N   N    T    A   A  IIIII  N   N  EEEEE  R  R 
+
+
+
diff --git a/test/svgo/pre-element.svg b/test/svgo/pre-element.svg new file mode 100644 index 000000000..71a6f9525 --- /dev/null +++ b/test/svgo/pre-element.svg @@ -0,0 +1,31 @@ + + +
+
 OOO   PPPP   EEEEE  N   N  SSSSS   OOO   U   U  RRRR    CCCC  EEEEE
+O   O  P   P  E      NN  N  SS     O   O  U   U  R   R  C      E    
+O   O  PPPP   EEE    N N N   SSS   O   O  U   U  RRRR   C      EEE  
+O   O  P      E      N  NN     SS  O   O  U   U  R R    C      E    
+ OOO   P      EEEEE  N   N  SSSSS   OOO    UUU   R  R    CCCC  EEEEE
+
+M   M   AAA   IIIII  N   N  TTTTT   AAA   IIIII  N   N  EEEEE  RRRR 
+MM MM  A   A    I    NN  N    T    A   A    I    NN  N  E      R   R
+M M M  AAAAA    I    N N N    T    AAAAA    I    N N N  EEE    RRRR 
+M   M  A   A    I    N  NN    T    A   A    I    N  NN  E      R R  
+M   M  A   A  IIIII  N   N    T    A   A  IIIII  N   N  EEEEE  R  R 
+
+
+
+ +@@@ + +
 OOO   PPPP   EEEEE  N   N  SSSSS   OOO   U   U  RRRR    CCCC  EEEEE
+O   O  P   P  E      NN  N  SS     O   O  U   U  R   R  C      E    
+O   O  PPPP   EEE    N N N   SSS   O   O  U   U  RRRR   C      EEE  
+O   O  P      E      N  NN     SS  O   O  U   U  R R    C      E    
+ OOO   P      EEEEE  N   N  SSSSS   OOO    UUU   R  R    CCCC  EEEEE
+
+M   M   AAA   IIIII  N   N  TTTTT   AAA   IIIII  N   N  EEEEE  RRRR 
+MM MM  A   A    I    NN  N    T    A   A    I    NN  N  E      R   R
+M M M  AAAAA    I    N N N    T    AAAAA    I    N N N  EEE    RRRR 
+M   M  A   A    I    N  NN    T    A   A    I    N  NN  E      R R  
+M   M  A   A  IIIII  N   N    T    A   A  IIIII  N   N  EEEEE  R  R