mail page rest
This commit is contained in:
parent
02668624c0
commit
273442d42c
@ -6,21 +6,25 @@ export function decodeQuotedPrintable(text: string, charset: string): string {
|
|||||||
// Replace soft line breaks (=\r\n or =\n or =\r)
|
// Replace soft line breaks (=\r\n or =\n or =\r)
|
||||||
let decoded = text.replace(/=(?:\r\n|\n|\r)/g, '');
|
let decoded = text.replace(/=(?:\r\n|\n|\r)/g, '');
|
||||||
|
|
||||||
// Replace quoted-printable encoded characters (including non-ASCII characters)
|
// Replace quoted-printable encoded characters
|
||||||
decoded = decoded.replace(/=([0-9A-F]{2})/gi, (match, p1) => {
|
decoded = decoded
|
||||||
return String.fromCharCode(parseInt(p1, 16));
|
// 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
|
// Handle character encoding
|
||||||
try {
|
try {
|
||||||
// For browsers with TextDecoder support
|
|
||||||
if (typeof TextDecoder !== 'undefined') {
|
if (typeof TextDecoder !== 'undefined') {
|
||||||
// Convert string to array of byte values
|
|
||||||
const bytes = new Uint8Array(Array.from(decoded).map(c => c.charCodeAt(0)));
|
const bytes = new Uint8Array(Array.from(decoded).map(c => c.charCodeAt(0)));
|
||||||
return new TextDecoder(charset).decode(bytes);
|
return new TextDecoder(charset).decode(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback for older browsers or when charset handling is not critical
|
|
||||||
return decoded;
|
return decoded;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Charset conversion error:', e);
|
console.warn('Charset conversion error:', e);
|
||||||
@ -61,14 +65,17 @@ export function convertCharset(text: string, charset: string): string {
|
|||||||
if (!text) return '';
|
if (!text) return '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// For browsers with TextDecoder support
|
|
||||||
if (typeof TextDecoder !== 'undefined') {
|
if (typeof TextDecoder !== 'undefined') {
|
||||||
// Convert string to array of byte values
|
// Handle common charset aliases
|
||||||
const bytes = new Uint8Array(Array.from(text).map(c => c.charCodeAt(0)));
|
const normalizedCharset = charset.toLowerCase()
|
||||||
return new TextDecoder(charset).decode(bytes);
|
.replace(/^iso-8859-1$/, 'windows-1252')
|
||||||
}
|
.replace(/^iso-8859-15$/, 'windows-1252')
|
||||||
|
.replace(/^utf-8$/, 'utf-8')
|
||||||
|
.replace(/^us-ascii$/, 'utf-8');
|
||||||
|
|
||||||
// Fallback for older browsers
|
const bytes = new Uint8Array(Array.from(text).map(c => c.charCodeAt(0)));
|
||||||
|
return new TextDecoder(normalizedCharset).decode(bytes);
|
||||||
|
}
|
||||||
return text;
|
return text;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Charset conversion error:', 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, 'Œ')
|
||||||
|
.replace(/œ/g, 'œ')
|
||||||
|
.replace(/Æ/g, 'Æ')
|
||||||
|
.replace(/æ/g, 'æ')
|
||||||
// Clean up whitespace
|
// Clean up whitespace
|
||||||
.replace(/^\s+$/gm, '')
|
.replace(/^\s+$/gm, '')
|
||||||
.replace(/\n{3,}/g, '\n\n')
|
.replace(/\n{3,}/g, '\n\n')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user