Browse Source

fix: allow target="_blank" in dompurify

Sv443 7 months ago
parent
commit
3fd29b4b44
1 changed files with 7 additions and 0 deletions
  1. 7 0
      src/utils/dom.ts

+ 7 - 0
src/utils/dom.ts

@@ -239,6 +239,13 @@ export function copyToClipboard(text: Stringifiable) {
 
 let ttPolicy: TTPolicy | undefined;
 
+DOMPurify.addHook("afterSanitizeAttributes", function (node) {
+  if("target" in node) {
+    node.setAttribute("target", "_blank");
+    node.setAttribute("rel", "noopener noreferrer");
+  }
+});
+
 /** Sets innerHTML directly on Firefox and Safari, while on Chromium a [Trusted Types policy](https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API) is used to set the HTML */
 export function setInnerHtml(element: HTMLElement, html: string) {
   if(!ttPolicy && window?.trustedTypes?.createPolicy) {