[evolution] Some HTML signatures lost line breaks when inserted to the composer



commit 7ec735f3334f17584f90e38241705f684280d156
Author: Tomas Popela <tpopela redhat com>
Date:   Fri Mar 11 12:34:54 2016 +0100

    Some HTML signatures lost line breaks when inserted to the composer
    
    Usually this applies for HTML signatures that were created in the GtkHTML based
    editor.
    
    Also don't try to convert the signatures that were created after the signature
    editor changed the way how they are saved.

 composer/e-composer-private.c |   58 +++++++++++++++++++++++++---------------
 e-util/e-html-editor-view.c   |   21 ++++++++++++++-
 2 files changed, 56 insertions(+), 23 deletions(-)
---
diff --git a/composer/e-composer-private.c b/composer/e-composer-private.c
index 36f9b81..2f2cea0 100644
--- a/composer/e-composer-private.c
+++ b/composer/e-composer-private.c
@@ -1068,6 +1068,7 @@ composer_load_signature_cb (EMailSignatureComboBox *combo_box,
        WebKitDOMElement *signature_to_insert;
        WebKitDOMElement *insert_signature_in = NULL;
        WebKitDOMElement *signature_wrapper;
+       WebKitDOMElement *element, *converted_signature = NULL;
        WebKitDOMHTMLElement *body;
        WebKitDOMNodeList *signatures;
 
@@ -1111,26 +1112,6 @@ composer_load_signature_cb (EMailSignatureComboBox *combo_box,
        if (!contents)
                goto insert;
 
-       /* If inserting HTML signature in plain text composer we have to convert it. */
-       html_mode = e_html_editor_view_get_html_mode (view);
-       if (is_html && !html_mode) {
-               gchar *inner_text;
-               WebKitDOMNode *child;
-
-               webkit_dom_html_element_set_inner_html (
-                       WEBKIT_DOM_HTML_ELEMENT (insert_signature_in), contents, NULL);
-               e_html_editor_view_convert_element_from_html_to_plain_text (
-                       view, insert_signature_in);
-               inner_text = webkit_dom_html_element_get_inner_text (
-                       WEBKIT_DOM_HTML_ELEMENT (insert_signature_in));
-               while ((child = webkit_dom_node_get_last_child (WEBKIT_DOM_NODE (insert_signature_in))))
-                       remove_node (child);
-
-               g_free (contents);
-               contents = inner_text ? g_strstrip (inner_text) : g_strdup ("");
-               is_html = FALSE;
-       }
-
        if (!is_html) {
                gchar *html;
 
@@ -1149,6 +1130,25 @@ composer_load_signature_cb (EMailSignatureComboBox *combo_box,
                        NULL);
        }
 
+       /* If inserting HTML signature in plain text composer we have to convert it. */
+       html_mode = e_html_editor_view_get_html_mode (view);
+       if (is_html && !html_mode && !strstr (contents, "data-evo-signature-plain-text-mode")) {
+               gchar *inner_text;
+
+               /* Save the converted signature to avoid parsing it later again
+                * while inserting it into the view. */
+               converted_signature = webkit_dom_document_create_element (document, "pre", NULL);
+               webkit_dom_html_element_set_inner_html (
+                       WEBKIT_DOM_HTML_ELEMENT (converted_signature), contents, NULL);
+               e_html_editor_view_convert_element_from_html_to_plain_text (view, converted_signature);
+               inner_text = webkit_dom_html_element_get_inner_text (WEBKIT_DOM_HTML_ELEMENT 
(converted_signature));
+
+               g_free (contents);
+               contents = inner_text ? g_strstrip (inner_text) : g_strdup ("");
+               /* because of the -- \n check */
+               is_html = FALSE;
+       }
+
        /* The signature dash convention ("-- \n") is specified
         * in the "Son of RFC 1036", section 4.3.2.
         * http://www.chemie.fu-berlin.de/outerspace/netnews/son-of-1036.html
@@ -1175,8 +1175,22 @@ composer_load_signature_cb (EMailSignatureComboBox *combo_box,
                                WEBKIT_DOM_HTML_ELEMENT (insert_signature_in), delim, NULL);
        }
 
-       webkit_dom_html_element_insert_adjacent_html (
-               WEBKIT_DOM_HTML_ELEMENT (insert_signature_in), "beforeend", contents, NULL);
+       if (converted_signature) {
+               WebKitDOMNode *node;
+
+               while ((node = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (converted_signature))))
+                       webkit_dom_node_append_child (
+                               WEBKIT_DOM_NODE (insert_signature_in), node, NULL);
+               remove_node (WEBKIT_DOM_NODE (converted_signature));
+       } else
+               webkit_dom_html_element_insert_adjacent_html (
+                       WEBKIT_DOM_HTML_ELEMENT (insert_signature_in), "beforeend", contents, NULL);
+
+       element = webkit_dom_element_query_selector (
+               insert_signature_in, "[data-evo-signature-plain-text-mode]", NULL);
+       if (element)
+               webkit_dom_element_remove_attribute (
+                       element, "data-evo-signature-plain-text-mode");
        g_free (contents);
 
 insert:
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index fecea13..f9ab4c9 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -9522,11 +9522,12 @@ convert_element_from_html_to_plain_text (EHTMLEditorView *view,
                                          gboolean *wrap,
                                          gboolean *quote)
 {
-       gint blockquotes_count;
+       gint blockquotes_count, ii, length;
        gchar *inner_text, *inner_html;
        WebKitDOMDocument *document;
        WebKitDOMElement *top_signature, *signature, *blockquote, *main_blockquote;
        WebKitDOMNode *signature_clone, *from;
+       WebKitDOMNodeList *list;
 
        document = webkit_dom_node_get_owner_document (WEBKIT_DOM_NODE (element));
 
@@ -9557,6 +9558,24 @@ convert_element_from_html_to_plain_text (EHTMLEditorView *view,
        blockquotes_count = create_text_markers_for_citations_in_element (WEBKIT_DOM_ELEMENT (from));
        create_text_markers_for_selection_in_element (WEBKIT_DOM_ELEMENT (from));
 
+       /* Add the missing BR elements on the end of all DIV elements to correctly
+        * preserve the line breaks. */
+       list = webkit_dom_element_query_selector_all (WEBKIT_DOM_ELEMENT (from), "div", NULL);
+       length = webkit_dom_node_list_get_length (list);
+       for (ii = 0; ii < length; ii++) {
+               WebKitDOMNode *node;
+
+               node = webkit_dom_node_list_item (list, ii);
+               if (!WEBKIT_DOM_IS_HTMLBR_ELEMENT (webkit_dom_node_get_last_child (node))) {
+                       webkit_dom_node_append_child (
+                               node,
+                               WEBKIT_DOM_NODE (webkit_dom_document_create_element (document, "br", NULL)),
+                               NULL);
+               }
+               g_object_unref (node);
+       }
+       g_object_unref (list);
+
        inner_text = webkit_dom_html_element_get_inner_text (
                WEBKIT_DOM_HTML_ELEMENT (from));
 


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