mirror of
https://github.com/iconify/iconify.git
synced 2024-11-09 23:00:56 +00:00
fix(utils): do not assume that attributes are separated with spaces in loader
This commit is contained in:
parent
2e3ad11248
commit
78f9ae1c12
@ -3,6 +3,7 @@ import type { IconifyLoaderOptions } from './types';
|
||||
|
||||
const svgWidthRegex = /width\s*=\s*["'](\w+)["']/;
|
||||
const svgHeightRegex = /height\s*=\s*["'](\w+)["']/;
|
||||
const svgTagRegex = /<svg\s+/;
|
||||
|
||||
function configureSvgSize(
|
||||
svg: string,
|
||||
@ -59,12 +60,12 @@ export async function mergeIconProps(
|
||||
// add xml namespaces if necessary
|
||||
if (addXmlNs) {
|
||||
// add svg xmlns if missing
|
||||
if (!svg.includes(' xmlns=') && !props['xmlns']) {
|
||||
if (!svg.includes('xmlns=') && !props['xmlns']) {
|
||||
props['xmlns'] = 'http://www.w3.org/2000/svg';
|
||||
}
|
||||
// add xmlns:xlink if xlink present and the xmlns missing
|
||||
if (
|
||||
!svg.includes(' xmlns:xlink=') &&
|
||||
!svg.includes('xmlns:xlink=') &&
|
||||
svg.includes('xlink:') &&
|
||||
!props['xmlns:xlink']
|
||||
) {
|
||||
@ -80,18 +81,18 @@ export async function mergeIconProps(
|
||||
)
|
||||
.filter((p) => p != null);
|
||||
if (propsToAdd.length) {
|
||||
svg = svg.replace('<svg ', `<svg ${propsToAdd.join(' ')} `);
|
||||
svg = svg.replace(svgTagRegex, `<svg ${propsToAdd.join(' ')} `);
|
||||
}
|
||||
|
||||
if (options) {
|
||||
const { defaultStyle, defaultClass } = options;
|
||||
// additional props and iconCustomizer takes precedence
|
||||
if (defaultClass && !svg.includes(' class=')) {
|
||||
svg = svg.replace('<svg ', `<svg class="${defaultClass}" `);
|
||||
if (defaultClass && !svg.includes('class=')) {
|
||||
svg = svg.replace(svgTagRegex, `<svg class="${defaultClass}" `);
|
||||
}
|
||||
// additional props and iconCustomizer takes precedence
|
||||
if (defaultStyle && !svg.includes(' style=')) {
|
||||
svg = svg.replace('<svg ', `<svg style="${defaultStyle}" `);
|
||||
if (defaultStyle && !svg.includes('style=')) {
|
||||
svg = svg.replace(svgTagRegex, `<svg style="${defaultStyle}" `);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ describe('Testing FileSystemIconLoader', () => {
|
||||
|
||||
test('FileSystemIconLoader with transform', async () => {
|
||||
const result = await FileSystemIconLoader(fixturesDir, (icon) => {
|
||||
return icon.replace('<svg ', '<svg width="1em" height="1em" ');
|
||||
return icon.replace(/<svg\s+/, '<svg width="1em" height="1em" ');
|
||||
})('circle');
|
||||
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
||||
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
|
||||
|
7
packages/utils/tests/fixtures/circle.svg
vendored
7
packages/utils/tests/fixtures/circle.svg
vendored
@ -1 +1,6 @@
|
||||
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg"><circle cx="60" cy="60" r="50"/></svg>
|
||||
<svg
|
||||
viewBox="0 0 120 120"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<circle cx="60" cy="60" r="50"/>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 101 B After Width: | Height: | Size: 116 B |
@ -61,7 +61,7 @@ describe('Testing getCustomIcon', () => {
|
||||
},
|
||||
transform(svg) {
|
||||
return svg.replace(
|
||||
'<svg ',
|
||||
/<svg\s+/,
|
||||
'<svg width="1em" height="1em" '
|
||||
);
|
||||
},
|
||||
|
@ -51,7 +51,7 @@ describe('Testing loadIcon', () => {
|
||||
customizations: {
|
||||
transform(svg) {
|
||||
return svg.replace(
|
||||
'<svg ',
|
||||
/<svg\s+/,
|
||||
'<svg width="1em" height="1em" '
|
||||
);
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user