Skip to content

Commit

Permalink
fix(Icon): correct prepareStringData (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGenius authored Nov 7, 2023
1 parent a16e40e commit 8e4817e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/components/Icon/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {prepareStringData} from '../utils';

describe('Icon utils', () => {
it('should remove width and height only inside svg tag', () => {
const data =
'<svg width="100" viewBox="0 0 200 200" height=200>\n<rect x="50" y="50" width="100" height="100" fill="red" />\n</svg>';
expect(prepareStringData(data)).toEqual(
'<svg viewBox="0 0 200 200">\n<rect x="50" y="50" width="100" height="100" fill="red" />\n</svg>',
);
});
});
7 changes: 6 additions & 1 deletion src/components/Icon/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ export function isStringSvgData(data: SVGIconData): data is SVGIconStringData {
return typeof data === 'string';
}
export function prepareStringData(data: SVGIconStringData) {
return data.replace(/(width|height)=(["']?)\d+\2/g, '');
return data.replace(/<svg[^>]*>/, (match) => {
return match
.replace(/(width|height)=(["']?)\d+\2/g, '')
.replace(/(\s){2,}\b/g, '$1')
.replace(/(\s)+>/g, '>');
});
}

export function getStringViewBox(data: SVGIconStringData) {
Expand Down

0 comments on commit 8e4817e

Please sign in to comment.