[evolution/wip/webkit2] Paste Quoted - be able to paste HTML too + added some tests



commit 009594462da46f4afe20716580d1fc312de7c820
Author: Milan Crha <mcrha redhat com>
Date:   Tue Aug 9 11:53:09 2016 +0200

    Paste Quoted - be able to paste HTML too + added some tests

 e-util/e-html-editor-actions.c                     |   46 +++++-
 e-util/test-html-editor-units.c                    |  172 ++++++++++++++++++--
 modules/webkit-editor/e-webkit-editor.c            |    2 +-
 .../web-extension/e-editor-dom-functions.c         |   31 +++-
 .../web-extension/e-editor-dom-functions.h         |    3 +-
 .../web-extension/e-editor-undo-redo-manager.c     |    2 +-
 .../web-extension/e-editor-web-extension.c         |    6 +-
 7 files changed, 233 insertions(+), 29 deletions(-)
---
diff --git a/e-util/e-html-editor-actions.c b/e-util/e-html-editor-actions.c
index ab7b38d..aaf9dd7 100644
--- a/e-util/e-html-editor-actions.c
+++ b/e-util/e-html-editor-actions.c
@@ -34,6 +34,7 @@
 #include "e-image-chooser-dialog.h"
 #include "e-spell-checker.h"
 #include "e-misc-utils.h"
+#include "e-selection.h"
 #include "e-content-editor.h"
 
 static void
@@ -562,18 +563,47 @@ action_paste_as_text_cb (GtkAction *action,
 }
 
 static void
-clipboard_text_received_for_paste_quote (GtkClipboard *clipboard,
-                                         const gchar *text,
-                                         EHTMLEditor *editor)
+paste_quote_text (EHTMLEditor *editor,
+                 const gchar *text,
+                 gboolean is_html)
 {
        EContentEditor *cnt_editor;
 
+       g_return_if_fail (E_IS_HTML_EDITOR (editor));
+       g_return_if_fail (text != NULL);
+
        cnt_editor = e_html_editor_get_content_editor (editor);
        e_content_editor_insert_content (
                cnt_editor,
                text,
                E_CONTENT_EDITOR_INSERT_QUOTE_CONTENT |
-               E_CONTENT_EDITOR_INSERT_TEXT_PLAIN);
+               (is_html ? E_CONTENT_EDITOR_INSERT_TEXT_HTML : E_CONTENT_EDITOR_INSERT_TEXT_PLAIN));
+}
+
+static void
+clipboard_html_received_for_paste_quote (GtkClipboard *clipboard,
+                                         const gchar *text,
+                                         gpointer user_data)
+{
+       EHTMLEditor *editor = user_data;
+
+       g_return_if_fail (E_IS_HTML_EDITOR (editor));
+       g_return_if_fail (text != NULL);
+
+       paste_quote_text (editor, text, TRUE);
+}
+
+static void
+clipboard_text_received_for_paste_quote (GtkClipboard *clipboard,
+                                         const gchar *text,
+                                         gpointer user_data)
+{
+       EHTMLEditor *editor = user_data;
+
+       g_return_if_fail (E_IS_HTML_EDITOR (editor));
+       g_return_if_fail (text != NULL);
+
+       paste_quote_text (editor, text, FALSE);
 }
 
 static void
@@ -591,10 +621,10 @@ action_paste_quote_cb (GtkAction *action,
                gdk_display_get_default (),
                GDK_SELECTION_CLIPBOARD);
 
-       gtk_clipboard_request_text (
-               clipboard,
-               (GtkClipboardTextReceivedFunc) clipboard_text_received_for_paste_quote,
-               editor);
+       if (e_clipboard_wait_is_html_available (clipboard))
+               e_clipboard_request_html (clipboard, clipboard_html_received_for_paste_quote, editor);
+       else if (gtk_clipboard_wait_is_text_available (clipboard))
+               gtk_clipboard_request_text (clipboard, clipboard_text_received_for_paste_quote, editor);
 }
 
 static void
diff --git a/e-util/test-html-editor-units.c b/e-util/test-html-editor-units.c
index eeb7be4..f0d370d 100644
--- a/e-util/test-html-editor-units.c
+++ b/e-util/test-html-editor-units.c
@@ -1406,7 +1406,7 @@ test_paste_multiline_plain2plain (TestFixture *fixture)
 }
 
 static void
