[evolution] I#783 - e-editor.js: Remove body margin/padding styles and attributes



commit 30986bcd419fa70dd2e83f5c97d4516c5e090899
Author: Milan Crha <mcrha redhat com>
Date:   Wed May 6 19:09:04 2020 +0200

    I#783 - e-editor.js: Remove body margin/padding styles and attributes
    
    Closes https://gitlab.gnome.org/GNOME/evolution/-/issues/783

 data/webkit/e-editor.js                  | 159 ++++++++++++++++++++++++++++---
 src/e-util/test-html-editor-units-bugs.c | 102 ++++++++++++++++++++
 2 files changed, 249 insertions(+), 12 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index 65ef1039d9..372dac2d04 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -2201,6 +2201,25 @@ EvoEditor.removeQuoteMarks = function(element)
                element.normalize();
 }
 
+EvoEditor.cleanupForPlainText = function()
+{
+       if (!document.body)
+               return;
+
+       // remove all body attributes, to not influence the Plain Text mode
+       var ii;
+
+       for (ii = document.body.attributes.length - 1; ii >= 0; ii--) {
+               document.body.removeAttribute(document.body.attributes[ii].nodeName);
+       }
+
+       // style sheets
+       for (ii = document.styleSheets.length - 1; ii >= 0; ii--) {
+               if (document.styleSheets[ii].ownerNode)
+                       document.styleSheets[ii].ownerNode.remove();
+       }
+}
+
 EvoEditor.SetMode = function(mode)
 {
        if (EvoEditor.mode != mode) {
@@ -2229,15 +2248,7 @@ EvoEditor.SetMode = function(mode)
                        if (mode == EvoEditor.MODE_PLAIN_TEXT) {
                                EvoEditor.convertTags();
                                EvoEditor.convertParagraphs(document.body, 0, 
EvoEditor.NORMAL_PARAGRAPH_WIDTH, false);
-
-                               if (document.body) {
-                                       // remove all body attributes, to not influence the Plain Text mode
-                                       var ii;
-
-                                       for (ii = document.body.attributes.length - 1; ii >= 0; ii--) {
-                                               
document.body.removeAttribute(document.body.attributes[ii].nodeName);
-                                       }
-                               }
+                               EvoEditor.cleanupForPlainText();
                        } else {
                                EvoEditor.convertParagraphs(document.body, 0, -1, false);
                        }
@@ -5544,9 +5555,133 @@ EvoEditor.processLoadedContent = function()
                        }
                }
 
-               // remove all body attributes, to not influence the Plain Text mode
-               for (ii = document.body.attributes.length - 1; ii >= 0; ii--) {
-                       document.body.removeAttribute(document.body.attributes[ii].nodeName);
+               EvoEditor.cleanupForPlainText();
+       } else {
+               // drop margin/padding-related attributes and styles
+               var unsetMarginPadding = function(elem, style) {
+                       if (elem) {
+                               var ii;
+
+                               for (ii = elem.attributes.length - 1; ii >= 0; ii--) {
+                                       var name = elem.attributes[ii].nodeName;
+
+                                       if (!name)
+                                               continue;
+
+                                       name = name.toLowerCase();
+
+                                       if (name.indexOf("margin") >= 0 || name.indexOf("padding") >= 0)
+                                               elem.removeAttribute(name);
+                               }
+
+                               if (!style)
+                                       style = elem.style;
+                       }
+
+                       if (!style)
+                               return false;
+
+                       var changed = false;
+
+                       if (style.margin) {
+                               style.margin = null;
+                               changed = true;
+                       }
+                       if (style.marginLeft) {
+                               style.marginLeft = null;
+                               changed = true;
+                       }
+                       if (style.marginTop) {
+                               style.marginTop = null;
+                               changed = true;
+                       }
+                       if (style.marginRight) {
+                               style.marginRight = null;
+                               changed = true;
+                       }
+                       if (style.marginBottom) {
+                               style.marginBottom = null;
+                               changed = true;
+                       }
+
+                       if (style.padding) {
+                               style.padding = null;
+                               changed = true;
+                       }
+                       if (style.paddingLeft) {
+                               style.paddingLeft = null;
+                               changed = true;
+                       }
+                       if (style.paddingTop) {
+                               style.paddingTop = null;
+                               changed = true;
+                       }
+                       if (style.paddingRight) {
+                               style.paddingRight = null;
+                               changed = true;
+                       }
+                       if (style.paddingBottom) {
+                               style.paddingBottom = null;
+                               changed = true;
+                       }
+
+                       return changed;
+               };
+
+               unsetMarginPadding(document.documentElement);
+               unsetMarginPadding(document.body);
+
+               var ii;
+
+               for (ii = document.styleSheets.length - 1; ii >= 0; ii--) {
+                       var sheet = document.styleSheets[ii];
+
+                       if (!sheet.ownerNode)
+                               continue;
+
+                       var rules = sheet.cssRules;
+
+                       if (rules) {
+                               var jj, newCss = null;
+
+                               for (jj = 0; jj < rules.length; jj++) {
+                                       if (rules[jj].selectorText && 
rules[jj].selectorText.toLowerCase().indexOf("body") >= 0) {
+                                               if (unsetMarginPadding(null, rules[jj].style)) {
+                                                       if (newCss === null) {
+                                                               var kk;
+
+                                                               newCss = "";
+
+                                                               for (kk = 0; kk < jj; kk++) {
+                                                                       if (newCss)
+                                                                               newCss += "\n";
+
+                                                                       newCss += rules[kk].cssText;
+                                                               }
+                                                       }
+
+                                                       if (rules[jj].style.cssText) {
+                                                               if (newCss)
+                                                                       newCss += "\n";
+
+                                                               newCss += rules[jj].cssText;
+                                                       }
+                                               }
+                                       } else if (newCss !== null) {
+                                               if (newCss)
+                                                       newCss += "\n";
+
+                                               newCss += rules[jj].cssText;
+                                       }
+                               }
+
+                               if (newCss !== null) {
+                                       if (newCss)
+                                               sheet.ownerNode.innerHTML = newCss;
+                                       else
+                                               sheet.ownerNode.remove();
+                               }
+                       }
                }
        }
 
