[evolution/wip/webkit2] Fix singleline plain text paste and add more paste tests



commit 9d4501b10fad37139a1e2fd4117c96ae26e9e79a
Author: Milan Crha <mcrha redhat com>
Date:   Tue Aug 9 09:16:38 2016 +0200

    Fix singleline plain text paste and add more paste tests

 e-util/test-html-editor-units.c                    |  174 ++++++++++++++++++--
 .../web-extension/e-editor-dom-functions.c         |   23 ++-
 2 files changed, 173 insertions(+), 24 deletions(-)
---
diff --git a/e-util/test-html-editor-units.c b/e-util/test-html-editor-units.c
index 981ef3b..eeb7be4 100644
--- a/e-util/test-html-editor-units.c
+++ b/e-util/test-html-editor-units.c
@@ -1214,7 +1214,7 @@ test_paragraph_wrap_lines (TestFixture *fixture)
 }
 
 static void
-test_paste_html2html (TestFixture *fixture)
+test_paste_singleline_html2html (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("<html><body>some <b>bold</b> text</body></html>", TRUE);
 
@@ -1224,12 +1224,12 @@ test_paste_html2html (TestFixture *fixture)
                "action:paste\n"
                "type: text after\n",
                HTML_PREFIX "<p>text before some <b>bold</b> text text after</p>" HTML_SUFFIX,
-               "text before some bold text text after\n"))
+               "text before some bold text text after"))
                g_test_fail ();
 }
 
 static void
-test_paste_html2plain (TestFixture *fixture)
+test_paste_singleline_html2plain (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("<html><body>some <b>bold</b> text</body></html>", TRUE);
 
@@ -1239,12 +1239,12 @@ test_paste_html2plain (TestFixture *fixture)
                "action:paste\n"
                "type: text after\n",
                HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before some bold text text after</p>" 
HTML_SUFFIX,
-               "text before some bold text text after\n"))
+               "text before some bold text text after"))
                g_test_fail ();
 }
 
 static void
-test_paste_plain2html (TestFixture *fixture)
+test_paste_singleline_plain2html (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("some plain text", FALSE);
 
@@ -1254,12 +1254,12 @@ test_paste_plain2html (TestFixture *fixture)
                "action:paste\n"
                "type: text after\n",
                HTML_PREFIX "<p>text before some plain text text after</p>" HTML_SUFFIX,
-               "text before some plain text text after\n"))
+               "text before some plain text text after"))
                g_test_fail ();
 }
 
 static void
