| 1 | import { isStr } from './utils'; |
| 2 | |
| 3 | export 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 | |
| 30 | export const isValid = (elm: HTMLElement) => { |
| 31 | if (elm.nodeType === 1) { |