Skip to content

Commit 0ac630a

Browse files
committed
chore: fix prettyPrint option for SVG class
1 parent 22d6fda commit 0ac630a

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

@iconify/tools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "module",
44
"description": "Collection of functions for cleaning up and parsing SVG for Iconify project",
55
"author": "Vjacheslav Trushkin",
6-
"version": "5.0.1",
6+
"version": "5.0.2",
77
"license": "MIT",
88
"bugs": "https://github.com/iconify/tools/issues",
99
"homepage": "https://github.com/iconify/tools",

@iconify/tools/src/svg/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IconifyIcon } from '@iconify/types';
2-
import { iconToSVG, trimSVG } from '@iconify/utils';
2+
import { iconToSVG, prettifySVG, trimSVG } from '@iconify/utils';
33
import type { CommonIconProps } from '../icon-set/types';
44
import type { IconifyIconCustomisations } from '@iconify/utils/lib/customisations/defaults';
55
import {
@@ -54,7 +54,10 @@ export class SVG {
5454
svgAttributes += ' ' + key + '="' + value + '"';
5555
}
5656

57-
return '<svg' + svgAttributes + '>' + data.body + '</svg>';
57+
const svg = '<svg' + svgAttributes + '>' + data.body + '</svg>';
58+
return prettyPrint === true
59+
? prettifySVG(svg) || svg
60+
: trimSVG(svg);
5861
}
5962

6063
// Get icon as is if customisations are not set
@@ -75,17 +78,19 @@ export class SVG {
7578
}
7679
}
7780

78-
return stringifyXMLContent([$root], {
81+
const result = stringifyXMLContent([$root], {
7982
prettyPrint,
8083
})!;
84+
85+
// Additional trim for content (such as multi-line "d" attributes) because XML parser does not trim attributes
86+
return prettyPrint === false ? trimSVG(result) : result;
8187
}
8288

8389
/**
8490
* Get SVG as string without whitespaces
8591
*/
8692
toMinifiedString(customisations?: IconifyIconCustomisations): string {
87-
// Additional trimming for better result
88-
return trimSVG(this.toString(customisations, false));
93+
return this.toString(customisations, false);
8994
}
9095

9196
/**

@iconify/tools/tests/svg/svg-test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,20 @@ describe('Loading SVG', () => {
118118
}
119119
expect(threwError).toBe(true);
120120
});
121+
122+
test('Bugged SVG', () => {
123+
const svg = new SVG(
124+
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3h7v18h-14v-18h7Z"/><path d="M14.5 3.5v3h-5v-3"/><path d="M9 13l2 2l4 -4" stroke-dasharray="10" stroke-dashoffset="10"><animate attributeName="stroke-dashoffset" values="10;0" dur="0.2s" fill="freeze"/></path></g><path d="M6 4H10V6H14V4H18V20H6V4Z" fill="currentColor" fill-opacity="0.3"/></svg>'
125+
);
126+
expect(
127+
svg.toString(
128+
{
129+
height: 'auto',
130+
},
131+
true
132+
)
133+
).toBe(
134+
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">\n\t<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">\n\t\t<path d="M12 3h7v18h-14v-18h7Z" />\n\t\t<path d="M14.5 3.5v3h-5v-3" />\n\t\t<path d="M9 13l2 2l4 -4" stroke-dasharray="10" stroke-dashoffset="10">\n\t\t\t<animate attributeName="stroke-dashoffset" values="10;0" dur="0.2s" fill="freeze" />\n\t\t</path>\n\t</g>\n\t<path d="M6 4H10V6H14V4H18V20H6V4Z" fill="currentColor" fill-opacity="0.3" />\n</svg>\n'
135+
);
136+
});
121137
});

0 commit comments

Comments
 (0)