[evolution/wip/mcrha/webkit-jsc-api: 262/292] Be more strict with emoticon conversion to plain text



commit 79c5fbbb151e577394afe503dcca40fcd34df650
Author: Milan Crha <mcrha redhat com>
Date:   Fri Jan 17 08:42:53 2020 +0100

    Be more strict with emoticon conversion to plain text

 data/webkit/e-convert.js                    | 51 ++++++++++++++++++++++++++---
 data/webkit/e-editor.js                     | 27 +++++++++------
 src/e-util/test-web-view-jsc.c              |  4 +--
 src/modules/webkit-editor/e-webkit-editor.c | 28 +++++-----------
 4 files changed, 75 insertions(+), 35 deletions(-)
---
diff --git a/data/webkit/e-convert.js b/data/webkit/e-convert.js
index d5a39d32f1..2ee96ebe86 100644
--- a/data/webkit/e-convert.js
+++ b/data/webkit/e-convert.js
@@ -571,6 +571,47 @@ EvoConvert.formatParagraph = function(str, ltr, align, indent, whiteSpace, wrapW
        return str;
 }
 
+EvoConvert.ImgToText = function(img)
+{
+       if (!img)
+               return "";
+
+       var txt;
+
+       txt = img.alt;
+
+       if (!txt) {
+               txt = img.src;
+
+               if (txt.startsWith("cid:"))
+                       txt = "";
+       }
+
+       return txt;
+}
+
+EvoConvert.extractElemText = function(elem, normalDivWidth)
+{
+       if (!elem)
+               return "";
+
+       if (!elem.childNodes.length)
+               return elem.innerText;
+
+       var str = "", ii;
+
+       for (ii = 0; ii < elem.childNodes.length; ii++) {
+               var node = elem.childNodes.item(ii);
+
+               if (!node)
+                       continue;
+
+               str += EvoConvert.processNode(node, normalDivWidth);
+       }
+
+       return str;
+}
+
 EvoConvert.processNode = function(node, normalDivWidth)
 {
        var str = "";
@@ -631,7 +672,7 @@ EvoConvert.processNode = function(node, normalDivWidth)
 
                whiteSpace = style ? style.whiteSpace.toLowerCase() : "";
 
-               if (node.tagName == "DIV") {
+               if (node.tagName == "DIV" || node.tagName == "P") {
                        var liText, extraIndent, width;
 
                        liText = node.getAttribute("x-evo-li-text");
@@ -653,13 +694,15 @@ EvoConvert.processNode = function(node, normalDivWidth)
                                width = normalDivWidth;
                        }
 
-                       str = EvoConvert.formatParagraph(node.innerText, ltr, align, indent, whiteSpace, 
width, extraIndent, liText);
+                       str = EvoConvert.formatParagraph(EvoConvert.extractElemText(node, normalDivWidth), 
ltr, align, indent, whiteSpace, width, extraIndent, liText);
                } else if (node.tagName == "PRE") {
-                       str = EvoConvert.formatParagraph(node.innerText, ltr, align, indent, "pre", -1, 0, 
"");
+                       str = EvoConvert.formatParagraph(EvoConvert.extractElemText(node, normalDivWidth), 
ltr, align, indent, "pre", -1, 0, "");
                } else if (node.tagName == "BR") {
                        str = "\n";
+               } else if (node.tagName == "IMG") {
+                       str = EvoConvert.ImgToText(node);
                } else {
-                       str = node.innerText;
+                       str = EvoConvert.extractElemText(node, normalDivWidth);
 
                        if (str != "\n" && ((style && style.display == "block") || node.tagName == 
"ADDRESS")) {
                                str += "\n";
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index cc05bb5fb8..00968426d4 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -1727,6 +1727,17 @@ EvoEditor.SetNormalParagraphWidth = function(value)
        }
 }
 
+EvoEditor.convertImages = function()
+{
+       var ii;
+
+       for (ii = document.images.length - 1; ii >= 0; ii--) {
+               var img = document.images[ii];
+
+               img.outerText = EvoConvert.ImgToText(img);
+       }
+}
+
 EvoEditor.SetMode = function(mode)
 {
        if (EvoEditor.mode != mode) {
@@ -1751,6 +1762,7 @@ EvoEditor.SetMode = function(mode)
                        EvoEditor.mode = mode;
 
                        if (mode == EvoEditor.MODE_PLAIN_TEXT) {
+                               EvoEditor.convertImages();
                                EvoEditor.convertParagraphs(document.body, EvoEditor.NORMAL_PARAGRAPH_WIDTH);
                        } else {
                                EvoEditor.convertParagraphs(document.body, -1);
@@ -2060,7 +2072,7 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
                                    src.startsWith("evo-file://")) {
                                        for (jj = 0; jj < img_elems.length; jj++) {
                                                if (elem.src == img_elems[jj].orig_src) {
-                                                       elem.subelems[elem.subelems.length] = elem;
+                                                       img_elems[jj].subelems[img_elems[jj].subelems.length] 
= elem;
                                                        elem.src = img_elems[jj].cid;
                                                        break;
                                                }
@@ -2104,7 +2116,7 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
 
                                        for (jj = 0; jj < bkg_elems.length; jj++) {
                                                if (bkg == bkg_elems[jj].orig_src) {
-                                                       elem.subelems[elem.subelems.length] = elem;
+                                                       bkg_elems[jj].subelems[bkg_elems[jj].subelems.length] 
= elem;
                                                        elem.setAttribute("background", bkg_elems[jj].cid);
                                                        break;
                                                }
@@ -3426,18 +3438,13 @@ EvoEditor.MoveSelectionToPoint = function(xx, yy, cancel_if_not_collapsed)
        }
 }
 
-EvoEditor.InsertEmoticon = function(text, imageUri)
+EvoEditor.InsertEmoticon = function(text, imageUri, width, height)
 {
        if (imageUri) {
                EvoEditor.InsertHTML("InsertEmoticon",
-                       "<span class=\"-x-evo-smiley-wrapper\">" +
-                       "<img class=\"-x-evo-smiley-img\" src=\"" + imageUri + "\" alt=\"" +
+                       "<img src=\"" + imageUri + "\" alt=\"" +
                                text.replace(/\&/g, "&amp;").replace(/\"/g, "&quot;").replace(/\'/g, 
"&apos;") +
-                       "\">" +
-                       "<span class=\"-x-evo-smiley-text\">" +
-                               text.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") +
-                       "</span>" +
-                       "</span>&#x200b;");
+                       "\" width=\"" + width + "px\" height=\"" + height + "px\">");
        } else {
                EvoEditor.InsertHTML("InsertEmoticon", text);
        }
diff --git a/src/e-util/test-web-view-jsc.c b/src/e-util/test-web-view-jsc.c
index f9ca7c09c8..28a75630b8 100644
--- a/src/e-util/test-web-view-jsc.c
+++ b/src/e-util/test-web-view-jsc.c
@@ -1618,7 +1618,7 @@ test_get_content (TestFixture *fixture)
        test_get_document_content_verify (fixture, "", NULL, expect_html);
        test_get_document_content_verify (fixture, "", expect_plain, expect_html);
 
-       expect_plain = "frm1 div";
+       expect_plain = "frm1 div\n";
        expect_html = html_frm1;
        test_get_document_content_verify (fixture, "frm1", expect_plain, NULL);
        test_get_document_content_verify (fixture, "frm1", NULL, expect_html);
@@ -1662,7 +1662,7 @@ test_get_content (TestFixture *fixture)
        test_get_element_content_verify (fixture, "", "*body", TRUE, NULL, expect_html);
        test_get_element_content_verify (fixture, "", "*body", TRUE, expect_plain, expect_html);
 
-       expect_plain = "frm1 div";
+       expect_plain = "frm1 div\n";
        expect_html ="<span id=\"frm1p\"><div id=\"frst\">frm1 div</div></span>";
        test_get_element_content_verify (fixture, "frm1", "*body", FALSE, expect_plain, NULL);
        test_get_element_content_verify (fixture, "frm1", "*body", FALSE, NULL, expect_html);
diff --git a/src/modules/webkit-editor/e-webkit-editor.c b/src/modules/webkit-editor/e-webkit-editor.c
index a04c019f21..fb738ac8c8 100644
--- a/src/modules/webkit-editor/e-webkit-editor.c
+++ b/src/modules/webkit-editor/e-webkit-editor.c
@@ -1376,22 +1376,6 @@ webkit_editor_update_styles (EContentEditor *editor)
                "  font-family: Monospace; \n"
                "}\n");
 
-       if (wk_editor->priv->html_mode) {
-               g_string_append (
-                       stylesheet,
-                       "span.-x-evo-smiley-text"
-                       "{\n"
-                       "  display: none \n"
-                       "}\n");
-       } else {
-               g_string_append (
-                       stylesheet,
-                       "img.-x-evo-smiley-img"
-                       "{\n"
-                       "  display: none \n"
-                       "}\n");
-       }
-
        g_string_append (
                stylesheet,
                "[data-evo-paragraph] "
@@ -2413,9 +2397,10 @@ webkit_editor_insert_emoticon (EContentEditor *editor,
        GSettings *settings;
        const gchar *text;
        gchar *image_uri = NULL;
+       gint width = 0, height = 0;
 
        g_return_if_fail (E_IS_WEBKIT_EDITOR (editor));
-       g_return_if_fail  (emoticon != NULL);
+       g_return_if_fail (emoticon != NULL);
 
        settings = e_util_ref_settings ("org.gnome.evolution.mail");
 
@@ -2424,13 +2409,18 @@ webkit_editor_insert_emoticon (EContentEditor *editor,
        } else {
                text = emoticon->text_face;
                image_uri = e_emoticon_get_uri (emoticon);
+
+               if (image_uri) {
+                       width = 16;
+                       height = 16;
+               }
        }
 
        wk_editor = E_WEBKIT_EDITOR (editor);
 
        e_web_view_jsc_run_script (WEBKIT_WEB_VIEW (wk_editor), wk_editor->priv->cancellable,
-               "EvoEditor.InsertEmoticon(%s, %s);",
-               text, image_uri);
+               "EvoEditor.InsertEmoticon(%s, %s, %d, %d);",
+               text, image_uri, width, height);
 
        g_clear_object (&settings);
        g_free (image_uri);


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