[evolution] EHTMLEditorView - Simplify and improve how the undo/redo of delete operation in quoted content is pe



commit 0848a57062cb27ee3114306ce799ae47c593193a
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Mar 30 14:20:46 2016 +0200

    EHTMLEditorView - Simplify and improve how the undo/redo of delete operation in quoted content is 
performed
    
    After processing the first and the last block in the deleted content insert the
    fragment with the rest as a whole to the given position and let the
    merge_siblings_if_necessary to do its work. Previous behaviour could skip some
    blocks or insert them into different position or reverse them. With this
    approach there is no need for marking the blocks with the special id and treat
    them separately.

 e-util/e-html-editor-view.c |  217 ++++++++++++++++---------------------------
 1 files changed, 80 insertions(+), 137 deletions(-)
---
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 865e5af..634e906 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -3349,7 +3349,6 @@ body_key_up_event_process_backspace_or_delete (EHTMLEditorView *view,
        EHTMLEditorSelection *selection;
        gint level;
        WebKitDOMElement *selection_start_marker, *selection_end_marker;
-       WebKitDOMElement *tmp_element;
        WebKitDOMDocument *document;
        WebKitDOMNode *parent, *node;
 
@@ -3424,45 +3423,6 @@ body_key_up_event_process_backspace_or_delete (EHTMLEditorView *view,
                                NULL);
        }
 
-       /* Situation where the start of the selection was in the beginning
-        * of the block in quoted content and the end in the beginning of
-        * content that is after the citation or the selection end was in
-        * the end of the quoted content (showed by ^). The correct structure
-        * in these cases is to have empty block after the citation.
-        *
-        * > |xxx
-        * > xxx^
-        * |xxx
-        * */
-       tmp_element = webkit_dom_document_get_element_by_id (document, "-x-evo-tmp-block");
-       if (tmp_element) {
-               remove_wrapping_from_element (tmp_element);
-               remove_quoting_from_element (tmp_element);
-
-               /* Append the BR element if the block is empty, but the
-                * selection is there to be able to move to the block
-                * with caret later. */
-               if (!webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (selection_end_marker)) &&
-                   !webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (selection_start_marker)))
-                       webkit_dom_node_append_child (
-                               WEBKIT_DOM_NODE (tmp_element),
-                               WEBKIT_DOM_NODE (webkit_dom_document_create_element (
-                                       document, "br", NULL)),
-                               NULL);
-
-               webkit_dom_element_remove_attribute (tmp_element, "id");
-
-               parent = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (tmp_element));
-               while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (webkit_dom_node_get_parent_node (parent)))
-                       parent = webkit_dom_node_get_parent_node (parent);
-
-               webkit_dom_node_insert_before (
-                       webkit_dom_node_get_parent_node (parent),
-                       WEBKIT_DOM_NODE (tmp_element),
-                       webkit_dom_node_get_next_sibling (parent),
-                       NULL);
-       }
-
        merge_siblings_if_necessary (document, NULL);
 
        e_html_editor_selection_restore (selection);
@@ -4698,16 +4658,8 @@ save_history_for_delete_or_backspace (EHTMLEditorView *view,
                        WebKitDOMNode *node;
 
                        node = WEBKIT_DOM_NODE (tmp_element);
-                       while (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (webkit_dom_node_get_parent_node (node))) {
-                               WebKitDOMNode *next_sibling;
-
-                               next_sibling = webkit_dom_node_get_next_sibling (node);
-                               if (next_sibling &&
-                                   (!WEBKIT_DOM_IS_HTMLBR_ELEMENT (next_sibling) ||
-                                    webkit_dom_node_get_next_sibling (next_sibling)))
-                                       break;
+                       while (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (webkit_dom_node_get_parent_node (node)))
                                node = webkit_dom_node_get_parent_node (node);
-                       }
 
                        if (node && WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (node)) {
                                WebKitDOMNode *last_child;
@@ -4889,13 +4841,6 @@ fix_structure_after_delete_before_quoted_content (EHTMLEditorView *view,
                                parent = next_parent;
                        }
                }
-
-               node = webkit_dom_node_get_next_sibling (
-                       WEBKIT_DOM_NODE (selection_end_marker));
-               if (!node || WEBKIT_DOM_IS_HTMLBR_ELEMENT (node)) {
-                       webkit_dom_element_set_id (
-                               WEBKIT_DOM_ELEMENT (block), "-x-evo-tmp-block");
-               }
        }
  restore:
        if (event)