-test_paste_plain2plain (TestFixture *fixture)
+test_paste_singleline_plain2plain (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("some plain text", FALSE);
 
@@ -1269,12 +1269,144 @@ test_paste_plain2plain (TestFixture *fixture)
                "action:paste\n"
                "type: text after\n",
                HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before some plain text text after</p>" 
HTML_SUFFIX,
-               "text before some plain text text after\n"))
+               "text before some plain text text after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_html2html (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("<html><body><b>bold</b> text<br><i>italic</i> 
text<br><u>underline</u> text<br></body></html>", TRUE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:html\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:text after\n",
+               HTML_PREFIX "<p>text before <b>bold</b> text</p><p><i>italic</i> text</p><p><u>underline</u> 
text</p><p>text after</p>" HTML_SUFFIX,
+               "text before bold text\nitalic text\nunderline text\ntext after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_html2plain (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("<html><body><b>bold</b> text<br><i>italic</i> 
text<br><u>underline</u> text</body></html>", TRUE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:plain\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:\\ntext after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before bold text</p>"
+               "<p style=\"width: 71ch;\">italic text</p>"
+               "<p style=\"width: 71ch;\">underline text</p>"
+               "<p style=\"width: 71ch;\">text after</p>" HTML_SUFFIX,
+               "text before bold text\nitalic text\nunderline text\ntext after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_div_html2html (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("<html><body><div><b>bold</b> text</div><div><i>italic</i> 
text</div><div><u>underline</u> text</div><div></div></body></html>", TRUE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:html\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:text after\n",
+               HTML_PREFIX "<p>text before <b>bold</b> text</p><p><i>italic</i> text</p><p><u>underline</u> 
text</p><p>text after</p>" HTML_SUFFIX,
+               "text before bold text\nitalic text\nunderline text\ntext after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_div_html2plain (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("<html><body><div><b>bold</b> text</div><div><i>italic</i> 
text</div><div><u>underline</u> text</div></body></html>", TRUE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:plain\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:\\ntext after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before bold text</p>"
+               "<p style=\"width: 71ch;\">italic text</p>"
+               "<p style=\"width: 71ch;\">underline text</p>"
+               "<p style=\"width: 71ch;\">text after</p>" HTML_SUFFIX,
+               "text before bold text\nitalic text\nunderline text\ntext after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_p_html2html (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("<html><body><p><b>bold</b> text</p><p><i>italic</i> 
text</p><p><u>underline</u> text</p><p></p></body></html>", TRUE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:html\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:text after\n",
+               HTML_PREFIX "<p>text before <b>bold</b> text</p><p><i>italic</i> text</p><p><u>underline</u> 
text</p><p>text after</p>" HTML_SUFFIX,
+               "text before bold text\nitalic text\nunderline text\ntext after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_p_html2plain (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("<html><body><p><b>bold</b> text</p><p><i>italic</i> 
text</p><p><u>underline</u> text</p></body></html>", TRUE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:plain\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:\\ntext after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before bold text</p>"
+               "<p style=\"width: 71ch;\">italic text</p>"
+               "<p style=\"width: 71ch;\">underline text</p>"
+               "<p style=\"width: 71ch;\">text after</p>" HTML_SUFFIX,
+               "text before bold text\nitalic text\nunderline text\ntext after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_plain2html (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("line 1\nline 2\nline 3\n", FALSE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:html\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:text after\n",
+               HTML_PREFIX "<p>text before line 1</p><p>line 2</p><p>line 3</p><p>text after</p>" 
HTML_SUFFIX,
+               "text before line 1\nline 2\nline 3\ntext after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_multiline_plain2plain (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("line 1\nline 2\nline 3", FALSE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:plain\n"
+               "type:text before \n"
+               "action:paste\n"
+               "type:\\ntext after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before line 1</p>"
+               "<p style=\"width: 71ch;\">line 2</p>"
+               "<p style=\"width: 71ch;\">line 3</p>"
+               "<p style=\"width: 71ch;\">text after</p>" HTML_SUFFIX,
+               "text before line 1\nline 2\nline 3\ntext after"))
                g_test_fail ();
 }
 
 static void
-test_paste_quoted_html (TestFixture *fixture)
+test_paste_quoted_singleline_html (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("<html><body>some <b>bold</b> text</body></html>", TRUE);
 
@@ -1284,12 +1416,12 @@ test_paste_quoted_html (TestFixture *fixture)
                "action:paste-quote\n"
                "type: text after\n",
                HTML_PREFIX "<p>text before some <b>bold</b> text text after</p>" HTML_SUFFIX,
-               "text before some bold text text after\n"))
+               "text before some bold text text after"))
                g_test_fail ();
 }
 
 static void
