[evolution] EHTMLEditorSelection - Remove the old functions that manipulated with caret



commit c5a2d21b7ded2f3b85eb156d34970e12eab9cb26
Author: Tomas Popela <tpopela redhat com>
Date:   Fri Mar 13 11:16:18 2015 +0100

    EHTMLEditorSelection - Remove the old functions that manipulated with caret
    
    They were not really helpful as they caused more damage than benefit.
    The only good way to work with selection (as well as caret, because
    caret is just collapsed selection) is to use the
    e_html_editor_selection_save and e_html_editor_selection_restore
    functions.

 e-util/e-html-editor-selection.c |  283 +++++---------------------------------
 e-util/e-html-editor-selection.h |   12 +--
 e-util/e-html-editor-view.c      |   59 ++------
 3 files changed, 50 insertions(+), 304 deletions(-)
---
diff --git a/e-util/e-html-editor-selection.c b/e-util/e-html-editor-selection.c
index c5ad239..b8cfdb3 100644
--- a/e-util/e-html-editor-selection.c
+++ b/e-util/e-html-editor-selection.c
@@ -1865,8 +1865,7 @@ is_selection_position_node (WebKitDOMNode *node)
 
        element = WEBKIT_DOM_ELEMENT (node);
 
-       return element_has_id (element, "-x-evo-caret-position") ||
-              element_has_id (element, "-x-evo-selection-start-marker") ||
+       return element_has_id (element, "-x-evo-selection-start-marker") ||
               element_has_id (element, "-x-evo-selection-end-marker");
 }
 
@@ -2201,6 +2200,28 @@ add_selection_markers_into_element_start (WebKitDOMDocument *document,
 }
 
 static void
