[evolution] Bug 729256: Busy loop under replace_local_image_links()



commit 2ee38c849d96546a1795babf041bd45af37fd3ea
Author: Tomas Popela <tpopela redhat com>
Date:   Wed Apr 30 14:33:34 2014 +0200

    Bug 729256: Busy loop under replace_local_image_links()
    
    Rework replace_local_image_links to use CSS selectors to avoid iterating
    through the DOM tree.

 e-util/e-mail-signature-preview.c |   67 +++++++++++++++---------------------
 1 files changed, 28 insertions(+), 39 deletions(-)
---
diff --git a/e-util/e-mail-signature-preview.c b/e-util/e-mail-signature-preview.c
index d493bc0..c2eeaa6 100644
--- a/e-util/e-mail-signature-preview.c
+++ b/e-util/e-mail-signature-preview.c
@@ -56,48 +56,46 @@ G_DEFINE_TYPE (
        E_TYPE_WEB_VIEW)
 
 static void
-replace_local_image_links (WebKitDOMElement *element)
+replace_local_image_links (WebKitDOMDocument *document)
 {
-       WebKitDOMElement *child;
+       gint ii, length;
+       WebKitDOMNodeList *list;
 
-       if (element == NULL)
-               return;
+       list = webkit_dom_document_query_selector_all (
+               document, "img[src^=\"file://\"]", NULL);
+       length = webkit_dom_node_list_get_length (list);
 
-       if (WEBKIT_DOM_IS_HTML_IMAGE_ELEMENT (element)) {
+       for (ii = 0; ii < length; ii++) {
+               gchar *src, *new_src;
                WebKitDOMHTMLImageElement *img;
-               gchar *src;
 
-               img = WEBKIT_DOM_HTML_IMAGE_ELEMENT (element);
+               img = WEBKIT_DOM_HTML_IMAGE_ELEMENT (
+                       webkit_dom_node_list_item (list, ii));
                src = webkit_dom_html_image_element_get_src (img);
-               if (src && g_ascii_strncasecmp (src, "file://", 7) == 0) {
-                       gchar *new_src;
-
-                       /* this forms "evo-file://", which can be loaded,
-                        * while "file://" cannot be, due to webkit policy */
-                       new_src = g_strconcat ("evo-", src, NULL);
-                       webkit_dom_html_image_element_set_src (img, new_src);
-                       g_free (new_src);
-               }
 
+               /* this forms "evo-file://", which can be loaded,
+                * while "file://" cannot be, due to WebKit policy */
+               new_src = g_strconcat ("evo-", src, NULL);
+               webkit_dom_html_image_element_set_src (img, new_src);
+               g_free (new_src);
                g_free (src);
        }
 
-       if (WEBKIT_DOM_IS_HTML_IFRAME_ELEMENT (element)) {
-               WebKitDOMDocument *frame_document;
+       list = webkit_dom_document_get_elements_by_tag_name ( document, "iframe");
+       length = webkit_dom_node_list_get_length (list);
+       for (ii = 0; ii < length; ii++) {
+               WebKitDOMDocument *content_document;
+               WebKitDOMHTMLIFrameElement *iframe;
 
-               frame_document =
-                       webkit_dom_html_iframe_element_get_content_document (
-                               WEBKIT_DOM_HTML_IFRAME_ELEMENT (element));
-               replace_local_image_links (WEBKIT_DOM_ELEMENT (frame_document));
-       }
+               iframe = WEBKIT_DOM_HTML_IFRAME_ELEMENT (
+                       webkit_dom_node_list_item (list, ii));
 
-       child = webkit_dom_element_get_first_element_child (element);
-       replace_local_image_links (child);
+               content_document =
+                       webkit_dom_html_iframe_element_get_content_document (iframe);
 
-       do {
-               element = webkit_dom_element_get_next_element_sibling (element);
-               replace_local_image_links (element);
-       } while (element != NULL);
+               if (content_document && WEBKIT_DOM_IS_DOCUMENT (content_document))
+                       replace_local_image_links (content_document);
+       }
 }
 
 static void
@@ -105,16 +103,7 @@ signature_preview_document_loaded_cb (WebKitWebView *web_view,
                                       WebKitWebFrame *web_frame,
                                       gpointer user_data)
 {
-       WebKitDOMDocument *document;
-       WebKitDOMNode *node;
-
-       document = webkit_web_view_get_dom_document (web_view);
-       for (node = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (document));
-            node;
-            node = webkit_dom_node_get_next_sibling (node)) {
-               if (WEBKIT_DOM_IS_ELEMENT (node))
-                       replace_local_image_links (WEBKIT_DOM_ELEMENT (node));
-       }
+       replace_local_image_links (webkit_web_view_get_dom_document (web_view));
 }
 
 static void


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