-test_paste_quoted_plain (TestFixture *fixture)
+test_paste_quoted_singleline_plain (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("some plain text", FALSE);
 
@@ -1412,12 +1544,20 @@ main (gint argc,
        add_test ("/paragraph/header6/selection", test_paragraph_header6_selection);
        add_test ("/paragraph/header6/typed", test_paragraph_header6_typed);
        add_test ("/paragraph/wrap-lines", test_paragraph_wrap_lines);
-       add_test ("/paste/html2html", test_paste_html2html);
-       add_test ("/paste/html2plain", test_paste_html2plain);
-       add_test ("/paste/plain2html", test_paste_plain2html);
-       add_test ("/paste/plain2plain", test_paste_plain2plain);
-       add_test ("/paste/quoted/html", test_paste_quoted_html);
-       add_test ("/paste/quoted/plain", test_paste_quoted_plain);
+       add_test ("/paste/singleline/html2html", test_paste_singleline_html2html);
+       add_test ("/paste/singleline/html2plain", test_paste_singleline_html2plain);
+       add_test ("/paste/singleline/plain2html", test_paste_singleline_plain2html);
+       add_test ("/paste/singleline/plain2plain", test_paste_singleline_plain2plain);
+       add_test ("/paste/multiline/html2html", test_paste_multiline_html2html);
+       add_test ("/paste/multiline/html2plain", test_paste_multiline_html2plain);
+       add_test ("/paste/multiline/div/html2html", test_paste_multiline_div_html2html);
+       add_test ("/paste/multiline/div/html2plain", test_paste_multiline_div_html2plain);
+       add_test ("/paste/multiline/p/html2html", test_paste_multiline_p_html2html);
+       add_test ("/paste/multiline/p/html2plain", test_paste_multiline_p_html2plain);
+       add_test ("/paste/multiline/plain2html", test_paste_multiline_plain2html);
+       add_test ("/paste/multiline/plain2plain", test_paste_multiline_plain2plain);
+       add_test ("/paste/quoted/singleline/html", test_paste_quoted_singleline_html);
+       add_test ("/paste/quoted/singleline/plain", test_paste_quoted_singleline_plain);
 
        #undef add_test
 
diff --git a/modules/webkit-editor/web-extension/e-editor-dom-functions.c 
b/modules/webkit-editor/web-extension/e-editor-dom-functions.c
index 466b776..a58b831 100644
--- a/modules/webkit-editor/web-extension/e-editor-dom-functions.c
+++ b/modules/webkit-editor/web-extension/e-editor-dom-functions.c
@@ -5150,14 +5150,18 @@ parse_html_into_blocks (EEditorPage *editor_page,
 
        prev_br = html->str;
        next_br = strstr (prev_br, "<br>");
+       processing_last = !next_br;
 
-       while (next_br) {
+       while (next_br || processing_last) {
                const gchar *citation_start = NULL, *citation_end = NULL;
                const gchar *rest = NULL, *with_br = NULL;
                gchar *to_process = NULL, *to_insert = NULL;
                guint to_insert_start = 0, to_insert_end = 0;
 
-               if ((to_process = g_utf8_substring (prev_br, 0, g_utf8_pointer_to_offset (prev_br, next_br))) 
&& !*to_process && !processing_last) {
+               if (!next_br) {
+                       to_process = g_strdup (prev_br);
+                       processing_last = TRUE;
+               } else if ((to_process = g_utf8_substring (prev_br, 0, g_utf8_pointer_to_offset (prev_br, 
next_br))) && !*to_process && !processing_last) {
                        g_free (to_process);
                        to_process = g_strdup (next_br);
                        processing_last = TRUE;
@@ -5250,12 +5254,17 @@ parse_html_into_blocks (EEditorPage *editor_page,
                if (citation_end)
                        append_citation_mark (document, parent, "##CITATION_END##");
 
+               g_free (to_process);
+
                prev_br = next_br;
-               next_br = strstr (prev_br + 4, "<br>");
+               next_br = (prev_br && *prev_br) ? strstr (prev_br + 1, "<br>") : NULL;
                if (!next_br && !processing_last) {
-                       if (g_utf8_strlen (prev_br, -1) > 4)
+                       if (!prev_br)
+                               break;
+
+                       if (g_utf8_strlen (prev_br, -1) > 4) {
                                next_br = prev_br;
-                       else {
+                       } else {
                                WebKitDOMNode *child;
 
                                child = webkit_dom_node_get_last_child (
@@ -5280,12 +5289,12 @@ parse_html_into_blocks (EEditorPage *editor_page,
                                        create_and_append_new_block (
                                                editor_page, parent, block_template, "<br>");
                                }
-                               g_free (to_process);
                                break;
                        }
                        processing_last = TRUE;
+               } else if (processing_last && !prev_br && !next_br) {
+                       break;
                }
-               g_free (to_process);
        }
 
        if (has_citation) {


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