[evolution/wip/webkit-composer: 41/262] Change the node-traversal helper functions



commit 97be34997a225d8c92a5045b3211a395ca595fa4
Author: Dan Vrátil <dvratil redhat com>
Date:   Tue Aug 14 22:15:53 2012 +0200

    Change the node-traversal helper functions
    
    Rename e_editor_dom_node_get_parent_element() to
    e_editor_dom_node_find_parent_element() and change both functions
    to search not by GType of the object returned by WebKit, but by
    the actual tag name. Search by GType was broken, because WebKit
    often returns objects wich are of different type then the actual
    DOM element they wrap. Usually individual HTML elements, like
    WebKitDOMHTMLTableElement, are returned as instances of
    WebKitDOMHTMLElement and thus the GType check fails.

 e-util/e-editor-hrule-dialog.c |   11 +++------
 e-util/e-editor-image-dialog.c |    9 +++----
 e-util/e-editor-link-dialog.c  |   16 +++++---------
 e-util/e-editor-selection.c    |    3 +-
 e-util/e-editor-table-dialog.c |    5 +--
 e-util/e-editor-utils.c        |   44 ++++++++++++++++++++++++++++++++-------
 e-util/e-editor-utils.h        |    6 ++--
 e-util/e-editor.c              |   12 +++++-----
 8 files changed, 62 insertions(+), 44 deletions(-)
---
diff --git a/e-util/e-editor-hrule-dialog.c b/e-util/e-editor-hrule-dialog.c
index 4c2e5a7..0528c12 100644
--- a/e-util/e-editor-hrule-dialog.c
+++ b/e-util/e-editor-hrule-dialog.c
@@ -223,13 +223,11 @@ editor_hrule_dialog_show (GtkWidget *widget)
 
        range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL);
 
-       rule = e_editor_dom_node_get_parent_element (
-               webkit_dom_range_get_start_container (range, NULL),
-               WEBKIT_TYPE_DOM_HTMLHR_ELEMENT);
+       rule = e_editor_dom_node_find_parent_element (
+               webkit_dom_range_get_start_container (range, NULL), "HR");
        if (!rule) {
                rule = e_editor_dom_node_find_child_element (
-                       webkit_dom_range_get_start_container (range, NULL),
-                       WEBKIT_TYPE_DOM_HTMLHR_ELEMENT);
+                       webkit_dom_range_get_start_container (range, NULL), "HR");
        }
 
        if (!rule) {
@@ -237,8 +235,7 @@ editor_hrule_dialog_show (GtkWidget *widget)
                        document, "insertHorizontalRule", FALSE, "");
 
                rule = e_editor_dom_node_find_child_element (
-                       webkit_dom_range_get_start_container (range, NULL),
-                       WEBKIT_TYPE_DOM_HTMLHR_ELEMENT);
+                       webkit_dom_range_get_start_container (range, NULL), "HR");
 
                dialog->priv->hr_element = WEBKIT_DOM_HTMLHR_ELEMENT (rule);
 
diff --git a/e-util/e-editor-image-dialog.c b/e-util/e-editor-image-dialog.c
index 56bcecf..40686e0 100644
--- a/e-util/e-editor-image-dialog.c
+++ b/e-util/e-editor-image-dialog.c
@@ -274,9 +274,8 @@ editor_image_dialog_set_url (EEditorImageDialog *dialog)
        const gchar *url;
 
        url = gtk_entry_get_text (GTK_ENTRY (dialog->priv->url_edit));
-       link = e_editor_dom_node_get_parent_element (
-               (WebKitDOMNode *) dialog->priv->image,
-               WEBKIT_TYPE_DOM_HTML_ANCHOR_ELEMENT);
+       link = e_editor_dom_node_find_parent_element (
+               (WebKitDOMNode *) dialog->priv->image, "A");
 
        if (link) {
                if (!url || !*url) {
@@ -378,8 +377,8 @@ editor_image_dialog_show (GtkWidget *gtk_widget)
                GTK_SPIN_BUTTON (dialog->priv->y_padding_edit), val);
 
 
-       link = e_editor_dom_node_get_parent_element (
-               WEBKIT_DOM_NODE (dialog->priv->image), WEBKIT_TYPE_DOM_HTML_ANCHOR_ELEMENT);
+       link = e_editor_dom_node_find_parent_element (
+                       WEBKIT_DOM_NODE (dialog->priv->image), "A");
        if (link) {
                tmp = webkit_dom_html_anchor_element_get_href (
                                (WebKitDOMHTMLAnchorElement *) link);
diff --git a/e-util/e-editor-link-dialog.c b/e-util/e-editor-link-dialog.c
index ce68c03..ae7707f 100644
--- a/e-util/e-editor-link-dialog.c
+++ b/e-util/e-editor-link-dialog.c
@@ -99,9 +99,8 @@ editor_link_dialog_ok (EEditorLinkDialog *dialog)
        }
 
        range = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL);
