Skip to content

Commit

Permalink
Fix open in new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jun 28, 2022
1 parent 912fdb8 commit 9eb71bb
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions packages/core/ui/SanitizedHTML.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const htmlTags = [
]

let added = false

// adapted from is-html
// https://github.com/sindresorhus/is-html/blob/master/index.js
const full = new RegExp(htmlTags.map(tag => `<${tag}\\b[^>]*>`).join('|'), 'i')
Expand All @@ -49,20 +50,18 @@ export function isHTML(str: string) {

export default function SanitizedHTML({ html }: { html: string }) {
const value = isHTML(html) ? html : escapeHTML(html)
useEffect(() => {
if (!added) {
added = true
// see https://github.com/cure53/DOMPurify/issues/317
// only have to add this once, and can't do it globally because dompurify
// not yet initialized at global scope
dompurify.addHook('afterSanitizeAttributes', node => {
if (node.tagName === 'A') {
node.setAttribute('rel', 'noopener noreferrer')
node.setAttribute('target', '_blank')
}
})
}
}, [])
if (!added) {
added = true
// see https://github.com/cure53/DOMPurify/issues/317
// only have to add this once, and can't do it globally because dompurify
// not yet initialized at global scope
dompurify.addHook('afterSanitizeAttributes', node => {
if (node.tagName === 'A') {
node.setAttribute('rel', 'noopener noreferrer')
node.setAttribute('target', '_blank')
}
})
}

return (
<div
Expand Down

0 comments on commit 9eb71bb

Please sign in to comment.