[evolution] e-editor.js: Remove insignificant new lines when pre-processing loaded HTML



commit 9ebe232b79f6fffd8af355661a5dd64baa118596
Author: Milan Crha <mcrha redhat com>
Date:   Tue Apr 28 10:45:01 2020 +0200

    e-editor.js: Remove insignificant new lines when pre-processing loaded HTML
    
    Insignificant new lines in the text are those in regular <div> and such,
    where the white-space style doesn't mean to keep them. Those could cause
    trouble when switching from HTML to the Plain Text mode, like to have doubled
    new lines. WebKit doesn't remove them on its own, unfortunately, thus this
    special processing is needed.

 data/webkit/e-convert.js                 | 15 ++++++++++--
 data/webkit/e-editor.js                  | 41 ++++++++++++++++++++++++++------
 src/e-util/test-html-editor-units-bugs.c | 18 +++++++-------
 src/e-util/test-html-editor-units.c      | 20 +++++++---------
 4 files changed, 65 insertions(+), 29 deletions(-)
---
diff --git a/data/webkit/e-convert.js b/data/webkit/e-convert.js
index 7f7056e45a..ef5404ac1a 100644
--- a/data/webkit/e-convert.js
+++ b/data/webkit/e-convert.js
@@ -729,11 +729,11 @@ EvoConvert.mergeConsecutiveSpaces = function(str)
        return str;
 }
 
-EvoConvert.processNode = function(node, normalDivWidth, quoteLevel)
+EvoConvert.RemoveInsignificantNewLines = function(node)
 {
        var str = "";
 
-       if (node.nodeType == node.TEXT_NODE) {
+       if (node && node.nodeType == node.TEXT_NODE) {
                str = node.nodeValue;
 
                if (str.indexOf("\r") >= 0 ||
@@ -775,6 +775,17 @@ EvoConvert.processNode = function(node, normalDivWidth, quoteLevel)
                                }
                        }
                }
+       }
+
+       return str;
+}
+
+EvoConvert.processNode = function(node, normalDivWidth, quoteLevel)
+{
+       var str = "";
+
+       if (node.nodeType == node.TEXT_NODE) {
+               str = EvoConvert.RemoveInsignificantNewLines(node);
        } else if (node.nodeType == node.ELEMENT_NODE) {
                if (node.hidden ||
                    node.tagName == "STYLE" ||
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index 9054f8f1c5..924b1e6914 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -4979,13 +4979,9 @@ EvoEditor.InsertContent = function(text, isHTML, quote)
                                if (node.nodeType == node.ELEMENT_NODE && node.tagName == "P") {
                                        removeNode = true;
 
-                                       if (node.tagName == "P") {
-                                               var div = document.createElement("DIV");
-                                               EvoEditor.moveNodeContent(node, div);
-                                               node.parentElement.insertBefore(div, node.nextSibling);
-                                       } else {
-                                               EvoEditor.moveNodeContent(node);
-                                       }
+                                       var div = document.createElement("DIV");
+                                       EvoEditor.moveNodeContent(node, div);
+                                       node.parentElement.insertBefore(div, node.nextSibling);
                                }
 
                                next = EvoEditor.getNextNodeInHierarchy(node, content);
@@ -5331,6 +5327,26 @@ EvoEditor.splitPreTexts = function(node, isInPre, newNodes)
        }
 }
 
+EvoEditor.traverseToRemoveInsignificantNewLines = function(parent)
+{
+       if (!parent)
+               return;
+
+       var child;
+
+       for (child = parent.firstChild; child; child = child.nextSibling) {
+               if (child.nodeType == child.TEXT_NODE) {
+                       var str = EvoConvert.RemoveInsignificantNewLines(child);
+
+                       if (str != child.nodeValue) {
+                               child.nodeValue = str;
+                       }
+               } else if (child.firstChild) {
+                       EvoEditor.traverseToRemoveInsignificantNewLines(child);
+               }
+       }
+}
+
 EvoEditor.processLoadedContent = function()
 {
        if (!document.body)
@@ -5360,6 +5376,17 @@ EvoEditor.processLoadedContent = function()
                }
        }
 
+       // This is to have prepared the text nodes for plain text. The plain text mode
+       // sets white-space for div-s to 'pre-wrap', which means the new lines are
+       // significant, but before the conversion they are insignificant, because
+       // the loaded content is regular HTML, not Plain Text-like HTML.
+       EvoEditor.UpdateStyleSheet("processLoadedContent", "body div { white-space: normal; }");
+       try {
+               EvoEditor.traverseToRemoveInsignificantNewLines(document.body);
+       } finally {
+               EvoEditor.UpdateStyleSheet("processLoadedContent", null);
+       }
+
        node = document.querySelector("SPAN.-x-evo-cite-body");
 
        didCite = node;
diff --git a/src/e-util/test-html-editor-units-bugs.c b/src/e-util/test-html-editor-units-bugs.c
index 7bebfb9276..e35d2d8a3e 100644
--- a/src/e-util/test-html-editor-units-bugs.c
+++ b/src/e-util/test-html-editor-units-bugs.c
@@ -50,15 +50,15 @@ test_bug_750657 (TestFixture *fixture)
        if (!test_utils_run_simple_test (fixture,
                "seq:CecuuuSuusD\n",
                HTML_PREFIX
-               "<blockquote type=\"cite\" " BLOCKQUOTE_STYLE ">\n"
-               "<div>This is the first paragraph of a quoted text which has some long text to test. It has 
the second sentence as well.</div>\n"
-               "<div><br></div>\n"
-               "<div>This is the third paragraph of a quoted text which has some long text to test. It has 
the second sentence as well.</div>\n"
-               "<blockquote type=\"cite\" " BLOCKQUOTE_STYLE ">\n"
-               "<div><br></div>\n"
-               "</blockquote>\n"
-               "<div>This is the fourth paragraph of a quoted text which has some long text to test. It has 
the second sentence as well.</div>\n"
-               "</blockquote>\n"
+               "<blockquote type=\"cite\" " BLOCKQUOTE_STYLE ">"
+               "<div>This is the first paragraph of a quoted text which has some long text to test. It has 
the second sentence as well.</div>"
+               "<div><br></div>"
+               "<div>This is the third paragraph of a quoted text which has some long text to test. It has 
the second sentence as well.</div>"
+               "<blockquote type=\"cite\" " BLOCKQUOTE_STYLE ">"
+               "<div><br></div>"
+               "</blockquote>"
+               "<div>This is the fourth paragraph of a quoted text which has some long text to test. It has 
the second sentence as well.</div>"
+               "</blockquote>"
                "<div><br></div>"
                HTML_SUFFIX,
                NULL)) {
diff --git a/src/e-util/test-html-editor-units.c b/src/e-util/test-html-editor-units.c
index ddbf0470e9..dfd1b047b1 100644
--- a/src/e-util/test-html-editor-units.c
+++ b/src/e-util/test-html-editor-units.c
@@ -4927,7 +4927,7 @@ test_cite_reply_link (TestFixture *fixture)
 
        test_utils_insert_content (fixture,
                "<html><head></head><body><div><span>123 (here <a href=\"https://www.example.com\";>\n"
-               "https://www.example.com/1234567890/1234567890/1234567890/1234567890/1234567890/";
+               "https://www.example.com/1234567890/1234567890/1234567890/1234567890/1234567890/</a>"
                ") and </span>here ěščřžýáíé <a href=\"https://www.example.com\";>www.example.com</a>"
                " with closing text after.</div>"
                "<div>www.example1.com</div>"
@@ -4942,8 +4942,8 @@ test_cite_reply_link (TestFixture *fixture)
                "",
                HTML_PREFIX "<div style=\"width: 71ch;\">On Today, User wrote:</div>"
                "<blockquote type=\"cite\">"
-               "<div>" QUOTE_SPAN (QUOTE_CHR) "123 (here </div>"
-               "<div>" QUOTE_SPAN (QUOTE_CHR) "<a 
href=\"https://www.example.com/1234567890/1234567890/1234567890/1234567890/1234567890/\";>"
+               "<div>" QUOTE_SPAN (QUOTE_CHR) "123 (here<br class=\"-x-evo-wrap-br\">"
+               QUOTE_SPAN (QUOTE_CHR) "<a 
href=\"https://www.example.com/1234567890/1234567890/1234567890/1234567890/1234567890/\";>"
                        
"https://www.example.com/1234567890/1234567890/1234567890/1234567890/1234567890/</a>)<br 
class=\"-x-evo-wrap-br\">"
                QUOTE_SPAN (QUOTE_CHR) "and here ěščřžýáíé <a 
href=\"https://www.example.com\";>www.example.com</a> with closing text after.</div>"
                "<div>" QUOTE_SPAN (QUOTE_CHR) "<a 
href=\"https://www.example1.com\";>www.example1.com</a></div>"
@@ -4952,7 +4952,7 @@ test_cite_reply_link (TestFixture *fixture)
                "<div>" QUOTE_SPAN (QUOTE_CHR) "😏😉🙂 <a href=\"mailto:user@no.where\";>user@no.where</a> line 
with Emoji</div>"
                "</blockquote>" HTML_SUFFIX,
                "On Today, User wrote:\n"
-               "> 123 (here \n"
+               "> 123 (here\n"
                "> https://www.example.com/1234567890/1234567890/1234567890/1234567890/1234567890/\n";
                "> ) and here ěščřžýáíé www.example.com with closing text after.\n"
                "> www.example1.com\n"
@@ -6275,7 +6275,7 @@ test_pre_split_complex_html (TestFixture *fixture)
                        "<pre>level 1</pre>"
                        "<pre>E-mail: &lt;<a href=\"mailto:user@no.where\";>user@no.where</a>&gt; line</pre>"
                        "<pre>Phone: 1234567890</pre>"
-                       "<pre><div>div in\npre</div></pre>"
+                       "<pre><div>div in pre</div></pre>"
                        "<blockquote type=\"cite\" " BLOCKQUOTE_STYLE ">"
                                "<pre>level 2</pre>"
                                "<pre><br></pre>"
@@ -6301,8 +6301,8 @@ test_pre_split_complex_html (TestFixture *fixture)
                "> level 1\n"
                "> E-mail: <user@no.where> line\n"
                "> Phone: 1234567890\n"
-               "> > div in\n" /* this and th ebelow line is level 1 in quotation, but due to nested <div> in 
<pre> the EvoConvert */
-               "> > pre\n"    /* doubles quotation marks. It's not ideal, but it's a broken HTML anyway 
(broken for the HTML editor). */
+               "> > div in pre\n" /* this line is level 1 in quotation, but due to nested <div> in <pre> the 
EvoConvert */
+                                  /* doubles quotation marks. It's not ideal, but it's a broken HTML anyway 
(broken for the HTML editor). */
                "> > level 2\n"
                "> > \n"
                "> > level 2\n"
@@ -6367,8 +6367,7 @@ test_pre_split_complex_plain (TestFixture *fixture)
                        "<pre>" QUOTE_SPAN (QUOTE_CHR) "level 1</pre>"
                        "<pre>" QUOTE_SPAN (QUOTE_CHR) "E-mail: &lt;<a 
href=\"mailto:user@no.where\";>user@no.where</a>&gt; line</pre>"
                        "<pre>" QUOTE_SPAN (QUOTE_CHR) "Phone: 1234567890</pre>"
-                       "<pre>" QUOTE_SPAN (QUOTE_CHR) "div in</pre>"
-                       "<pre>" QUOTE_SPAN (QUOTE_CHR) "pre</pre>"
+                       "<pre>" QUOTE_SPAN (QUOTE_CHR) "div in pre</pre>"
                        "<blockquote type=\"cite\">"
                                "<pre>" QUOTE_SPAN (QUOTE_CHR QUOTE_CHR) "level 2</pre>"
                                "<pre>" QUOTE_SPAN (QUOTE_CHR QUOTE_CHR) "<br></pre>"
@@ -6394,8 +6393,7 @@ test_pre_split_complex_plain (TestFixture *fixture)
                "> level 1\n"
                "> E-mail: <user@no.where> line\n"
                "> Phone: 1234567890\n"
-               "> div in\n"
-               "> pre\n"
+               "> div in pre\n"
                "> > level 2\n"
                "> > \n"
                "> > level 2\n"


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