@gechiui/dom
Version:
DOM utilities module for GeChiUI.
43 lines (37 loc) • 928 B
JavaScript
/**
* Internal dependencies
*/
import remove from './remove';
/**
* Strips scripts and on* attributes from HTML.
*
* @param {string} html HTML to sanitize.
*
* @return {string} The sanitized HTML.
*/
export default function safeHTML(html) {
const {
body
} = document.implementation.createHTMLDocument('');
body.innerHTML = html;
const elements = body.getElementsByTagName('*');
let elementIndex = elements.length;
while (elementIndex--) {
const element = elements[elementIndex];
if (element.tagName === 'SCRIPT') {
remove(element);
} else {
let attributeIndex = element.attributes.length;
while (attributeIndex--) {
const {
name: key
} = element.attributes[attributeIndex];
if (key.startsWith('on')) {
element.removeAttribute(key);
}
}
}
}
return body.innerHTML;
}
//# sourceMappingURL=safe-html.js.map