[evolution] Bug 733954 - Enter doesn't delete selection with cited text



commit a331e7edfc77b3fe0585a7b077726b87f83bf0b2
Author: Tomas Popela <tpopela redhat com>
Date:   Thu Jul 31 11:04:10 2014 +0200

    Bug 733954 - Enter doesn't delete selection with cited text
    
    The problem was that we were manipulating with the selection before the
    WebKit Editor call (and we hit again problems with selections in
    WebKit). So as a fix we reworked how the new line is inserted when the
    enter is pressed on empty line in quoted content (and completely avoid
    the WebKit Editor call there).

 e-util/e-html-editor-view.c |  133 +++++++++++++++++++++++++++----------------
 1 files changed, 84 insertions(+), 49 deletions(-)
---
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index bafa86f..a9dd979 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -563,36 +563,11 @@ return_pressed_in_empty_line (EHTMLEditorSelection *selection,
                first_child = webkit_dom_node_get_first_child (node);
                if (first_child && WEBKIT_DOM_IS_ELEMENT (first_child) &&
                    element_has_class (WEBKIT_DOM_ELEMENT (first_child), "-x-evo-quoted")) {
-                       WebKitDOMNode *next_sibling;
-
-                       next_sibling = webkit_dom_node_get_next_sibling (first_child);
-                       if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (next_sibling)) {
-                               next_sibling = webkit_dom_node_get_next_sibling (node);
-                               if (webkit_dom_node_get_first_child (next_sibling)) {
-                                       WebKitDOMElement *element;
+                       WebKitDOMNode *prev_sibling;
 
-                                       element = webkit_dom_document_create_element (
-                                               document, "SPAN", NULL);
-                                       webkit_dom_element_set_id (
-                                               element, "-x-evo-selection-end-marker");
-                                       webkit_dom_node_insert_before (
-                                               next_sibling,
-                                               WEBKIT_DOM_NODE (element),
-                                               webkit_dom_node_get_first_child (next_sibling),
-                                               NULL);
-                                       element = webkit_dom_document_create_element (
-                                               document, "SPAN", NULL);
-                                       webkit_dom_element_set_id (
-                                               element, "-x-evo-selection-start-marker");
-                                       webkit_dom_node_insert_before (
-                                               next_sibling,
-                                               WEBKIT_DOM_NODE (element),
-                                               webkit_dom_node_get_first_child (next_sibling),
-                                               NULL);
-                                       e_html_editor_selection_restore (selection);
-                                       return TRUE;
-                               }
-                       }
+                       prev_sibling = webkit_dom_node_get_previous_sibling (node);
+                       if (!prev_sibling)
+                               return webkit_dom_range_get_collapsed (range, NULL);
                }
        }
 
@@ -603,7 +578,7 @@ static WebKitDOMElement *
 insert_new_line_into_citation (EHTMLEditorView *view,
                                const gchar *html_to_insert)
 {
-       gboolean html_mode, ret_val, fix_after_return_pressed_in_empty_line;
+       gboolean html_mode, ret_val, avoid_editor_call;
        EHTMLEditorSelection *selection;
        WebKitDOMDocument *document;
        WebKitDOMElement *element, *paragraph = NULL;
@@ -614,33 +589,93 @@ insert_new_line_into_citation (EHTMLEditorView *view,
 
        document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
 
-       fix_after_return_pressed_in_empty_line =
+       avoid_editor_call =
                return_pressed_in_empty_line (selection, document);
 
-       ret_val = e_html_editor_view_exec_command (
-               view, E_HTML_EDITOR_VIEW_COMMAND_INSERT_NEW_LINE_IN_QUOTED_CONTENT, NULL);
+       if (avoid_editor_call) {
+               WebKitDOMElement *selection_start_marker;
+               WebKitDOMNode *current_block, *parent, *parent_block, *block_clone;
 
-       if (!ret_val)
-               return NULL;
+               e_html_editor_selection_save (selection);
 
-       element = webkit_dom_document_query_selector (
-               document, "body>br", NULL);
+               selection_start_marker = webkit_dom_document_get_element_by_id (
+                       document, "-x-evo-selection-start-marker");
 
-       if (!element)
-               return NULL;
+               current_block = webkit_dom_node_get_parent_node (
+                       WEBKIT_DOM_NODE (selection_start_marker));
 
-       if (fix_after_return_pressed_in_empty_line) {
-               WebKitDOMNode *node;
+               block_clone = webkit_dom_node_clone_node (current_block, TRUE);
+               /* Find selection start marker and restore it after the new line
+                * is inserted */
+               selection_start_marker = webkit_dom_element_query_selector (
+                       WEBKIT_DOM_ELEMENT (block_clone), "#-x-evo-selection-start-marker", NULL);
+
+               /* Find parent node that is immediate child of the BODY */
+               /* Build the same structure of parent nodes of the current block */
+               parent_block = current_block;
+               parent = webkit_dom_node_get_parent_node (parent_block);
+               while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
+                       WebKitDOMNode *node;
 
-               node = webkit_dom_node_get_previous_sibling (
-                       WEBKIT_DOM_NODE (element));
+                       parent_block = parent;
+                       node = webkit_dom_node_clone_node (parent_block, FALSE);
+                       webkit_dom_node_append_child (node, block_clone, NULL);
+                       block_clone = node;
+                       parent = webkit_dom_node_get_parent_node (parent_block);
+               }
+
+               paragraph = e_html_editor_selection_get_paragraph_element (
+                       selection, document, -1, 0);
 
-               node = webkit_dom_node_get_last_child (node);
-               while (node && is_citation_node (node))
-                       node = webkit_dom_node_get_last_child (node);
+               webkit_dom_html_element_set_inner_text (
+                       WEBKIT_DOM_HTML_ELEMENT (paragraph),
+                       UNICODE_ZERO_WIDTH_SPACE,
+                       NULL);
+
+               /* Insert the selection markers to right place */
+               webkit_dom_node_insert_before (
+                       WEBKIT_DOM_NODE (paragraph),
+                       webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (selection_start_marker)),
+                       webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (paragraph)),
+                       NULL);
+               webkit_dom_node_insert_before (
+                       WEBKIT_DOM_NODE (paragraph),
+                       WEBKIT_DOM_NODE (selection_start_marker),
+                       webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (paragraph)),
+                       NULL);
+
+               /* Insert the cloned nodes before the BODY parent node */
+               webkit_dom_node_insert_before (
+                       webkit_dom_node_get_parent_node (parent_block),
+                       block_clone,
+                       parent_block,
+                       NULL);
+
+               /* Insert the new empty paragraph before the BODY parent node */
+               webkit_dom_node_insert_before (
+                       webkit_dom_node_get_parent_node (parent_block),
+                       WEBKIT_DOM_NODE (paragraph),
+                       parent_block,
+                       NULL);
+
+               /* Remove the old block (its copy was moved to the right place) */
+               remove_node (current_block);
+
+               e_html_editor_selection_restore (selection);
+
+               return NULL;
+       } else {
+               ret_val = e_html_editor_view_exec_command (
+                       view, E_HTML_EDITOR_VIEW_COMMAND_INSERT_NEW_LINE_IN_QUOTED_CONTENT, NULL);
+
+               if (!ret_val)
+                       return NULL;
+
+               element = webkit_dom_document_query_selector (
+                       document, "body>br", NULL);
 
-               if (node)
-                       remove_node (webkit_dom_node_get_last_child (node));
+               if (!element)
+                       return NULL;
        }
 
        if (!html_mode) {


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