-test_paste_quoted_singleline_html (TestFixture *fixture)
+test_paste_quoted_singleline_html2html (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("<html><body>some <b>bold</b> text</body></html>", TRUE);
 
@@ -1414,14 +1414,59 @@ test_paste_quoted_singleline_html (TestFixture *fixture)
                "mode:html\n"
                "type:text before \n"
                "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"))
+               "type:\\n\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX "<p>text before </p>"
+               "<blockquote type=\"cite\"><p>some <b>bold</b> text</p></blockquote>"
+               "<p>text after</p>" HTML_SUFFIX,
+               "text before \n"
+               "> some bold text\n"
+               "text after"))
                g_test_fail ();
 }
 
 static void
-test_paste_quoted_singleline_plain (TestFixture *fixture)
+test_paste_quoted_singleline_html2plain (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("<html><body>some <b>bold</b> text</body></html>", TRUE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:plain\n"
+               "type:text before \n"
+               "action:paste-quote\n"
+               "type:\\n\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before </p>"
+               "<blockquote type=\"cite\"><p style=\"width: 71ch;\">&gt; some <b>bold</b> 
text</p></blockquote>"
+               "<p style=\"width: 71ch;\">text after</p>" HTML_SUFFIX,
+               "text before \n"
+               "> some bold text\n"
+               "text after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_quoted_singleline_plain2html (TestFixture *fixture)
+{
+       test_utils_set_clipboard_text ("some plain text", FALSE);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:html\n"
+               "type:text before \n"
+               "action:paste-quote\n"
+               "type:\\n\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX "<p>text before </p>"
+               "<blockquote type=\"cite\"><p>some plain text</p></blockquote>"
+               "<p>text after</p>" HTML_SUFFIX,
+               "text before \n"
+               "> some plain text\n"
+               "text after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_quoted_singleline_plain2plain (TestFixture *fixture)
 {
        test_utils_set_clipboard_text ("some plain text", FALSE);
 
@@ -1429,9 +1474,110 @@ test_paste_quoted_singleline_plain (TestFixture *fixture)
                "mode:plain\n"
                "type:text before \n"
                "action:paste-quote\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"))
+               "type:\\n\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before </p>"
+               "<blockquote type=\"cite\"><p style=\"width: 71ch;\">&gt; some plain text</p></blockquote>"
+               "<p style=\"width: 71ch;\">text after</p>" HTML_SUFFIX,
+               "text before \n"
+               "> some plain text\n"
+               "text after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_quoted_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-quote\n"
+               "seq:b\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX "<p>text before </p>"
+               "<blockquote type=\"cite\">&gt; <b>bold</b> text</p>"
+               "<p>&gt; <i>italic</i> text</p>"
+               "<p>&gt; <u>underline</u> text</p></blockquote>"
+               "<p>text after</p>" HTML_SUFFIX,
+               "text before \n"
+               "> bold text\n"
+               "> italic text\n"
+               "> underline text\n"
+               "text after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_quoted_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-quote\n"
+               "type:\\n\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before </p>"
+               "<blockquote type=\"cite\"><p>&gt; bold text</p>"
+               "<p style=\"width: 71ch;\">&gt; italic text</p>"
+               "<p style=\"width: 71ch;\">&gt; underline text</p></blockquote>"
+               "<p style=\"width: 71ch;\">&gt; text after</p>" HTML_SUFFIX,
+               "text before \n"
+               "> bold text\n"
+               "> italic text\n"
+               "> underline text\n"
+               "text after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_quoted_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-quote\n"
+               "seq:b\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX "<p>text before </p>"
+               "<blockquote type=\"cite\"><p>line 1</p>"
+               "<p>line 2</p>"
+               "<p>line 3</p></blockquote>"
+               "<p>text after</p>" HTML_SUFFIX,
+               "text before \n"
+               "> line 1\n"
+               "> line 2\n"
+               "> line 3\n"
+               "text after"))
+               g_test_fail ();
+}
+
+static void
+test_paste_quoted_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-quote\n"
+               "type:\\n\n" /* stop quotting */
+               "type:text after\n",
+               HTML_PREFIX_PLAIN "<p style=\"width: 71ch;\">text before </p>"
+               "<blockquote type=\"cite\"><p style=\"width: 71ch;\">&gt; line 1</p>"
+               "<p style=\"width: 71ch;\">&gt; line 2</p>"
+               "<p style=\"width: 71ch;\">&gt; line 3</p></blockquote>"
+               "<p style=\"width: 71ch;\">text after</p>" HTML_SUFFIX,
+               "text before\n"
+               "> line 1\n"
+               "> line 2\n"
+               "> line 3\n"
+               "text after"))
                g_test_fail ();
 }
 
