[evolution/wip/webkit2] EHTMLEditorView - Setting the link color doesn't work



commit 03f32e1e8986e31b8f8506e8f46cb650d81351da
Author: Tomas Popela <tpopela redhat com>
Date:   Mon Mar 30 11:32:28 2015 +0200

    EHTMLEditorView - Setting the link color doesn't work
    
    There were two issues. First one was that we were setting the link
    color to the user style and it override the value that we later
    set to link attribute on BODY element in Page dialog. The second issue
    was that the if we changed the link attribute value, the color of existing
    anchors in view sometimes didn't changed at all (probably because the
    body is contenteditable). As a solution we are now changing the color
    of links through the CSS style sheeet in HEAD as well.

 e-util/e-html-editor-view.c                       |   48 ++++++++++++++------
 web-extensions/e-html-editor-view-dom-functions.c |   31 +++++++++++++
 web-extensions/e-html-editor-view-dom-functions.h |    3 +
 web-extensions/e-html-editor-web-extension.c      |   11 +++-
 4 files changed, 75 insertions(+), 18 deletions(-)
---
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 14f14df..805d411 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -2360,7 +2360,6 @@ void
 e_html_editor_view_update_fonts (EHTMLEditorView *view)
 {
        gboolean mark_citations, use_custom_font;
-       GdkColor *link = NULL;
        GdkColor *visited = NULL;
        gchar *font, *aa = NULL, *citation_color;
        const gchar *styles[] = { "normal", "oblique", "italic" };
@@ -2456,15 +2455,7 @@ e_html_editor_view_update_fonts (EHTMLEditorView *view)
 
        context = gtk_widget_get_style_context (GTK_WIDGET (view));
        gtk_style_context_get_style (
-               context,
-               "link-color", &link,
-               "visited-link-color", &visited,
-               NULL);
-
-       if (link == NULL) {
-               link = g_slice_new0 (GdkColor);
-               link->blue = G_MAXINT16;
-       }
+               context, "visited-link-color", &visited, NULL);
 
        if (visited == NULL) {
                visited = g_slice_new0 (GdkColor);
@@ -2473,13 +2464,9 @@ e_html_editor_view_update_fonts (EHTMLEditorView *view)
 
        g_string_append_printf (
                stylesheet,
-               "a {\n"
-               "  color: #%06x;\n"
-               "}\n"
                "a:visited {\n"
                "  color: #%06x;\n"
                "}\n",
-               e_color_to_value (link),
                e_color_to_value (visited));
 
        /* See bug #689777 for details */
@@ -2729,7 +2716,6 @@ e_html_editor_view_update_fonts (EHTMLEditorView *view)
                "}\n",
                e_web_view_get_citation_color_for_level (5));
 
-       gdk_color_free (link);
        gdk_color_free (visited);
 
        settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (view));
@@ -3294,3 +3280,35 @@ e_html_editor_view_save_selection (EHTMLEditorView *view)
 {
        e_html_editor_view_call_simple_extension_function (view, "DOMSaveSelection");
 }
+
+static void
+set_link_color (EHTMLEditorView *view)
+{
+       GdkColor *color = NULL;
+       GdkRGBA rgba;
+       GtkStyleContext *context;
+
+       context = gtk_widget_get_style_context (GTK_WIDGET (view));
+       gtk_style_context_get_style (
+               context, "link-color", &color, NULL);
+
+       if (color == NULL) {
+               rgba.alpha = 1;
+               rgba.red = 0;
+               rgba.green = 0;
+               rgba.blue = 1;
+       } else {
+               rgba.alpha = 1;
+               rgba.red = ((gdouble) color->red) / G_MAXUINT16;
+               rgba.green = ((gdouble) color->green) / G_MAXUINT16;
+               rgba.blue = ((gdouble) color->blue) / G_MAXUINT16;
+       }
+
+       /* FIXME WK2
+        * This set_link_color needs to be called when the document is loaded
+        * (so we will probably emit the signal from WebProcess to Evo when this
+        * happens).
+       e_html_editor_view_set_link_color (view, &rgba); */
+
+       gdk_color_free (color);
+}
diff --git a/web-extensions/e-html-editor-view-dom-functions.c 
b/web-extensions/e-html-editor-view-dom-functions.c
index db7e8a8..c48dcb5 100644
--- a/web-extensions/e-html-editor-view-dom-functions.c
+++ b/web-extensions/e-html-editor-view-dom-functions.c
@@ -5853,6 +5853,10 @@ dom_process_content_for_html (WebKitDOMDocument *document,
                WEBKIT_DOM_ELEMENT (document_clone), "style#-x-evo-quote-style", NULL));
        if (node)
                remove_node (node);
