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



commit 206c600d18ef205e4581b12eeefa54712fcc3b87
Author: Tomas Popela <tpopela redhat com>
Date:   Tue May 31 11:02:03 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-html-editor-undo-redo-manager.c              |  164 ++++++++++----------
 .../e-html-editor-view-dom-functions.c             |   58 +-------
 2 files changed, 83 insertions(+), 139 deletions(-)
---
diff --git a/modules/webkit-content-editor/web-extension/e-html-editor-undo-redo-manager.c 
b/modules/webkit-content-editor/web-extension/e-html-editor-undo-redo-manager.c
index 8c2d135..005f32c 100644
--- a/modules/webkit-content-editor/web-extension/e-html-editor-undo-redo-manager.c
+++ b/modules/webkit-content-editor/web-extension/e-html-editor-undo-redo-manager.c
@@ -460,12 +460,18 @@ undo_delete (WebKitDOMDocument *document,
                                        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),
@@ -553,7 +559,6 @@ undo_delete (WebKitDOMDocument *document,
                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);
@@ -621,56 +626,47 @@ undo_delete (WebKitDOMDocument *document,
 
                        next_sibling = webkit_dom_node_get_next_sibling (node);
                        parent_node = webkit_dom_node_get_parent_node (node);
-                       if (!next_sibling && WEBKIT_DOM_IS_HTML_BR_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_HTML_BR_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 (document, extension, 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));
-
-                                       dom_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);
-                                       }
+                               /* 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));
 
-                                       dom_remove_selection_markers (document);
+                               dom_merge_siblings_if_necessary (document, event->data.fragment);
 
-                                       restore_selection_to_history_event_state (document, event->before);
+                               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);
+                               }
 
-                                       dom_force_spell_check_in_viewport (document, extension);
+                               dom_remove_selection_markers (document);
 
-                                       g_object_unref (dom_selection);
+                               restore_selection_to_history_event_state (document, event->before);
 
-                                       return;
-                               }
+                               dom_force_spell_check_in_viewport (document, extension);
+
+                               g_object_unref (dom_selection);
+
+                               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);
 
@@ -690,44 +686,46 @@ undo_delete (WebKitDOMDocument *document,
                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);
-
-                       /* 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);
                }
 
+               /* 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);
+
+               /* 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 (document, extension, WEBKIT_DOM_ELEMENT (current_block));
 
                if (WEBKIT_DOM_IS_ELEMENT (last_child))
diff --git a/modules/webkit-content-editor/web-extension/e-html-editor-view-dom-functions.c 
b/modules/webkit-content-editor/web-extension/e-html-editor-view-dom-functions.c
index 9c947e4..b2cb0e2 100644
--- a/modules/webkit-content-editor/web-extension/e-html-editor-view-dom-functions.c
+++ b/modules/webkit-content-editor/web-extension/e-html-editor-view-dom-functions.c
@@ -3194,7 +3194,6 @@ body_key_up_event_process_backspace_or_delete (WebKitDOMDocument *document,
 {
        gint level;
        WebKitDOMElement *selection_start_marker, *selection_end_marker;
-       WebKitDOMElement *tmp_element;
        WebKitDOMNode *parent, *node;
 
        if (e_html_editor_web_extension_get_html_mode (extension))
@@ -3265,45 +3264,6 @@ body_key_up_event_process_backspace_or_delete (WebKitDOMDocument *document,
                                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) {
-               dom_remove_wrapping_from_element (tmp_element);
-               dom_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);
-       }
-
        dom_merge_siblings_if_necessary (document, NULL);
 
        dom_selection_restore (document);
@@ -8692,16 +8652,8 @@ save_history_for_delete_or_backspace (WebKitDOMDocument *document,
                        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_HTML_BR_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;
@@ -8882,14 +8834,8 @@ dom_fix_structure_after_delete_before_quoted_content (WebKitDOMDocument *documen
                                parent = next_parent;
                        }
                }
-
-               node = webkit_dom_node_get_next_sibling (
-                       WEBKIT_DOM_NODE (selection_end_marker));
-               if (!node || WEBKIT_DOM_IS_HTML_BR_ELEMENT (node)) {
-                       webkit_dom_element_set_id (
-                               WEBKIT_DOM_ELEMENT (block), "-x-evo-tmp-block");
-               }
        }
+
  restore:
        if (key_code != ~0)
                save_history_for_delete_or_backspace (


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