-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add validation checks for SVG and encoded data urls
- Loading branch information
1 parent
6d46d74
commit 0918252
Showing
2 changed files
with
35 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,49 @@ | ||
import { validateContent } from './validate'; | ||
import { isEncodedDataUrl, isSvgDataUrl, validateContent } from './validate'; | ||
|
||
export const pdsIconContent = new Map<string, string>(); | ||
const requests = new Map<string, Promise<any>>(); // eslint-disable-line @typescript-eslint/no-explicit-any | ||
|
||
let parser: DOMParser; | ||
|
||
export const getSvgContent = (url: string, sanitize = false) => { | ||
let req = requests.get(url); | ||
|
||
if(!req) { | ||
if (typeof fetch != 'undefined' && typeof document !== 'undefined') { | ||
// we don't have a request | ||
req = fetch(url).then((rsp) => { | ||
if (rsp.ok) { | ||
return rsp.text().then((svgContent) => { | ||
if (svgContent && sanitize !== false) { | ||
svgContent = validateContent(svgContent); | ||
} | ||
pdsIconContent.set(url, svgContent || ''); | ||
}); | ||
if (isSvgDataUrl(url) && isEncodedDataUrl(url)) { | ||
if (!parser) { | ||
parser = new DOMParser(); | ||
} | ||
|
||
const doc = parser.parseFromString(url, 'text/html'); | ||
const svg = doc.querySelector('svg'); | ||
|
||
if (svg) { | ||
pdsIconContent.set(url, svg.outerHTML); | ||
} | ||
pdsIconContent.set(url, ''); | ||
}); | ||
|
||
requests.set(url, req); | ||
return Promise.resolve(); | ||
} else { | ||
// we don't have a request | ||
req = fetch(url).then((rsp) => { | ||
if (rsp.ok) { | ||
return rsp.text().then((svgContent) => { | ||
if (svgContent && sanitize !== false) { | ||
svgContent = validateContent(svgContent); | ||
} | ||
pdsIconContent.set(url, svgContent || ''); | ||
}); | ||
} | ||
pdsIconContent.set(url, ''); | ||
}); | ||
|
||
requests.set(url, req); | ||
} | ||
} else { | ||
pdsIconContent.set(url, ''); | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
return req; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters