[evolution/webkit-composer: 96/210] Bug #689801 - Neverending text 'Replace All'



commit eb82faa39427b9302b6f028b1079574536b3d12f
Author: Dan Vrátil <dvratil redhat com>
Date:   Thu Dec 6 23:52:06 2012 +0100

    Bug #689801 - Neverending text 'Replace All'
    
    Fix possible endless loop when replacing a string by another
    which differ only in case of some letters (i.e. 'a' -> 'A')
    in case-insensitive mode.
    
    Also make the replace operation an undoable action.

 e-util/e-editor-replace-dialog.c |    3 +
 e-util/e-editor-selection.c      |   79 +++++++++++++++++++++++++++++++++-----
 e-util/e-editor-selection.h      |   12 ++++++
 3 files changed, 84 insertions(+), 10 deletions(-)
---
diff --git a/e-util/e-editor-replace-dialog.c b/e-util/e-editor-replace-dialog.c
index fbf7771..0b09f21 100644
--- a/e-util/e-editor-replace-dialog.c
+++ b/e-util/e-editor-replace-dialog.c
@@ -128,6 +128,9 @@ editor_replace_dialog_replace_all_cb (EEditorReplaceDialog *dialog)
        while (jump (dialog)) {
                e_editor_selection_replace (selection, replacement);
                i++;
+               
+               /* Jump behind the word */
+               e_editor_selection_move (selection, TRUE, E_EDITOR_SELECTION_GRANULARITY_WORD);
        }
 
        result = g_strdup_printf (_("%d occurences replaced"), i);
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index d374782..2b1fde5 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -831,18 +831,12 @@ void
 e_editor_selection_replace (EEditorSelection *selection,
                            const gchar *new_string)
 {
-       WebKitDOMDocumentFragment *frag;
-       WebKitDOMRange *range;
-
        g_return_if_fail (E_IS_EDITOR_SELECTION (selection));
 
-       range = editor_selection_get_current_range (selection);
-
-       frag = webkit_dom_range_create_contextual_fragment (
-                       range, new_string, NULL);
-
-       webkit_dom_range_delete_contents (range, NULL);
-       webkit_dom_range_insert_node (range, WEBKIT_DOM_NODE (frag), NULL);
+       e_editor_widget_exec_command (
+               E_EDITOR_WIDGET (selection->priv->webview),
+               E_EDITOR_WIDGET_COMMAND_INSERT_TEXT,
+               new_string);
 }
 
 /**
@@ -2189,3 +2183,68 @@ e_editor_selection_restore (EEditorSelection* selection)
              webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (marker)),
              WEBKIT_DOM_NODE (marker), NULL);
 }
+
+
+static void
+editor_selection_modify (EEditorSelection *selection,
+                        const gchar *alter,
+                        gboolean forward,
+                        EEditorSelectionGranularity granularity)
+{
+       WebKitDOMDocument *document;
+       WebKitDOMDOMWindow *window;
+       WebKitDOMDOMSelection *dom_selection;
+       const gchar *granularity_str;
+
+       g_return_if_fail (E_IS_EDITOR_SELECTION (selection));
+
+       document = webkit_web_view_get_dom_document (selection->priv->webview);
+       window = webkit_dom_document_get_default_view (document);
+       dom_selection = webkit_dom_dom_window_get_selection (window);
+
+       switch (granularity) {
+               case E_EDITOR_SELECTION_GRANULARITY_CHARACTER:
+                       granularity_str = "character";
+                       break;
+               case E_EDITOR_SELECTION_GRANULARITY_WORD:
+                       granularity_str = "word";
+                       break;
+       }
+
+       webkit_dom_dom_selection_modify (
+               dom_selection, alter,
+               (forward ? "forward" : "backward"),
+               granularity_str);
+}
+
+/**
+ * e_editor_selection_extend:
+ * @selection: an #EEditorSelection
+ * @forward: whether to extend selection forward or backward
+ * @granularity: granularity of the extension
+ *
+ * Extends current selection in given direction by given granularity.
+ */
+void
+e_editor_selection_extend (EEditorSelection* selection,
+                          gboolean forward,
+                          EEditorSelectionGranularity granularity)
+{
+       editor_selection_modify (selection, "extend", forward, granularity);
+}
+
+/**
+ * e_editor_selection_move:
+ * @selection: an #EEditorSelection
+ * @forward: whether to move the selection forward or backward
+ * @granularity: granularity of the movement
+ *
+ * Moves current selection in given direction by given granularity
+ */
+void
+e_editor_selection_move (EEditorSelection* selection,
+                        gboolean forward,
+                        EEditorSelectionGranularity granularity)
+{
+       editor_selection_modify (selection, "move", forward, granularity);
+}
diff --git a/e-util/e-editor-selection.h b/e-util/e-editor-selection.h
index 1933282..8b07373 100644
--- a/e-util/e-editor-selection.h
+++ b/e-util/e-editor-selection.h
@@ -87,6 +87,11 @@ typedef enum {
        E_EDITOR_SELECTION_ALIGNMENT_RIGHT
 } EEditorSelectionAlignment;
 
+typedef enum {
+       E_EDITOR_SELECTION_GRANULARITY_CHARACTER,
+       E_EDITOR_SELECTION_GRANULARITY_WORD
+} EEditorSelectionGranularity;
+
 struct _EEditorSelection {
        GObject parent;
 
@@ -209,6 +214,13 @@ void                       e_editor_selection_wrap_lines   (EEditorSelection *selection);
 void                   e_editor_selection_save         (EEditorSelection *selection);
 void                   e_editor_selection_restore      (EEditorSelection *selection);
 
+void                   e_editor_selection_move         (EEditorSelection *selection,
+                                                        gboolean forward,
+                                                        EEditorSelectionGranularity granularity);
+void                   e_editor_selection_extend       (EEditorSelection *selection,
+                                                        gboolean forward,
+                                                        EEditorSelectionGranularity granularity);
+
 G_END_DECLS
 
 #endif /* E_EDITOR_SELECTION_H */


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