Speedup cleanHtml() parser

This commit is contained in:
the-djmaze 2023-02-07 08:56:51 +01:00
parent a1441c9f22
commit 7aa5142047

View file

@ -103,6 +103,7 @@ export const
* @returns {string} * @returns {string}
*/ */
cleanHtml = (html, oAttachments) => { cleanHtml = (html, oAttachments) => {
let aColor;
const const
debug = false, // Config()->Get('debug', 'enable', false); debug = false, // Config()->Get('debug', 'enable', false);
detectHiddenImages = true, // !!SettingsGet('try_to_detect_hidden_images'), detectHiddenImages = true, // !!SettingsGet('try_to_detect_hidden_images'),
@ -121,11 +122,7 @@ export const
// convert body attributes to CSS // convert body attributes to CSS
tasks = { tasks = {
link: value => { link: value => aColor = value,
if (/^#[a-fA-Z0-9]{3,6}$/.test(value)) {
tpl.content.querySelectorAll('a').forEach(node => node.style.color || (node.style.color = value))
}
},
text: (value, node) => node.style.color = value, text: (value, node) => node.style.color = value,
topmargin: (value, node) => node.style.marginTop = pInt(value) + 'px', topmargin: (value, node) => node.style.marginTop = pInt(value) + 'px',
leftmargin: (value, node) => node.style.marginLeft = pInt(value) + 'px', leftmargin: (value, node) => node.style.marginLeft = pInt(value) + 'px',
@ -162,9 +159,11 @@ export const
'colspan', 'rowspan', 'headers' 'colspan', 'rowspan', 'headers'
], ],
disallowedTags = [ disallowedTags = [
'HEAD','STYLE','SVG','SCRIPT','TITLE','LINK','BASE','META', 'STYLE','SVG','SCRIPT','TITLE','LINK','BASE','META',
'INPUT','OUTPUT','SELECT','BUTTON','TEXTAREA', 'INPUT','OUTPUT','SELECT','BUTTON','TEXTAREA',
'BGSOUND','KEYGEN','SOURCE','OBJECT','EMBED','APPLET','IFRAME','FRAME','FRAMESET','VIDEO','AUDIO','AREA','MAP' 'BGSOUND','KEYGEN','SOURCE','OBJECT','EMBED','APPLET','IFRAME','FRAME','FRAMESET','VIDEO','AUDIO','AREA','MAP'
// Not supported by <template> element
// ,'HTML','HEAD','BODY'
], ],
nonEmptyTags = [ nonEmptyTags = [
'A','B','EM','I','SPAN','STRONG' 'A','B','EM','I','SPAN','STRONG'
@ -172,11 +171,11 @@ export const
tpl.innerHTML = html tpl.innerHTML = html
// .replace(/<pre[^>]*>[\s\S]*?<\/pre>/gi, pre => pre.replace(/\n/g, '\n<br>')) // .replace(/<pre[^>]*>[\s\S]*?<\/pre>/gi, pre => pre.replace(/\n/g, '\n<br>'))
.replace(/<!doctype[^>]*>/gi, '')
.replace(/<\?xml[^>]*\?>/gi, '')
// Not supported by <template> element // Not supported by <template> element
// .replace(/<!doctype[^>]*>/gi, '')
// .replace(/<\?xml[^>]*\?>/gi, '')
.replace(/<(\/?)body(\s[^>]*)?>/gi, '<$1div class="mail-body"$2>') .replace(/<(\/?)body(\s[^>]*)?>/gi, '<$1div class="mail-body"$2>')
.replace(/<\/?(html|head)[^>]*>/gi, '') // .replace(/<\/?(html|head)[^>]*>/gi, '')
// Fix Reddit https://github.com/the-djmaze/snappymail/issues/540 // Fix Reddit https://github.com/the-djmaze/snappymail/issues/540
.replace(/<span class="preview-text"[\s\S]+?<\/span>/, '') .replace(/<span class="preview-text"[\s\S]+?<\/span>/, '')
// https://github.com/the-djmaze/snappymail/issues/900 // https://github.com/the-djmaze/snappymail/issues/900
@ -287,6 +286,7 @@ export const
// setAttribute('rel', 'external nofollow noopener noreferrer'); // setAttribute('rel', 'external nofollow noopener noreferrer');
} }
setAttribute('tabindex', '-1'); setAttribute('tabindex', '-1');
aColor && !oElement.style.color && (oElement.style.color = aColor);
} }
// if (['CENTER','FORM'].includes(name)) { // if (['CENTER','FORM'].includes(name)) {