[evolution/wip/mcrha/webkit-jsc-api] Various preparations and fixes



commit a3d143a074873de9016d7aa8835d23eddf3446e4
Author: Milan Crha <mcrha redhat com>
Date:   Thu Nov 21 16:08:43 2019 +0100

    Various preparations and fixes

 data/webkit/e-convert.js                    |   2 +-
 data/webkit/e-editor.js                     | 112 ++++++++++++++++++++-
 src/e-util/e-content-editor.c               | 100 +++++++++----------
 src/e-util/e-content-editor.h               |  33 ++++---
 src/e-util/test-html-editor-units.c         |  24 ++---
 src/e-util/test-html-editor.c               |   3 +
 src/modules/webkit-editor/e-webkit-editor.c | 146 ++++++++++++++++++----------
 7 files changed, 293 insertions(+), 127 deletions(-)
---
diff --git a/data/webkit/e-convert.js b/data/webkit/e-convert.js
index ec21881455..9701f70ae8 100644
--- a/data/webkit/e-convert.js
+++ b/data/webkit/e-convert.js
@@ -437,7 +437,7 @@ EvoConvert.formatParagraph = function(str, ltr, align, indent, whiteSpace, wrapW
                        worker.commit(ii);
                }
        } else {
-               lines[lines.length] = str;
+               lines[lines.length] = str.endsWith("\n") ? str.substr(0, str.length - 1) : str;
        }
 
        var extraIndentStr = extraIndent > 0 ? " ".repeat(extraIndent) : null;
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
index b1a342fa14..55a3b0c2c8 100644
--- a/data/webkit/e-editor.js
+++ b/data/webkit/e-editor.js
@@ -67,6 +67,7 @@ var EvoEditor = {
 
        mode : 1, // one of the MODE constants
        storedSelection : null,
+       inheritThemeColors : false,
        forceFormatStateUpdate : false,
        formattingState : {
                mode : -1,
@@ -1309,10 +1310,82 @@ EvoEditor.SetMode = function(mode)
                } finally {
                        EvoUndoRedo.Enable();
                        EvoUndoRedo.StopRecord(EvoUndoRedo.RECORD_KIND_DOCUMENT, opType);
+
+                       EvoEditor.maybeUpdateFormattingState(EvoEditor.FORCE_YES);
                }
        }
 }
 
