[evolution] I#695 - Option to unset colors provided in HTML mails for preview



commit 4dad3e4c5b7c65a6cf70309ebe818995828abe5d
Author: Milan Crha <mcrha redhat com>
Date:   Wed Feb 12 15:30:17 2020 +0100

    I#695 - Option to unset colors provided in HTML mails for preview
    
    Closes https://gitlab.gnome.org/GNOME/evolution/issues/695

 data/org.gnome.evolution.mail.gschema.xml.in |  5 ++
 data/webkit/e-web-view.js                    | 81 ++++++++++++++++++++++++++++
 src/em-format/e-mail-formatter-text-html.c   |  8 ++-
 3 files changed, 92 insertions(+), 2 deletions(-)
---
diff --git a/data/org.gnome.evolution.mail.gschema.xml.in b/data/org.gnome.evolution.mail.gschema.xml.in
index 410e040a04..0979a6f77b 100644
--- a/data/org.gnome.evolution.mail.gschema.xml.in
+++ b/data/org.gnome.evolution.mail.gschema.xml.in
@@ -832,6 +832,11 @@
       <_summary>Whether display delivery notification parts inline.</_summary>
       <_description>If enabled, the message/delivery-status and message/disposition-notification parts are 
shown automatically inline.</_description>
     </key>
+    <key name="preview-unset-html-colors" type="b">
+      <default>false</default>
+      <_summary>Whether unset colors provided in HTML mails.</_summary>
+      <_description>If enabled, unset colors in HTML messages, forcing use of desktop theme colors 
instead.</_description>
+    </key>
 
     <!-- The following keys are deprecated. -->
 
diff --git a/data/webkit/e-web-view.js b/data/webkit/e-web-view.js
index 677f5f6705..5847b7ec39 100644
--- a/data/webkit/e-web-view.js
+++ b/data/webkit/e-web-view.js
@@ -916,6 +916,83 @@ Evo.mailDisplayVCardModeButtonClicked = function(elem)
        }
 }
 
+Evo.unsetHTMLColors = function(doc)
+{
+       var ii, isz = doc.styleSheets.length;
+
+       // to change only iframe-s, which are marked as such
+       if (!doc.defaultView.frameElement ||
+           !doc.defaultView.frameElement.hasAttribute("x-e-unset-colors")) {
+               return;
+       }
+
+       for (ii = 0; ii < isz; ii++) {
+               var sheet = doc.styleSheets[ii];
+
+               if (!sheet.cssRules ||
+                   sheet.id == "-e-web-view-style-sheet" ||
+                   sheet.id == "-e-mail-formatter-style-sheet") {
+                       continue;
+               }
+
+               var jj, jsz = sheet.cssRules.length;
+
+               for (jj = 0; jj < jsz; jj++) {
+                       var rule = sheet.cssRules[jj];
+
+                       if (!rule.style || !rule.selectorText || 
rule.selectorText.startsWith(".-e-web-view-") || rule.selectorText.startsWith(".-e-mail-formatter-"))
+                               continue;
+
+                       if (rule.style.color)
+                               rule.style.color = "inherit";
+
+                       if (rule.style.backgroundColor)
+                               rule.style.backgroundColor = "inherit";
+               }
+       }
+
+       var elems = doc.querySelectorAll("[style],[color],[bgcolor]");
+
+       isz = elems.length;
+
+       for (ii = 0; ii < isz; ii++) {
+               var elem = elems[ii];
+
+               if (elem.tagName != "HTML" && elem.tagName != "IFRAME" && elem.tagName != "INPUT" && 
elem.tagName != "BUTTON" && elem.tagName != "IMG") {
+                       if (elem.style) {
+                               if (elem.style.color)
+                                       elem.style.color = "inherit";
+
+                               if (elem.style.backgroundColor)
+                                       elem.style.backgroundColor = "inherit";
+                       }
+
+                       elem.removeAttribute("color");
+                       elem.removeAttribute("bgcolor");
+               }
+       }
+
+       elems = doc.querySelectorAll("body");
+
+       isz = elems.length;
+
+       for (ii = 0; ii < isz; ii++) {
+               var elem = elems[ii];
+
+               elem.removeAttribute("bgcolor");
+               elem.removeAttribute("text");
+               elem.removeAttribute("link");
+               elem.removeAttribute("alink");
+               elem.removeAttribute("vlink");
+
+               if (!elem.classList.contains("-e-web-view-text-color"))
+                       elem.classList.add("-e-web-view-text-color");
+
+               if (!elem.classList.contains("-e-web-view-background-color"))
+                       elem.classList.add("-e-web-view-background-color");
+       }
+}
+
 Evo.MailDisplayBindDOM = function(iframe_id)
 {
        var traversar = {
@@ -1020,12 +1097,16 @@ Evo.MailDisplayBindDOM = function(iframe_id)
                                        "-e-mail-formatter-style-sheet",
                                        "a.evo-awrap",
                                        "white-space: normal; word-break: break-all;");
+                               Evo.unsetHTMLColors(doc);
                        }
 
                        return true;
                }
        };
 
+       if (!iframe_id)
+               iframe_id = "*";
+
        Evo.runTraversarForIFrameId(iframe_id, traversar);
 
        Evo.mailDisplayResizeContentToPreviewWidth();
diff --git a/src/em-format/e-mail-formatter-text-html.c b/src/em-format/e-mail-formatter-text-html.c
index b41b51b620..708c29a5e4 100644
--- a/src/em-format/e-mail-formatter-text-html.c
+++ b/src/em-format/e-mail-formatter-text-html.c
@@ -327,10 +327,12 @@ emfe_text_html_format (EMailFormatterExtension *extension,
                g_string_free (string, TRUE);
        } else {
                CamelFolder *folder;
+               GSettings *settings;
                const gchar *message_uid;
                const gchar *default_charset, *charset;
                gchar *uri, *str;
 
+               settings = e_util_ref_settings ("org.gnome.evolution.mail");
                folder = e_mail_part_list_get_folder (context->part_list);
                message_uid = e_mail_part_list_get_message_uid (context->part_list);
                default_charset = e_mail_formatter_get_default_charset (formatter);
@@ -360,18 +362,20 @@ emfe_text_html_format (EMailFormatterExtension *extension,
                        " frameborder=\"0\" src=\"%s\" "
                        " id=\"%s.iframe\" name=\"%s\" "
                        " class=\"-e-mail-formatter-frame-color %s\" "
-                       " style=\"background-color: #ffffff; \">"
+                       " %s>"
                        "</iframe>"
                        "</div>",
                        uri,
                        e_mail_part_get_id (part),
                        e_mail_part_get_id (part),
-                       e_mail_part_get_frame_security_style (part));
+                       e_mail_part_get_frame_security_style (part),
+                       g_settings_get_boolean (settings, "preview-unset-html-colors") ? 
"x-e-unset-colors=\"1\"" : "style=\"background-color: #ffffff;\"");
 
                g_output_stream_write_all (
                        stream, str, strlen (str),
                        NULL, cancellable, NULL);
 
+               g_clear_object (&settings);
                g_free (str);
                g_free (uri);
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]