+       node = WEBKIT_DOM_NODE (webkit_dom_element_query_selector (
+               WEBKIT_DOM_ELEMENT (document_clone), "style#-x-evo-a-color-style", NULL));
+       if (node)
+               remove_node (node);
        /* When the Ctrl + Enter is pressed for sending, the links are activated. */
        node = WEBKIT_DOM_NODE (webkit_dom_element_query_selector (
                WEBKIT_DOM_ELEMENT (document_clone), "style#-x-evo-style-a", NULL));
@@ -6777,3 +6781,30 @@ dom_drag_and_drop_end (WebKitDOMDocument *document,
 
        dom_force_spell_check (document, extension);
 }
+
+void
+dom_set_link_color (WebKitDOMDocument *document,
+                    const gchar *color)
+{
+       gchar *color_str = NULL;
+       WebKitDOMHTMLHeadElement *head;
+       WebKitDOMElement *style_element;
+
+       g_return_if_fail (color != NULL);
+
+       head = webkit_dom_document_get_head (document);
+
+       style_element = webkit_dom_document_get_element_by_id (document, "-x-evo-a-color-style");
+       if (!style_element) {
+               style_element = webkit_dom_document_create_element (document, "style", NULL);
+               webkit_dom_element_set_id (style_element, "-x-evo-a-color-style");
+               webkit_dom_node_append_child (
+                       WEBKIT_DOM_NODE (head), WEBKIT_DOM_NODE (style_element), NULL);
+       }
+
+       color_str = g_strdup_printf ("a { color: #%06x; }", color);
+       webkit_dom_html_element_set_inner_html (
+               WEBKIT_DOM_HTML_ELEMENT (style_element), color_str, NULL);
+
+       g_free (color_str);
+}
diff --git a/web-extensions/e-html-editor-view-dom-functions.h 
b/web-extensions/e-html-editor-view-dom-functions.h
index 84dfc9c..1c44069 100644
--- a/web-extensions/e-html-editor-view-dom-functions.h
+++ b/web-extensions/e-html-editor-view-dom-functions.h
@@ -108,6 +108,9 @@ gint                dom_get_caret_position          (WebKitDOMDocument *document);
 void           dom_drag_and_drop_end           (WebKitDOMDocument *document,
                                                 EHTMLEditorWebExtension *extension);
 
+void           dom_set_link_color              (WebKitDOMDocument *document,
+                                                const gchar *color);
+
 G_END_DECLS
 
 #endif /* E_HTML_EDITOR_VIEW_DOM_FUNCTIONS_H */
diff --git a/web-extensions/e-html-editor-web-extension.c b/web-extensions/e-html-editor-web-extension.c
index af4359a..c5ff856 100644
--- a/web-extensions/e-html-editor-web-extension.c
+++ b/web-extensions/e-html-editor-web-extension.c
@@ -674,9 +674,14 @@ handle_method_call (GDBusConnection *connection,
 
                document = webkit_web_page_get_dom_document (web_page);
                element = webkit_dom_document_query_selector (document, selector, NULL);
-               if (element)
-                       webkit_dom_element_set_attribute (
-                               element, attribute, value, NULL);
+               if (element) {
+                       if (g_strcmp0 (selector, "body") == 0 &&
+                           g_strcmp0 (attribute, "link") == 0)
+                               dom_set_link_color (document, value);
+                       else
+                               webkit_dom_element_set_attribute (
+                                       element, attribute, value, NULL);
+               }
 
                g_dbus_method_invocation_return_value (invocation, NULL);
        } else if (g_strcmp0 (method_name, "ElementGetTagName") == 0) {


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