+EvoEditor.convertHtmlToSend = function()
+{
+       var html, bgcolor, text, link, vlink;
+       var unsetBgcolor = false, unsetText = false, unsetLink = false, unsetVlink = false;
+       var themeCss, inheritThemeColors = EvoEditor.inheritThemeColors;
+
+       themeCss = EvoEditor.UpdateThemeStyleSheet(null);
+       bgcolor = document.documentElement.getAttribute("x-evo-bgcolor");
+       text = document.documentElement.getAttribute("x-evo-text");
+       link = document.documentElement.getAttribute("x-evo-link");
+       vlink = document.documentElement.getAttribute("x-evo-vlink");
+
+       document.documentElement.removeAttribute("x-evo-bgcolor");
+       document.documentElement.removeAttribute("x-evo-text");
+       document.documentElement.removeAttribute("x-evo-link");
+       document.documentElement.removeAttribute("x-evo-vlink");
+
+       if (inheritThemeColors) {
+               if (bgcolor && !document.body.getAttribute("bgcolor")) {
+                       document.body.setAttribute("bgcolor", bgcolor);
+                       unsetBgcolor = true;
+               }
+
+               if (text && !document.body.getAttribute("text")) {
+                       document.body.setAttribute("text", text);
+                       unsetText = true;
+               }
+
+               if (link && !document.body.getAttribute("link")) {
+                       document.body.setAttribute("link", link);
+                       unsetLink = true;
+               }
+
+               if (vlink && !document.body.getAttribute("vlink")) {
+                       document.body.setAttribute("vlink", vlink);
+                       unsetVlink = true;
+               }
+       }
+
+       html = document.documentElement.outerHTML;
+
+       if (bgcolor)
+               document.documentElement.setAttribute("x-evo-bgcolor", bgcolor);
+       if (text)
+               document.documentElement.setAttribute("x-evo-text", text);
+       if (link)
+               document.documentElement.setAttribute("x-evo-link", link);
+       if (vlink)
+               document.documentElement.setAttribute("x-evo-vlink", vlink);
+
+       if (inheritThemeColors) {
+               if (unsetBgcolor)
+                       document.body.removeAttribute("bgcolor");
+
+               if (unsetText)
+                       document.body.removeAttribute("text");
+
+               if (unsetLink)
+                       document.body.removeAttribute("link");
+
+               if (unsetVlink)
+                       document.body.removeAttribute("vlink");
+       }
+
+       if (themeCss)
+               EvoEditor.UpdateThemeStyleSheet(themeCss);
+
+       return html;
+}
+
 EvoEditor.GetContent = function(flags, cid_uid_prefix)
 {
        var content_data = {}, img_elems = [], elems, ii, jj;
@@ -1372,10 +1445,12 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
 
                        for (ii = 0; ii < document.images.length; ii++) {
                                var elem = document.images.item(ii);
+                               var src = (elem && elem.src) ? elem.src.toLowerCase() : "";
 
-                               if (elem && elem.src && (
-                                   elem.src.toLowerCase().startsWith("data:") ||
-                                   elem.src.toLowerCase().startsWith("file://"))) {
+                               if (elem &&
+                                   src.startsWith("data:") ||
+                                   src.startsWith("file://") ||
+                                   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;
@@ -1401,7 +1476,7 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
                                                };
                                                elem.src = img_obj.cid;
                                        }
-                               } else if (elem && elem.src && elem.src.toLowerCase().startsWith("cid:")) {
+                               } else if (elem && src.startsWith("cid:")) {
                                        images[images.length] = {
                                                cid : elem.src,
                                                src : elem.src
@@ -1447,6 +1522,35 @@ EvoEditor.GetContent = function(flags, cid_uid_prefix)
        return content_data;
 }
 
+EvoEditor.UpdateThemeStyleSheet = function(css)
+{
+       var styles, ii, res = null;
+
+       styles = document.head.getElementsByTagName("style");
+
+       for (ii = 0; ii < styles.length; ii++) {
+               if (styles[ii].id == "x-evo-theme-sheet") {
+                       res = styles[ii].innerHTML;
+
+                       if (css)
+                               styles[ii].innerHTML = css;
+                       else
+                               document.head.removeChild(styles[ii]);
+
+                       return res;
+               }
+       }
+
+       if (css) {
+               styles = document.createElement("STYLE");
+               styles.id = "x-evo-theme-sheet";
+               styles.innerText = css;
+               document.head.append(styles);
+       }
+
+       return res;
+}
+
 document.onload = EvoEditor.initializeContent;
 
 document.onselectionchange = function() {
diff --git a/src/e-util/e-content-editor.c b/src/e-util/e-content-editor.c
index cb01a9aa9b..0aa44acdfa 100644
--- a/src/e-util/e-content-editor.c
+++ b/src/e-util/e-content-editor.c
@@ -37,6 +37,7 @@ enum {
        REPLACE_ALL_DONE,
        DROP_HANDLED,
        CONTENT_CHANGED,
+       REQUEST_RESOURCE,
        LAST_SIGNAL
 };
 
@@ -631,6 +632,26 @@ e_content_editor_default_init (EContentEditorInterface *iface)
                NULL, NULL,
                NULL,
                G_TYPE_NONE, 0);
+
+       /**
+        * EContentEditor:request-resource
+        *
+        * This is used by the content editor, when it wants to get
+        * some resource. The listener to this signal should finish
+        * the call with e_content_editor_resource_loaded().
+        *
+        * Since: 3.36
+        */
+       signals[REQUEST_RESOURCE] = g_signal_new (
+               "request-resource",
+               E_TYPE_CONTENT_EDITOR,
+               G_SIGNAL_RUN_LAST,
+               G_STRUCT_OFFSET (EContentEditorInterface, request_resource),
+               NULL, NULL,
+               NULL,
+               G_TYPE_NONE, 2,
+               G_TYPE_STRING,
+               G_TYPE_CANCELLABLE);
 }
 
 ESpellChecker *
@@ -2119,30 +2140,6 @@ e_content_editor_select_all (EContentEditor *editor)
        iface->select_all (editor);
 }
 