-       link = e_editor_dom_node_get_parent_element (
-               webkit_dom_range_get_start_container (range, NULL),
-               WEBKIT_TYPE_DOM_HTML_ANCHOR_ELEMENT);
+       link = e_editor_dom_node_find_parent_element (
+                       webkit_dom_range_get_start_container (range, NULL), "A");
        if (!link) {
                if ((webkit_dom_range_get_start_container (range, NULL) !=
                        webkit_dom_range_get_end_container (range, NULL)) ||
@@ -111,8 +110,7 @@ editor_link_dialog_ok (EEditorLinkDialog *dialog)
                        WebKitDOMDocumentFragment *fragment;
                        fragment = webkit_dom_range_extract_contents (range, NULL);
                        link = e_editor_dom_node_find_child_element (
-                               WEBKIT_DOM_NODE (fragment),
-                               WEBKIT_TYPE_DOM_HTML_ANCHOR_ELEMENT);
+                               WEBKIT_DOM_NODE (fragment), "A");
                        webkit_dom_range_insert_node (
                                range, WEBKIT_DOM_NODE (fragment), NULL);
 
@@ -195,9 +193,8 @@ editor_link_dialog_show (GtkWidget *widget)
        }
 
        range = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL);
-       link = e_editor_dom_node_get_parent_element (
-               webkit_dom_range_get_start_container (range, NULL),
-               WEBKIT_TYPE_DOM_HTML_ANCHOR_ELEMENT);
+       link = e_editor_dom_node_find_parent_element (
+               webkit_dom_range_get_start_container (range, NULL), "A");
        if (!link) {
                if ((webkit_dom_range_get_start_container (range, NULL) !=
                        webkit_dom_range_get_end_container (range, NULL)) ||
@@ -207,8 +204,7 @@ editor_link_dialog_show (GtkWidget *widget)
                        WebKitDOMDocumentFragment *fragment;
                        fragment = webkit_dom_range_clone_contents (range, NULL);
                        link = e_editor_dom_node_find_child_element (
-                               WEBKIT_DOM_NODE (fragment),
-                               WEBKIT_TYPE_DOM_HTML_ANCHOR_ELEMENT);
+                                       WEBKIT_DOM_NODE (fragment), "A");
                }
        }
 
diff --git a/e-util/e-editor-selection.c b/e-util/e-editor-selection.c
index f1818a2..710b914 100644
--- a/e-util/e-editor-selection.c
+++ b/e-util/e-editor-selection.c
@@ -169,8 +169,7 @@ get_font_property (EEditorSelection *selection,
        }
 
        node = webkit_dom_range_get_common_ancestor_container (range, NULL);
-       element = e_editor_dom_node_get_parent_element (
-                       node, WEBKIT_TYPE_DOM_HTML_FONT_ELEMENT);
+       element = e_editor_dom_node_find_parent_element (node, "FONT");
        if (!element) {
                return NULL;
        }
diff --git a/e-util/e-editor-table-dialog.c b/e-util/e-editor-table-dialog.c
index 6016aaf..20fde99 100644
--- a/e-util/e-editor-table-dialog.c
+++ b/e-util/e-editor-table-dialog.c
@@ -565,9 +565,8 @@ editor_table_dialog_show (GtkWidget *widget)
                WebKitDOMRange *range;
 
                range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL);