@@ -13182,12 +13127,18 @@ undo_delete (EHTMLEditorView *view,
                                        if (get_citation_level (block, FALSE) > 0) {
                                                WebKitDOMNode *next_node;
 
-                                               next_node = split_node_into_two (block, -1);
-                                               webkit_dom_node_insert_before (
-                                                       webkit_dom_node_get_parent_node (next_node),
-                                                       node,
-                                                       next_node,
-                                                       NULL);
+                                               if ((next_node = split_node_into_two (block, -1)))
+                                                       webkit_dom_node_insert_before (
+                                                               webkit_dom_node_get_parent_node (next_node),
+                                                               node,
+                                                               next_node,
+                                                               NULL);
+                                               else
+                                                       webkit_dom_node_insert_before (
+                                                               webkit_dom_node_get_parent_node (block),
+                                                               node,
+                                                               block,
+                                                               NULL);
                                        } else
                                                webkit_dom_node_insert_before (
                                                        webkit_dom_node_get_parent_node (block),
@@ -13278,7 +13229,6 @@ undo_delete (EHTMLEditorView *view,
                WebKitDOMElement *signature;
                WebKitDOMNode *node, *current_block, *last_child;
                WebKitDOMNode *next_block, *insert_before;
-               WebKitDOMNode *parent_deleted_content, *parent_current_block;
 
                range = get_range_for_point (document, event->after.start);
                webkit_dom_dom_selection_remove_all_ranges (dom_selection);
@@ -13345,56 +13295,47 @@ undo_delete (EHTMLEditorView *view,
 
                        next_sibling = webkit_dom_node_get_next_sibling (node);
                        parent_node = webkit_dom_node_get_parent_node (node);
-                       if (!next_sibling && WEBKIT_DOM_IS_HTMLBR_ELEMENT (node)) {
-                               /* Check if the whole element was deleted. If so replace it and
-                                * skip the code down there. */
-                               if (!webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (element))) {
-                                       WebKitDOMNode *tmp_node;
-                                       WebKitDOMElement *tmp_element;
-
-                                       tmp_node = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE 
(element));
-                                       webkit_dom_node_replace_child (
-                                               webkit_dom_node_get_parent_node (tmp_node),
-                                               fragment,
-                                               tmp_node,
-                                               NULL);
+                       /* Check if the whole element was deleted. If so replace it. */
+                       if (!next_sibling && WEBKIT_DOM_IS_HTMLBR_ELEMENT (node) &&
+                           !webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (element))) {
+                               WebKitDOMNode *tmp_node;
+                               WebKitDOMElement *tmp_element;
 
-                                       tmp_element = webkit_dom_document_get_element_by_id (
-                                               document, "-x-evo-tmp-block");
-                                       if (tmp_element) {
-                                               webkit_dom_element_remove_attribute (tmp_element, "id");
-                                               wrap_and_quote_element (view, tmp_element);
-                                       }
+                               tmp_node = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element));
+                               webkit_dom_node_replace_child (
+                                       webkit_dom_node_get_parent_node (tmp_node),
+                                       fragment,
+                                       tmp_node,
+                                       NULL);
 
-                                       /* Remove empty blockquotes, if presented. */
-                                       tmp_element = webkit_dom_document_query_selector (
-                                               document, "blockquote[type=cite]:empty", NULL);
-                                       if (tmp_element)
-                                               remove_node (WEBKIT_DOM_NODE (tmp_element));
+                               /* Remove empty blockquotes, if presented. */
+                               tmp_element = webkit_dom_document_query_selector (
+                                       document, "blockquote[type=cite]:empty", NULL);
+                               if (tmp_element)
+                                       remove_node (WEBKIT_DOM_NODE (tmp_element));
 
-                                       merge_siblings_if_necessary (document, event->data.fragment);
+                               merge_siblings_if_necessary (document, event->data.fragment);
 
-                                       tmp_node = webkit_dom_node_get_last_child (last_child);
-                                       if (tmp_node && WEBKIT_DOM_IS_ELEMENT (tmp_node) &&
-                                           element_has_class (WEBKIT_DOM_ELEMENT (tmp_node), 
"-x-evo-quoted")) {
-                                               webkit_dom_node_append_child (
-                                                       last_child,
-                                                       WEBKIT_DOM_NODE (
-                                                               webkit_dom_document_create_element (
-                                                                       document, "br", NULL)),
-                                                       NULL);
-                                       }
+                               tmp_node = webkit_dom_node_get_last_child (last_child);
+                               if (tmp_node && WEBKIT_DOM_IS_ELEMENT (tmp_node) &&
+                                   element_has_class (WEBKIT_DOM_ELEMENT (tmp_node), "-x-evo-quoted")) {
+                                       webkit_dom_node_append_child (
+                                               last_child,
+                                               WEBKIT_DOM_NODE (
+                                                       webkit_dom_document_create_element (
+                                                               document, "br", NULL)),
+                                               NULL);
+                               }
 
-                                       remove_selection_markers (document);
+                               remove_selection_markers (document);
 
-                                       restore_selection_to_history_event_state (view, event->before);
+                               restore_selection_to_history_event_state (view, event->before);
 
-                                       e_html_editor_view_force_spell_check_in_viewport (view);
+                               e_html_editor_view_force_spell_check_in_viewport (view);
 
-                                       g_object_unref (dom_selection);
+                               g_object_unref (dom_selection);
 
-                                       return;
-                               }
+                               return;
                        } else if (!next_sibling && !webkit_dom_node_is_same_node (parent_node, 
current_block))
                                next_sibling = webkit_dom_node_get_next_sibling (parent_node);
 