-/**
- * e_content_editor_get_selected_text:
- * @editor: an #EContentEditor
- *
- * Returns currently selected string.
- *
- * Returns: (transfer-full): A newly allocated string with the content of current selection.
- *
- * Since: 3.22
- **/
-gchar *
-e_content_editor_get_selected_text (EContentEditor *editor)
-{
-       EContentEditorInterface *iface;
-
-       g_return_val_if_fail (E_IS_CONTENT_EDITOR (editor), NULL);
-
-       iface = E_CONTENT_EDITOR_GET_IFACE (editor);
-       g_return_val_if_fail (iface != NULL, NULL);
-       g_return_val_if_fail (iface->get_selected_text != NULL, NULL);
-
-       return iface->get_selected_text (editor);
-}
-
 /**
  * e_content_editor_get_caret_word:
  * @editor: an #EContentEditor
@@ -2221,31 +2218,6 @@ e_content_editor_selection_unindent (EContentEditor *editor)
        iface->selection_unindent (editor);
 }
 
-/**
- * e_content_editor_selection_create_link:
- * @editor: an #EContentEditor
- * @uri: destination of the new link
- *
- * Converts current selection into a link pointing to @url.
- *
- * Since: 3.22
- **/
-void
-e_content_editor_selection_create_link (EContentEditor *editor,
-                                        const gchar *uri)
-{
-       EContentEditorInterface *iface;
-
-       g_return_if_fail (E_IS_CONTENT_EDITOR (editor));
-       g_return_if_fail (uri != NULL);
-
-       iface = E_CONTENT_EDITOR_GET_IFACE (editor);
-       g_return_if_fail (iface != NULL);
-       g_return_if_fail (iface->selection_create_link != NULL);
-
-       iface->selection_create_link (editor, uri);
-}
-
 /**
  * e_content_editor_selection_unlink:
  * @editor: an #EContentEditor
@@ -4117,6 +4089,25 @@ e_content_editor_on_find_dialog_close (EContentEditor *editor)
        iface->on_find_dialog_close (editor);
 }
 
+void
+e_content_editor_resource_loaded (EContentEditor *editor,
+                                 const gchar *uri,
+                                 GInputStream *stream,
+                                 gint64 stream_length,
+                                 const gchar *mime_type,
+                                 const GError *error)
+{
+       EContentEditorInterface *iface;
+
+       g_return_if_fail (E_IS_CONTENT_EDITOR (editor));
+
+       iface = E_CONTENT_EDITOR_GET_IFACE (editor);
+       g_return_if_fail (iface != NULL);
+       g_return_if_fail (iface->resource_loaded != NULL);
+
+       iface->resource_loaded (editor, uri, stream, stream_length, mime_type, error);
+}
+
 void
 e_content_editor_emit_load_finished (EContentEditor *editor)
 {
@@ -4196,3 +4187,14 @@ e_content_editor_emit_content_changed (EContentEditor *editor)
 
        g_signal_emit (editor, signals[CONTENT_CHANGED], 0);
 }
+
+void
+e_content_editor_emit_request_resource (EContentEditor *editor,
+                                       const gchar *uri,
+                                       GCancellable *cancellable)
+{
+       g_return_if_fail (E_IS_CONTENT_EDITOR (editor));
+       g_return_if_fail (uri != NULL);
+
+       g_signal_emit (editor, signals[REQUEST_RESOURCE], 0, uri, cancellable);
+}
diff --git a/src/e-util/e-content-editor.h b/src/e-util/e-content-editor.h
index 9b2c971a9a..d96170cffb 100644
--- a/src/e-util/e-content-editor.h
+++ b/src/e-util/e-content-editor.h
@@ -101,8 +101,6 @@ struct _EContentEditorInterface {
        void            (*set_spell_checking_languages) (EContentEditor *editor,
                                                         const gchar **languages);
 
-       gchar *         (*get_selected_text)            (EContentEditor *editor);
-
        gchar *         (*get_caret_word)               (EContentEditor *editor);
 
        void            (*replace_caret_word)           (EContentEditor *editor,
@@ -114,9 +112,6 @@ struct _EContentEditorInterface {
 
        void            (*selection_unindent)           (EContentEditor *editor);
 
-       void            (*selection_create_link)        (EContentEditor *editor,
-                                                        const gchar *uri);
-
        void            (*selection_unlink)             (EContentEditor *editor);
 
        void            (*find)                         (EContentEditor *editor,
@@ -442,6 +437,13 @@ struct _EContentEditorInterface {
 
        void            (*on_find_dialog_close)         (EContentEditor *editor);
 
+       void            (*resource_loaded)              (EContentEditor *editor,
+                                                        const gchar *uri,
+                                                        GInputStream *stream,
+                                                        gint64 stream_length,
+                                                        const gchar *mime_type,
+                                                        const GError *error);
+
        /* Signals */
        void            (*load_finished)                (EContentEditor *editor);
        gboolean        (*paste_clipboard)              (EContentEditor *editor);
