MCPcopy
hub / github.com/ionic-team/ionicons / validateContent

Function validateContent

src/components/icon/validate.ts:3–28  ·  view source on GitHub ↗
(svgContent: string)

Source from the content-addressed store, hash-verified

1import { isStr } from './utils';
2
3export const validateContent = (svgContent: string) => {
4 const div = document.createElement('div');
5 div.innerHTML = svgContent;
6
7 // setup this way to ensure it works on our buddy IE
8 for (let i = div.childNodes.length - 1; i >= 0; i--) {
9 if (div.childNodes[i].nodeName.toLowerCase() !== 'svg') {
10 div.removeChild(div.childNodes[i]);
11 }
12 }
13
14 // must only have 1 root element
15 const svgElm = div.firstElementChild;
16 if (svgElm && svgElm.nodeName.toLowerCase() === 'svg') {
17 const svgClass = svgElm.getAttribute('class') || '';
18 svgElm.setAttribute('class', (svgClass + ' s-ion-icon').trim());
19
20 // root element must be an svg
21 // lets double check we've got valid elements
22 // do not allow scripts
23 if (isValid(svgElm as any)) {
24 return div.innerHTML;
25 }
26 }
27 return '';
28};
29
30export const isValid = (elm: HTMLElement) => {
31 if (elm.nodeType === 1) {

Callers 1

fetchSvgFunction · 0.90

Calls 1

isValidFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…