+add_selection_markers_into_element_end (WebKitDOMDocument *document,
+                                        WebKitDOMElement *element,
+                                        WebKitDOMElement **selection_start_marker,
+                                        WebKitDOMElement **selection_end_marker)
+{
+       WebKitDOMElement *marker;
+
+       remove_selection_markers (document);
+       marker = create_selection_marker (document, TRUE);
+       webkit_dom_node_append_child (
+               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
+       if (selection_start_marker)
+               *selection_start_marker = marker;
+
+       marker = create_selection_marker (document, FALSE);
+       webkit_dom_node_append_child (
+               WEBKIT_DOM_NODE (element), WEBKIT_DOM_NODE (marker), NULL);
+       if (selection_end_marker)
+               *selection_end_marker = marker;
+}
+
+static void
 format_change_block_to_block (EHTMLEditorSelection *selection,
                               EHTMLEditorSelectionBlockFormat format,
                               EHTMLEditorView *view,
@@ -2288,16 +2309,20 @@ format_change_block_to_list (EHTMLEditorSelection *selection,
        if (webkit_dom_element_query_selector (
                WEBKIT_DOM_ELEMENT (block), "span.-x-evo-quoted", NULL)) {
                WebKitDOMElement *element;
+               WebKitDOMDOMWindow *window;
+               WebKitDOMDOMSelection *dom_selection;
+               WebKitDOMRange *range;
 
                in_quote = TRUE;
 
-               webkit_dom_node_insert_before (
-                       webkit_dom_node_get_parent_node (block),
-                       e_html_editor_selection_get_caret_position_node (document),
-                       block,
-                       NULL);
+               window = webkit_dom_document_get_default_view (document);
+               dom_selection = webkit_dom_dom_window_get_selection (window);
+               range = webkit_dom_document_create_range (document);
 
-               e_html_editor_selection_restore_caret_position (selection);
+               webkit_dom_range_select_node (range, block, NULL);
+               webkit_dom_range_collapse (range, TRUE, NULL);
+               webkit_dom_dom_selection_remove_all_ranges (dom_selection);
+               webkit_dom_dom_selection_add_range (dom_selection, range);
 
                e_html_editor_view_exec_command (
                        view, E_HTML_EDITOR_VIEW_COMMAND_INSERT_NEW_LINE_IN_QUOTED_CONTENT, NULL);
@@ -5473,243 +5498,6 @@ e_html_editor_selection_move_caret_into_element (WebKitDOMDocument *document,
        webkit_dom_dom_selection_add_range (window_selection, new_range);
 }
 
-/**
- * e_html_editor_selection_clear_caret_position_marker:
- * @selection: an #EHTMLEditorSelection
- *
- * Removes previously set caret position marker from composer.
- */
-void
-e_html_editor_selection_clear_caret_position_marker (EHTMLEditorSelection *selection)
-{
-       EHTMLEditorView *view;
-       WebKitDOMDocument *document;
-       WebKitDOMElement *element;
-
-       g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
-
-       view = e_html_editor_selection_ref_html_editor_view (selection);
-       g_return_if_fail (view != NULL);
-
-       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
-
-       element = webkit_dom_document_get_element_by_id (document, "-x-evo-caret-position");
-
-       if (element)
-               remove_node (WEBKIT_DOM_NODE (element));
-
-       g_object_unref (view);
-}
-
-WebKitDOMNode *
-e_html_editor_selection_get_caret_position_node (WebKitDOMDocument *document)
-{
-       WebKitDOMElement *element;
-
-       element = webkit_dom_document_create_element (document, "SPAN", NULL);
-       webkit_dom_element_set_id (element, "-x-evo-caret-position");
-       webkit_dom_element_set_attribute (
-               element, "style", "color: red", NULL);
-       webkit_dom_html_element_set_inner_html (
-               WEBKIT_DOM_HTML_ELEMENT (element), "*", NULL);
-
-       return WEBKIT_DOM_NODE (element);
-}
-
-/**
- * e_html_editor_selection_save_caret_position:
- * @selection: an #EHTMLEditorSelection
- *
- * Saves current caret position in composer.
- *
- * Returns: #WebKitDOMElement that was created on caret position
- */
-WebKitDOMElement *
-e_html_editor_selection_save_caret_position (EHTMLEditorSelection *selection)
-{
-       EHTMLEditorView *view;
-       WebKitDOMDocument *document;
-       WebKitDOMNode *split_node;
-       WebKitDOMNode *start_offset_node;
-       WebKitDOMNode *caret_node;
-       WebKitDOMRange *range;
-       gulong start_offset;
-
-       g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);
-
-       view = e_html_editor_selection_ref_html_editor_view (selection);
-       g_return_val_if_fail (view != NULL, NULL);
-
-       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
-       g_object_unref (view);
-
-       e_html_editor_selection_clear_caret_position_marker (selection);
-
-       range = html_editor_selection_get_current_range (selection);
-       if (!range)
-               return NULL;
-
-       start_offset = webkit_dom_range_get_start_offset (range, NULL);
-       start_offset_node = webkit_dom_range_get_end_container (range, NULL);
-
-       caret_node = e_html_editor_selection_get_caret_position_node (document);
-
-       if (WEBKIT_DOM_IS_TEXT (start_offset_node) && start_offset != 0) {
-               WebKitDOMText *split_text;
-
-               split_text = webkit_dom_text_split_text (
-                               WEBKIT_DOM_TEXT (start_offset_node),
-                               start_offset, NULL);
-               split_node = WEBKIT_DOM_NODE (split_text);
-       } else {
-               split_node = start_offset_node;
-       }
-
-       webkit_dom_node_insert_before (
-               webkit_dom_node_get_parent_node (start_offset_node),
-               caret_node,
-               split_node,
-               NULL);
-
-       return WEBKIT_DOM_ELEMENT (caret_node);
-}
-
-static void
-fix_quoting_nodes_after_caret_restoration (WebKitDOMDOMSelection *window_selection,
-                                           WebKitDOMNode *prev_sibling,
-                                           WebKitDOMNode *next_sibling)
-{
-       WebKitDOMNode *tmp_node;
-
-       if (!element_has_class (WEBKIT_DOM_ELEMENT (prev_sibling), "-x-evo-temp-text-wrapper"))
-               return;
-
-       webkit_dom_dom_selection_modify (
-               window_selection, "move", "forward", "character");
-       tmp_node = webkit_dom_node_get_next_sibling (
-               webkit_dom_node_get_first_child (prev_sibling));
-
-       webkit_dom_node_insert_before (
-               webkit_dom_node_get_parent_node (prev_sibling),
-               tmp_node,
-               next_sibling,
-               NULL);
-
-       tmp_node = webkit_dom_node_get_first_child (prev_sibling);
-
-       webkit_dom_node_insert_before (
-               webkit_dom_node_get_parent_node (prev_sibling),
-               tmp_node,
-               webkit_dom_node_get_previous_sibling (next_sibling),
-               NULL);
-
-       remove_node (prev_sibling);
-
-       webkit_dom_dom_selection_modify (
-               window_selection, "move", "backward", "character");
-}
-
-/**
- * e_html_editor_selection_restore_caret_position:
- * @selection: an #EHTMLEditorSelection
- *
- * Restores previously saved caret position in composer.
- */
-void
-e_html_editor_selection_restore_caret_position (EHTMLEditorSelection *selection)
-{
-       EHTMLEditorView *view;
-       WebKitDOMDocument *document;
-       WebKitDOMElement *element;
-       gboolean fix_after_quoting;
-       gboolean swap_direction = FALSE;
-
-       g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
-
-       view = e_html_editor_selection_ref_html_editor_view (selection);
-       g_return_if_fail (view != NULL);
-
-       document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
-       g_object_unref (view);
-
-       e_html_editor_selection_block_selection_changed (selection);
-
-       element = webkit_dom_document_get_element_by_id (
-               document, "-x-evo-caret-position");
-       fix_after_quoting = element_has_class (element, "-x-evo-caret-quoting");
-
-       if (element) {
-               WebKitDOMDOMWindow *window;
-               WebKitDOMNode *parent_node;
-               WebKitDOMDOMSelection *window_selection;
-               WebKitDOMNode *prev_sibling;
-               WebKitDOMNode *next_sibling;
-
-               if (!webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (element)))
-                       swap_direction = TRUE;
-
-               window = webkit_dom_document_get_default_view (document);
-               window_selection = webkit_dom_dom_window_get_selection (window);
-               parent_node = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element));
-               /* If parent is BODY element, we try to restore the position on the 
-                * element that is next to us */
-               if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent_node)) {
-                       /* Look if we have DIV on right */
-                       next_sibling = webkit_dom_node_get_next_sibling (
-                               WEBKIT_DOM_NODE (element));
-                       if (!WEBKIT_DOM_IS_ELEMENT (next_sibling)) {
-                               e_html_editor_selection_clear_caret_position_marker (selection);
-                               e_html_editor_selection_unblock_selection_changed (selection);
-                               return;
-                       }
-
-                       if (element_has_class (WEBKIT_DOM_ELEMENT (next_sibling), "-x-evo-paragraph")) {
-                               remove_node (WEBKIT_DOM_NODE (element));
-
-                               e_html_editor_selection_move_caret_into_element (
-                                       document, WEBKIT_DOM_ELEMENT (next_sibling), FALSE);
-
-                               goto out;
-                       }
-               }
-
-               e_html_editor_selection_move_caret_into_element (document, element, FALSE);
-
-               if (fix_after_quoting) {
-                       prev_sibling = webkit_dom_node_get_previous_sibling (
-                               WEBKIT_DOM_NODE (element));
-                       next_sibling = webkit_dom_node_get_next_sibling (
-                               WEBKIT_DOM_NODE (element));
-                       if (!next_sibling)
-                               fix_after_quoting = FALSE;
-               }
-
-               remove_node (WEBKIT_DOM_NODE (element));
-
-               if (fix_after_quoting)
-                       fix_quoting_nodes_after_caret_restoration (
-                               window_selection, prev_sibling, next_sibling);
- out:
-               /* FIXME If caret position is restored and afterwards the
-                * position is saved it is not on the place where it supposed
-                * to be (it is in the beginning of parent's element. It can
-                * be avoided by moving with the caret. */
-               if (swap_direction) {
-                       webkit_dom_dom_selection_modify (
-                               window_selection, "move", "forward", "character");
-                       webkit_dom_dom_selection_modify (
-                               window_selection, "move", "backward", "character");
-               } else {
-                       webkit_dom_dom_selection_modify (
-                               window_selection, "move", "backward", "character");
-                       webkit_dom_dom_selection_modify (
-                               window_selection, "move", "forward", "character");
-               }
-       }
-
-       e_html_editor_selection_unblock_selection_changed (selection);
-}
-
 static gint
 find_where_to_break_line (WebKitDOMNode *node,
                           gint max_len,
@@ -6298,7 +6086,7 @@ WebKitDOMElement *
 e_html_editor_selection_put_node_into_paragraph (EHTMLEditorSelection *selection,
                                                  WebKitDOMDocument *document,
                                                  WebKitDOMNode *node,
-                                                 WebKitDOMNode *caret_position)
+                                                 gboolean with_input)
 {
        WebKitDOMRange *range;
        WebKitDOMElement *container;
@@ -6308,7 +6096,8 @@ e_html_editor_selection_put_node_into_paragraph (EHTMLEditorSelection *selection
        webkit_dom_range_select_node (range, node, NULL);
        webkit_dom_range_surround_contents (range, WEBKIT_DOM_NODE (container), NULL);
        /* We have to move caret position inside this container */
-       webkit_dom_node_append_child (WEBKIT_DOM_NODE (container), caret_position, NULL);
+       if (with_input)
+               add_selection_markers_into_element_end (document, container, NULL, NULL);
 
        return container;
 }
diff --git a/e-util/e-html-editor-selection.h b/e-util/e-html-editor-selection.h
index 67bbe6b..544c09e 100644
--- a/e-util/e-html-editor-selection.h
+++ b/e-util/e-html-editor-selection.h
@@ -187,16 +187,6 @@ void               e_html_editor_selection_move_caret_into_element
                                                (WebKitDOMDocument *document,
                                                 WebKitDOMElement *element,
                                                 gboolean to_start);
-void           e_html_editor_selection_clear_caret_position_marker
-                                               (EHTMLEditorSelection *selection);
-WebKitDOMNode *
-               e_html_editor_selection_get_caret_position_node
-                                               (WebKitDOMDocument *document);
-WebKitDOMElement *
-               e_html_editor_selection_save_caret_position
-                                               (EHTMLEditorSelection *selection);
-void           e_html_editor_selection_restore_caret_position
-                                               (EHTMLEditorSelection *selection);
 void           e_html_editor_selection_set_indented_style
                                                (EHTMLEditorSelection *selection,
                                                 WebKitDOMElement *element,
@@ -223,7 +213,7 @@ WebKitDOMElement *
                                                (EHTMLEditorSelection *selection,
                                                 WebKitDOMDocument *document,
                                                 WebKitDOMNode *node,
-                                                WebKitDOMNode *caret_position);
+                                                gboolean with_input);
 void           e_html_editor_selection_wrap_lines
                                                (EHTMLEditorSelection *selection);
 WebKitDOMElement *
diff --git a/e-util/e-html-editor-view.c b/e-util/e-html-editor-view.c
index 9b2c08c..4a2c5e9 100644
--- a/e-util/e-html-editor-view.c
+++ b/e-util/e-html-editor-view.c
@@ -1253,8 +1253,7 @@ html_editor_view_check_magic_links (EHTMLEditorView *view,
                document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
 
                if (!view->priv->return_key_pressed)
-                       e_html_editor_selection_save_caret_position (
-                               e_html_editor_view_get_selection (view));
+                       e_html_editor_selection_save (view->priv->selection);
 
                g_match_info_fetch_pos (match_info, 0, &start_pos_url, &end_pos_url);
 
@@ -1305,8 +1304,7 @@ html_editor_view_check_magic_links (EHTMLEditorView *view,
                        NULL);
 
                if (!view->priv->return_key_pressed)
-                       e_html_editor_selection_restore_caret_position (
-                               e_html_editor_view_get_selection (view));
+                       e_html_editor_selection_restore (view->priv->selection);
 
                g_free (url_end_raw);
                g_free (final_url);
@@ -2005,10 +2003,7 @@ surround_text_with_paragraph_if_needed (EHTMLEditorSelection *selection,
        if (WEBKIT_DOM_IS_TEXT (node) &&
            WEBKIT_DOM_IS_HTML_BODY_ELEMENT (webkit_dom_node_get_parent_node (node))) {
                element = e_html_editor_selection_put_node_into_paragraph (
-                       selection,
-                       document,
-                       node,
-                       e_html_editor_selection_get_caret_position_node (document));
+                       selection, document, node, TRUE);
 
                if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (next_sibling))
                        remove_node (next_sibling);
@@ -2207,9 +2202,12 @@ body_input_event_cb (WebKitDOMElement *element,
                node = webkit_dom_range_get_end_container (range, NULL);
 
                if (surround_text_with_paragraph_if_needed (selection, document, node)) {
-                       e_html_editor_selection_restore_caret_position (selection);
-                       node = webkit_dom_range_get_end_container (range, NULL);
-                       range = html_editor_view_get_dom_range (view);
+                       WebKitDOMElement *element;
+
+                       element = webkit_dom_document_get_element_by_id (
+                               document, "-x-evo-selection-start-marker");
+                       node = webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (element));
+                       e_html_editor_selection_restore (selection);
                }
 
                if (WEBKIT_DOM_IS_TEXT (node)) {
@@ -2739,10 +2737,7 @@ e_html_editor_view_insert_quoted_text (EHTMLEditorView *view,
        webkit_dom_html_element_set_inner_text (
                WEBKIT_DOM_HTML_ELEMENT (element), escaped_text, NULL);
 
-       webkit_dom_node_append_child (
-               WEBKIT_DOM_NODE (element),
-               e_html_editor_selection_get_caret_position_node (document),
-               NULL);
+       add_selection_markers_into_element_end (document, element, NULL, NULL);
 
        blockquote = webkit_dom_document_create_element (document, "blockquote", NULL);
        webkit_dom_element_set_attribute (blockquote, "type", "cite", NULL);
@@ -2801,8 +2796,6 @@ e_html_editor_view_insert_quoted_text (EHTMLEditorView *view,
                        NULL);
        }
 
-       e_html_editor_selection_restore_caret_position (selection);
-
        if (ev) {
                e_html_editor_selection_get_selection_coordinates (
                        selection,
@@ -2813,6 +2806,8 @@ e_html_editor_view_insert_quoted_text (EHTMLEditorView *view,
                e_html_editor_view_insert_new_history_event (view, ev);
        }
 
+       e_html_editor_selection_restore (selection);
+
        e_html_editor_view_force_spell_check_for_current_paragraph (view);
 
        e_html_editor_view_set_changed (view, TRUE);
@@ -4724,17 +4719,6 @@ quote_plain_text_recursive (WebKitDOMDocument *document,
                if (!(WEBKIT_DOM_IS_ELEMENT (node) || WEBKIT_DOM_IS_HTML_ELEMENT (node)))
                        goto next_node;
 
-               if (element_has_id (WEBKIT_DOM_ELEMENT (node), "-x-evo-caret-position")) {
-                       if (quote_level > 0)
-                               element_add_class (
-                                       WEBKIT_DOM_ELEMENT (node), "-x-evo-caret-quoting");
-
-                       move_next = TRUE;
-                       suppress_next = TRUE;
-                       next = FALSE;
-                       goto next_node;
-               }
-
                if (element_is_selection_marker (WEBKIT_DOM_ELEMENT (node))) {
                        /* If there is collapsed selection in the beginning of line
                         * we cannot suppress first text that is after the end of
@@ -7293,21 +7277,6 @@ process_elements (EHTMLEditorView *view,
                if (WEBKIT_DOM_IS_COMMENT (child) || !WEBKIT_DOM_IS_ELEMENT (child))
                        goto next;
 
-               /* Leave caret position untouched */
-               if (element_has_id (WEBKIT_DOM_ELEMENT (child), "-x-evo-caret-position")) {
-                       if (changing_mode && to_plain_text) {
-                               content = webkit_dom_html_element_get_outer_html (
-                                       WEBKIT_DOM_HTML_ELEMENT (child));
-                               g_string_append (buffer, content);
-                               g_free (content);
-                       }
-                       if (to_html)
-                               remove_node (child);
-
-                       skip_node = TRUE;
-                       goto next;
-               }
-
                if (element_has_class (WEBKIT_DOM_ELEMENT (child), "Apple-tab-span")) {
                        if (!changing_mode && to_plain_text) {
                                gchar *content, *tmp;
@@ -8083,9 +8052,7 @@ process_content_for_plain_text (EHTMLEditorView *view)
        g_object_unref (paragraphs);
 
        paragraphs = webkit_dom_element_query_selector_all (
-               WEBKIT_DOM_ELEMENT (source),
-               "span[id^=\"-x-evo-selection-\"], span#-x-evo-caret-position",
-               NULL);
+               WEBKIT_DOM_ELEMENT (source), "span[id^=\"-x-evo-selection-\"]", NULL);
 
        length = webkit_dom_node_list_get_length (paragraphs);
        for (ii = 0; ii < length; ii++) {


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