[evolution/wip/webkit-composer] EEditorSelection: Fix functions for saving/restoring selection



commit 8b7e03813e4e99dd9f63a1f99707c9016d0bc10a
Author: Tomas Popela <tpopela redhat com>
Date:   Thu Feb 27 22:50:17 2014 +0100

    EEditorSelection: Fix functions for saving/restoring selection

 e-util/e-editor-selection.c |   73 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 62 insertions(+), 11 deletions(-)
---
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index 3a5f080..6ba3169 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -4450,10 +4450,11 @@ e_editor_selection_save (EEditorSelection *selection)
 
                marker = webkit_dom_document_create_element (
                        document, "SPAN", NULL);
-               webkit_dom_element_set_id (marker, "-x-evolution-selection-start-marker");
+               webkit_dom_element_set_id (
+                       marker, "-x-evolution-selection-start-marker");
 
                container = webkit_dom_range_get_start_container (range, NULL);
-               if (WEBKIT_DOM_IS_TEXT (container)) {
+               if (WEBKIT_DOM_IS_TEXT (container) && start_offset != 0) {
                        WebKitDOMText *split_text;
 
                        split_text = webkit_dom_text_split_text (
@@ -4461,21 +4462,31 @@ e_editor_selection_save (EEditorSelection *selection)
                                start_offset, NULL);
                        split_node = WEBKIT_DOM_NODE (split_text);
                } else {
-                       split_node = container;
+                       if (!webkit_dom_node_get_previous_sibling (container)) {
+                               split_node = webkit_dom_node_get_parent_node (
+                                       container);
+                       } else if (!webkit_dom_node_get_next_sibling (container)) {
+                               split_node = webkit_dom_node_get_parent_node (
+                                       container);
+                               split_node = webkit_dom_node_get_next_sibling (
+                                       split_node);
+                       } else
+                               split_node = container;
                }
 
                marker_node = WEBKIT_DOM_NODE (marker);
-               parent_node = webkit_dom_node_get_parent_node (container);
+               parent_node = webkit_dom_node_get_parent_node (split_node);
 
                webkit_dom_node_insert_before (
                        parent_node, marker_node, split_node, NULL);
 
                marker = webkit_dom_document_create_element (
                        document, "SPAN", NULL);
-               webkit_dom_element_set_id (marker, "-x-evolution-selection-end-marker");
+               webkit_dom_element_set_id (
+                       marker, "-x-evolution-selection-end-marker");
 
                container = webkit_dom_range_get_end_container (range, NULL);
-               if (WEBKIT_DOM_IS_TEXT (container)) {
+               if (WEBKIT_DOM_IS_TEXT (container) && start_offset != 0) {
                        WebKitDOMText *split_text;
 
                        split_text = webkit_dom_text_split_text (
@@ -4483,11 +4494,20 @@ e_editor_selection_save (EEditorSelection *selection)
                                start_offset, NULL);
                        split_node = WEBKIT_DOM_NODE (split_text);
                } else {
-                       split_node = container;
+                       if (!webkit_dom_node_get_previous_sibling (container)) {
+                               split_node = webkit_dom_node_get_parent_node (
+                                       container);
+                       } else if (!webkit_dom_node_get_next_sibling (container)) {
+                               split_node = webkit_dom_node_get_parent_node (
+                                       container);
+                               split_node = webkit_dom_node_get_next_sibling (
+                                       split_node);
+                       } else
+                               split_node = container;
                }
 
                marker_node = WEBKIT_DOM_NODE (marker);
-               parent_node = webkit_dom_node_get_parent_node (container);
+               parent_node = webkit_dom_node_get_parent_node (split_node);
 
                webkit_dom_node_insert_before (
                        parent_node, marker_node, split_node, NULL);
@@ -4512,6 +4532,8 @@ e_editor_selection_restore (EEditorSelection *selection)
        EEditorWidget *editor_widget;
        WebKitWebView *web_view;
        WebKitDOMDocument *document;
+       WebKitDOMDOMWindow *window;
+       WebKitDOMDOMSelection *dom_selection;
        WebKitDOMRange *range;
        WebKitDOMElement *marker;
 
@@ -4523,7 +4545,9 @@ e_editor_selection_restore (EEditorSelection *selection)
        web_view = WEBKIT_WEB_VIEW (editor_widget);
 
        document = webkit_web_view_get_dom_document (web_view);
-       range = editor_selection_get_current_range (selection);
+       window = webkit_dom_document_get_default_view (document);
+       dom_selection = webkit_dom_dom_window_get_selection (window);
+       range = webkit_dom_document_create_range (document);
 
        if (range != NULL) {
                WebKitDOMNode *marker_node;
@@ -4531,7 +4555,19 @@ e_editor_selection_restore (EEditorSelection *selection)
 
                marker = webkit_dom_document_get_element_by_id (
                        document, "-x-evolution-selection-start-marker");
-               g_return_if_fail (marker != NULL);
+               if (!marker) {
+                       marker = webkit_dom_document_get_element_by_id (
+                               document, "-x-evolution-selection-end-marker");
+
+                       if (marker) {
+                               webkit_dom_node_remove_child (
+                                       webkit_dom_node_get_parent_node (
+                                               WEBKIT_DOM_NODE (marker)),
+                                       WEBKIT_DOM_NODE (marker),
+                                       NULL);
+                       }
+                       return;
+               }
 
                marker_node = WEBKIT_DOM_NODE (marker);
                parent_node = webkit_dom_node_get_parent_node (marker_node);
@@ -4541,13 +4577,28 @@ e_editor_selection_restore (EEditorSelection *selection)
 
                marker = webkit_dom_document_get_element_by_id (
                        document, "-x-evolution-selection-end-marker");
-               g_return_if_fail (marker != NULL);
+               if (!marker) {
+                       marker = webkit_dom_document_get_element_by_id (
+                               document, "-x-evolution-selection-start-marker");
+
+                       if (marker) {
+                               webkit_dom_node_remove_child (
+                                       webkit_dom_node_get_parent_node (
+                                               WEBKIT_DOM_NODE (marker)),
+                                       WEBKIT_DOM_NODE (marker),
+                                       NULL);
+                       }
+                       return;
+               }
 
                marker_node = WEBKIT_DOM_NODE (marker);
                parent_node = webkit_dom_node_get_parent_node (marker_node);
 
                webkit_dom_range_set_end_before (range, marker_node, NULL);
                webkit_dom_node_remove_child (parent_node, marker_node, NULL);
+
+               webkit_dom_dom_selection_remove_all_ranges (dom_selection);
+               webkit_dom_dom_selection_add_range (dom_selection, range);
        }
 
        g_object_unref (editor_widget);


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