@@ -1556,8 +1702,14 @@ main (gint argc,
        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);
+       add_test ("/paste/quoted/singleline/html2html", test_paste_quoted_singleline_html2html);
+       add_test ("/paste/quoted/singleline/html2plain", test_paste_quoted_singleline_html2plain);
+       add_test ("/paste/quoted/singleline/plain2html", test_paste_quoted_singleline_plain2html);
+       add_test ("/paste/quoted/singleline/plain2plain", test_paste_quoted_singleline_plain2plain);
+       add_test ("/paste/quoted/multiline/html2html", test_paste_quoted_multiline_html2html);
+       add_test ("/paste/quoted/multiline/html2plain", test_paste_quoted_multiline_html2plain);
+       add_test ("/paste/quoted/multiline/plain2html", test_paste_quoted_multiline_plain2html);
+       add_test ("/paste/quoted/multiline/plain2plain", test_paste_quoted_multiline_plain2plain);
 
        #undef add_test
 
diff --git a/modules/webkit-editor/e-webkit-editor.c b/modules/webkit-editor/e-webkit-editor.c
index 0304648..04e62b0 100644
--- a/modules/webkit-editor/e-webkit-editor.c
+++ b/modules/webkit-editor/e-webkit-editor.c
@@ -1561,7 +1561,7 @@ webkit_editor_insert_content (EContentEditor *editor,
                        wk_editor->priv->web_extension,
                        "DOMQuoteAndInsertTextIntoSelection",
                        g_variant_new (
-                               "(ts)", current_page_id (wk_editor), content),
+                               "(tsb)", current_page_id (wk_editor), content, (flags & 
E_CONTENT_EDITOR_INSERT_TEXT_HTML) != 0),
                        G_DBUS_CALL_FLAGS_NONE,
                        -1,
                        NULL,
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 a58b831..ecc833d 100644
--- a/modules/webkit-editor/web-extension/e-editor-dom-functions.c
+++ b/modules/webkit-editor/web-extension/e-editor-dom-functions.c
@@ -5321,7 +5321,8 @@ parse_html_into_blocks (EEditorPage *editor_page,
 
 void
 e_editor_dom_quote_and_insert_text_into_selection (EEditorPage *editor_page,
-                                                  const gchar *text)
+                                                  const gchar *text,
+                                                  gboolean is_html)
 {
        WebKitDOMDocument *document;
        WebKitDOMElement *blockquote, *element, *selection_start;
@@ -5337,11 +5338,29 @@ e_editor_dom_quote_and_insert_text_into_selection (EEditorPage *editor_page,
 
        document = e_editor_page_get_document (editor_page);
 
-       /* This is a trick to escape any HTML characters (like <, > or &).
-        * <textarea> automatically replaces all these unsafe characters
-        * by &lt;, &gt; etc. */
-       element = webkit_dom_document_create_element (document, "textarea", NULL);
-       webkit_dom_html_element_set_inner_text (WEBKIT_DOM_HTML_ELEMENT (element), text, NULL);
+       if (is_html) {
+               element = webkit_dom_document_create_element (document, "div", NULL);
+
+               if (strstr (text, "\n")) {
+                       GRegex *regex;
+                       gchar *tmp;
+
+                       /* Strip new lines between tags to avoid unwanted line breaks. */
+                       regex = g_regex_new ("\\>[\\s]+\\<", 0, 0, NULL);
+                       tmp = g_regex_replace (regex, text, -1, 0, "> <", 0, NULL);
+                       webkit_dom_element_set_inner_html (element, tmp, NULL);
+                       g_free (tmp);
+                       g_regex_unref (regex);
+               } else {
+                       webkit_dom_element_set_inner_html (element, text, NULL);
+               }
+       } else {
+               /* This is a trick to escape any HTML characters (like <, > or &).
+                * <textarea> automatically replaces all these unsafe characters
+                * by &lt;, &gt; etc. */
+               element = webkit_dom_document_create_element (document, "textarea", NULL);
+               webkit_dom_html_element_set_inner_text (WEBKIT_DOM_HTML_ELEMENT (element), text, NULL);
+       }
 
        inner_html = webkit_dom_element_get_inner_html (element);
 
diff --git a/modules/webkit-editor/web-extension/e-editor-dom-functions.h 
b/modules/webkit-editor/web-extension/e-editor-dom-functions.h
index 51ed1d7..747dd11 100644
--- a/modules/webkit-editor/web-extension/e-editor-dom-functions.h
+++ b/modules/webkit-editor/web-extension/e-editor-dom-functions.h
@@ -98,7 +98,8 @@ void          e_editor_dom_remove_input_event_listener_from_body
                                                (EEditorPage *editor_page);
 void           e_editor_dom_quote_and_insert_text_into_selection
                                                (EEditorPage *editor_page,
-                                                const gchar *text);
+                                                const gchar *text,
+                                                gboolean is_html);
 void           e_editor_dom_check_magic_links  (EEditorPage *editor_page,
                                                 gboolean include_space_by_user);
 void           e_editor_dom_insert_smiley      (EEditorPage *editor_page,
diff --git a/modules/webkit-editor/web-extension/e-editor-undo-redo-manager.c 
b/modules/webkit-editor/web-extension/e-editor-undo-redo-manager.c
index 10aa8ed..76031d2 100644
--- a/modules/webkit-editor/web-extension/e-editor-undo-redo-manager.c
+++ b/modules/webkit-editor/web-extension/e-editor-undo-redo-manager.c
@@ -1585,7 +1585,7 @@ undo_redo_paste (EEditorPage *editor_page,
                if (event->type == HISTORY_PASTE)
                        e_editor_dom_convert_and_insert_html_into_selection (editor_page, 
event->data.string.to, FALSE);
                else if (event->type == HISTORY_PASTE_QUOTED)
-                       e_editor_dom_quote_and_insert_text_into_selection (editor_page, 
event->data.string.to);
+                       e_editor_dom_quote_and_insert_text_into_selection (editor_page, 
event->data.string.to, FALSE);
                else if (event->type == HISTORY_INSERT_HTML)
                        e_editor_dom_insert_html (editor_page, event->data.string.to);
                else
diff --git a/modules/webkit-editor/web-extension/e-editor-web-extension.c 
b/modules/webkit-editor/web-extension/e-editor-web-extension.c
index db67eae..f387e26 100644
--- a/modules/webkit-editor/web-extension/e-editor-web-extension.c
+++ b/modules/webkit-editor/web-extension/e-editor-web-extension.c
@@ -434,6 +434,7 @@ static const gchar *introspection_xml =
 "    <method name='DOMQuoteAndInsertTextIntoSelection'>"
 "      <arg type='t' name='page_id' direction='in'/>"
 "      <arg type='s' name='text' direction='in'/>"
+"      <arg type='b' name='is_html' direction='in'/>"
 "    </method>"
 "    <method name='DOMConvertAndInsertHTMLIntoSelection'>"
 "      <arg type='t' name='page_id' direction='in'/>"
@@ -1633,15 +1634,16 @@ handle_method_call (GDBusConnection *connection,
                e_editor_dom_turn_spell_check_off (editor_page);
                g_dbus_method_invocation_return_value (invocation, NULL);
        } else if (g_strcmp0 (method_name, "DOMQuoteAndInsertTextIntoSelection") == 0) {
+               gboolean is_html = FALSE;
                const gchar *text;
 
-               g_variant_get (parameters, "(t&s)", &page_id, &text);
+               g_variant_get (parameters, "(t&sb)", &page_id, &text, &is_html);
 
                editor_page = get_editor_page_or_return_dbus_error (invocation, extension, page_id);
                if (!editor_page)
                        goto error;
 
-               e_editor_dom_quote_and_insert_text_into_selection (editor_page, text);
+               e_editor_dom_quote_and_insert_text_into_selection (editor_page, text, is_html);
                g_dbus_method_invocation_return_value (invocation, NULL);
        } else if (g_strcmp0 (method_name, "DOMConvertAndInsertHTMLIntoSelection") == 0) {
                gboolean is_html;


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