UNPKG

@jbrowse/core

Version:

JBrowse 2 core libraries used by plugins

65 lines (64 loc) 1.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = SanitizedHTML; const jsx_runtime_1 = require("react/jsx-runtime"); const dompurify_1 = __importDefault(require("dompurify")); const escape_html_1 = __importDefault(require("escape-html")); const util_1 = require("../util"); const htmlTags = [ 'a', 'b', 'br', 'code', 'div', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'img', 'li', 'p', 'pre', 'span', 'small', 'strong', 'table', 'tbody', 'sup', 'sub', 'td', 'tfoot', 'th', 'thead', 'tr', 'u', 'ul', ]; let added = false; const full = new RegExp(htmlTags.map(tag => `<${tag}\\b[^>]*>`).join('|'), 'i'); function isHTML(str) { return full.test(str); } function SanitizedHTML({ html: pre, className, }) { const html = (0, util_1.linkify)(`${pre}`); const value = isHTML(html) ? html : (0, escape_html_1.default)(html); if (!added) { added = true; dompurify_1.default.addHook('afterSanitizeAttributes', node => { if (node.tagName === 'A') { node.setAttribute('rel', 'noopener noreferrer'); node.setAttribute('target', '_blank'); } }); } return ((0, jsx_runtime_1.jsx)("span", { className: className, dangerouslySetInnerHTML: { __html: dompurify_1.default.sanitize(value), } })); }