[evolution/wip/mcrha/webkit-jsc-api: 278/292] Fix and enhance /image/insert editor unit test



commit 4747a97b58e0ee4eee91c49f925555d29b18dee2
Author: Milan Crha <mcrha redhat com>
Date:   Mon Feb 3 12:50:32 2020 +0100

    Fix and enhance /image/insert editor unit test
    
    And fix bugs discovered with this test.

 data/webkit/e-convert.js                    |  9 +-----
 data/webkit/e-editor.js                     | 13 ++++++++
 data/webkit/e-selection.js                  | 50 ++++++++++++++++++++++++++---
 src/e-util/test-html-editor-units.c         | 12 ++-----
 src/modules/webkit-editor/e-webkit-editor.c | 24 +++++++++++++-
 5 files changed, 85 insertions(+), 23 deletions(-)
---
diff --git a/data/webkit/e-convert.js b/data/webkit/e-convert.js
index 7ce8a897df..e952921730 100644
--- a/data/webkit/e-convert.js
+++ b/data/webkit/e-convert.js
@@ -615,14 +615,7 @@ EvoConvert.ImgToText = function(img)
 
        txt = img.alt;
 
-       if (!txt) {
-               txt = img.src;
-
-               if (txt.startsWith("cid:"))
-                       txt = "";
-       }
-
-       return txt;
+       return txt ? txt : "";
 }
 
 EvoConvert.extractElemText = function(elem, normalDivWidth, quoteLevel)
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index b09f7f8bf5..efb608c29a 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -3688,6 +3688,19 @@ EvoEditor.InsertEmoticon = function(text, imageUri, width, height)
        EvoEditor.InsertHTML("InsertEmoticon", EvoEditor.createEmoticonHTML(text, imageUri, width, height));
 }
 
+EvoEditor.InsertImage = function(imageUri, width, height)
+{
+       var html = "<img src=\"" + imageUri + "\"";
+
+       if (width > 0 && height > 0) {
+               html += " width=\"" + width + "px\" height=\"" + height + "px\"";
+       }
+
+       html += ">";
+
+       EvoEditor.InsertHTML("InsertImage", html);
+}
+
 EvoEditor.GetCurrentSignatureUid = function()
 {
        var elem = document.querySelector(".-x-evo-signature[id]");
diff --git a/data/webkit/e-selection.js b/data/webkit/e-selection.js
index bb880a11c9..12e1ce30f5 100644
--- a/data/webkit/e-selection.js
+++ b/data/webkit/e-selection.js
@@ -131,9 +131,17 @@ EvoSelection.Store = function(doc)
        selection.baseElem = sel.baseNode ? EvoSelection.GetChildPath(doc.body, sel.baseNode) : [];
        selection.baseOffset = sel.baseOffset + EvoSelection.GetOverallTextOffset(sel.baseNode);
 
+       if (sel.baseNode && sel.baseNode.nodeType == sel.baseNode.ELEMENT_NODE) {
+               selection.baseIsElement = true;
+       }
+
        if (!sel.isCollapsed) {
                selection.extentElem = EvoSelection.GetChildPath(doc.body, sel.extentNode);
                selection.extentOffset = sel.extentOffset + EvoSelection.GetOverallTextOffset(sel.extentNode);
+
+               if (sel.extentNode && sel.extentNode.nodeType == sel.extentNode.ELEMENT_NODE) {
+                       selection.extentIsElement = true;
+               }
        }
 
        return selection;
@@ -160,15 +168,19 @@ EvoSelection.Restore = function(doc, selection)
                base_offset = 0;
        }
 
-       base_node = EvoSelection.GetTextOffsetNode(base_node, base_offset);
-       base_offset -= EvoSelection.GetOverallTextOffset(base_node);
+       if (!selection.baseIsElement) {
+               base_node = EvoSelection.GetTextOffsetNode(base_node, base_offset);
+               base_offset -= EvoSelection.GetOverallTextOffset(base_node);
+       }
 
        extent_node = EvoSelection.FindElementByPath(doc.body, selection.extentElem);
        extent_offset = selection.extentOffset;
 
        if (extent_node) {
-               extent_node = EvoSelection.GetTextOffsetNode(extent_node, extent_offset);
-               extent_offset -= EvoSelection.GetOverallTextOffset(extent_node);
+               if (!selection.extentIsElement) {
+                       extent_node = EvoSelection.GetTextOffsetNode(extent_node, extent_offset);
+                       extent_offset -= EvoSelection.GetOverallTextOffset(extent_node);
+               }
        }
 
        if (extent_node)
@@ -213,9 +225,17 @@ EvoSelection.ToString = function(selection)
        str += "baseElem=" + utils.arrayToString(base_elem);
        str += " baseOffset=" + (base_offset ? base_offset : 0);
 
