From 0ea773d75ba75b00d541e0a5da5824e6e5674a32 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 | 4 ++-- lib/stringifier.js | 4 ++-- lib/svgo/tools.js | 10 +++++++++ plugins/_collections.js | 16 +++++++++----- test/svgo/_index.test.js | 15 +++++++++++++ test/svgo/pre-element-pretty.svg | 37 ++++++++++++++++++++++++++++++++ test/svgo/pre-element.svg | 31 ++++++++++++++++++++++++++ 7 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..597bd2bcc 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 { spaceSensitiveElems } = require('./svgo/tools'); class SvgoParserError extends Error { /** @@ -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} */ diff --git a/lib/stringifier.js b/lib/stringifier.js index 26cff6c56..9da2106c1 100644 --- a/lib/stringifier.js +++ b/lib/stringifier.js @@ -12,7 +12,7 @@ * @typedef {import('./types').StringifyOptions} StringifyOptions */ -const { textElems } = require('../plugins/_collections.js'); +const { spaceSensitiveElems } = require('./svgo/tools'); /** * @typedef {{ @@ -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 = ''; diff --git a/lib/svgo/tools.js b/lib/svgo/tools.js index 43bfee95a..e4e043c06 100644 --- a/lib/svgo/tools.js +++ b/lib/svgo/tools.js @@ -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. * diff --git a/plugins/_collections.js b/plugins/_collections.js index dfa8bff60..8834a149f 100644 --- a/plugins/_collections.js +++ b/plugins/_collections.js @@ -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>} + * @see https://www.w3.org/TR/SVG11/intro.html#Definitions */ exports.attrsGroups = { animationAddition: ['additive', 'accumulate'], @@ -304,7 +304,6 @@ exports.attrsGroupsDefaults = { }, }; -// https://www.w3.org/TR/SVG11/eltindex.html /** * @type {Record, @@ -313,6 +312,7 @@ exports.attrsGroupsDefaults = { * contentGroups?: Array, * content?: Array, * }>} + * @see https://www.w3.org/TR/SVG11/eltindex.html */ exports.elems = { a: { @@ -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', @@ -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', @@ -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', 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