react-inlinesvg 4.0.3 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +19 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -9
- package/dist/index.mjs.map +1 -1
- package/dist/provider.d.ts +1 -0
- package/dist/provider.js +7 -1
- package/dist/provider.js.map +1 -1
- package/dist/provider.mjs.map +1 -1
- package/package.json +19 -18
- package/src/helpers.ts +1 -1
- package/src/index.tsx +12 -8
package/README.md
CHANGED
|
@@ -69,7 +69,10 @@ A description for your SVG. It will override an existing `<desc>` tag.
|
|
|
69
69
|
Custom options for the [request](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
|
|
70
70
|
|
|
71
71
|
**innerRef** {React.Ref}
|
|
72
|
-
Set a ref in SVGElement.
|
|
72
|
+
Set a ref in SVGElement.
|
|
73
|
+
|
|
74
|
+
>The SVG is processed and parsed so the ref won't be set on the initial render.
|
|
75
|
+
You can use the `onLoad` callback to get and use the ref instead.
|
|
73
76
|
|
|
74
77
|
**loader** {node}
|
|
75
78
|
A component to be shown while the SVG is loading.
|
package/dist/index.d.ts
CHANGED
|
@@ -71,3 +71,4 @@ declare let cacheStore: CacheStore;
|
|
|
71
71
|
declare function InlineSVG(props: Props): string | number | boolean | Iterable<React.ReactNode> | react_jsx_runtime.JSX.Element | null | undefined;
|
|
72
72
|
|
|
73
73
|
export { ErrorCallback, FetchError, LoadCallback, PlainObject, PreProcessorCallback, Props, State, Status, StorageItem, cacheStore, InlineSVG as default };
|
|
74
|
+
export = InlineSVG
|
package/dist/index.js
CHANGED
|
@@ -64,7 +64,7 @@ function isSupportedEnvironment() {
|
|
|
64
64
|
async function request(url, options) {
|
|
65
65
|
const response = await fetch(url, options);
|
|
66
66
|
const contentType = response.headers.get("content-type");
|
|
67
|
-
const [fileType] = (contentType
|
|
67
|
+
const [fileType] = (contentType ?? "").split(/ ?; ?/);
|
|
68
68
|
if (response.status > 299) {
|
|
69
69
|
throw new Error("Not found");
|
|
70
70
|
}
|
|
@@ -278,7 +278,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
278
278
|
isCached: !!props.cacheRequests && cacheStore.isCached(props.src),
|
|
279
279
|
status: STATUS.IDLE
|
|
280
280
|
};
|
|
281
|
-
this.hash = props.uniqueHash
|
|
281
|
+
this.hash = props.uniqueHash ?? randomString(8);
|
|
282
282
|
}
|
|
283
283
|
componentDidMount() {
|
|
284
284
|
this.isActive = true;
|
|
@@ -307,7 +307,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
307
307
|
return;
|
|
308
308
|
}
|
|
309
309
|
const { isCached, status } = this.state;
|
|
310
|
-
const { onLoad, src } = this.props;
|
|
310
|
+
const { description, onLoad, src, title } = this.props;
|
|
311
311
|
if (previousState.status !== STATUS.READY && status === STATUS.READY) {
|
|
312
312
|
if (onLoad) {
|
|
313
313
|
onLoad(src, isCached);
|
|
@@ -320,6 +320,9 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
320
320
|
}
|
|
321
321
|
this.load();
|
|
322
322
|
}
|
|
323
|
+
if (previousProps.title !== title || previousProps.description !== description) {
|
|
324
|
+
this.getElement();
|
|
325
|
+
}
|
|
323
326
|
}
|
|
324
327
|
componentWillUnmount() {
|
|
325
328
|
this.isActive = false;
|
|
@@ -350,7 +353,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
350
353
|
const svg = this.updateSVGAttributes(node);
|
|
351
354
|
if (description) {
|
|
352
355
|
const originalDesc = svg.querySelector("desc");
|
|
353
|
-
if (originalDesc
|
|
356
|
+
if (originalDesc?.parentNode) {
|
|
354
357
|
originalDesc.parentNode.removeChild(originalDesc);
|
|
355
358
|
}
|
|
356
359
|
const descElement = document.createElementNS("http://www.w3.org/2000/svg", "desc");
|
|
@@ -359,7 +362,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
359
362
|
}
|
|
360
363
|
if (typeof title !== "undefined") {
|
|
361
364
|
const originalTitle = svg.querySelector("title");
|
|
362
|
-
if (originalTitle
|
|
365
|
+
if (originalTitle?.parentNode) {
|
|
363
366
|
originalTitle.parentNode.removeChild(originalTitle);
|
|
364
367
|
}
|
|
365
368
|
if (title) {
|
|
@@ -384,7 +387,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
384
387
|
},
|
|
385
388
|
async () => {
|
|
386
389
|
const { cacheRequests, fetchOptions, src } = this.props;
|
|
387
|
-
const dataURI =
|
|
390
|
+
const dataURI = /^data:image\/svg[^,]*?(;base64)?,(.*)/u.exec(src);
|
|
388
391
|
let inlineSrc;
|
|
389
392
|
if (dataURI) {
|
|
390
393
|
inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);
|
|
@@ -426,11 +429,11 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
426
429
|
return node;
|
|
427
430
|
}
|
|
428
431
|
[...node.children].forEach((d) => {
|
|
429
|
-
if (d.attributes
|
|
432
|
+
if (d.attributes?.length) {
|
|
430
433
|
const attributes = Object.values(d.attributes).map((a) => {
|
|
431
434
|
const attribute = a;
|
|
432
|
-
const match =
|
|
433
|
-
if (match
|
|
435
|
+
const match = /url\((.*?)\)/.exec(a.value);
|
|
436
|
+
if (match?.[1]) {
|
|
434
437
|
attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);
|
|
435
438
|
}
|
|
436
439
|
return attribute;
|
|
@@ -509,4 +512,10 @@ function InlineSVG(props) {
|
|
|
509
512
|
0 && (module.exports = {
|
|
510
513
|
cacheStore
|
|
511
514
|
});
|
|
512
|
-
//# sourceMappingURL=index.js.map
|
|
515
|
+
//# sourceMappingURL=index.js.map
|
|
516
|
+
// fix-cjs-exports
|
|
517
|
+
if (module.exports.default) {
|
|
518
|
+
Object.assign(module.exports.default, module.exports);
|
|
519
|
+
module.exports = module.exports.default;
|
|
520
|
+
delete module.exports.default;
|
|
521
|
+
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash || randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { onLoad, src } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc && originalDesc.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle && originalTitle.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = src.match(/^data:image\\/svg[^,]*?(;base64)?,(.*)/u);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes && d.attributes.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = a.value.match(/url\\((.*?)\\)/);\n\n if (match && match[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = React.useRef(false);\n const [isReady, setReady] = React.useState(cacheStore.isReady);\n\n React.useEffect(() => {\n if (!hasCallback.current) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n\n hasCallback.current = true;\n }\n }, []);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType || '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* istanbul ignore next */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n /* istanbul ignore else */\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { canUseDOM, request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n }\n\n if (usePersistentCache) {\n caches.open(cacheName).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\n this.subscribers.forEach(callback => callback());\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n let retryCount = 0;\n\n // eslint-disable-next-line no-await-in-loop\n while (this.cacheStore.get(url)?.status === STATUS.LOADING && retryCount < CACHE_MAX_RETRIES) {\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n retryCount += 1;\n }\n\n if (retryCount >= CACHE_MAX_RETRIES) {\n await callback();\n }\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n for (const key of keys) {\n // eslint-disable-next-line no-await-in-loop\n await this.cacheApi.delete(key);\n }\n }\n\n this.cacheStore.clear();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,YAAuB;AACvB,4BAAoB;;;ACDb,IAAM,aAAa;AACnB,IAAM,oBAAoB;AAE1B,IAAM,SAAS;AAAA,EACpB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,aAAa;AACf;;;ACRO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,YAAY,OAAO,SAAS;AAChF;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAEA,eAAsB,QAAQ,KAAa,SAAuB;AAChE,QAAM,WAAW,MAAM,MAAM,KAAK,OAAO;AACzC,QAAM,cAAc,SAAS,QAAQ,IAAI,cAAc;AACvD,QAAM,CAAC,QAAQ,KAAK,eAAe,IAAI,MAAM,OAAO;AAEpD,MAAI,SAAS,SAAS,KAAK;AACzB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O,4CAAC,kBAAgB,GAAG,OAAO;AACpC;","names":["convert"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash ?? randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { description, onLoad, src, title } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n\n if (previousProps.title !== title || previousProps.description !== description) {\n this.getElement();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc?.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle?.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = /^data:image\\/svg[^,]*?(;base64)?,(.*)/u.exec(src);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes?.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = /url\\((.*?)\\)/.exec(a.value);\n\n if (match?.[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = React.useRef(false);\n const [isReady, setReady] = React.useState(cacheStore.isReady);\n\n React.useEffect(() => {\n if (!hasCallback.current) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n\n hasCallback.current = true;\n }\n }, []);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* istanbul ignore next */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n /* istanbul ignore else */\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { canUseDOM, request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n }\n\n if (usePersistentCache) {\n caches.open(cacheName).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\n this.subscribers.forEach(callback => callback());\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n let retryCount = 0;\n\n // eslint-disable-next-line no-await-in-loop\n while (this.cacheStore.get(url)?.status === STATUS.LOADING && retryCount < CACHE_MAX_RETRIES) {\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n retryCount += 1;\n }\n\n if (retryCount >= CACHE_MAX_RETRIES) {\n await callback();\n }\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n for (const key of keys) {\n // eslint-disable-next-line no-await-in-loop\n await this.cacheApi.delete(key);\n }\n }\n\n this.cacheStore.clear();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,YAAuB;AACvB,4BAAoB;;;ACDb,IAAM,aAAa;AACnB,IAAM,oBAAoB;AAE1B,IAAM,SAAS;AAAA,EACpB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,aAAa;AACf;;;ACRO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,YAAY,OAAO,SAAS;AAChF;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAEA,eAAsB,QAAQ,KAAa,SAAuB;AAChE,QAAM,WAAW,MAAM,MAAM,KAAK,OAAO;AACzC,QAAM,cAAc,SAAS,QAAQ,IAAI,cAAc;AACvD,QAAM,CAAC,QAAQ,KAAK,eAAe,IAAI,MAAM,OAAO;AAEpD,MAAI,SAAS,SAA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AG,CAAC,CAAC;AAEL,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,SAAO,4CAAC,kBAAgB,GAAG,OAAO;AACpC;","names":["convert"]}
|
package/dist/index.mjs
CHANGED
|
@@ -31,7 +31,7 @@ function isSupportedEnvironment() {
|
|
|
31
31
|
async function request(url, options) {
|
|
32
32
|
const response = await fetch(url, options);
|
|
33
33
|
const contentType = response.headers.get("content-type");
|
|
34
|
-
const [fileType] = (contentType
|
|
34
|
+
const [fileType] = (contentType ?? "").split(/ ?; ?/);
|
|
35
35
|
if (response.status > 299) {
|
|
36
36
|
throw new Error("Not found");
|
|
37
37
|
}
|
|
@@ -245,7 +245,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
245
245
|
isCached: !!props.cacheRequests && cacheStore.isCached(props.src),
|
|
246
246
|
status: STATUS.IDLE
|
|
247
247
|
};
|
|
248
|
-
this.hash = props.uniqueHash
|
|
248
|
+
this.hash = props.uniqueHash ?? randomString(8);
|
|
249
249
|
}
|
|
250
250
|
componentDidMount() {
|
|
251
251
|
this.isActive = true;
|
|
@@ -274,7 +274,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
274
274
|
return;
|
|
275
275
|
}
|
|
276
276
|
const { isCached, status } = this.state;
|
|
277
|
-
const { onLoad, src } = this.props;
|
|
277
|
+
const { description, onLoad, src, title } = this.props;
|
|
278
278
|
if (previousState.status !== STATUS.READY && status === STATUS.READY) {
|
|
279
279
|
if (onLoad) {
|
|
280
280
|
onLoad(src, isCached);
|
|
@@ -287,6 +287,9 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
287
287
|
}
|
|
288
288
|
this.load();
|
|
289
289
|
}
|
|
290
|
+
if (previousProps.title !== title || previousProps.description !== description) {
|
|
291
|
+
this.getElement();
|
|
292
|
+
}
|
|
290
293
|
}
|
|
291
294
|
componentWillUnmount() {
|
|
292
295
|
this.isActive = false;
|
|
@@ -317,7 +320,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
317
320
|
const svg = this.updateSVGAttributes(node);
|
|
318
321
|
if (description) {
|
|
319
322
|
const originalDesc = svg.querySelector("desc");
|
|
320
|
-
if (originalDesc
|
|
323
|
+
if (originalDesc?.parentNode) {
|
|
321
324
|
originalDesc.parentNode.removeChild(originalDesc);
|
|
322
325
|
}
|
|
323
326
|
const descElement = document.createElementNS("http://www.w3.org/2000/svg", "desc");
|
|
@@ -326,7 +329,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
326
329
|
}
|
|
327
330
|
if (typeof title !== "undefined") {
|
|
328
331
|
const originalTitle = svg.querySelector("title");
|
|
329
|
-
if (originalTitle
|
|
332
|
+
if (originalTitle?.parentNode) {
|
|
330
333
|
originalTitle.parentNode.removeChild(originalTitle);
|
|
331
334
|
}
|
|
332
335
|
if (title) {
|
|
@@ -351,7 +354,7 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
351
354
|
},
|
|
352
355
|
async () => {
|
|
353
356
|
const { cacheRequests, fetchOptions, src } = this.props;
|
|
354
|
-
const dataURI =
|
|
357
|
+
const dataURI = /^data:image\/svg[^,]*?(;base64)?,(.*)/u.exec(src);
|
|
355
358
|
let inlineSrc;
|
|
356
359
|
if (dataURI) {
|
|
357
360
|
inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);
|
|
@@ -393,11 +396,11 @@ var ReactInlineSVG = class extends React.PureComponent {
|
|
|
393
396
|
return node;
|
|
394
397
|
}
|
|
395
398
|
[...node.children].forEach((d) => {
|
|
396
|
-
if (d.attributes
|
|
399
|
+
if (d.attributes?.length) {
|
|
397
400
|
const attributes = Object.values(d.attributes).map((a) => {
|
|
398
401
|
const attribute = a;
|
|
399
|
-
const match =
|
|
400
|
-
if (match
|
|
402
|
+
const match = /url\((.*?)\)/.exec(a.value);
|
|
403
|
+
if (match?.[1]) {
|
|
401
404
|
attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);
|
|
402
405
|
}
|
|
403
406
|
return attribute;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash || randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { onLoad, src } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc && originalDesc.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle && originalTitle.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = src.match(/^data:image\\/svg[^,]*?(;base64)?,(.*)/u);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes && d.attributes.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = a.value.match(/url\\((.*?)\\)/);\n\n if (match && match[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = React.useRef(false);\n const [isReady, setReady] = React.useState(cacheStore.isReady);\n\n React.useEffect(() => {\n if (!hasCallback.current) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n\n hasCallback.current = true;\n }\n }, []);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType || '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* istanbul ignore next */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n /* istanbul ignore else */\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { canUseDOM, request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n }\n\n if (usePersistentCache) {\n caches.open(cacheName).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\n this.subscribers.forEach(callback => callback());\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n let retryCount = 0;\n\n // eslint-disable-next-line no-await-in-loop\n while (this.cacheStore.get(url)?.status === STATUS.LOADING && retryCount < CACHE_MAX_RETRIES) {\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n retryCount += 1;\n }\n\n if (retryCount >= CACHE_MAX_RETRIES) {\n await callback();\n }\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n for (const key of keys) {\n // eslint-disable-next-line no-await-in-loop\n await this.cacheApi.delete(key);\n }\n }\n\n this.cacheStore.clear();\n }\n}\n"],"mappings":";;;;;;;;AAAA,YAAY,WAAW;AACvB,OAAO,aAAa;;;ACDb,IAAM,aAAa;AACnB,IAAM,oBAAoB;AAE1B,IAAM,SAAS;AAAA,EACpB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,aAAa;AACf;;;ACRO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,YAAY,OAAO,SAAS;AAChF;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAEA,eAAsB,QAAQ,KAAa,SAAuB;AAChE,QAAM,WAAW,MAAM,MAAM,KAAK,OAAO;AACzC,QAAM,cAAc,SAAS,QAAQ,IAAI,cAAc;AACvD,QAAM,CAAC,QAAQ,KAAK,eAAe,IAAI,MAAM,OAAO;AAEpD,MAAI,SAAS,SAAS,KAAK;AACzB,UAAM,IAAI,MAAM,WAAW;AAAA,EAC7B;AAEA,MAAI,CAAC,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index.tsx","../src/config.ts","../src/helpers.ts","../src/cache.ts"],"sourcesContent":["import * as React from 'react';\nimport convert from 'react-from-dom';\n\nimport CacheStore from './cache';\nimport { STATUS } from './config';\nimport { canUseDOM, isSupportedEnvironment, omit, randomString, request } from './helpers';\nimport { FetchError, Props, State, Status } from './types';\n\n// eslint-disable-next-line import/no-mutable-exports\nexport let cacheStore: CacheStore;\n\nclass ReactInlineSVG extends React.PureComponent<Props, State> {\n private readonly hash: string;\n private isActive = false;\n private isInitialized = false;\n\n public static defaultProps = {\n cacheRequests: true,\n uniquifyIDs: false,\n };\n\n constructor(props: Props) {\n super(props);\n\n this.state = {\n content: '',\n element: null,\n isCached: !!props.cacheRequests && cacheStore.isCached(props.src),\n status: STATUS.IDLE,\n };\n\n this.hash = props.uniqueHash ?? randomString(8);\n }\n\n public componentDidMount(): void {\n this.isActive = true;\n\n if (!canUseDOM() || this.isInitialized) {\n return;\n }\n\n const { status } = this.state;\n const { src } = this.props;\n\n try {\n /* istanbul ignore else */\n if (status === STATUS.IDLE) {\n /* istanbul ignore else */\n if (!isSupportedEnvironment()) {\n throw new Error('Browser does not support SVG');\n }\n\n /* istanbul ignore else */\n if (!src) {\n throw new Error('Missing src');\n }\n\n this.load();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n\n this.isInitialized = true;\n }\n\n public componentDidUpdate(previousProps: Props, previousState: State): void {\n if (!canUseDOM()) {\n return;\n }\n\n const { isCached, status } = this.state;\n const { description, onLoad, src, title } = this.props;\n\n if (previousState.status !== STATUS.READY && status === STATUS.READY) {\n /* istanbul ignore else */\n if (onLoad) {\n onLoad(src, isCached);\n }\n }\n\n if (previousProps.src !== src) {\n if (!src) {\n this.handleError(new Error('Missing src'));\n\n return;\n }\n\n this.load();\n }\n\n if (previousProps.title !== title || previousProps.description !== description) {\n this.getElement();\n }\n }\n\n public componentWillUnmount(): void {\n this.isActive = false;\n }\n\n private fetchContent = async () => {\n const { fetchOptions, src } = this.props;\n\n const content: string = await request(src, fetchOptions);\n\n this.handleLoad(content);\n };\n\n private getElement() {\n try {\n const node = this.getNode() as Node;\n const element = convert(node);\n\n if (!element || !React.isValidElement(element)) {\n throw new Error('Could not convert the src to a React element');\n }\n\n this.setState({\n element,\n status: STATUS.READY,\n });\n } catch (error: any) {\n this.handleError(new Error(error.message));\n }\n }\n\n private getNode() {\n const { description, title } = this.props;\n\n try {\n const svgText = this.processSVG();\n const node = convert(svgText, { nodeOnly: true });\n\n if (!node || !(node instanceof SVGSVGElement)) {\n throw new Error('Could not convert the src to a DOM Node');\n }\n\n const svg = this.updateSVGAttributes(node);\n\n if (description) {\n const originalDesc = svg.querySelector('desc');\n\n if (originalDesc?.parentNode) {\n originalDesc.parentNode.removeChild(originalDesc);\n }\n\n const descElement = document.createElementNS('http://www.w3.org/2000/svg', 'desc');\n\n descElement.innerHTML = description;\n svg.prepend(descElement);\n }\n\n if (typeof title !== 'undefined') {\n const originalTitle = svg.querySelector('title');\n\n if (originalTitle?.parentNode) {\n originalTitle.parentNode.removeChild(originalTitle);\n }\n\n if (title) {\n const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title');\n\n titleElement.innerHTML = title;\n svg.prepend(titleElement);\n }\n }\n\n return svg;\n } catch (error: any) {\n return this.handleError(error);\n }\n }\n\n private handleError = (error: Error | FetchError) => {\n const { onError } = this.props;\n const status =\n error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;\n\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState({ status }, () => {\n /* istanbul ignore else */\n if (typeof onError === 'function') {\n onError(error);\n }\n });\n }\n };\n\n private handleLoad = (content: string, hasCache = false) => {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content,\n isCached: hasCache,\n status: STATUS.LOADED,\n },\n this.getElement,\n );\n }\n };\n\n private load() {\n /* istanbul ignore else */\n if (this.isActive) {\n this.setState(\n {\n content: '',\n element: null,\n isCached: false,\n status: STATUS.LOADING,\n },\n async () => {\n const { cacheRequests, fetchOptions, src } = this.props;\n\n const dataURI = /^data:image\\/svg[^,]*?(;base64)?,(.*)/u.exec(src);\n let inlineSrc;\n\n if (dataURI) {\n inlineSrc = dataURI[1] ? window.atob(dataURI[2]) : decodeURIComponent(dataURI[2]);\n } else if (src.includes('<svg')) {\n inlineSrc = src;\n }\n\n if (inlineSrc) {\n this.handleLoad(inlineSrc);\n\n return;\n }\n\n try {\n if (cacheRequests) {\n const content = await cacheStore.get(src, fetchOptions);\n\n this.handleLoad(content, true);\n } else {\n await this.fetchContent();\n }\n } catch (error: any) {\n this.handleError(error);\n }\n },\n );\n }\n }\n\n private processSVG() {\n const { content } = this.state;\n const { preProcessor } = this.props;\n\n if (preProcessor) {\n return preProcessor(content);\n }\n\n return content;\n }\n\n private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {\n const { baseURL = '', uniquifyIDs } = this.props;\n const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];\n const linkAttributes = ['href', 'xlink:href'];\n const isDataValue = (name: string, value: string) =>\n linkAttributes.includes(name) && (value ? !value.includes('#') : false);\n\n if (!uniquifyIDs) {\n return node;\n }\n\n [...node.children].forEach(d => {\n if (d.attributes?.length) {\n const attributes = Object.values(d.attributes).map(a => {\n const attribute = a;\n const match = /url\\((.*?)\\)/.exec(a.value);\n\n if (match?.[1]) {\n attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);\n }\n\n return attribute;\n });\n\n replaceableAttributes.forEach(r => {\n const attribute = attributes.find(a => a.name === r);\n\n if (attribute && !isDataValue(r, attribute.value)) {\n attribute.value = `${attribute.value}__${this.hash}`;\n }\n });\n }\n\n if (d.children.length) {\n return this.updateSVGAttributes(d as SVGSVGElement);\n }\n\n return d;\n });\n\n return node;\n }\n\n public render(): React.ReactNode {\n const { element, status } = this.state;\n const { children = null, innerRef, loader = null } = this.props;\n const elementProps = omit(\n this.props,\n 'baseURL',\n 'cacheRequests',\n 'children',\n 'description',\n 'fetchOptions',\n 'innerRef',\n 'loader',\n 'onError',\n 'onLoad',\n 'preProcessor',\n 'src',\n 'title',\n 'uniqueHash',\n 'uniquifyIDs',\n );\n\n if (!canUseDOM()) {\n return loader;\n }\n\n if (element) {\n return React.cloneElement(element as React.ReactElement, { ref: innerRef, ...elementProps });\n }\n\n if (([STATUS.UNSUPPORTED, STATUS.FAILED] as Status[]).includes(status)) {\n return children;\n }\n\n return loader;\n }\n}\n\nexport default function InlineSVG(props: Props) {\n if (!cacheStore) {\n cacheStore = new CacheStore();\n }\n\n const { loader } = props;\n const hasCallback = React.useRef(false);\n const [isReady, setReady] = React.useState(cacheStore.isReady);\n\n React.useEffect(() => {\n if (!hasCallback.current) {\n cacheStore.onReady(() => {\n setReady(true);\n });\n\n hasCallback.current = true;\n }\n }, []);\n\n if (!isReady) {\n return loader;\n }\n\n return <ReactInlineSVG {...props} />;\n}\n\nexport * from './types';\n","export const CACHE_NAME = 'react-inlinesvg';\nexport const CACHE_MAX_RETRIES = 10;\n\nexport const STATUS = {\n IDLE: 'idle',\n LOADING: 'loading',\n LOADED: 'loaded',\n FAILED: 'failed',\n READY: 'ready',\n UNSUPPORTED: 'unsupported',\n} as const;\n","import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* istanbul ignore next */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n /* istanbul ignore else */\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n","import { CACHE_MAX_RETRIES, CACHE_NAME, STATUS } from './config';\nimport { canUseDOM, request, sleep } from './helpers';\nimport { StorageItem } from './types';\n\nexport default class CacheStore {\n private cacheApi: Cache | undefined;\n private readonly cacheStore: Map<string, StorageItem>;\n private readonly subscribers: Array<() => void> = [];\n public isReady = false;\n\n constructor() {\n this.cacheStore = new Map<string, StorageItem>();\n\n let cacheName = CACHE_NAME;\n let usePersistentCache = false;\n\n if (canUseDOM()) {\n cacheName = window.REACT_INLINESVG_CACHE_NAME ?? CACHE_NAME;\n usePersistentCache = !!window.REACT_INLINESVG_PERSISTENT_CACHE;\n }\n\n if (usePersistentCache) {\n caches.open(cacheName).then(cache => {\n this.cacheApi = cache;\n this.isReady = true;\n\n this.subscribers.forEach(callback => callback());\n });\n } else {\n this.isReady = true;\n }\n }\n\n public onReady(callback: () => void) {\n if (this.isReady) {\n callback();\n } else {\n this.subscribers.push(callback);\n }\n }\n\n public async get(url: string, fetchOptions?: RequestInit) {\n await (this.cacheApi\n ? this.fetchAndAddToPersistentCache(url, fetchOptions)\n : this.fetchAndAddToInternalCache(url, fetchOptions));\n\n return this.cacheStore.get(url)?.content ?? '';\n }\n\n public set(url: string, data: StorageItem) {\n this.cacheStore.set(url, data);\n }\n\n public isCached(url: string) {\n return this.cacheStore.get(url)?.status === STATUS.LOADED;\n }\n\n private async fetchAndAddToInternalCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToInternalCache(url, fetchOptions);\n });\n\n return;\n }\n\n if (!cache?.content) {\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n try {\n const content = await request(url, fetchOptions);\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n }\n\n private async fetchAndAddToPersistentCache(url: string, fetchOptions?: RequestInit) {\n const cache = this.cacheStore.get(url);\n\n if (cache?.status === STATUS.LOADED) {\n return;\n }\n\n if (cache?.status === STATUS.LOADING) {\n await this.handleLoading(url, async () => {\n this.cacheStore.set(url, { content: '', status: STATUS.IDLE });\n await this.fetchAndAddToPersistentCache(url, fetchOptions);\n });\n\n return;\n }\n\n this.cacheStore.set(url, { content: '', status: STATUS.LOADING });\n\n const data = await this.cacheApi?.match(url);\n\n if (data) {\n const content = await data.text();\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n\n return;\n }\n\n try {\n await this.cacheApi?.add(new Request(url, fetchOptions));\n\n const response = await this.cacheApi?.match(url);\n const content = (await response?.text()) ?? '';\n\n this.cacheStore.set(url, { content, status: STATUS.LOADED });\n } catch (error: any) {\n this.cacheStore.set(url, { content: '', status: STATUS.FAILED });\n throw error;\n }\n }\n\n private async handleLoading(url: string, callback: () => Promise<void>) {\n let retryCount = 0;\n\n // eslint-disable-next-line no-await-in-loop\n while (this.cacheStore.get(url)?.status === STATUS.LOADING && retryCount < CACHE_MAX_RETRIES) {\n // eslint-disable-next-line no-await-in-loop\n await sleep(0.1);\n retryCount += 1;\n }\n\n if (retryCount >= CACHE_MAX_RETRIES) {\n await callback();\n }\n }\n\n public keys(): Array<string> {\n return [...this.cacheStore.keys()];\n }\n\n public data(): Array<Record<string, StorageItem>> {\n return [...this.cacheStore.entries()].map(([key, value]) => ({ [key]: value }));\n }\n\n public async delete(url: string) {\n if (this.cacheApi) {\n await this.cacheApi.delete(url);\n }\n\n this.cacheStore.delete(url);\n }\n\n public async clear() {\n if (this.cacheApi) {\n const keys = await this.cacheApi.keys();\n\n for (const key of keys) {\n // eslint-disable-next-line no-await-in-loop\n await this.cacheApi.delete(key);\n }\n }\n\n this.cacheStore.clear();\n }\n}\n"],"mappings":";;;;;;;;AAAA,YAAY,WAAW;AACvB,OAAO,aAAa;;;ACDb,IAAM,aAAa;AACnB,IAAM,oBAAoB;AAE1B,IAAM,SAAS;AAAA,EACpB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,aAAa;AACf;;;ACRO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,YAAY,OAAO,SAAS;AAChF;AAEO,SAAS,yBAAkC;AAChD,SAAO,kBAAkB,KAAK,OAAO,WAAW,eAAe,WAAW;AAC5E;AAEA,eAAsB,QAAQ,KAAa,SAAuB;AAChE,QAAM,WAAW,MAAM,MAAM,KAAK,OAAO;AACzC,QAAM,cAAc,SAAS,QAAQ,IAAI,cAAc;AACvD,QAAM,CAAC,QAAQ,KAAK,eAAe,IAAI,MAAM,OAAO;AAEpD,MAAI,SAAS,SAAS,KAAK;AACzB,UAAM,IAAI,MAAM,WAAW;AAAA,EAC7B;AAEA,MAAI,CAAC,CAAC,iBAAiB,YAAY,EAAE,KAAK,OAAK,SAAS,SAAS,CAAC,CAAC,GAAG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CHANGED
|
@@ -37,4 +37,10 @@ function CacheProvider({ children, name }) {
|
|
|
37
37
|
}
|
|
38
38
|
return children;
|
|
39
39
|
}
|
|
40
|
-
//# sourceMappingURL=provider.js.map
|
|
40
|
+
//# sourceMappingURL=provider.js.map
|
|
41
|
+
// fix-cjs-exports
|
|
42
|
+
if (module.exports.default) {
|
|
43
|
+
Object.assign(module.exports.default, module.exports);
|
|
44
|
+
module.exports = module.exports.default;
|
|
45
|
+
delete module.exports.default;
|
|
46
|
+
}
|
package/dist/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.tsx","../src/helpers.ts"],"sourcesContent":["import { JSX } from 'react';\n\nimport { canUseDOM } from './helpers';\n\ninterface Props {\n children: JSX.Element;\n name?: string;\n}\n\nexport default function CacheProvider({ children, name }: Props) {\n if (canUseDOM()) {\n window.REACT_INLINESVG_CACHE_NAME = name;\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n }\n\n return children;\n}\n","import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType
|
|
1
|
+
{"version":3,"sources":["../src/provider.tsx","../src/helpers.ts"],"sourcesContent":["import { JSX } from 'react';\n\nimport { canUseDOM } from './helpers';\n\ninterface Props {\n children: JSX.Element;\n name?: string;\n}\n\nexport default function CacheProvider({ children, name }: Props) {\n if (canUseDOM()) {\n window.REACT_INLINESVG_CACHE_NAME = name;\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n }\n\n return children;\n}\n","import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* istanbul ignore next */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n /* istanbul ignore else */\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,YAAY,OAAO,SAAS;AAChF;;;ADKe,SAAR,cAA+B,EAAE,UAAU,KAAK,GAAU;AAC/D,MAAI,UAAU,GAAG;AACf,WAAO,6BAA6B;AACpC,WAAO,mCAAmC;AAAA,EAC5C;AAEA,SAAO;AACT;","names":[]}
|
package/dist/provider.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/helpers.ts","../src/provider.tsx"],"sourcesContent":["import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType
|
|
1
|
+
{"version":3,"sources":["../src/helpers.ts","../src/provider.tsx"],"sourcesContent":["import type { PlainObject } from './types';\n\nexport function canUseDOM(): boolean {\n return !!(typeof window !== 'undefined' && window.document && window.document.createElement);\n}\n\nexport function isSupportedEnvironment(): boolean {\n return supportsInlineSVG() && typeof window !== 'undefined' && window !== null;\n}\n\nexport async function request(url: string, options?: RequestInit) {\n const response = await fetch(url, options);\n const contentType = response.headers.get('content-type');\n const [fileType] = (contentType ?? '').split(/ ?; ?/);\n\n if (response.status > 299) {\n throw new Error('Not found');\n }\n\n if (!['image/svg+xml', 'text/plain'].some(d => fileType.includes(d))) {\n throw new Error(`Content type isn't valid: ${fileType}`);\n }\n\n return response.text();\n}\n\nexport function sleep(seconds = 1) {\n return new Promise(resolve => {\n setTimeout(resolve, seconds * 1000);\n });\n}\n\nexport function supportsInlineSVG(): boolean {\n /* istanbul ignore next */\n if (!document) {\n return false;\n }\n\n const div = document.createElement('div');\n\n div.innerHTML = '<svg />';\n const svg = div.firstChild as SVGSVGElement;\n\n return !!svg && svg.namespaceURI === 'http://www.w3.org/2000/svg';\n}\n\nfunction randomCharacter(character: string) {\n return character[Math.floor(Math.random() * character.length)];\n}\n\nexport function randomString(length: number): string {\n const letters = 'abcdefghijklmnopqrstuvwxyz';\n const numbers = '1234567890';\n const charset = `${letters}${letters.toUpperCase()}${numbers}`;\n\n let R = '';\n\n for (let index = 0; index < length; index++) {\n R += randomCharacter(charset);\n }\n\n return R;\n}\n\n/**\n * Remove properties from an object\n */\nexport function omit<T extends PlainObject, K extends keyof T>(\n input: T,\n ...filter: K[]\n): Omit<T, K> {\n const output: any = {};\n\n for (const key in input) {\n /* istanbul ignore else */\n if ({}.hasOwnProperty.call(input, key)) {\n if (!filter.includes(key as unknown as K)) {\n output[key] = input[key];\n }\n }\n }\n\n return output as Omit<T, K>;\n}\n","import { JSX } from 'react';\n\nimport { canUseDOM } from './helpers';\n\ninterface Props {\n children: JSX.Element;\n name?: string;\n}\n\nexport default function CacheProvider({ children, name }: Props) {\n if (canUseDOM()) {\n window.REACT_INLINESVG_CACHE_NAME = name;\n window.REACT_INLINESVG_PERSISTENT_CACHE = true;\n }\n\n return children;\n}\n"],"mappings":";AAEO,SAAS,YAAqB;AACnC,SAAO,CAAC,EAAE,OAAO,WAAW,eAAe,OAAO,YAAY,OAAO,SAAS;AAChF;;;ACKe,SAAR,cAA+B,EAAE,UAAU,KAAK,GAAU;AAC/D,MAAI,UAAU,GAAG;AACf,WAAO,6BAA6B;AACpC,WAAO,mCAAmC;AAAA,EAC5C;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-inlinesvg",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"description": "An SVG loader for React",
|
|
5
5
|
"author": "Gil Barbara <gilbarbara@gmail.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -54,27 +54,28 @@
|
|
|
54
54
|
"react-from-dom": "^0.6.2"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@gilbarbara/eslint-config": "^0.5.
|
|
58
|
-
"@gilbarbara/helpers": "^0.8.
|
|
57
|
+
"@gilbarbara/eslint-config": "^0.5.4",
|
|
58
|
+
"@gilbarbara/helpers": "^0.8.6",
|
|
59
59
|
"@gilbarbara/prettier-config": "^1.0.0",
|
|
60
60
|
"@gilbarbara/tsconfig": "^0.2.3",
|
|
61
|
-
"@size-limit/preset-small-lib": "^
|
|
62
|
-
"@testing-library/jest-dom": "^6.1.
|
|
61
|
+
"@size-limit/preset-small-lib": "^9.0.0",
|
|
62
|
+
"@testing-library/jest-dom": "^6.1.3",
|
|
63
63
|
"@testing-library/react": "^14.0.0",
|
|
64
64
|
"@types/exenv": "^1.2.0",
|
|
65
|
-
"@types/fetch-mock": "^7.3.
|
|
66
|
-
"@types/jest": "^29.5.
|
|
67
|
-
"@types/node": "^20.
|
|
68
|
-
"@types/node-fetch": "^2.6.
|
|
69
|
-
"@types/react": "^18.2.
|
|
70
|
-
"@types/react-dom": "^18.2.
|
|
65
|
+
"@types/fetch-mock": "^7.3.6",
|
|
66
|
+
"@types/jest": "^29.5.5",
|
|
67
|
+
"@types/node": "^20.8.2",
|
|
68
|
+
"@types/node-fetch": "^2.6.6",
|
|
69
|
+
"@types/react": "^18.2.24",
|
|
70
|
+
"@types/react-dom": "^18.2.8",
|
|
71
71
|
"browser-cache-mock": "^0.1.7",
|
|
72
72
|
"cross-fetch": "^4.0.0",
|
|
73
|
-
"del-cli": "^5.
|
|
73
|
+
"del-cli": "^5.1.0",
|
|
74
|
+
"fix-tsup-cjs": "^1.2.0",
|
|
74
75
|
"http-server": "^14.1.1",
|
|
75
76
|
"husky": "^8.0.3",
|
|
76
|
-
"jest": "^29.
|
|
77
|
-
"jest-environment-jsdom": "^29.
|
|
77
|
+
"jest": "^29.7.0",
|
|
78
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
78
79
|
"jest-extended": "^4.0.1",
|
|
79
80
|
"jest-fetch-mock": "^3.0.3",
|
|
80
81
|
"jest-serializer-html": "^7.1.0",
|
|
@@ -82,19 +83,19 @@
|
|
|
82
83
|
"react": "^18.2.0",
|
|
83
84
|
"react-dom": "^18.2.0",
|
|
84
85
|
"repo-tools": "^0.2.2",
|
|
85
|
-
"size-limit": "^
|
|
86
|
-
"start-server-and-test": "^2.0.
|
|
86
|
+
"size-limit": "^9.0.0",
|
|
87
|
+
"start-server-and-test": "^2.0.1",
|
|
87
88
|
"ts-jest": "^29.1.1",
|
|
88
89
|
"ts-node": "^10.9.1",
|
|
89
90
|
"tsup": "^7.2.0",
|
|
90
91
|
"typescript": "^5.2.2"
|
|
91
92
|
},
|
|
92
93
|
"scripts": {
|
|
93
|
-
"build": "pnpm run clean && tsup",
|
|
94
|
+
"build": "pnpm run clean && tsup && fix-tsup-cjs",
|
|
94
95
|
"watch": "tsup --watch",
|
|
95
96
|
"clean": "del dist/*",
|
|
96
97
|
"start": "http-server test/__fixtures__ -p 1337 --cors",
|
|
97
|
-
"lint": "eslint
|
|
98
|
+
"lint": "eslint src test",
|
|
98
99
|
"test": "start-server-and-test start http://127.0.0.1:1337 test:coverage",
|
|
99
100
|
"test:coverage": "jest --coverage --bail",
|
|
100
101
|
"test:watch": "jest --watchAll --verbose",
|
package/src/helpers.ts
CHANGED
|
@@ -11,7 +11,7 @@ export function isSupportedEnvironment(): boolean {
|
|
|
11
11
|
export async function request(url: string, options?: RequestInit) {
|
|
12
12
|
const response = await fetch(url, options);
|
|
13
13
|
const contentType = response.headers.get('content-type');
|
|
14
|
-
const [fileType] = (contentType
|
|
14
|
+
const [fileType] = (contentType ?? '').split(/ ?; ?/);
|
|
15
15
|
|
|
16
16
|
if (response.status > 299) {
|
|
17
17
|
throw new Error('Not found');
|
package/src/index.tsx
CHANGED
|
@@ -29,7 +29,7 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
|
|
|
29
29
|
status: STATUS.IDLE,
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
this.hash = props.uniqueHash
|
|
32
|
+
this.hash = props.uniqueHash ?? randomString(8);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
public componentDidMount(): void {
|
|
@@ -70,7 +70,7 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
const { isCached, status } = this.state;
|
|
73
|
-
const { onLoad, src } = this.props;
|
|
73
|
+
const { description, onLoad, src, title } = this.props;
|
|
74
74
|
|
|
75
75
|
if (previousState.status !== STATUS.READY && status === STATUS.READY) {
|
|
76
76
|
/* istanbul ignore else */
|
|
@@ -88,6 +88,10 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
|
|
|
88
88
|
|
|
89
89
|
this.load();
|
|
90
90
|
}
|
|
91
|
+
|
|
92
|
+
if (previousProps.title !== title || previousProps.description !== description) {
|
|
93
|
+
this.getElement();
|
|
94
|
+
}
|
|
91
95
|
}
|
|
92
96
|
|
|
93
97
|
public componentWillUnmount(): void {
|
|
@@ -136,7 +140,7 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
|
|
|
136
140
|
if (description) {
|
|
137
141
|
const originalDesc = svg.querySelector('desc');
|
|
138
142
|
|
|
139
|
-
if (originalDesc
|
|
143
|
+
if (originalDesc?.parentNode) {
|
|
140
144
|
originalDesc.parentNode.removeChild(originalDesc);
|
|
141
145
|
}
|
|
142
146
|
|
|
@@ -149,7 +153,7 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
|
|
|
149
153
|
if (typeof title !== 'undefined') {
|
|
150
154
|
const originalTitle = svg.querySelector('title');
|
|
151
155
|
|
|
152
|
-
if (originalTitle
|
|
156
|
+
if (originalTitle?.parentNode) {
|
|
153
157
|
originalTitle.parentNode.removeChild(originalTitle);
|
|
154
158
|
}
|
|
155
159
|
|
|
@@ -210,7 +214,7 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
|
|
|
210
214
|
async () => {
|
|
211
215
|
const { cacheRequests, fetchOptions, src } = this.props;
|
|
212
216
|
|
|
213
|
-
const dataURI =
|
|
217
|
+
const dataURI = /^data:image\/svg[^,]*?(;base64)?,(.*)/u.exec(src);
|
|
214
218
|
let inlineSrc;
|
|
215
219
|
|
|
216
220
|
if (dataURI) {
|
|
@@ -264,12 +268,12 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
|
|
|
264
268
|
}
|
|
265
269
|
|
|
266
270
|
[...node.children].forEach(d => {
|
|
267
|
-
if (d.attributes
|
|
271
|
+
if (d.attributes?.length) {
|
|
268
272
|
const attributes = Object.values(d.attributes).map(a => {
|
|
269
273
|
const attribute = a;
|
|
270
|
-
const match =
|
|
274
|
+
const match = /url\((.*?)\)/.exec(a.value);
|
|
271
275
|
|
|
272
|
-
if (match
|
|
276
|
+
if (match?.[1]) {
|
|
273
277
|
attribute.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);
|
|
274
278
|
}
|
|
275
279
|
|