+       if (selection.baseIsElement) {
+               str += " baseIsElement=1";
+       }
+
        if (extent_elem) {
                str += " extentElem=" + utils.arrayToString(extent_elem);
                str += " extentOffset=" + (extent_offset ? extent_offset : 0);
+
+               if (selection.extentIsElement) {
+                       str += " extentIsElement=1";
+               }
        }
 
        return str;
@@ -286,6 +306,17 @@ EvoSelection.FromString = function(str)
                        continue;
                }
 
+               name = "baseIsElement";
+               if (split_str[ii].startsWith(name + "=")) {
+                       var value;
+
+                       value = parseInt(split_str[ii].slice(name.length + 1), 10);
+                       if (Number.isInteger(value) && value == 1) {
+                               selection[name] = true;
+                       }
+                       continue;
+               }
+
                name = "extentElem";
                if (split_str[ii].startsWith(name + "=")) {
                        selection[name] = utils.arrayFromString(split_str[ii].slice(name.length + 1));
@@ -301,6 +332,17 @@ EvoSelection.FromString = function(str)
                                selection[name] = value;
                        }
                }
+
+               name = "extentIsElement";
+               if (split_str[ii].startsWith(name + "=")) {
+                       var value;
+
+                       value = parseInt(split_str[ii].slice(name.length + 1), 10);
+                       if (Number.isInteger(value) && value == 1) {
+                               selection[name] = true;
+                       }
+                       continue;
+               }
        }
 
        /* The "baseElem" is required, the rest is optional */
diff --git a/src/e-util/test-html-editor-units.c b/src/e-util/test-html-editor-units.c
index 9b6399f83a..a0690f422f 100644
--- a/src/e-util/test-html-editor-units.c
+++ b/src/e-util/test-html-editor-units.c
@@ -3649,7 +3649,6 @@ test_image_insert (TestFixture *fixture)
        EContentEditor *cnt_editor;
        gchar *expected_html;
        gchar *filename;
-       gchar *image_data_base64;
        gchar *uri;
        GError *error = NULL;
 
@@ -3670,18 +3669,11 @@ test_image_insert (TestFixture *fixture)
        /* Wait some time until the operation is finished */
        test_utils_wait_milliseconds (500);
 
-       image_data_base64 = test_utils_get_base64_data_for_image (filename);
+       expected_html = g_strconcat (HTML_PREFIX "<div>before*<img src=\"", uri, "\" width=\"24px\" 
height=\"24px\">+after</div>" HTML_SUFFIX, NULL);
 
        g_free (uri);
        g_free (filename);
 
-       g_return_if_fail (image_data_base64 != NULL);
-
-       expected_html = g_strconcat (HTML_PREFIX "<div>before*<img src=\"data:image/png;base64,",
-               image_data_base64, "\">+after</div>" HTML_SUFFIX, NULL);
-
-       g_free (image_data_base64);
-
        if (!test_utils_run_simple_test (fixture,
                "undo:save\n" /* 1 */
                "undo:undo\n"
@@ -3689,7 +3681,7 @@ test_image_insert (TestFixture *fixture)
                "undo:test:1\n"
                "type:+after\n",
                expected_html,
-               "before*+after"))
+               "before*+after\n"))
                g_test_fail ();
 
        g_free (expected_html);
diff --git a/src/modules/webkit-editor/e-webkit-editor.c b/src/modules/webkit-editor/e-webkit-editor.c
index 32e60385ee..b1f9d36643 100644
--- a/src/modules/webkit-editor/e-webkit-editor.c
+++ b/src/modules/webkit-editor/e-webkit-editor.c
@@ -3050,7 +3050,29 @@ static void
 webkit_editor_insert_image (EContentEditor *editor,
                             const gchar *image_uri)
 {
-       webkit_web_view_execute_editing_command_with_argument (WEBKIT_WEB_VIEW (editor), "InsertImage", 
image_uri);
+       EWebKitEditor *wk_editor = E_WEBKIT_EDITOR (editor);
+       gint width = -1, height = -1;
+
+       g_return_if_fail (image_uri != NULL);
+
+       if (g_ascii_strncasecmp (image_uri, "file://", 7) == 0) {
+               gchar *filename;
+
+               filename = g_filename_from_uri (image_uri, NULL, NULL);
+
+               if (filename) {
+                       if (!gdk_pixbuf_get_file_info (filename, &width, &height)) {
+                               width = -1;
+                               height = -1;
+                       }
+
+                       g_free (filename);
+               }
+       }
+
+       e_web_view_jsc_run_script (WEBKIT_WEB_VIEW (wk_editor), wk_editor->priv->cancellable,
+               "EvoEditor.InsertImage(%s, %d, %d);",
+               image_uri, width, height);
 }
 
 static void


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