2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-24 17:41:58 +00:00
iconify/packages/utils/src/loader/utils.ts

129 lines
3.2 KiB
TypeScript
Raw Normal View History

import type { Awaitable } from '@antfu/utils';
import type { IconifyLoaderOptions } from './types';
2021-12-09 21:12:00 +00:00
2022-03-17 09:07:29 +00:00
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
/*
const svgWidthRegex = /(width\s*=\s*["'](\w+)["'])/d
const svgHeightRegex = /(height\s*=\s*["'](\w+)["'])/d
function configureSvgSize(svg: string, props: Record<string, string>, scale: number) {
if (props.width && props.height) {
return;
}
const svgNode = svg.slice(0, svg.indexOf('>'))
let height: string | undefined
let width: string | undefined
let result = svgWidthRegex.exec(svgNode)
if (result) {
}
if (!width) {
width = props.widht ?? `${scale}em`
}
result = svgHeightRegex.exec(svgNode)
if (result) {
}
if (!height) {
height = props.height ?? `${scale}em`
}
}
*/
2022-01-10 12:43:35 +00:00
export async function mergeIconProps(
svg: string,
collection: string,
icon: string,
options?: IconifyLoaderOptions,
propsProvider?: () => Awaitable<Record<string, string>>
2022-01-10 12:43:35 +00:00
): Promise<string> {
const { scale, addXmlNs = false } = options ?? {};
const { additionalProps = {}, iconCustomizer } =
options?.customizations ?? {};
const props: Record<string, string> = (await propsProvider?.()) ?? {};
if (
!svg.includes(' width=') &&
!svg.includes(' height=') &&
typeof scale === 'number'
) {
if (
(typeof props.width === 'undefined' || props.width === null) &&
(typeof props.height === 'undefined' || props.height === null)
) {
2022-02-26 16:47:57 +00:00
props.width = `${scale}em`;
props.height = `${scale}em`;
}
}
2022-01-10 16:08:40 +00:00
await iconCustomizer?.(collection, icon, props);
2022-01-10 12:43:35 +00:00
Object.keys(additionalProps).forEach((p) => {
2022-01-10 16:08:40 +00:00
const v = additionalProps[p];
if (v !== undefined && v !== null) props[p] = v;
});
2022-03-17 09:07:29 +00:00
const addUsedProps = options && options.usedProps;
// we need to parse the svg if we need to also return used props
// and there are no width nor height in the props
// if (addUsedProps) {
// if (typeof props.width === 'undefined' || props.width === null)
// }
// add xml namespaces if necessary
if (addXmlNs) {
// add svg xmlns if missing
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('xlink:') &&
!props['xmlns:xlink']
) {
props['xmlns:xlink'] = 'http://www.w3.org/1999/xlink';
}
}
svg = svg.replace(
'<svg ',
`<svg ${Object.keys(props)
.map((p) => `${p}="${props[p]}"`)
.join(' ')}`
2022-01-10 16:08:40 +00:00
);
if (svg && options) {
const { defaultStyle, defaultClass } = options;
// additional props and iconCustomizer takes precedence
if (defaultClass && !svg.includes(' class=')) {
svg = svg.replace('<svg ', `<svg class="${defaultClass}" `);
}
// additional props and iconCustomizer takes precedence
if (defaultStyle && !svg.includes(' style=')) {
svg = svg.replace('<svg ', `<svg style="${defaultStyle}" `);
}
}
2022-03-17 09:07:29 +00:00
if (addUsedProps) {
options!.usedProps = Object.keys(props).reduce((acc, k) => {
if (k) {
switch (k) {
case 'scale':
case 'xmlns':
case 'xmlns:xlink':
break;
default:
acc[k] = props[k];
break;
}
}
return acc;
}, {} as Record<string, string>);
}
return svg;
2022-01-10 12:43:35 +00:00
}