mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-30 05:42:44 -08:00
Optimize DOMPurify instantiation in sanitize
This commit is contained in:
1 parent
c3dff6bb70
commit
ebd0e2af7a
1 file changed
+10
-1
@@ -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) => {
|
||||
|
||||
Reference in new issue
Block a user