[evolution/wip/mcrha/webkit-jsc-api] EvoEditor.wrapParagraph: Cover nested elements in the paragraph text



commit 6b6a9037d28b84355e85ff81bfcd1423f91747db
Author: Milan Crha <mcrha redhat com>
Date:   Mon Jan 27 14:27:39 2020 +0100

    EvoEditor.wrapParagraph: Cover nested elements in the paragraph text

 data/webkit/e-editor.js                   | 23 +++++++++++----
 src/e-util/test-html-editor-units-utils.c | 42 +++++++++++++++++++++++----
 src/e-util/test-html-editor-units.c       | 48 +++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 11 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index f8d16f5ef2..0b2a14bbc8 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -3755,7 +3755,7 @@ EvoEditor.PasteText = function(text, isHTML, quote)
 {
 }
 
-EvoEditor.wrapParagraph = function(paragraphNode, maxLetters, currentPar, usedLetters)
+EvoEditor.wrapParagraph = function(paragraphNode, maxLetters, currentPar, usedLetters, wasNestedElem)
 {
        var child = paragraphNode.firstChild, nextChild, appendBR;
 
@@ -3782,7 +3782,8 @@ EvoEditor.wrapParagraph = function(paragraphNode, maxLetters, currentPar, usedLe
                                        spacePos = text.indexOf(" ");
 
                                if (spacePos > 0 && (!usedLetters || usedLetters + spacePos <= maxLetters)) {
-                                       var textNode = document.createTextNode((usedLetters > 0 ? " " : "") + 
text.substr(0, spacePos));
+                                       var textNode = document.createTextNode(((usedLetters > 0 && 
!wasNestedElem) ? " " : "") +
+                                               text.substr(0, spacePos));
 
                                        if (currentPar)
                                                currentPar.appendChild(textNode);
@@ -3805,12 +3806,16 @@ EvoEditor.wrapParagraph = function(paragraphNode, maxLetters, currentPar, usedLe
                                        break;
                        }
 
-                       child.nodeValue = (usedLetters > 0 ? " " : "") + text;
-                       usedLetters += (usedLetters > 0 ? 1 : 0) + text.length;
+                       child.nodeValue = ((usedLetters > 0 && !wasNestedElem) ? " " : "") + text;
+                       usedLetters += ((usedLetters > 0 && !wasNestedElem) ? 1 : 0) + text.length;
 
                        if (usedLetters > maxLetters)
                                appendBR = true;
+
+                       wasNestedElem = false;
                } else if (child.tagName == "BR") {
+                       wasNestedElem = false;
+
                        if (!child.nextSibling) {
                                return -1;
                        }
@@ -3844,6 +3849,7 @@ EvoEditor.wrapParagraph = function(paragraphNode, maxLetters, currentPar, usedLe
                        }
                } else if (child.tagName == "IMG") {
                        // just skip it, do not count it into the line length
+                       wasNestedElem = false;
                } else if (child.tagName == "B" ||
                           child.tagName == "I" ||
                           child.tagName == "U" ||
@@ -3853,9 +3859,14 @@ EvoEditor.wrapParagraph = function(paragraphNode, maxLetters, currentPar, usedLe
                           child.tagName == "FONT" ||
                           child.tagName == "SPAN" ||
                           child.tagName == "A") {
+                       usedLetters = EvoEditor.wrapParagraph(child, maxLetters, null, usedLetters, true);
+                       if (usedLetters == -1)
+                               usedLetters = 0;
+                       wasNestedElem = true;
                } else if (child.nodeType == child.ELEMENT_NODE) {
                        // everything else works like a line stopper, with a new line added after it
                        appendBR = true;
+                       wasNestedElem = false;
                }
 
                nextChild = child.nextSibling;
@@ -3943,7 +3954,7 @@ EvoEditor.WrapSelection = function()
                                        currentPar = null;
                                        usedLetters = 0;
                                } else {
-                                       usedLetters = EvoEditor.wrapParagraph(nodeFrom, maxLetters, 
currentPar, usedLetters);
+                                       usedLetters = EvoEditor.wrapParagraph(nodeFrom, maxLetters, 
currentPar, usedLetters, false);
 
                                        if (usedLetters == -1) {
                                                currentPar = null;
@@ -3954,7 +3965,7 @@ EvoEditor.WrapSelection = function()
                                }
                        }
 
-                       // cannot break it now, because want to delete the last empty paragraph
+                       // cannot break the cycle now, because want to delete the last empty paragraph
                        var done = nodeFrom === nodeTo;
 
                        if (!nodeFrom.childNodes.length) {
diff --git a/src/e-util/test-html-editor-units-utils.c b/src/e-util/test-html-editor-units-utils.c
index 86e7ca58b2..06e09c3c4b 100644
--- a/src/e-util/test-html-editor-units-utils.c
+++ b/src/e-util/test-html-editor-units-utils.c
@@ -227,6 +227,28 @@ test_utils_web_process_crashed_cb (WebKitWebView *web_view,
        return FALSE;
 }
 
+/* <Control>+<Shift>+I */
+#define WEBKIT_INSPECTOR_MOD  (GDK_CONTROL_MASK | GDK_SHIFT_MASK)
+#define WEBKIT_INSPECTOR_KEY  (GDK_KEY_I)
+
+static gboolean
+wk_editor_key_press_event_cb (WebKitWebView *web_view,
+                             GdkEventKey *event)
+{
+       WebKitWebInspector *inspector;
+       gboolean handled = FALSE;
+
+       inspector = webkit_web_view_get_inspector (web_view);
+
+       if ((event->state & WEBKIT_INSPECTOR_MOD) == WEBKIT_INSPECTOR_MOD &&
+           event->keyval == WEBKIT_INSPECTOR_KEY) {
+               webkit_web_inspector_show (inspector);
+               handled = TRUE;
+       }
+
+       return handled;
+}
+
 typedef struct _CreateData {
        gpointer async_data;
        TestFixture *fixture;
@@ -304,12 +326,22 @@ test_utils_html_editor_created_cb (GObject *source_object,
        g_signal_connect (cnt_editor, "web-process-crashed",
                G_CALLBACK (test_utils_web_process_crashed_cb), NULL);
 
-       if (!test_utils_get_multiple_web_processes () && !global_web_context &&
-           WEBKIT_IS_WEB_VIEW (cnt_editor)) {
-               WebKitWebContext *web_context;
+       if (WEBKIT_IS_WEB_VIEW (cnt_editor)) {
+               WebKitSettings *web_settings;
+
+               web_settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (cnt_editor));
+               webkit_settings_set_enable_developer_extras (web_settings, TRUE);
 
-               web_context = webkit_web_view_get_context (WEBKIT_WEB_VIEW (cnt_editor));
-               global_web_context = g_object_ref (web_context);
+               g_signal_connect (
+                       cnt_editor, "key-press-event",
+                       G_CALLBACK (wk_editor_key_press_event_cb), NULL);
+
+               if (!test_utils_get_multiple_web_processes () && !global_web_context) {
+                       WebKitWebContext *web_context;
+
+                       web_context = webkit_web_view_get_context (WEBKIT_WEB_VIEW (cnt_editor));
+                       global_web_context = g_object_ref (web_context);
+               }
        }
 
        gtk_window_set_focus (GTK_WINDOW (fixture->window), GTK_WIDGET (cnt_editor));
diff --git a/src/e-util/test-html-editor-units.c b/src/e-util/test-html-editor-units.c
index 151fe6f34e..9b6399f83a 100644
--- a/src/e-util/test-html-editor-units.c
+++ b/src/e-util/test-html-editor-units.c
@@ -5500,6 +5500,53 @@ test_wrap_basic (TestFixture *fixture)
        }
 }
 
+static void
+test_wrap_nested (TestFixture *fixture)
+{
+       test_utils_fixture_change_setting_int32 (fixture, "org.gnome.evolution.mail", 
"composer-word-wrap-length", 10);
+
+       if (!test_utils_run_simple_test (fixture,
+               "mode:html\n"
+               "type:123 4 \n"
+               "action:bold\n"
+               "type:b\n"
+               "action:bold\n"
+               "type: 5 67 89 \n"
+               "action:bold\n"
+               "type:bold text\n"
+               "action:bold\n"
+               "type: 123 456 \n"
+               "action:italic\n"
+               "type:italic text \n"
+               "action:underline\n"
+               "type:and underline text\n"
+               "action:underline\n"
+               "type: xyz\n"
+               "action:italic\n"
+               "type: 7 8 9 1 2 3 4 5\n"
+               "action:select-all\n"
+               "action:wrap-lines\n",
+               HTML_PREFIX "<div>123 4 <b>b</b> 5<br>"
+               "67 89 <b>bold<br>"
+               "text</b> 123<br>"
+               "456 <i>italic<br>"
+               "text <u>and<br>"
+               "underline<br>"
+               "text</u> xyz</i> 7<br>"
+               "8 9 1 2 3<br>"
+               "4 5</div>" HTML_SUFFIX,
+               "123 4 b 5\n"
+               "67 89 bold\n"
+               "text 123\n"
+               "456 italic\n"
+               "text and\n"
+               "underline\n"
+               "text xyz 7\n"
+               "8 9 1 2 3\n"
+               "4 5\n"))
+               g_test_fail ();
+}
+
 gint
 main (gint argc,
       gchar *argv[])
@@ -5694,6 +5741,7 @@ main (gint argc,
        test_utils_add_test ("/replace/dialog", test_replace_dialog);
        test_utils_add_test ("/replace-all/dialog", test_replace_dialog_all);
        test_utils_add_test ("/wrap/basic", test_wrap_basic);
+       test_utils_add_test ("/wrap/nested", test_wrap_nested);
 
        test_add_html_editor_bug_tests ();
 


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