[evolution/wip/mcrha/webkit-jsc-api] webkit-editor: Fix send of HTML messages with inline images



commit 25e4c1ae2bd6d374a6bef934a6f51846bf186213
Author: Milan Crha <mcrha redhat com>
Date:   Thu Apr 23 10:16:23 2020 +0200

    webkit-editor: Fix send of HTML messages with inline images

 data/webkit/e-editor.js                     | 38 ++++++++++++++++++++++++++++-
 src/e-util/e-content-editor.c               | 13 ++++++----
 src/modules/webkit-editor/e-webkit-editor.c |  8 +++---
 3 files changed, 50 insertions(+), 9 deletions(-)
---
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index b749f35a52..e6cc025252 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -2471,11 +2471,12 @@ EvoEditor.convertHtmlToSend = function()
 
 EvoEditor.GetContent = function(flags, cid_uid_prefix)
 {
-       var content_data = {}, img_elems = [], bkg_elems = [], elems, ii, jj, currentElemsArray = null;
+       var content_data = {};
 
        if (!document.body)
                return content_data;
 
+       var img_elems = [], data_names = [], bkg_elems = [], elems, ii, jj, currentElemsArray = null;
        var scrollX = window.scrollX, scrollY = window.scrollY;
 
        EvoUndoRedo.Disable();
@@ -2568,6 +2569,9 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
                                                        src : elem.src
                                                };
                                                elem.src = img_obj.cid;
+
+                                               if (elem.hasAttribute("data-name"))
+                                                       images[images.length - 1].name = 
elem.getAttribute("data-name");
                                        }
                                } else if (elem && src.startsWith("cid:")) {
                                        images[images.length] = {
@@ -2575,6 +2579,20 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
                                                src : elem.src
                                        };
                                }
+
+                               if (elem) {
+                                       // just remove the attribute used by the old editor
+                                       elem.removeAttribute("data-inline");
+
+                                       if (elem.hasAttribute("data-name")) {
+                                               data_names[data_names.length] = {
+                                                       elem : elem,
+                                                       name : elem.getAttribute("data-name")
+                                               };
+
+                                               elem.removeAttribute("data-name");
+                                       }
+                               }
                        }
 
                        var backgrounds = document.querySelectorAll("[background]");
@@ -2613,6 +2631,9 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
                                                        src : elem.getAttribute("background")
                                                };
                                                elem.setAttribute("background", bkg_obj.cid);
+
+                                               if (elem.hasAttribute("data-name"))
+                                                       images[images.length - 1].name = 
elem.getAttribute("data-name");
                                        }
                                } else if (elem && src.startsWith("cid:")) {
                                        images[images.length] = {
@@ -2620,6 +2641,17 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
                                                src : elem.getAttribute("background")
                                        };
                                }
+
+                               if (elem) {
+                                       if (elem.hasAttribute("data-name")) {
+                                               data_names[data_names.length] = {
+                                                       elem : elem,
+                                                       name : elem.getAttribute("data-name")
+                                               };
+
+                                               elem.removeAttribute("data-name");
+                                       }
+                               }
                        }
 
                        if (images.length)
@@ -2652,6 +2684,10 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
                                }
                        }
 
+                       for (ii = 0; ii < data_names.length; ii++) {
+                               data_names[ii].elem.setAttribute("data-name", data_names[ii].name);
+                       }
+
                        for (ii = 0; ii < bkg_elems.length; ii++) {
                                var bkg_obj = bkg_elems[ii];
 
diff --git a/src/e-util/e-content-editor.c b/src/e-util/e-content-editor.c
index fce296fd1e..9f69638c39 100644
--- a/src/e-util/e-content-editor.c
+++ b/src/e-util/e-content-editor.c
@@ -1819,15 +1819,13 @@ e_content_editor_util_create_data_mimepart (const gchar *uri,
 
                file = g_file_new_for_uri (uri);
                file_stream = g_file_read (file, NULL, NULL);
-               g_clear_object (&file);
 
                if (file_stream) {
                        if (!prefer_filename) {
-                               file_info = g_file_input_stream_query_info (file_stream, 
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, cancellable, NULL);
+                               file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 
G_FILE_QUERY_INFO_NONE, cancellable, NULL);
 
-                               if (file_info) {
+                               if (file_info)
                                        prefer_filename = g_file_info_get_display_name (file_info);
-                               }
                        }
 
                        if (!prefer_mime_type)
@@ -1835,6 +1833,8 @@ e_content_editor_util_create_data_mimepart (const gchar *uri,
 
                        input_stream = (GInputStream *) file_stream;
                }
+
+               g_clear_object (&file);
        }
 
        if (data || input_stream) {
@@ -1865,7 +1865,10 @@ e_content_editor_util_create_data_mimepart (const gchar *uri,
                if (mime_part) {
                        camel_mime_part_set_disposition (mime_part, as_inline ? "inline" : "attachment");
 
-                       if (cid)
+                       if (cid && g_ascii_strncasecmp (cid, "cid:", 4) == 0)
+                               cid += 4;
+
+                       if (cid && *cid)
                                camel_mime_part_set_content_id (mime_part, cid);
 
                        if (prefer_filename && *prefer_filename)
diff --git a/src/modules/webkit-editor/e-webkit-editor.c b/src/modules/webkit-editor/e-webkit-editor.c
index ecc63c17da..885b0f42c1 100644
--- a/src/modules/webkit-editor/e-webkit-editor.c
+++ b/src/modules/webkit-editor/e-webkit-editor.c
@@ -2202,10 +2202,11 @@ webkit_editor_get_content_finish (EContentEditor *editor,
                                                }
 
                                                if (jsc_value_is_object (item_value)) {
-                                                       gchar *src, *cid;
+                                                       gchar *src, *cid, *name;
 
                                                        src = e_web_view_jsc_get_object_property_string 
(item_value, "src", NULL);
                                                        cid = e_web_view_jsc_get_object_property_string 
(item_value, "cid", NULL);
+                                                       name = e_web_view_jsc_get_object_property_string 
(item_value, "name", NULL);
 
                                                        if (src && *src && cid && *cid) {
                                                                CamelMimePart *part = NULL;
@@ -2214,7 +2215,7 @@ webkit_editor_get_content_finish (EContentEditor *editor,
                                                                        part = 
e_content_editor_emit_ref_mime_part (editor, src);
 
                                                                if (!part) {
-                                                                       part = 
e_content_editor_util_create_data_mimepart (src, cid, TRUE, NULL, NULL,
+                                                                       part = 
e_content_editor_util_create_data_mimepart (src, cid, TRUE, name, NULL,
                                                                                E_WEBKIT_EDITOR 
(editor)->priv->cancellable);
                                                                }
 
@@ -2222,6 +2223,7 @@ webkit_editor_get_content_finish (EContentEditor *editor,
                                                                        image_parts = g_slist_prepend 
(image_parts, part);
                                                        }
 
+                                                       g_free (name);
                                                        g_free (src);
                                                        g_free (cid);
                                                }
@@ -2230,7 +2232,7 @@ webkit_editor_get_content_finish (EContentEditor *editor,
                                        }
 
                                        if (image_parts)
-                                               e_content_editor_util_take_content_data_images (content_hash, 
image_parts);
+                                               e_content_editor_util_take_content_data_images (content_hash, 
g_slist_reverse (image_parts));
                                } else if (!jsc_value_is_undefined (images_value) && !jsc_value_is_null 
(images_value)) {
                                        g_warn_if_reached ();
                                }


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