-               table = e_editor_dom_node_get_parent_element (
-                       webkit_dom_range_get_start_container (range, NULL),
-                       WEBKIT_TYPE_DOM_HTML_TABLE_ELEMENT);
+               table = e_editor_dom_node_find_parent_element (
+                       webkit_dom_range_get_start_container (range, NULL), "TABLE");
 
                if (!table) {
                        dialog->priv->table_element = (WebKitDOMHTMLTableElement *)
diff --git a/e-util/e-editor-utils.c b/e-util/e-editor-utils.c
index 29ca04c..5daabff 100644
--- a/e-util/e-editor-utils.c
+++ b/e-util/e-editor-utils.c
@@ -21,16 +21,31 @@
 #endif
 
 #include "e-editor-utils.h"
-
+#include <string.h>
 
 WebKitDOMElement *
-e_editor_dom_node_get_parent_element (WebKitDOMNode *node,
-                                     GType parent_type)
+e_editor_dom_node_find_parent_element (WebKitDOMNode *node,
+                                      const gchar *tagname)
 {
+       gint taglen = strlen (tagname);
+
        while (node) {
 
-               if (G_TYPE_CHECK_INSTANCE_TYPE (node, parent_type))
-                       return (WebKitDOMElement *) node;
+               if (WEBKIT_DOM_IS_ELEMENT (node)) {
+                       gchar *node_tagname;
+
+                       node_tagname = webkit_dom_element_get_tag_name (
+                                               WEBKIT_DOM_ELEMENT (node));
+
+                       if (node_tagname &&
+                           (strlen (node_tagname) == taglen) &&
+                           (g_ascii_strncasecmp (node_tagname, tagname, taglen) == 0)) {
+                               g_free (node_tagname);
+                               return (WebKitDOMElement *) node;
+                       }
+
+                       g_free (node_tagname);
+               }
 
                node = (WebKitDOMNode *) webkit_dom_node_get_parent_element (node);
        }
@@ -40,13 +55,26 @@ e_editor_dom_node_get_parent_element (WebKitDOMNode *node,
 
 WebKitDOMElement *
 e_editor_dom_node_find_child_element (WebKitDOMNode *node,
-                                     GType child_type)
+                                     const gchar *tagname)
 {
        WebKitDOMNode *start_node = node;
+       gint taglen = strlen (tagname);
 
        do {
-               if (G_TYPE_CHECK_INSTANCE_TYPE (node, child_type)) {
-                       return WEBKIT_DOM_ELEMENT (node);
+               if (WEBKIT_DOM_IS_ELEMENT (node)) {
+                       gchar *node_tagname;
+
+                       node_tagname = webkit_dom_element_get_tag_name (
+                                       WEBKIT_DOM_ELEMENT (node));
+
+                       if (node_tagname &&
+                           (strlen (node_tagname) == taglen) &&
+                           (g_ascii_strncasecmp (node_tagname, tagname, taglen) == 0)) {
+                               g_free (node_tagname);
+                               return (WebKitDOMElement *) node;
+                       }
+
+                       g_free (node_tagname);
                }
 
                if (webkit_dom_node_has_child_nodes (node)) {
diff --git a/e-util/e-editor-utils.h b/e-util/e-editor-utils.h
index 6786628..5e370c7 100644
--- a/e-util/e-editor-utils.h
+++ b/e-util/e-editor-utils.h
@@ -25,12 +25,12 @@
 
 #include <webkit/webkitdom.h>
 
-WebKitDOMElement *     e_editor_dom_node_get_parent_element
+WebKitDOMElement *     e_editor_dom_node_find_parent_element
                                                        (WebKitDOMNode *node,
-                                                        GType parent_type);
+                                                        const gchar *tagname);
 
 WebKitDOMElement *     e_editor_dom_node_find_child_element
                                                        (WebKitDOMNode *node,
-                                                        GType child_type);
+                                                        const gchar *tagname);
 
 #endif /* E_EDITOR_UTILS_H */
diff --git a/e-util/e-editor.c b/e-util/e-editor.c
index fe94236..23ee597 100644
--- a/e-util/e-editor.c
+++ b/e-util/e-editor.c
@@ -96,13 +96,13 @@ editor_update_actions (EEditor *editor,
         *   - Cursor is on a link.
         *   - Cursor is on an image that has a URL or target.
         */
-       visible = (e_editor_dom_node_get_parent_element (
-                       node, WEBKIT_TYPE_DOM_HTML_ANCHOR_ELEMENT) != NULL);
+       visible = (WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (node) ||
+                  (e_editor_dom_node_find_parent_element (node, "A") != NULL));
        gtk_action_set_visible (ACTION (CONTEXT_REMOVE_LINK), visible);
 
 
-       visible = (e_editor_dom_node_get_parent_element (
-                       node, WEBKIT_TYPE_DOM_HTML_TABLE_CELL_ELEMENT) != NULL);
+       visible = (WEBKIT_DOM_IS_HTML_TABLE_CELL_ELEMENT (node) ||
+                  (e_editor_dom_node_find_parent_element (node, "TD") != NULL));
        gtk_action_set_visible (ACTION (CONTEXT_DELETE_CELL), visible);
        gtk_action_set_visible (ACTION (CONTEXT_DELETE_COLUMN), visible);
        gtk_action_set_visible (ACTION (CONTEXT_DELETE_ROW), visible);
@@ -115,8 +115,8 @@ editor_update_actions (EEditor *editor,
        gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_CELL), visible);
 
        /* Note the |= (cursor must be in a table cell). */
-       visible |= (e_editor_dom_node_get_parent_element (
-                       node, WEBKIT_TYPE_DOM_HTML_TABLE_ELEMENT) != NULL);
+       visible |= (WEBKIT_DOM_IS_HTML_TABLE_ELEMENT (node) ||
+                   (e_editor_dom_node_find_parent_element (node, "TABLE") != NULL));
        gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_TABLE), visible);
 
        /********************** Spell Check Suggestions **********************/


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