[evolution/gnome-3-18] EHTMLEditorSelection - Trailing and leading spaces could be lost



commit 9e588b74c06a494ba7b80024d301ce60140ff797
Author: Tomas Popela <tpopela redhat com>
Date:   Fri Oct 16 14:39:15 2015 +0200

    EHTMLEditorSelection - Trailing and leading spaces could be lost
    
    This could happen when we are switching from 'Preformatted' to another format.
    Change the affected spaces to non-breaking spaces to preserve them.

 e-util/e-html-editor-selection.c |   96 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 96 insertions(+), 0 deletions(-)
---
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index 5eb4204..d524b36 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -2380,6 +2380,83 @@ format_change_list_to_block (EHTMLEditorSelection *selection,
        do_format_change_list_to_block (selection, format, item, value, document);
 }
 
+static void
+change_leading_space_to_nbsp (WebKitDOMNode *block)
+{
+       WebKitDOMNode *child;
+
+       if (!WEBKIT_DOM_IS_HTML_PRE_ELEMENT (block))
+               return;
+
+       if ((child = webkit_dom_node_get_first_child (block)) &&
+            WEBKIT_DOM_IS_CHARACTER_DATA (child)) {
+               gchar *data;
+
+               data = webkit_dom_character_data_substring_data (
+                       WEBKIT_DOM_CHARACTER_DATA (child), 0, 1, NULL);
+
+               if (data && *data == ' ')
+                       webkit_dom_character_data_replace_data (
+                               WEBKIT_DOM_CHARACTER_DATA (child), 0, 1, UNICODE_NBSP, NULL);
+               g_free (data);
+       }
+}
+
+static void
+change_trailing_space_in_block_to_nbsp (WebKitDOMNode *block)
+{
+       WebKitDOMNode *child;
+
+       if ((child = webkit_dom_node_get_last_child (block)) &&
+           WEBKIT_DOM_IS_CHARACTER_DATA (child)) {
+               gchar *tmp;
+               gulong length;
+
+               length = webkit_dom_character_data_get_length (
+                       WEBKIT_DOM_CHARACTER_DATA (child));
+
+               tmp = webkit_dom_character_data_substring_data (
+                       WEBKIT_DOM_CHARACTER_DATA (child), length - 1, 1, NULL);
+               if (tmp && *tmp == ' ') {
+                       webkit_dom_character_data_replace_data (
+                               WEBKIT_DOM_CHARACTER_DATA (child),
+                               length - 1,
+                               1,
+                               UNICODE_NBSP,
+                               NULL);
+               }
+               g_free (tmp);
+       }
+}
+
+static void
+change_space_before_selection_to_nbsp (WebKitDOMNode *node)
+{
+       WebKitDOMNode *prev_sibling;
+
+       if ((prev_sibling = webkit_dom_node_get_previous_sibling (node))) {
+               if (WEBKIT_DOM_IS_CHARACTER_DATA (prev_sibling)) {
+                       gchar *tmp;
+                       gulong length;
+
+                       length = webkit_dom_character_data_get_length (
+                               WEBKIT_DOM_CHARACTER_DATA (prev_sibling));
+
+                       tmp = webkit_dom_character_data_substring_data (
+                               WEBKIT_DOM_CHARACTER_DATA (prev_sibling), length - 1, 1, NULL);
+                       if (tmp && *tmp == ' ') {
+                               webkit_dom_character_data_replace_data (
+                                       WEBKIT_DOM_CHARACTER_DATA (prev_sibling),
+                                       length - 1,
+                                       1,
+                                       UNICODE_NBSP,
+                                       NULL);
+                       }
+                       g_free (tmp);
+               }
+       }
+}
+
 static gboolean
 process_block_to_block (EHTMLEditorSelection *selection,
                         EHTMLEditorView *view,
@@ -2465,6 +2542,9 @@ process_block_to_block (EHTMLEditorSelection *selection,
                empty = !*content || (g_strcmp0 (content, UNICODE_ZERO_WIDTH_SPACE) == 0);
                g_free (content);
 
+               change_leading_space_to_nbsp (block);
+               change_trailing_space_in_block_to_nbsp (block);
+
                while ((child = webkit_dom_node_get_first_child (block))) {
                        if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (child))
                                empty = FALSE;
@@ -2691,6 +2771,9 @@ format_change_block_to_list (EHTMLEditorSelection *selection,
                empty = !*content || (g_strcmp0 (content, UNICODE_ZERO_WIDTH_SPACE) == 0);
                g_free (content);
 
+               change_leading_space_to_nbsp (block);
+               change_trailing_space_in_block_to_nbsp (block);
+
                while ((child = webkit_dom_node_get_first_child (block))) {
                        if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (child))
                                empty = FALSE;
@@ -2974,6 +3057,19 @@ e_html_editor_selection_set_block_format (EHTMLEditorSelection *selection,
 
        g_object_unref (range);
 
+       if (current_format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PRE) {
+               WebKitDOMElement *selection_marker;
+
+               selection_marker = webkit_dom_document_get_element_by_id (
+                       document, "-x-evo-selection-start-marker");
+               if (selection_marker)
+                       change_space_before_selection_to_nbsp (WEBKIT_DOM_NODE (selection_marker));
+               selection_marker = webkit_dom_document_get_element_by_id (
+                       document, "-x-evo-selection-end-marker");
+               if (selection_marker)
+                       change_space_before_selection_to_nbsp (WEBKIT_DOM_NODE (selection_marker));
+       }
+
        if (from_list && to_list)
                format_change_list_to_list (selection, format, document, html_mode);
 


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