From 9d93ba8272392dda7cd5c76d747e8b088f860eb3 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Tue, 14 Feb 2023 01:08:42 +0200 Subject: [PATCH] fix: incorrect custom icon size detection in utils --- packages/utils/src/loader/utils.ts | 28 ++++++-------------- packages/utils/tests/get-custom-icon-test.ts | 26 +++++++++++++++--- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/utils/src/loader/utils.ts b/packages/utils/src/loader/utils.ts index d4f1ba3..17a760a 100644 --- a/packages/utils/src/loader/utils.ts +++ b/packages/utils/src/loader/utils.ts @@ -2,8 +2,8 @@ import type { Awaitable } from '@antfu/utils'; import { isUnsetKeyword } from '../svg/build'; import type { IconifyLoaderOptions } from './types'; -const svgWidthRegex = /width\s*=\s*["'](\w+)["']/; -const svgHeightRegex = /height\s*=\s*["'](\w+)["']/; +const svgWidthRegex = /\swidth\s*=\s*["'](\w+)["']/; +const svgHeightRegex = /\sheight\s*=\s*["'](\w+)["']/; const svgTagRegex = / { const result = regex.exec(svgNode); - const w = result != null; + const isSet = result != null; const propValue = props[prop]; - let value: string | undefined; - - if (!isUnsetKeyword(propValue)) { - if (propValue) { - // Do not change it - return w; - } + if (!propValue && !isUnsetKeyword(propValue)) { if (typeof scale === 'number') { // Scale icon, unless scale is 0 - if (scale) { - value = `${scale}em`; + if (scale > 0) { + props[prop] = `${scale}em`; } } else if (result) { // Use result from iconToSVG() - value = result[1]; + props[prop] = result[1]; } } - // Change / unset - if (!value) { - delete props[prop]; - return false; - } - props[prop] = value; - return true; + return isSet; }; return [check('width', svgWidthRegex), check('height', svgHeightRegex)]; diff --git a/packages/utils/tests/get-custom-icon-test.ts b/packages/utils/tests/get-custom-icon-test.ts index a6e6c4d..7ea1ca0 100644 --- a/packages/utils/tests/get-custom-icon-test.ts +++ b/packages/utils/tests/get-custom-icon-test.ts @@ -81,7 +81,7 @@ describe('Testing getCustomIcon', () => { expect(usedProps.height).toEqual('4em'); }); - test.skip('Icon with XML heading', async () => { + test('Icon with XML heading', async () => { // Intercept console.warn let warned = false; const warn = console.warn; @@ -96,7 +96,27 @@ describe('Testing getCustomIcon', () => { // Restore console.warn console.warn = warn; - expect(svg).toEqual(result); - expect(warned).toEqual(true); + expect(result).toEqual( + svg.replace( + '', + '' + ) + ); + expect(warned).toEqual(false); + }); + + test('Scale custom icon', async () => { + const svg = ` + + + `; + + const options: IconifyLoaderOptions = { + scale: 1.2, + }; + const result = await getCustomIcon(() => svg, 'a', 'b', options); + expect(result && result.indexOf(' width="1.2em"') > -1).toBeTruthy(); + expect(result && result.indexOf(' height="1.2em"') > -1).toBeTruthy(); }); });