@@ -455,6 +457,9 @@ struct _EContentEditorInterface {
                                                         guint replaced_count);
        void            (*drop_handled)                 (EContentEditor *editor);
        void            (*content_changed)              (EContentEditor *editor);
+       void            (*request_resource)             (EContentEditor *editor,
+                                                        const gchar *uri,
+                                                        GCancellable *cancellable);
 };
 
 /* Properties */
@@ -639,9 +644,6 @@ void                e_content_editor_set_spell_checking_languages
 
 void           e_content_editor_select_all     (EContentEditor *editor);
 
-gchar *                e_content_editor_get_selected_text
-                                               (EContentEditor *editor);
-
 gchar *                e_content_editor_get_caret_word (EContentEditor *editor);
 
 void           e_content_editor_replace_caret_word
@@ -654,10 +656,6 @@ void               e_content_editor_selection_indent
 void           e_content_editor_selection_unindent
                                                (EContentEditor *editor);
 
-void           e_content_editor_selection_create_link
-                                               (EContentEditor *editor,
-                                                const gchar *uri);
-
 void           e_content_editor_selection_unlink
                                                (EContentEditor *editor);
 
@@ -1080,6 +1078,13 @@ void             e_content_editor_on_find_dialog_open
 void           e_content_editor_on_find_dialog_close
                                                (EContentEditor *editor);
 
+void           e_content_editor_resource_loaded(EContentEditor *editor,
+                                                const gchar *uri,
+                                                GInputStream *stream,
+                                                gint64 stream_length,
+                                                const gchar *mime_type,
+                                                const GError *error);
+
 /* Signal helpers */
 
 void           e_content_editor_emit_load_finished
@@ -1101,6 +1106,10 @@ void             e_content_editor_emit_drop_handled
                                                (EContentEditor *editor);
 void           e_content_editor_emit_content_changed
                                                (EContentEditor *editor);
+void           e_content_editor_emit_request_resource
+                                               (EContentEditor *editor,
+                                                const gchar *uri,
+                                                GCancellable *cancellable);
 
 G_END_DECLS
 
diff --git a/src/e-util/test-html-editor-units.c b/src/e-util/test-html-editor-units.c
index ead54c464b..7df77430c7 100644
--- a/src/e-util/test-html-editor-units.c
+++ b/src/e-util/test-html-editor-units.c
@@ -47,7 +47,7 @@ test_style_bold_selection (TestFixture *fixture)
                "seq:hCrcrCSrsc\n"
                "action:bold\n",
                HTML_PREFIX "<div>some <b>bold</b> text</div>" HTML_SUFFIX,
-               "some bold text"))
+               "some bold text\n"))
                g_test_fail ();
 }
 
@@ -62,7 +62,7 @@ test_style_bold_typed (TestFixture *fixture)
                "action:bold\n"
                "type: text\n",
                HTML_PREFIX "<div>some <b>bold</b> text</div>" HTML_SUFFIX,
-               "some bold text"))
+               "some bold text\n"))
                g_test_fail ();
 }
 
@@ -75,7 +75,7 @@ test_style_italic_selection (TestFixture *fixture)
                "seq:hCrcrCSrsc\n"
                "action:italic\n",
                HTML_PREFIX "<div>some <i>italic</i> text</div>" HTML_SUFFIX,
-               "some italic text"))
+               "some italic text\n"))
                g_test_fail ();
 }
 
@@ -90,7 +90,7 @@ test_style_italic_typed (TestFixture *fixture)
                "action:italic\n"
                "type: text\n",
                HTML_PREFIX "<div>some <i>italic</i> text</div>" HTML_SUFFIX,
