import DOMPurify from 'dompurify' /** * 净化 HTML 内容,防止 XSS * @param {string} dirty 原始 HTML * @returns {string} 安全 HTML */ export function safeHtml(dirty) { if (dirty == null || dirty === '') { return '' } return DOMPurify.sanitize(String(dirty), { ALLOWED_TAGS: [ 'a', 'b', 'br', 'div', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'img', 'li', 'ol', 'p', 'span', 'strong', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'u', 'ul' ], ALLOWED_ATTR: [ 'href', 'target', 'rel', 'src', 'alt', 'title', 'class', 'style', 'width', 'height', 'colspan', 'rowspan' ] }) } export default safeHtml