From 273442d42c861b747a3ca95cb1b259b1a2036447 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 16:01:59 +0200 Subject: [PATCH] mail page rest --- lib/infomaniak-mime-decoder.ts | 51 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/infomaniak-mime-decoder.ts b/lib/infomaniak-mime-decoder.ts index bcd21264..9117b766 100644 --- a/lib/infomaniak-mime-decoder.ts +++ b/lib/infomaniak-mime-decoder.ts @@ -6,21 +6,25 @@ export function decodeQuotedPrintable(text: string, charset: string): string { // Replace soft line breaks (=\r\n or =\n or =\r) let decoded = text.replace(/=(?:\r\n|\n|\r)/g, ''); - // Replace quoted-printable encoded characters (including non-ASCII characters) - decoded = decoded.replace(/=([0-9A-F]{2})/gi, (match, p1) => { - return String.fromCharCode(parseInt(p1, 16)); - }); + // Replace quoted-printable encoded characters + decoded = decoded + // Handle common encoded characters + .replace(/=3D/g, '=') + .replace(/=20/g, ' ') + .replace(/=09/g, '\t') + .replace(/=0A/g, '\n') + .replace(/=0D/g, '\r') + // Handle other quoted-printable encoded characters + .replace(/=([0-9A-F]{2})/gi, (match, p1) => { + return String.fromCharCode(parseInt(p1, 16)); + }); // Handle character encoding try { - // For browsers with TextDecoder support if (typeof TextDecoder !== 'undefined') { - // Convert string to array of byte values const bytes = new Uint8Array(Array.from(decoded).map(c => c.charCodeAt(0))); return new TextDecoder(charset).decode(bytes); } - - // Fallback for older browsers or when charset handling is not critical return decoded; } catch (e) { console.warn('Charset conversion error:', e); @@ -61,14 +65,17 @@ export function convertCharset(text: string, charset: string): string { if (!text) return ''; try { - // For browsers with TextDecoder support if (typeof TextDecoder !== 'undefined') { - // Convert string to array of byte values + // Handle common charset aliases + const normalizedCharset = charset.toLowerCase() + .replace(/^iso-8859-1$/, 'windows-1252') + .replace(/^iso-8859-15$/, 'windows-1252') + .replace(/^utf-8$/, 'utf-8') + .replace(/^us-ascii$/, 'utf-8'); + const bytes = new Uint8Array(Array.from(text).map(c => c.charCodeAt(0))); - return new TextDecoder(charset).decode(bytes); + return new TextDecoder(normalizedCharset).decode(bytes); } - - // Fallback for older browsers return text; } catch (e) { console.warn('Charset conversion error:', e); @@ -158,6 +165,24 @@ export function cleanHtml(html: string): string { .replace(/</g, '<') .replace(/&/g, '&') .replace(/"/g, '"') + .replace(/é/g, 'é') + .replace(/è/g, 'è') + .replace(/ê/g, 'ê') + .replace(/ë/g, 'ë') + .replace(/à/g, 'à') + .replace(/â/g, 'â') + .replace(/ä/g, 'ä') + .replace(/î/g, 'î') + .replace(/ï/g, 'ï') + .replace(/ô/g, 'ô') + .replace(/ö/g, 'ö') + .replace(/û/g, 'û') + .replace(/ü/g, 'ü') + .replace(/ç/g, 'ç') + .replace(/Œ/g, 'Œ') + .replace(/œ/g, 'œ') + .replace(/Æ/g, 'Æ') + .replace(/æ/g, 'æ') // Clean up whitespace .replace(/^\s+$/gm, '') .replace(/\n{3,}/g, '\n\n')