-               "some italic text"))
+               "some italic text\n"))
                g_test_fail ();
 }
 
@@ -103,7 +103,7 @@ test_style_underline_selection (TestFixture *fixture)
                "seq:hCrcrCSrsc\n"
                "action:underline\n",
                HTML_PREFIX "<div>some <u>underline</u> text</div>" HTML_SUFFIX,
-               "some underline text"))
+               "some underline text\n"))
                g_test_fail ();
 }
 
@@ -118,7 +118,7 @@ test_style_underline_typed (TestFixture *fixture)
                "action:underline\n"
                "type: text\n",
                HTML_PREFIX "<div>some <u>underline</u> text</div>" HTML_SUFFIX,
-               "some underline text"))
+               "some underline text\n"))
                g_test_fail ();
 }
 
@@ -131,7 +131,7 @@ test_style_strikethrough_selection (TestFixture *fixture)
                "seq:hCrcrCSrsc\n"
                "action:strikethrough\n",
                HTML_PREFIX "<div>some <strike>strikethrough</strike> text</div>" HTML_SUFFIX,
-               "some strikethrough text"))
+               "some strikethrough text\n"))
                g_test_fail ();
 }
 
@@ -146,7 +146,7 @@ test_style_strikethrough_typed (TestFixture *fixture)
                "action:strikethrough\n"
                "type: text\n",
                HTML_PREFIX "<div>some <strike>strikethrough</strike> text</div>" HTML_SUFFIX,
-               "some strikethrough text"))
+               "some strikethrough text\n"))
                g_test_fail ();
 }
 
@@ -158,8 +158,8 @@ test_style_monospace_selection (TestFixture *fixture)
                "type:some monospace text\n"
                "seq:hCrcrCSrsc\n"
                "action:monospaced\n",
-               HTML_PREFIX "<div>some <font face=\"monospace\" size=\"3\">monospace</font> text</div>" 
HTML_SUFFIX,
-               "some monospace text"))
+               HTML_PREFIX "<div>some <font face=\"monospace\">monospace</font> text</div>" HTML_SUFFIX,
+               "some monospace text\n"))
                g_test_fail ();
 }
 
@@ -173,8 +173,8 @@ test_style_monospace_typed (TestFixture *fixture)
                "type:monospace\n"
                "action:monospaced\n"
                "type: text\n",
-               HTML_PREFIX "<div>some <font face=\"monospace\" size=\"3\">monospace</font> text</div>" 
HTML_SUFFIX,
-               "some monospace text"))
+               HTML_PREFIX "<div>some <font face=\"monospace\">monospace</font> text</div>" HTML_SUFFIX,
+               "some monospace text\n"))
                g_test_fail ();
 }
 
diff --git a/src/e-util/test-html-editor.c b/src/e-util/test-html-editor.c
index 9b7eaaf3a1..8c5218ada0 100644
--- a/src/e-util/test-html-editor.c
+++ b/src/e-util/test-html-editor.c
@@ -180,6 +180,9 @@ view_source_dialog_show (EHTMLEditor *editor,
                buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (content));
                gtk_text_buffer_set_text (buffer, content_text ? content_text : "", -1);
                gtk_text_view_set_editable (GTK_TEXT_VIEW (content), FALSE);
