2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-14 06:28:24 +00:00
iconify/packages/utils/src/svg/url.ts

30 lines
867 B
TypeScript
Raw Normal View History

/**
* Encode SVG for use in url()
*
* Short alternative to encodeURIComponent() that encodes only stuff used in SVG, generating
* smaller code.
*
* If icon is not optimised, run trimSVG() before this function to get rid of new lines.
* This function is intended to be used with Iconify icon sets, which are already optimised
* and do not contain new lines.
*/
export function encodeSVGforURL(svg: string): string {
return (
svg
.replace(/"/g, "'")
.replace(/%/g, '%25')
.replace(/#/g, '%23')
// .replace(/{/g, '%7B') // not needed in string inside double quotes
// .replace(/}/g, '%7D') // not needed in string inside double quotes
.replace(/</g, '%3C')
.replace(/>/g, '%3E')
);
}
/**
* Generate url() from SVG
*/
export function svgToURL(svg: string): string {
return 'url("data:image/svg+xml,' + encodeSVGforURL(svg) + '")';
}