Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Icon): correct prepareStringData #1096

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toBe is more straightforward

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.toBe is specifically useful for reference equality, for string comparison there is no difference here

'<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, '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep the same logic for quotes

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
Loading