Extract <body> and <style>'s from messages while sanitizing, remove comments

master
Vitaliy Filippov 2016-10-03 01:33:47 +03:00
parent e7bb14af6e
commit 0fa639ebc7
1 changed files with 13 additions and 1 deletions

View File

@ -139,7 +139,19 @@ function* getBody(pg, messages, boxId)
p.on('end', gen.cb());
p.write(msg[0].headers);
let [ obj ] = yield p.end();
obj.html = htmlawed.sanitize(obj.html||'', { safe: 1, elements: '* +style' });
let styles = '';
obj.html = (obj.html||'').replace(/<style[^<>]*>([\s\S]*?)<\/style\s*>/ig, function(m, m1)
{
styles += m1+'\n';
return '';
});
obj.html = obj.html.replace(/^[\s\S]*?<body[^<>]*>([\s\S]*)<\/body>[\s\S]*$/i, '$1');
obj.html = obj.html.replace(/^[\s\S]*?<html[^<>]*>([\s\S]*)<\/html>[\s\S]*$/i, '$1');
if (styles)
{
obj.html = '<style>\n'+styles+'</style>\n'+obj.html;
}
obj.html = htmlawed.sanitize(obj.html||'', { safe: 1, elements: '* +style', keep_bad: 2, comment: 1 });
let upd = { body_text: obj.text||'', body_html: obj.html };
upd.body_html_text = obj.html.replace(/<style[^>]*>.*<\/style\s*>|<\/?[^>]*>/g, '');
yield pg.update('messages m', upd).where({ folder_id: boxId, uid: msg[0].uid }).run(gen.ef());