Optimize DOMPurify instantiation in sanitize

This commit is contained in:
7w1 committed 2026-07-11 18:53:58 -05:00
1 parent c3dff6bb70
commit ebd0e2af7a
1 file changed
+10 -1
+10 -1
View File
@@ -283,13 +283,22 @@ const pruneInvalidEmptyElements = (
});
};
let purifyInstance: DOMPurify.DOMPurifyI | undefined;
function getPurify(): DOMPurify.DOMPurifyI {
if (!purifyInstance) {
purifyInstance = DOMPurify(window);
}
return purifyInstance;
}
export const sanitizeCustomHtml = (customHtml: string): string => {
if (typeof window === 'undefined') {
return sanitizeText(customHtml);
}
const { protectedHtml, protectedSources } = protectImageSources(customHtml);
const purify = DOMPurify(window);
const purify = getPurify();
purify.removeAllHooks();
const allowedHtmlAttributes = [...permittedHtmlAttributes, INTERNAL_IMG_SRC_ATTR];
purify.addHook('uponSanitizeAttribute', (currentNode, hookEvent) => {