+               gtk_text_view_set_monospace (GTK_TEXT_VIEW (content), TRUE);
+               if (!plain_text)
+                       gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (content), GTK_WRAP_WORD_CHAR);
        } else {
                content = webkit_web_view_new ();
                webkit_web_view_load_html (WEBKIT_WEB_VIEW (content), content_text ? content_text : "", 
"evo-file://");
diff --git a/src/modules/webkit-editor/e-webkit-editor.c b/src/modules/webkit-editor/e-webkit-editor.c
index 670fe30578..2207c3e690 100644
--- a/src/modules/webkit-editor/e-webkit-editor.c
+++ b/src/modules/webkit-editor/e-webkit-editor.c
@@ -90,7 +90,6 @@ struct _EWebKitEditorPrivate {
        gboolean copy_cut_actions_triggered;
        gboolean pasting_primary_clipboard;
        gboolean pasting_from_itself_extension_value;
-       gboolean suppress_color_changes;
 
        guint32 style_flags;
        guint32 temporary_style_flags; /* that's for collapsed selection, format changes only after something 
is typed */
@@ -103,6 +102,11 @@ struct _EWebKitEditorPrivate {
        GdkRGBA *body_link_color;
        GdkRGBA *body_vlink_color;
 
+       GdkRGBA theme_bgcolor;
+       GdkRGBA theme_fgcolor;
+       GdkRGBA theme_link_color;
+       GdkRGBA theme_vlink_color;
+
        gchar *font_name;
 
        guint font_size;
@@ -1359,9 +1363,26 @@ webkit_editor_update_styles (EContentEditor *editor)
 }
 
 static void
-webkit_editor_set_body_color_attribute (EContentEditor *editor,
-                                       const gchar *attr_name,
-                                       const GdkRGBA *value)
+webkit_editor_add_color_style (GString *css,
+                              const gchar *selector,
+                              const gchar *property,
+                              const GdkRGBA *value)
+{
+       g_return_if_fail (css != NULL);
+       g_return_if_fail (selector != NULL);
+       g_return_if_fail (property != NULL);
+
+       if (!value || value->alpha <= 1e-9)
+               return;
+
+       g_string_append_printf (css, "%s { %s : #%06x; }\n", selector, property, e_rgba_to_value (value));
+}
+
+static void
+webkit_editor_set_color_attribute (EContentEditor *editor,
+                                  GString *script, /* serves two purposes, also says whether write to body 
or not */
+                                  const gchar *attr_name,
+                                  const GdkRGBA *value)
 {
        EWebKitEditor *wk_editor = E_WEBKIT_EDITOR (editor);
 
@@ -1370,17 +1391,24 @@ webkit_editor_set_body_color_attribute (EContentEditor *editor,
 
                g_snprintf (color, 63, "#%06x", e_rgba_to_value (value));
 
-               e_web_view_jsc_run_script (WEBKIT_WEB_VIEW (wk_editor), wk_editor->priv->cancellable,
-                       "EvoUndoRedo.Disable();"
-                       "EvoEditor.SetBodyAttribute(%s, %s);"
-                       "EvoUndoRedo.Enable();",
-                       attr_name,
-                       color);
+               if (script) {
+                       e_web_view_jsc_printf_script_gstring (script,
+                               "document.documentElement.setAttribute(%s, %s);\n",
+                               attr_name,
+                               color);
+               } else {
+                       e_web_view_jsc_run_script (WEBKIT_WEB_VIEW (wk_editor), wk_editor->priv->cancellable,
+                               "EvoEditor.SetBodyAttribute(%s, %s);",
+                               attr_name,
+                               color);
+               }
+       } else if (script) {
+               e_web_view_jsc_printf_script_gstring (script,
+                       "document.documentElement.removeAttribute(%s);\n",
+                       attr_name);
        } else {
                e_web_view_jsc_run_script (WEBKIT_WEB_VIEW (wk_editor), wk_editor->priv->cancellable,
-                       "EvoUndoRedo.Disable();"
-                       "EvoEditor.SetBodyAttribute(%s, null);"
-                       "EvoUndoRedo.Enable();",
+                       "EvoEditor.SetBodyAttribute(%s, null);",
                        attr_name);
        }
 }
@@ -1389,7 +1417,7 @@ static void
 webkit_editor_page_set_text_color (EContentEditor *editor,
                                    const GdkRGBA *value)
 {
-       webkit_editor_set_body_color_attribute (editor, "text", value);
+       webkit_editor_set_color_attribute (editor, NULL, "text", value);
 }
 
 static void
@@ -1410,7 +1438,7 @@ static void
 webkit_editor_page_set_background_color (EContentEditor *editor,
                                          const GdkRGBA *value)
 {
-       webkit_editor_set_body_color_attribute (editor, "bgcolor", value);
+       webkit_editor_set_color_attribute (editor, NULL, "bgcolor", value);
 }
 
 static void
@@ -1431,7 +1459,7 @@ static void
 webkit_editor_page_set_link_color (EContentEditor *editor,
                                    const GdkRGBA *value)
 {
-       webkit_editor_set_body_color_attribute (editor, "link", value);
+       webkit_editor_set_color_attribute (editor, NULL, "link", value);
 }
 
 static void
@@ -1455,7 +1483,7 @@ static void
 webkit_editor_page_set_visited_link_color (EContentEditor *editor,
                                            const GdkRGBA *value)
 {
-       webkit_editor_set_body_color_attribute (editor, "vlink", value);
+       webkit_editor_set_color_attribute (editor, NULL, "vlink", value);
 }
 
 static void
@@ -1514,54 +1542,80 @@ get_color_from_context (GtkStyleContext *context,
 static void
 webkit_editor_style_updated_cb (EWebKitEditor *wk_editor)
 {
-       GdkRGBA rgba;
+       EContentEditor *cnt_editor;
+       GdkRGBA bgcolor, fgcolor, link_color, vlink_color;
        GtkStateFlags state_flags;
        GtkStyleContext *style_context;
+       GString *css, *script;
        gboolean backdrop;
+       gboolean inherit_theme_colors;
 
-       /* If the user set the colors in Page dialog, this callback is useless. */
-       if (wk_editor->priv->suppress_color_changes)
-               return;
+       g_return_if_fail (E_IS_WEBKIT_EDITOR (wk_editor));
+
+       cnt_editor = E_CONTENT_EDITOR (wk_editor);
 
+       inherit_theme_colors = g_settings_get_boolean (wk_editor->priv->mail_settings, 
"composer-inherit-theme-colors");
        state_flags = gtk_widget_get_state_flags (GTK_WIDGET (wk_editor));
        style_context = gtk_widget_get_style_context (GTK_WIDGET (wk_editor));
        backdrop = (state_flags & GTK_STATE_FLAG_BACKDROP) != 0;
 
-       if (wk_editor->priv->html_mode && !g_settings_get_boolean (wk_editor->priv->mail_settings, 
"composer-inherit-theme-colors")) {
+       if (wk_editor->priv->html_mode && !inherit_theme_colors) {
                /* Default to white background when not inheriting theme colors */
-               rgba.red = 1.0;
-               rgba.green = 1.0;
-               rgba.blue = 1.0;
-               rgba.alpha = 1.0;
+               bgcolor.red = 1.0;
+               bgcolor.green = 1.0;
+               bgcolor.blue = 1.0;
+               bgcolor.alpha = 1.0;
        } else if (!gtk_style_context_lookup_color (
                        style_context,
                        backdrop ? "theme_unfocused_base_color" : "theme_base_color",
-                       &rgba)) {
-               gdk_rgba_parse (&rgba, E_UTILS_DEFAULT_THEME_BASE_COLOR);
+                       &bgcolor)) {
+               gdk_rgba_parse (&bgcolor, E_UTILS_DEFAULT_THEME_BASE_COLOR);
        }
 
-       webkit_editor_page_set_background_color (E_CONTENT_EDITOR (wk_editor), &rgba);
-
-       if (wk_editor->priv->html_mode && !g_settings_get_boolean (wk_editor->priv->mail_settings, 
"composer-inherit-theme-colors")) {
+       if (wk_editor->priv->html_mode && !inherit_theme_colors) {
                /* Default to black text color when not inheriting theme colors */
-               rgba.red = 0.0;
-               rgba.green = 0.0;
-               rgba.blue = 0.0;
-               rgba.alpha = 1.0;
+               fgcolor.red = 0.0;
+               fgcolor.green = 0.0;
+               fgcolor.blue = 0.0;
+               fgcolor.alpha = 1.0;
        } else if (!gtk_style_context_lookup_color (
                        style_context,
                        backdrop ? "theme_unfocused_fg_color" : "theme_fg_color",
-                       &rgba)) {
-               gdk_rgba_parse (&rgba, E_UTILS_DEFAULT_THEME_FG_COLOR);
+                       &fgcolor)) {
+               gdk_rgba_parse (&fgcolor, E_UTILS_DEFAULT_THEME_FG_COLOR);
        }
 
-       webkit_editor_page_set_text_color (E_CONTENT_EDITOR (wk_editor), &rgba);
+       get_color_from_context (style_context, "link-color", &link_color);
+       get_color_from_context (style_context, "visited-link-color", &vlink_color);
+
+       if (gdk_rgba_equal (&bgcolor, &wk_editor->priv->theme_bgcolor) &&
+           gdk_rgba_equal (&fgcolor, &wk_editor->priv->theme_fgcolor) &&
+           gdk_rgba_equal (&link_color, &wk_editor->priv->theme_link_color) &&
+           gdk_rgba_equal (&vlink_color, &wk_editor->priv->theme_vlink_color))
+               return;
+
+       css = g_string_sized_new (160);
+       script = g_string_sized_new (256);
 
-       get_color_from_context (style_context, "link-color", &rgba);
-       webkit_editor_page_set_link_color (E_CONTENT_EDITOR (wk_editor), &rgba);
+       webkit_editor_set_color_attribute (cnt_editor, script, "x-evo-bgcolor", &bgcolor);
+       webkit_editor_set_color_attribute (cnt_editor, script, "x-evo-text", &fgcolor);
+       webkit_editor_set_color_attribute (cnt_editor, script, "x-evo-link", &link_color);
+       webkit_editor_set_color_attribute (cnt_editor, script, "x-evo-vlink", &vlink_color);
 
-       get_color_from_context (style_context, "visited-link-color", &rgba);
-       webkit_editor_page_set_visited_link_color (E_CONTENT_EDITOR (wk_editor), &rgba);
+       webkit_editor_add_color_style (css, "html", "background-color", &bgcolor);
+       webkit_editor_add_color_style (css, "html", "color", &fgcolor);
+       webkit_editor_add_color_style (css, "a", "color", &link_color);
+       webkit_editor_add_color_style (css, "html", "a:visited", &vlink_color);
+
+       e_web_view_jsc_printf_script_gstring (script,
+               "EvoEditor.UpdateThemeStyleSheet(%s);",
+               css->str);
+
+       e_web_view_jsc_run_script_take (WEBKIT_WEB_VIEW (wk_editor),
+               g_string_free (script, FALSE),
+               wk_editor->priv->cancellable);
+
+       g_string_free (css, TRUE);
 }
 
 static gboolean
@@ -1905,7 +1959,7 @@ webkit_editor_get_content_finish (EContentEditor *editor,
 
                                        if (image_parts)
                                                e_content_editor_util_take_content_data_images (content_hash, 
image_parts);
-                               } else {
+                               } else if (!jsc_value_is_undefined (images_value) && !jsc_value_is_null 
(images_value)) {
                                        g_warn_if_reached ();
                                }
 
@@ -2251,9 +2305,6 @@ webkit_editor_set_spell_check_enabled (EWebKitEditor *wk_editor,
 
        wk_editor->priv->spell_check_enabled = enable;
 
-       webkit_editor_call_simple_extension_function (
-               wk_editor, enable ? "DOMForceSpellCheck" : "DOMTurnSpellCheckOff");
-
        web_context = webkit_web_view_get_context (WEBKIT_WEB_VIEW (wk_editor));
        webkit_web_context_set_spell_checking_enabled (web_context, enable);
 
@@ -6457,7 +6508,6 @@ e_webkit_editor_init (EWebKitEditor *wk_editor)
        wk_editor->priv->pasting_primary_clipboard = FALSE;
        wk_editor->priv->pasting_from_itself_extension_value = FALSE;
        wk_editor->priv->current_user_stylesheet = NULL;
-       wk_editor->priv->suppress_color_changes = FALSE;
 
        wk_editor->priv->font_color = NULL;
        wk_editor->priv->background_color = NULL;
@@ -6492,13 +6542,11 @@ e_webkit_editor_content_editor_init (EContentEditorInterface *iface)
        iface->redo = webkit_editor_redo;
        iface->clear_undo_redo_history = webkit_editor_clear_undo_redo_history;
        iface->set_spell_checking_languages = webkit_editor_set_spell_checking_languages;
-       /* FIXME WK2 iface->get_selected_text = webkit_editor_get_selected_text; */
        iface->get_caret_word = webkit_editor_get_caret_word;
        iface->replace_caret_word = webkit_editor_replace_caret_word;
        iface->select_all = webkit_editor_select_all;
        iface->selection_indent = webkit_editor_selection_indent;
        iface->selection_unindent = webkit_editor_selection_unindent;
-       /* FIXME WK2 iface->create_link = webkit_editor_create_link; */
        iface->selection_unlink = webkit_editor_selection_unlink;
        iface->find = webkit_editor_find;
        iface->replace = webkit_editor_replace;


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