[evolution/wip/webkit2] Optimize some of the DOM functions related to selection



commit 48945fe6cc15dcd9ef6901d238e526867afc87f0
Author: Tomas Popela <tpopela redhat com>
Date:   Tue May 31 11:34:47 2016 +0200

    Optimize some of the DOM functions related to selection
    
    Utilize webkit_dom_dom_selection_get_is_collapsed when we don't need the Range
    object and don't clone the current range and traverse it when determining if the
    current selection has text.

 .../e-html-editor-actions-dom-functions.c          |    8 +--
 .../e-html-editor-selection-dom-functions.c        |   75 +++++++++-----------
 2 files changed, 35 insertions(+), 48 deletions(-)
---
diff --git a/modules/webkit-content-editor/web-extension/e-html-editor-actions-dom-functions.c 
b/modules/webkit-content-editor/web-extension/e-html-editor-actions-dom-functions.c
index 624fe47..29a25af 100644
--- a/modules/webkit-content-editor/web-extension/e-html-editor-actions-dom-functions.c
+++ b/modules/webkit-content-editor/web-extension/e-html-editor-actions-dom-functions.c
@@ -380,17 +380,13 @@ dom_save_history_for_cut (WebKitDOMDocument *document,
        dom_selection = webkit_dom_dom_window_get_selection (dom_window);
        g_object_unref (dom_window);
 
-       if (!webkit_dom_dom_selection_get_range_count (dom_selection)) {
+       if (!webkit_dom_dom_selection_get_range_count (dom_selection) ||
+           webkit_dom_dom_selection_get_is_collapsed (dom_selection)) {
                g_object_unref (dom_selection);
                return;
        }
 
        range = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL);
-       if (webkit_dom_range_get_collapsed (range, NULL)) {
-               g_object_unref (range);
-               g_object_unref (dom_selection);
-               return;
-       }
 
        ev = g_new0 (EHTMLEditorHistoryEvent, 1);
        ev->type = HISTORY_DELETE;
diff --git a/modules/webkit-content-editor/web-extension/e-html-editor-selection-dom-functions.c 
b/modules/webkit-content-editor/web-extension/e-html-editor-selection-dom-functions.c
index a14524a..3e97ded 100644
--- a/modules/webkit-content-editor/web-extension/e-html-editor-selection-dom-functions.c
+++ b/modules/webkit-content-editor/web-extension/e-html-editor-selection-dom-functions.c
@@ -2393,14 +2393,20 @@ gboolean
 dom_selection_is_collapsed (WebKitDOMDocument *document)
 {
        gboolean collapsed;
-       WebKitDOMRange *range;
+       WebKitDOMDOMWindow *dom_window;
+       WebKitDOMDOMSelection *dom_selection;
 
-       range = dom_get_current_range (document);
-       if (!range)
-               return TRUE;
+       if (!(dom_window = webkit_dom_document_get_default_view (document)))
+               return FALSE;
 
-       collapsed = webkit_dom_range_get_collapsed (range, NULL);
-       g_object_unref (range);
+       if (!(dom_selection = webkit_dom_dom_window_get_selection (dom_window))) {
+               g_object_unref (dom_window);
+               return FALSE;
+       }
+
+       collapsed = webkit_dom_dom_selection_get_is_collapsed (dom_selection);
+
+       g_object_unref (dom_selection);
 
        return collapsed;
 }
@@ -6650,48 +6656,33 @@ dom_get_caret_word (WebKitDOMDocument *document)
 gboolean
 dom_selection_has_text (WebKitDOMDocument *document)
 {
-       WebKitDOMRange *range;
-       WebKitDOMNode *node;
-
-       range = dom_get_current_range (document);
-
-       node = webkit_dom_range_get_start_container (range, NULL);
-       if (WEBKIT_DOM_IS_TEXT (node)) {
-               g_object_unref (range);
-               return TRUE;
-       }
+       gboolean has_text = FALSE;
+       gchar *text = NULL;
+       WebKitDOMDOMWindow *dom_window = NULL;
+       WebKitDOMDOMSelection *dom_selection = NULL;
+       WebKitDOMRange *range = NULL;
 
-       node = webkit_dom_range_get_end_container (range, NULL);
-       if (WEBKIT_DOM_IS_TEXT (node)) {
-               g_object_unref (range);
-               return TRUE;
-       }
+       if (!(dom_window = webkit_dom_document_get_default_view (document)))
+               goto out;
 
-       node = WEBKIT_DOM_NODE (webkit_dom_range_clone_contents (range, NULL));
-       while (node) {
-               if (WEBKIT_DOM_IS_TEXT (node)) {
-                       g_object_unref (range);
-                       return TRUE;
-               }
+       if (!(dom_selection = webkit_dom_dom_window_get_selection (dom_window)))
+               goto out;
 
-               if (webkit_dom_node_has_child_nodes (node)) {
-                       node = webkit_dom_node_get_first_child (node);
-               } else if (webkit_dom_node_get_next_sibling (node)) {
-                       node = webkit_dom_node_get_next_sibling (node);
-               } else {
-                       node = webkit_dom_node_get_parent_node (node);
-                       if (node) {
-                               node = webkit_dom_node_get_next_sibling (node);
-                       }
-               }
-       }
+       if (webkit_dom_dom_selection_get_is_collapsed (dom_selection))
+               goto out;
 
-       if (node)
-               g_object_unref (node);
+       if (!(range = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL)))
+               goto out;
 
-       g_object_unref (range);
+       text = webkit_dom_range_get_text (range);
+       has_text = text && *text;
+ out:
+       g_free (text);
+       g_clear_object (&dom_window);
+       g_clear_object (&dom_selection);
+       g_clear_object (&range);
 
-       return FALSE;
+       return has_text;
 }
 
 /**


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