@@ -13414,43 +13355,45 @@ undo_delete (EHTMLEditorView *view,
                while ((node = webkit_dom_node_get_first_child (first_child)))
                        webkit_dom_node_append_child (current_block, node, NULL);
 
-               parent_deleted_content = webkit_dom_node_get_parent_node (first_child);
-               parent_current_block = webkit_dom_node_get_parent_node (current_block);
-               insert_before = webkit_dom_node_get_next_sibling (current_block);
-
-               /* Remove the first block from deleted content as its content was already
-                * moved to the right place. */
-               remove_node (first_child);
-
                next_block = webkit_dom_node_get_next_sibling (current_block);
-               /* Move the deleted content back to the body. Start from the next sibling
-                * of the first block (if presented) where the delete occurred. */
-               while (parent_deleted_content) {
-                       WebKitDOMNode *tmp, *child;
+               insert_before = next_block;
 
-                       /* Move all the siblings from current level back to the body. */
-                       child = webkit_dom_node_get_first_child (parent_deleted_content);
-                       while (child) {
-                               WebKitDOMNode *next_sibling;
+               if (!insert_before) {
+                       WebKitDOMNode *parent = NULL;
 
-                               next_sibling = webkit_dom_node_get_next_sibling (child);
-                               webkit_dom_node_insert_before (
-                                       parent_current_block, child, insert_before, NULL);
-                               child = next_sibling;
+                       parent = current_block;
+                       while ((parent = webkit_dom_node_get_parent_node (parent)) &&
+                               !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
+                               insert_before = webkit_dom_node_get_next_sibling (parent);
+                               if (insert_before)
+                                       break;
                        }
-                       tmp = webkit_dom_node_get_parent_node (parent_deleted_content);
-                       remove_node (parent_deleted_content);
-                       parent_deleted_content = tmp;
-                       insert_before = webkit_dom_node_get_next_sibling (parent_current_block);
+               }
 
-                       if (!insert_before && next_block && WEBKIT_DOM_IS_HTML_BODY_ELEMENT (
-                           webkit_dom_node_get_parent_node (parent_current_block)))
-                               insert_before = split_node_into_two (next_block, -1);
+               /* Split a BLOCKQUOTE if the deleted content started with BLOCKQUOTE */
+               if (insert_before &&
+                   WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (
+                       webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (fragment))) &&
+                   get_citation_level (insert_before, FALSE > 0))
+                       insert_before = split_node_into_two (insert_before, -1);
 
-                       /* Be sure that we don't go above body. */
-                       if (!WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent_current_block))
-                               parent_current_block = webkit_dom_node_get_parent_node (parent_current_block);
-               }
+               /* Remove the first block from deleted content as its content was already
+                * moved to the right place. */
+               remove_node (first_child);
+
+               /* Insert the deleted content. */
+               if (insert_before)
+                       webkit_dom_node_insert_before (
+                               webkit_dom_node_get_parent_node (insert_before),
+                               WEBKIT_DOM_NODE (fragment),
+                               insert_before,
+                               NULL);
+               else
+                       webkit_dom_node_append_child (
+                               WEBKIT_DOM_NODE (
+                                       webkit_dom_document_get_body (document)),
+                               WEBKIT_DOM_NODE (fragment),
+                               NULL);
 
                wrap_and_quote_element (view, WEBKIT_DOM_ELEMENT (current_block));
 


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