fix templete

This commit is contained in:
hamid zarghami
2025-07-17 12:57:07 +03:30
parent 1139df424f
commit 76fa93ac42
3 changed files with 44 additions and 25 deletions
+16 -10
View File
@@ -89,22 +89,28 @@ export const sanitizeEmailHTML = (html: string): string => {
// Remove link tags (CSS files)
sanitized = sanitized.replace(/<link[^>]*>/gi, "");
// Remove all style attributes
sanitized = sanitized.replace(/\s*style\s*=\s*["'][^"']*["']/gi, "");
// Remove font-family and other font-related CSS properties
sanitized = sanitized
.replace(/font-family\s*:\s*[^;]*;?/gi, "")
.replace(/font-size\s*:\s*[^;]*;?/gi, "")
.replace(/font-weight\s*:\s*[^;]*;?/gi, "")
.replace(/color\s*:\s*[^;]*;?/gi, "");
// Remove script tags for security
sanitized = sanitized.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "");
// Remove potentially harmful attributes
sanitized = sanitized.replace(/\s*on\w+\s*=\s*["'][^"']*["']/gi, "");
// فقط font-family و font-size رو از inline styles حذف کن، بقیه رو حفظ کن
sanitized = sanitized.replace(
/style\s*=\s*["']([^"']*?)["']/gi,
(match, styles) => {
// فقط font-family و font-size رو حذف کن
const cleanedStyles = styles
.replace(/font-family\s*:\s*[^;]*;?/gi, "")
.replace(/font-size\s*:\s*[^;]*;?/gi, "")
.replace(/font-weight\s*:\s*[^;]*;?/gi, "")
.replace(/;\s*;/g, ";") // حذف سمیکالن‌های اضافی
.replace(/^\s*;\s*|\s*;\s*$/g, ""); // حذف سمیکالن‌های ابتدا و انتها
return cleanedStyles ? `style="${cleanedStyles}"` : "";
}
);
return sanitized;
} catch (error) {
console.error("خطا در پاک‌سازی HTML:", error);