mirror of
https://github.com/iconify/iconify.git
synced 2025-01-07 15:44:05 +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 svgWidthRegex = /width\s*=\s*["'](\w+)["']/;
|
||||||
const svgHeightRegex = /height\s*=\s*["'](\w+)["']/;
|
const svgHeightRegex = /height\s*=\s*["'](\w+)["']/;
|
||||||
|
const svgTagRegex = /<svg\s+/;
|
||||||
|
|
||||||
function configureSvgSize(
|
function configureSvgSize(
|
||||||
svg: string,
|
svg: string,
|
||||||
@ -59,12 +60,12 @@ export async function mergeIconProps(
|
|||||||
// add xml namespaces if necessary
|
// add xml namespaces if necessary
|
||||||
if (addXmlNs) {
|
if (addXmlNs) {
|
||||||
// add svg xmlns if missing
|
// 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';
|
props['xmlns'] = 'http://www.w3.org/2000/svg';
|
||||||
}
|
}
|
||||||
// add xmlns:xlink if xlink present and the xmlns missing
|
// add xmlns:xlink if xlink present and the xmlns missing
|
||||||
if (
|
if (
|
||||||
!svg.includes(' xmlns:xlink=') &&
|
!svg.includes('xmlns:xlink=') &&
|
||||||
svg.includes('xlink:') &&
|
svg.includes('xlink:') &&
|
||||||
!props['xmlns:xlink']
|
!props['xmlns:xlink']
|
||||||
) {
|
) {
|
||||||
@ -80,18 +81,18 @@ export async function mergeIconProps(
|
|||||||
)
|
)
|
||||||
.filter((p) => p != null);
|
.filter((p) => p != null);
|
||||||
if (propsToAdd.length) {
|
if (propsToAdd.length) {
|
||||||
svg = svg.replace('<svg ', `<svg ${propsToAdd.join(' ')} `);
|
svg = svg.replace(svgTagRegex, `<svg ${propsToAdd.join(' ')} `);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
const { defaultStyle, defaultClass } = options;
|
const { defaultStyle, defaultClass } = options;
|
||||||
// additional props and iconCustomizer takes precedence
|
// additional props and iconCustomizer takes precedence
|
||||||
if (defaultClass && !svg.includes(' class=')) {
|
if (defaultClass && !svg.includes('class=')) {
|
||||||
svg = svg.replace('<svg ', `<svg class="${defaultClass}" `);
|
svg = svg.replace(svgTagRegex, `<svg class="${defaultClass}" `);
|
||||||
}
|
}
|
||||||
// additional props and iconCustomizer takes precedence
|
// additional props and iconCustomizer takes precedence
|
||||||
if (defaultStyle && !svg.includes(' style=')) {
|
if (defaultStyle && !svg.includes('style=')) {
|
||||||
svg = svg.replace('<svg ', `<svg style="${defaultStyle}" `);
|
svg = svg.replace(svgTagRegex, `<svg style="${defaultStyle}" `);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ describe('Testing FileSystemIconLoader', () => {
|
|||||||
|
|
||||||
test('FileSystemIconLoader with transform', async () => {
|
test('FileSystemIconLoader with transform', async () => {
|
||||||
const result = await FileSystemIconLoader(fixturesDir, (icon) => {
|
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');
|
})('circle');
|
||||||
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
|
||||||
expect(result && result.indexOf('height="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) {
|
transform(svg) {
|
||||||
return svg.replace(
|
return svg.replace(
|
||||||
'<svg ',
|
/<svg\s+/,
|
||||||
'<svg width="1em" height="1em" '
|
'<svg width="1em" height="1em" '
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -51,7 +51,7 @@ describe('Testing loadIcon', () => {
|
|||||||
customizations: {
|
customizations: {
|
||||||
transform(svg) {
|
transform(svg) {
|
||||||
return svg.replace(
|
return svg.replace(
|
||||||
'<svg ',
|
/<svg\s+/,
|
||||||
'<svg width="1em" height="1em" '
|
'<svg width="1em" height="1em" '
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user