[evolution/wip/mcrha/webkit-jsc-api: 274/292] Correct and add tests for EvoConvert.ToPlainText() with quoted text



commit 8c1943178ad88d8386e517cd15f436c52f83a507
Author: Milan Crha <mcrha redhat com>
Date:   Thu Jan 30 16:29:00 2020 +0100

    Correct and add tests for EvoConvert.ToPlainText() with quoted text

 data/webkit/e-convert.js       |  45 ++++++++++---
 src/e-util/test-web-view-jsc.c | 150 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 185 insertions(+), 10 deletions(-)
---
diff --git a/data/webkit/e-convert.js b/data/webkit/e-convert.js
index 5bb949ba5d..7ce8a897df 100644
--- a/data/webkit/e-convert.js
+++ b/data/webkit/e-convert.js
@@ -336,7 +336,19 @@ EvoConvert.calcLineLetters = function(line)
        return len;
 }
 
-EvoConvert.formatParagraph = function(str, ltr, align, indent, whiteSpace, wrapWidth, extraIndent, liText)
+EvoConvert.getQuotePrefix = function(quoteLevel, ltr)
+{
+       var prefix = "";
+
+       if (quoteLevel > 0) {
+               prefix = ltr ? "> " : " <";
+               prefix = prefix.repeat(quoteLevel);
+       }
+
+       return prefix;
+}
+
+EvoConvert.formatParagraph = function(str, ltr, align, indent, whiteSpace, wrapWidth, extraIndent, liText, 
quoteLevel)
 {
        if (!str || str == "")
                return liText ? liText : str;
@@ -488,7 +500,10 @@ EvoConvert.formatParagraph = function(str, ltr, align, indent, whiteSpace, wrapW
                        worker.commit(ii);
                }
        } else {
-               lines[lines.length] = str.endsWith("\n") ? str.substr(0, str.length - 1) : str;
+               if (str.endsWith("\n"))
+                       str = str.substr(0, str.length - 1);
+
+               lines = str.split("\n");
        }
 
        var extraIndentStr = extraIndent > 0 ? " ".repeat(extraIndent) : null;
@@ -578,6 +593,13 @@ EvoConvert.formatParagraph = function(str, ltr, align, indent, whiteSpace, wrapW
                                line = line + indent;
                }
 
+               if (quoteLevel > 0) {
+                       if (ltr)
+                               line = EvoConvert.getQuotePrefix(quoteLevel, ltr) + line;
+                       else
+                               line = line + EvoConvert.getQuotePrefix(quoteLevel, ltr);
+               }
+
                str += line + "\n";
        }
 
@@ -603,7 +625,7 @@ EvoConvert.ImgToText = function(img)
        return txt;
 }
 
-EvoConvert.extractElemText = function(elem, normalDivWidth)
+EvoConvert.extractElemText = function(elem, normalDivWidth, quoteLevel)
 {
        if (!elem)
                return "";
@@ -619,13 +641,13 @@ EvoConvert.extractElemText = function(elem, normalDivWidth)
                if (!node)
                        continue;
 
-               str += EvoConvert.processNode(node, normalDivWidth);
+               str += EvoConvert.processNode(node, normalDivWidth, quoteLevel);
        }
 
        return str;
 }
 
-EvoConvert.processNode = function(node, normalDivWidth)
+EvoConvert.processNode = function(node, normalDivWidth, quoteLevel)
 {
        var str = "";
 
@@ -707,17 +729,20 @@ EvoConvert.processNode = function(node, normalDivWidth)
                                width = normalDivWidth;
                        }
 
-                       str = EvoConvert.formatParagraph(EvoConvert.extractElemText(node, normalDivWidth), 
ltr, align, indent, whiteSpace, width, extraIndent, liText);
+                       str = EvoConvert.formatParagraph(EvoConvert.extractElemText(node, normalDivWidth, 
quoteLevel), ltr, align, indent, whiteSpace, width, extraIndent, liText, quoteLevel);
                } else if (node.tagName == "PRE") {
-                       str = EvoConvert.formatParagraph(EvoConvert.extractElemText(node, normalDivWidth), 
ltr, align, indent, "pre", -1, 0, "");
+                       str = EvoConvert.formatParagraph(EvoConvert.extractElemText(node, normalDivWidth, 
quoteLevel), ltr, align, indent, "pre", -1, 0, "", quoteLevel);
                } else if (node.tagName == "BR") {
                        str = "\n";
                } else if (node.tagName == "IMG") {
                        str = EvoConvert.ImgToText(node);
                } else {
-                       str = EvoConvert.extractElemText(node, normalDivWidth);
+                       var isBlockquote = node.tagName == "BLOCKQUOTE";
+
+                       str = EvoConvert.extractElemText(node, normalDivWidth, quoteLevel + (isBlockquote ? 1 
: 0));
 
-                       if (str != "\n" && ((style && style.display == "block") || node.tagName == 
"ADDRESS")) {
+                       if ((!isBlockquote || !str.endsWith("\n")) &&
+                           str != "\n" && ((style && style.display == "block") || node.tagName == 
"ADDRESS")) {
                                str += "\n";
                        }
                }
@@ -772,7 +797,7 @@ EvoConvert.ToPlainText = function(element, normalDivWidth)
                if (!node)
                        continue;
 
-               str += EvoConvert.processNode(node, normalDivWidth);
+               str += EvoConvert.processNode(node, normalDivWidth, 0);
        }
 
        return str;
diff --git a/src/e-util/test-web-view-jsc.c b/src/e-util/test-web-view-jsc.c
index cabed21ee5..dd9f25c230 100644
--- a/src/e-util/test-web-view-jsc.c
+++ b/src/e-util/test-web-view-jsc.c
@@ -2543,6 +2543,155 @@ test_convert_to_plain (TestFixture *fixture)
        }
 }
 
+static void
+test_convert_to_plain_quoted (TestFixture *fixture)
+{
+       #define HTML(_body) ("<html><head><style><!-- span.Apple-tab-span { white-space:pre; } 
--></style></head><body style='font-family:monospace;'>" _body "</body></html>")
+
+       struct _tests {
+               const gchar *html;
+               const gchar *plain;
+               gint normal_div_width;
+       } tests[] = {
+       /* 0 */ { HTML ("<div style='width:10ch;'>123 456 789 123</div>"
+               "<blockquote type='cite'>"
+                       "<div style='width:8ch;'>123 456 789 1 2 3 4</div>"
+                       "<div style='width:8ch;'>abc def ghi j k l m</div>"
+               "</blockquote>"
+               "<div>end</div>"),
+               "123 456\n"
+               "789 123\n"
+               "> 123 456\n"
+               "> 789 1 2\n"
+               "> 3 4\n"
+               "> abc def\n"
+               "> ghi j k\n"
+               "> l m\n"
+               "end\n",
+               10 },
+       /* 1 */ { HTML ("<div style='width:12ch;'>123 456</div>"
+               "<blockquote type='cite'>"
+                       "<div style='width:10ch;'>123 456</div>"
+                       "<blockquote>"
+                               "<div style='width:8ch;'>789 1 2 3 4</div>"
+                       "</blockquote>"
+                       "<div style='width:10ch;'>mid</div>"
+                       "<blockquote>"
+                               "<blockquote>"
+                                       "<div style='width:6ch;'>abc</div>"
+                                       "<div style='width:6ch;'>def ghi j k l m</div>"
+                               "</blockquote>"
+                               "<div style='width:8ch;'>abc d e f g h i j</div>"
+                       "</blockquote>"
+                       "<div style='width:10ch;'>l1 a b c d e f g</div>"
+               "</blockquote>"
+               "<div>end</div>"),
+               "123 456\n"
+               "> 123 456\n"
+               "> > 789 1 2\n"
+               "> > 3 4\n"
+               "> mid\n"
+               "> > > abc\n"
+               "> > > def\n"
+               "> > > ghi j\n"
+               "> > > k l m\n"
+               "> > abc d e\n"
+               "> > f g h i\n"
+               "> > j\n"
+               "> l1 a b c d\n"
+               "> e f g\n"
+               "end\n",
+               10 },
+       /* 2 */ { HTML ("<div style='width:10ch;'>123 456<br>789 123</div>"
+               "<blockquote type='cite'>"
+                       "<div style='width:8ch;'>123 456<br>789 1 2 3 4</div>"
+                       "<blockquote type='cite'>"
+                               "<div style='width:6ch;'>abc<br>def g h i j k</div>"
+                       "</blockquote>"
+               "</blockquote>"
+               "<div>end</div>"),
+               "123 456\n"
+               "789 123\n"
+               "> 123 456\n"
+               "> 789 1 2\n"
+               "> 3 4\n"
+               "> > abc\n"
+               "> > def g\n"
+               "> > h i j\n"
+               "> > k\n"
+               "end\n",
+               10 },
+       /* 3 */ { HTML ("<p style='width:10ch;'>123 456<br>789 123</p>"
+               "<blockquote type='cite'>"
+                       "<p style='width:8ch;'>123 456<br>789 1 2 3 4</p>"
+                       "<blockquote type='cite'>"
+                               "<p style='width:6ch;'>abc<br>def g h i j k</p>"
+                       "</blockquote>"
+               "</blockquote>"
+               "<p>end</p>"),
+               "123 456\n"
+               "789 123\n"
+               "> 123 456\n"
+               "> 789 1 2\n"
+               "> 3 4\n"
+               "> > abc\n"
+               "> > def g\n"
+               "> > h i j\n"
+               "> > k\n"
+               "end\n",
+               10 },
+       /* 4 */ { HTML ("<pre>123 456 789 123</pre>"
+               "<blockquote type='cite'>"
+                       "<pre>123 456 789 1 2 3 4</pre>"
+                       "<blockquote type='cite'>"
+                               "<pre>abc def g h i j k</pre>"
+                       "</blockquote>"
+               "</blockquote>"
+               "<pre>end</pre>"),
+               "123 456 789 123\n"
+               "> 123 456 789 1 2 3 4\n"
+               "> > abc def g h i j k\n"
+               "end\n",
+               10 },
+       /* 5 */ { HTML ("<pre>123 456\n789 123</pre>"
+               "<blockquote type='cite'>"
+                       "<pre>123 456\n789 1 2 3 4</pre>"
+                       "<blockquote type='cite'>"
+                               "<pre>abc def\ng h\ni j k</pre>"
+                       "</blockquote>"
+                       "<pre>a b\nc\nd e f</pre>"
+               "</blockquote>"
+               "<pre>end</pre>"),
+               "123 456\n"
+               "789 123\n"
+               "> 123 456\n"
+               "> 789 1 2 3 4\n"
+               "> > abc def\n"
+               "> > g h\n"
+               "> > i j k\n"
+               "> a b\n"
+               "> c\n"
+               "> d e f\n"
+               "end\n",
+               10 }
+       };
+
+       #undef HTML
+
+       gchar *script;
+       gint ii;
+
+       for (ii = 0; ii < G_N_ELEMENTS (tests); ii++) {
+               test_utils_load_string (fixture, tests[ii].html);
+
+               script = e_web_view_jsc_printf_script ("EvoConvert.ToPlainText(document.body, %d);", 
tests[ii].normal_div_width);
+
+               test_utils_jsc_call_string_and_verify (fixture, script, tests[ii].plain);
+
+               g_free (script);
+       }
+}
+
 gint
 main (gint argc,
       gchar *argv[])
@@ -2574,6 +2723,7 @@ main (gint argc,
        test_utils_add_test ("/EWebView/GetContent", test_get_content);
        test_utils_add_test ("/EWebView/GetElementFromPoint", test_get_element_from_point);
        test_utils_add_test ("/EWebView/ConvertToPlain", test_convert_to_plain);
+       test_utils_add_test ("/EWebView/ConvertToPlainQuoted", test_convert_to_plain_quoted);
 
        res = g_test_run ();
 


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