Skip to content

Commit 79f5d35

Browse files
authored
Merge pull request #668 from code16/allow-target-blank-after-sanitize
Sanitization : Allow `a` target + ensure noopener
2 parents d7e0477 + 6babf16 commit 79f5d35

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

resources/js/utils/sanitize.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import DOMPurify from 'dompurify';
22

3+
DOMPurify.addHook('afterSanitizeAttributes', function (node) {
4+
if (node.tagName === 'A' && !node.getAttribute('rel')?.includes('noopener')) {
5+
node.setAttribute('rel', `${node.getAttribute('rel') ?? ''} noopener`.trim());
6+
}
7+
});
8+
39
export function sanitize(html: string | null) {
410
return html
511
? DOMPurify.sanitize(html, {
612
ADD_TAGS: ['iframe'],
13+
ADD_ATTR: ['target'],
714
CUSTOM_ELEMENT_HANDLING: {
815
tagNameCheck: () => true,
916
attributeNameCheck: (name) => {

0 commit comments

Comments
 (0)