diff --git a/src/e-util/test-html-editor-units-bugs.c b/src/e-util/test-html-editor-units-bugs.c
index 6cf21d4d76..a67b8a478d 100644
--- a/src/e-util/test-html-editor-units-bugs.c
+++ b/src/e-util/test-html-editor-units-bugs.c
@@ -1686,6 +1686,107 @@ test_issue_884 (TestFixture *fixture)
        }
 }
 
+static void
+test_issue_783 (TestFixture *fixture)
+{
+       if (!test_utils_process_commands (fixture,
+               "mode:html\n")) {
+               g_test_fail ();
+               return;
+       }
+
+       test_utils_insert_content (fixture,
+               "<html><head></head><body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" 
marginheight=\"0\" style=\"margin:0;padding:0;background-color:#c8c8c8\">"
+               "<div>Mailpoet</div>"
+               "</body></html>",
+               E_CONTENT_EDITOR_INSERT_REPLACE_ALL | E_CONTENT_EDITOR_INSERT_TEXT_HTML);
+
+       if (!test_utils_run_simple_test (fixture,
+               "",
+               HTML_PREFIX
+               "<div>Mailpoet</div>"
+               HTML_SUFFIX,
+               "Mailpoet\n")) {
+               g_test_fail ();
+               return;
+       }
+
+       test_utils_insert_content (fixture,
+               "<html><head><style type=\"text/css\">"
+               "body {\n"
+               "    margin:0;\n"
+               "    font:12px/16px Arial, sans-serif;\n"
+               "}\n"
+               "</style></head><body style=\"margin: 0; font: 12px/ 16px Arial, sans-serif\">"
+               "<div>Amazon</div>"
+               "</body></html>",
+               E_CONTENT_EDITOR_INSERT_REPLACE_ALL | E_CONTENT_EDITOR_INSERT_TEXT_HTML);
+
+       /* WebKit "normalizes" the 'font' rule; the important part is that the margin is gone from the HTML */
+       if (!test_utils_run_simple_test (fixture,
+               "",
+               "<html><head><style type=\"text/css\">"
+               "body { font-style: normal; font-variant-caps: normal; font-weight: normal; font-stretch: 
normal; "
+               "font-size: 12px; line-height: 16px; font-family: Arial, sans-serif; }"
+               "</style></head><body style=\"font-style: normal; font-variant-caps: normal; font-weight: 
normal; "
+               "font-stretch: normal; font-size: 12px; line-height: 16px; font-family: Arial, sans-serif;\">"
+               "<div>Amazon</div>"
+               HTML_SUFFIX,
+               "Amazon\n")) {
+               g_test_fail ();
+               return;
+       }
+
+       test_utils_insert_content (fixture,
+               "<html><head><style text=\"text/css\">"
+               "body { width: 100% !important; -webkit-text-size-adjust: 100% !important; "
+               "-ms-text-size-adjust: 100% !important; -webkit-font-smoothing: antialiased "
+               "!important; margin: 0 !important; padding: 0 8px 100px 8px; font-family: "
+               "'Market Sans', Helvetica, Arial, sans-serif !important; background-color:#ffffff}"
+               "</style></head><body yahoo=\"fix\">"
+               "<div>eBay</div>"
+               "</body></html>",
+               E_CONTENT_EDITOR_INSERT_REPLACE_ALL | E_CONTENT_EDITOR_INSERT_TEXT_HTML);
+
+       if (!test_utils_run_simple_test (fixture,
+               "",
+               "<html><head><style text=\"text/css\">"
+               "body { background-color: rgb(255, 255, 255); width: 100% !important; -webkit-font-smoothing: 
antialiased !important;"
+               " font-family: \"Market Sans\", Helvetica, Arial, sans-serif !important; }"
+               "</style></head><body yahoo=\"fix\">"
+               "<div>eBay</div>"
+               HTML_SUFFIX,
+               "eBay\n")) {
+               g_test_fail ();
+               return;
+       }
+
+       test_utils_insert_content (fixture,
+               "<html><head><style text=\"text/css\">"
+               "table { color: blue; }\n"
+               "body { color: yellow; }\n"
+               "body { padding: 10px; }\n"
+               "div { color: orange; }"
+               "</style></head><body>"
+               "<div>Custom</div>"
+               "</body></html>",
+               E_CONTENT_EDITOR_INSERT_REPLACE_ALL | E_CONTENT_EDITOR_INSERT_TEXT_HTML);
+
+       if (!test_utils_run_simple_test (fixture,
+               "",
+               "<html><head><style text=\"text/css\">"
+               "table { color: blue; }\n"
+               "body { color: yellow; }\n"
+               "div { color: orange; }"
+               "</style></head><body yahoo=\"fix\">"
+               "<div>Custom</div>"
+               HTML_SUFFIX,
+               "Custom\n")) {
+               g_test_fail ();
+               return;
+       }
+}
+
 void
 test_add_html_editor_bug_tests (void)
 {
@@ -1719,4 +1820,5 @@ test_add_html_editor_bug_tests (void)
        test_utils_add_test ("/issue/104", test_issue_104);
        test_utils_add_test ("/issue/107", test_issue_107);
        test_utils_add_test ("/issue/884", test_issue_884);
+       test_utils_add_test ("/issue/783", test_issue_783);
 }


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