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



commit b368658314160d133514ca8882ba11dd7bc438d6
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 2bf3bdd..b17a88d 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -830,18 +830,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);
 }
 
 /**
@@ -2188,3 +2182,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 5ffc91c..8bcea73 100644
--- a/e-util/e-editor-selection.h
+++ b/e-util/e-editor-selection.h
@@ -88,6 +88,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;
 
@@ -210,6 +215,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]