chore: fix dompurify types

This commit is contained in:
MiniDigger | Martin 2024-11-17 22:39:24 +01:00
parent 68e08efb4f
commit 2a97934a27
4 changed files with 6 additions and 17 deletions

View File

@ -73,7 +73,6 @@
"@types/bun": "1.1.13", "@types/bun": "1.1.13",
"@types/debug": "4.1.12", "@types/debug": "4.1.12",
"@types/diff-match-patch": "1.0.36", "@types/diff-match-patch": "1.0.36",
"@types/dompurify": "3.0.5",
"@types/jsdom": "21.1.7", "@types/jsdom": "21.1.7",
"@types/locale": "0.1.4", "@types/locale": "0.1.4",
"@types/lodash-es": "4.17.12", "@types/lodash-es": "4.17.12",

View File

@ -153,9 +153,6 @@ importers:
'@types/diff-match-patch': '@types/diff-match-patch':
specifier: 1.0.36 specifier: 1.0.36
version: 1.0.36 version: 1.0.36
'@types/dompurify':
specifier: 3.0.5
version: 3.0.5
'@types/jsdom': '@types/jsdom':
specifier: 21.1.7 specifier: 21.1.7
version: 21.1.7 version: 21.1.7
@ -1948,9 +1945,6 @@ packages:
'@types/diff-match-patch@1.0.36': '@types/diff-match-patch@1.0.36':
resolution: {integrity: sha512-xFdR6tkm0MWvBfO8xXCSsinYxHcqkQUlcHeSpMC2ukzOb6lwQAfDmW+Qt0AvlGd8HpsS28qKsB+oPeJn9I39jg==} resolution: {integrity: sha512-xFdR6tkm0MWvBfO8xXCSsinYxHcqkQUlcHeSpMC2ukzOb6lwQAfDmW+Qt0AvlGd8HpsS28qKsB+oPeJn9I39jg==}
'@types/dompurify@3.0.5':
resolution: {integrity: sha512-1Wg0g3BtQF7sSb27fJQAKck1HECM6zV1EB66j8JH9i3LCjYabJa0FSdiSgsD5K/RbrsR0SiraKacLB+T8ZVYAg==}
'@types/eslint@8.56.12': '@types/eslint@8.56.12':
resolution: {integrity: sha512-03ruubjWyOHlmljCVoxSuNDdmfZDzsrrz0P2LeJsOXr+ZwFQ+0yQIwNCwt/GYhV7Z31fgtXJTAEs+FYlEL851g==} resolution: {integrity: sha512-03ruubjWyOHlmljCVoxSuNDdmfZDzsrrz0P2LeJsOXr+ZwFQ+0yQIwNCwt/GYhV7Z31fgtXJTAEs+FYlEL851g==}
@ -9042,10 +9036,6 @@ snapshots:
'@types/diff-match-patch@1.0.36': {} '@types/diff-match-patch@1.0.36': {}
'@types/dompurify@3.0.5':
dependencies:
'@types/trusted-types': 2.0.7
'@types/eslint@8.56.12': '@types/eslint@8.56.12':
dependencies: dependencies:
'@types/estree': 1.0.6 '@types/estree': 1.0.6

View File

@ -1,4 +1,4 @@
import type { Config, DOMPurifyI } from "dompurify"; import type { default as DOMPurify, Config } from "dompurify";
export const config = { export const config = {
FORBID_TAGS: ["style", "base", "head", "link", "meta", "title", "body", "form", "input", "dialog", "embed", "button", "frame", "html", "textarea"], FORBID_TAGS: ["style", "base", "head", "link", "meta", "title", "body", "form", "input", "dialog", "embed", "button", "frame", "html", "textarea"],
@ -8,12 +8,12 @@ export const config = {
} as Config; } as Config;
export function useDomPurify(text?: string) { export function useDomPurify(text?: string) {
if (!text) return ""; if (!text) return "";
const dompurify = useNuxtApp().$dompurify as DOMPurifyI; const dompurify = useNuxtApp().$dompurify as typeof DOMPurify; // TODO cleanup when DOMPurify exports the type in the next release
// Manually handle iframe to allow YouTube video embeds // Manually handle iframe to allow YouTube video embeds
dompurify.addHook("uponSanitizeElement", (node: any, data) => { dompurify.addHook("uponSanitizeElement", (node, data) => {
if (data.tagName === "iframe") { if (data.tagName === "iframe") {
const src = node.getAttribute("src") || ""; const src = (node as HTMLIFrameElement).getAttribute("src") || "";
if (!src.startsWith("https://www.youtube.com/embed/")) { if (!src.startsWith("https://www.youtube.com/embed/")) {
return node.parentNode && node.parentNode.removeChild(node); return node.parentNode && node.parentNode.removeChild(node);
} }
@ -26,6 +26,6 @@ export function useDomPurify(text?: string) {
export const aggressiveConfig = { ALLOWED_TAGS: ["#text"] } as Config; export const aggressiveConfig = { ALLOWED_TAGS: ["#text"] } as Config;
export function stripAllHtml(text?: string) { export function stripAllHtml(text?: string) {
if (!text) return ""; if (!text) return "";
const dompurify = useNuxtApp().$dompurify as DOMPurifyI; const dompurify = useNuxtApp().$dompurify as typeof DOMPurify; // TODO cleanup when DOMPurify exports the type in the next release
return dompurify.sanitize(text, aggressiveConfig) as string; return dompurify.sanitize(text, aggressiveConfig) as string;
} }

View File

@ -1,7 +1,7 @@
import DOMPurify from "dompurify"; import DOMPurify from "dompurify";
export default defineNuxtPlugin((nuxtApp) => { export default defineNuxtPlugin((nuxtApp) => {
const purify = DOMPurify(); const purify = DOMPurify(window);
return { return {
provide: { provide: {
dompurify: purify, dompurify: purify,