[Xfce4-commits] <midori:master> Groundwork to make finding search engines testable

Christian Dywan noreply at xfce.org
Mon Sep 17 19:28:07 CEST 2012


Updating branch refs/heads/master
         to b3984ab2520888d0f4910137340bc6fe82bf8885 (commit)
       from 09432b2d1277b8d0cffe00e79c24d077e780e316 (commit)

commit b3984ab2520888d0f4910137340bc6fe82bf8885
Author: Christian Dywan <christian at twotoasts.de>
Date:   Mon Sep 17 18:27:17 2012 +0200

    Groundwork to make finding search engines testable

 katze/katze.vapi             |   10 +++-
 midori/midori-searchaction.c |  107 ++++++++++++++++++++++++++++++++++++++
 midori/midori-searchaction.h |   10 ++++
 midori/midori-view.c         |  116 +-----------------------------------------
 midori/midori.vapi           |   36 ++++++++-----
 tests/download.vala          |    4 --
 tests/searchaction.vala      |   46 +++++++++++++++++
 tests/speeddial.vala         |    4 --
 8 files changed, 196 insertions(+), 137 deletions(-)

diff --git a/katze/katze.vapi b/katze/katze.vapi
index a152a6c..1627568 100644
--- a/katze/katze.vapi
+++ b/katze/katze.vapi
@@ -3,12 +3,20 @@
 
 [CCode (cprefix = "Katze", lower_case_cprefix = "katze_")]
 namespace Katze {
-    public class Array : GLib.Object {
+    static string assert_str_equal (string input, string result, string expected);
+
+    [CCode (cheader_filename = "katze/katze.h")]
+    public class Array : Katze.Item {
         public Array (GLib.Type type);
         public void add_item (GLib.Object item);
     }
 
+    [CCode (cheader_filename = "katze/katze.h")]
     public class Item : GLib.Object {
+        public string? uri { get; set; }
+        public string? name { get; set; }
+        public string? text { get; set; }
+
         public bool get_meta_boolean (string key);
         public int64 get_meta_integer (string key);
         public void set_meta_integer (string key, int64 value);
diff --git a/midori/midori-searchaction.c b/midori/midori-searchaction.c
index 705e87d..9972c46 100644
--- a/midori/midori-searchaction.c
+++ b/midori/midori-searchaction.c
@@ -932,6 +932,113 @@ midori_search_action_token_for_uri (const gchar* uri)
     return g_strdup (hostname);
 }
 
+KatzeItem*
+midori_search_action_get_engine_for_form (WebKitWebView*     web_view,
+                                          PangoEllipsizeMode ellipsize)
+{
+    #if WEBKIT_CHECK_VERSION (1, 5, 0)
+    WebKitDOMDocument* doc;
+    WebKitDOMHTMLFormElement* active_form;
+    WebKitDOMHTMLCollection* form_nodes;
+    WebKitDOMElement* active_element;
+    gchar* token_element;
+    const gchar* title;
+    GString* uri_str;
+    gulong form_len;
+    guint i;
+    KatzeItem* item;
+    gchar** parts;
+
+    #if WEBKIT_CHECK_VERSION (1, 9, 5)
+    doc = webkit_web_frame_get_dom_document (web_view);
+    #else
+    if (webkit_web_view_get_focused_frame (web_view) != webkit_web_view_get_main_frame (web_view))
+        return NULL;
+    doc = webkit_web_view_get_dom_document (web_view);
+    #endif
+
+    active_element = webkit_dom_html_document_get_active_element ((WebKitDOMHTMLDocument*)doc);
+    active_form = webkit_dom_html_input_element_get_form ((WebKitDOMHTMLInputElement*)active_element);
+
+    if (!active_form)
+        return NULL;
+
+    token_element = webkit_dom_element_get_attribute (active_element, "name");
+
+    form_nodes = webkit_dom_html_form_element_get_elements (active_form);
+    form_len = webkit_dom_html_form_element_get_length (active_form);
+
+    uri_str = g_string_new (webkit_dom_html_form_element_get_action (active_form));
+    g_string_append (uri_str, "?");
+
+    for (i = 0; i < form_len; i++)
+    {
+        WebKitDOMNode* form_node = webkit_dom_html_collection_item (form_nodes, i);
+        WebKitDOMElement* form_element = (WebKitDOMElement*) form_node;
+        gchar* name = webkit_dom_element_get_attribute (form_element, "name");
+
+        if (name && *name)
+        {
+            if (!g_strcmp0 (token_element, name))
+                g_string_append_printf (uri_str, "%s=%s&", name, "\%s");
+            else
+            {
+                gchar* value;
+                if (!g_strcmp0 (webkit_dom_element_get_tag_name (form_element), "SELECT"))
+                {
+                    WebKitDOMHTMLSelectElement* select_element = (WebKitDOMHTMLSelectElement*) form_element;
+                    gulong pos = webkit_dom_html_select_element_get_selected_index (select_element);
+                    WebKitDOMNode* selected_node = webkit_dom_html_select_element_item (select_element, pos);
+                    WebKitDOMElement* selected_element = (WebKitDOMElement*) selected_node;
+
+                    value = webkit_dom_element_get_attribute (selected_element, "value");
+                }
+                else
+                    value = webkit_dom_element_get_attribute (form_element, "value");
+
+                g_string_append_printf (uri_str, "%s=%s&", name, value);
+                g_free (value);
+            }
+            g_free (name);
+        }
+    }
+
+    title = webkit_web_view_get_title (web_view);
+
+    item = katze_item_new ();
+    item->uri = g_string_free (uri_str, FALSE);
+    item->token = midori_search_action_token_for_uri (webkit_web_view_get_uri (web_view));
+
+    if (strstr (title, " - "))
+        parts = g_strsplit (title, " - ", 2);
+    else if (strstr (title, ": "))
+        parts = g_strsplit (title, ": ", 2);
+    else
+        parts = NULL;
+    if (parts != NULL)
+    {
+        /* See midori_view_set_title: title can be first or last */
+        if (ellipsize == PANGO_ELLIPSIZE_END)
+        {
+            item->name = g_strdup (parts[0]);
+            item->text = g_strdup (parts[1]);
+        }
+        else
+        {
+            item->name = g_strdup (parts[1]);
+            item->text = g_strdup (parts[2]);
+        }
+        g_strfreev (parts);
+    }
+    else
+        item->name = g_strdup (title);
+
+    g_free (token_element);
+    return item;
+    #else
+    return NULL;
+    #endif
+}
 
 void
 midori_search_action_get_editor (MidoriSearchAction* search_action,
diff --git a/midori/midori-searchaction.h b/midori/midori-searchaction.h
index c8d0ed8..1f1d220 100644
--- a/midori/midori-searchaction.h
+++ b/midori/midori-searchaction.h
@@ -13,6 +13,7 @@
 #define __MIDORI_SEARCH_ACTION_H__
 
 #include <katze/katze.h>
+#include <webkit/webkit.h>
 
 G_BEGIN_DECLS
 
@@ -72,6 +73,15 @@ midori_search_action_set_default_item      (MidoriSearchAction* search_action,
 GtkWidget*
 midori_search_action_get_dialog            (MidoriSearchAction* search_action);
 
+void
+midori_search_action_get_editor            (MidoriSearchAction* search_action,
+                                            KatzeItem*          item,
+                                            gboolean            new_engine);
+
+KatzeItem*
+midori_search_action_get_engine_for_form   (WebKitWebView*      web_view,
+                                            PangoEllipsizeMode  ellipsize);
+
 G_END_DECLS
 
 #endif /* __MIDORI_SEARCH_ACTION_H__ */
diff --git a/midori/midori-view.c b/midori/midori-view.c
index 3ef2637..a691dff 100644
--- a/midori/midori-view.c
+++ b/midori/midori-view.c
@@ -2433,11 +2433,6 @@ midori_web_view_menu_inspect_element_activate_cb (GtkWidget*  widget,
 }
 
 #if WEBKIT_CHECK_VERSION (1, 5, 0)
-void
-midori_search_action_get_editor (MidoriSearchAction* search_action,
-                                 KatzeItem*          item,
-                                 gboolean            new_engine);
-
 static void
 midori_view_menu_add_search_engine_cb (GtkWidget*  widget,
                                        MidoriView* view)
@@ -2448,114 +2443,6 @@ midori_view_menu_add_search_engine_cb (GtkWidget*  widget,
     KatzeItem* item = g_object_get_data (G_OBJECT (widget), "item");
     midori_search_action_get_editor (MIDORI_SEARCH_ACTION (action), item, TRUE);
 }
-
-gchar*
-midori_search_action_token_for_uri (const gchar* uri);
-
-static KatzeItem*
-midori_view_search_engine_for_form (MidoriView*    view,
-                                    WebKitWebView* web_view)
-{
-
-    WebKitDOMDocument* doc;
-    WebKitDOMHTMLFormElement* active_form;
-    WebKitDOMHTMLCollection* form_nodes;
-    WebKitDOMElement* active_element;
-    gchar* token_element;
-    const gchar* title;
-    GString* uri_str;
-    gulong form_len;
-    guint i;
-    KatzeItem* item;
-    gchar** parts;
-
-    #if WEBKIT_CHECK_VERSION (1, 9, 5)
-    doc = webkit_web_frame_get_dom_document (web_view);
-    #else
-    if (webkit_web_view_get_focused_frame (web_view) != webkit_web_view_get_main_frame (web_view))
-        return NULL;
-    doc = webkit_web_view_get_dom_document (web_view);
-    #endif
-
-    active_element = webkit_dom_html_document_get_active_element ((WebKitDOMHTMLDocument*)doc);
-    active_form = webkit_dom_html_input_element_get_form ((WebKitDOMHTMLInputElement*)active_element);
-
-    if (!active_form)
-        return NULL;
-
-    token_element = webkit_dom_element_get_attribute (active_element, "name");
-
-    form_nodes = webkit_dom_html_form_element_get_elements (active_form);
-    form_len = webkit_dom_html_form_element_get_length (active_form);
-
-    uri_str = g_string_new (webkit_dom_html_form_element_get_action (active_form));
-    g_string_append (uri_str, "?");
-
-    for (i = 0; i < form_len; i++)
-    {
-        WebKitDOMNode* form_node = webkit_dom_html_collection_item (form_nodes, i);
-        WebKitDOMElement* form_element = (WebKitDOMElement*) form_node;
-        gchar* name = webkit_dom_element_get_attribute (form_element, "name");
-
-        if (name && *name)
-        {
-            if (!g_strcmp0 (token_element, name))
-                g_string_append_printf (uri_str, "%s=%s&", name, "\%s");
-            else
-            {
-                gchar* value;
-                if (!g_strcmp0 (webkit_dom_element_get_tag_name (form_element), "SELECT"))
-                {
-                    WebKitDOMHTMLSelectElement* select_element = (WebKitDOMHTMLSelectElement*) form_element;
-                    gulong pos = webkit_dom_html_select_element_get_selected_index (select_element);
-                    WebKitDOMNode* selected_node = webkit_dom_html_select_element_item (select_element, pos);
-                    WebKitDOMElement* selected_element = (WebKitDOMElement*) selected_node;
-
-                    value = webkit_dom_element_get_attribute (selected_element, "value");
-                }
-                else
-                    value = webkit_dom_element_get_attribute (form_element, "value");
-
-                g_string_append_printf (uri_str, "%s=%s&", name, value);
-                g_free (value);
-            }
-            g_free (name);
-        }
-    }
-
-    title = webkit_web_view_get_title (web_view);
-
-    item = katze_item_new ();
-    katze_item_set_uri (item, g_string_free (uri_str, FALSE));
-    item->token = midori_search_action_token_for_uri (view->uri);
-
-    if (strstr (title, " - "))
-        parts = g_strsplit (title, " - ", 2);
-    else if (strstr (title, ": "))
-        parts = g_strsplit (title, ": ", 2);
-    else
-        parts = NULL;
-    if (parts != NULL)
-    {
-        /* See midori_view_set_title: title can be first or last */
-        if (view->ellipsize == PANGO_ELLIPSIZE_END)
-        {
-            katze_item_set_name (item, g_strdup (parts[0]));
-            katze_item_set_text (item, g_strdup (parts[1]));
-        }
-        else
-        {
-            katze_item_set_name (item, g_strdup (parts[1]));
-            katze_item_set_text (item, g_strdup (parts[2]));
-        }
-        g_strfreev (parts);
-    }
-    else
-        katze_item_set_name (item, title);
-
-    g_free (token_element);
-    return item;
-}
 #endif
 
 static GtkWidget*
@@ -2666,7 +2553,8 @@ midori_view_populate_popup (MidoriView* view,
 
         #if WEBKIT_CHECK_VERSION (1, 5, 0)
         {
-            KatzeItem* item = midori_view_search_engine_for_form (view, web_view);
+            KatzeItem* item = midori_search_action_get_engine_for_form (
+                WEBKIT_WEB_VIEW (view->web_view), view->ellipsize);
             if (item != NULL)
             {
                 menuitem = midori_view_insert_menu_item (menu_shell, -1,
diff --git a/midori/midori.vapi b/midori/midori.vapi
index 62688c7..0ac85fb 100644
--- a/midori/midori.vapi
+++ b/midori/midori.vapi
@@ -16,17 +16,17 @@ namespace Midori {
         [NoAccessorMethod]
         public Midori.WebSettings settings { owned get; set; }
         [NoAccessorMethod]
-        public GLib.Object bookmarks { get; set; }
+        public Katze.Array bookmarks { get; set; }
         [NoAccessorMethod]
-        public GLib.Object trash { get; set; }
+        public Katze.Array trash { get; set; }
         [NoAccessorMethod]
-        public GLib.Object search_engines { get; set; }
+        public Katze.Array search_engines { get; set; }
         [NoAccessorMethod]
-        public GLib.Object history { get; set; }
+        public Katze.Array history { get; set; }
         [NoAccessorMethod]
-        public GLib.Object extensions { get; set; }
+        public Katze.Array extensions { get; set; }
         [NoAccessorMethod]
-        public GLib.Object browsers { get; }
+        public Katze.Array browsers { get; }
         public Browser? browser { get; }
 
         [HasEmitter]
@@ -35,9 +35,11 @@ namespace Midori {
         [HasEmitter]
         public signal void quit ();
     }
+
+    [CCode (cheader_filename = "midori/midori.h")]
     public class Browser : Gtk.Window {
         public Browser ();
-        public int add_item (GLib.Object item);
+        public int add_item (Katze.Item item);
         public int add_uri (string uri);
         public unowned View get_nth_tab (int n);
         public GLib.List<weak View> get_tabs ();
@@ -46,7 +48,7 @@ namespace Midori {
         public unowned Gtk.ActionGroup get_action_group ();
         public unowned Browser get_for_widget (Gtk.Widget widget);
         public unowned string[] get_toolbar_actions ();
-        public unowned GLib.Object get_proxy_items ();
+        public unowned Katze.Array get_proxy_items ();
 
         [NoAccessorMethod]
         public Gtk.MenuBar menubar { owned get; }
@@ -67,13 +69,13 @@ namespace Midori {
         public string statusbar_text { owned get; set; }
         public Midori.WebSettings settings { get; set; }
         [NoAccessorMethod]
-        public GLib.Object bookmarks { owned get; set; }
+        public Katze.Array? bookmarks { owned get; set; }
         [NoAccessorMethod]
-        public GLib.Object trash { owned get; set; }
+        public Katze.Array? trash { owned get; set; }
         [NoAccessorMethod]
-        public GLib.Object search_engines { owned get; set; }
+        public Katze.Array? search_engines { owned get; set; }
         [NoAccessorMethod]
-        public GLib.Object history { owned get; set; }
+        public Katze.Array? history { owned get; set; }
         [NoAccessorMethod]
         public bool show_tabs { get; set; }
 
@@ -85,7 +87,7 @@ namespace Midori {
         public signal void switch_tab (View? old_view, View? new_view);
         [HasEmitter]
         public signal void activate_action (string name);
-        public signal void add_download (GLib.Object download);
+        public signal void add_download (WebKit.Download download);
         public signal void populate_tool_menu (Gtk.Menu menu);
         [HasEmitter]
         public signal void quit ();
@@ -126,6 +128,7 @@ namespace Midori {
         public signal void open_preferences ();
     }
 
+    [CCode (cheader_filename = "midori/midori.h")]
     public class View : Gtk.VBox {
         [CCode (type = "GtkWidget*")]
         public View (GLib.Object net);
@@ -163,7 +166,7 @@ namespace Midori {
         public double progress { get; set; }
         public bool minimized { get; }
         public float zoom_level { get; }
-        public GLib.Object news_feeds { get; }
+        public Katze.Array news_feeds { get; }
         public string statusbar_text { get; }
         public WebSettings settings { get; set; }
         public GLib.Object net { get; }
@@ -173,6 +176,11 @@ namespace Midori {
 
     }
 
+    [CCode (cheader_filename = "midori/midori.h")]
+    public class SearchAction : Gtk.Action {
+        public static Katze.Item? get_engine_for_form (WebKit.WebView web_view, Pango.EllipsizeMode ellipsize);
+    }
+
     [CCode (cheader_filename = "midori/midori-view.h", cprefix = "MIDORI_DOWNLOAD_")]
     public enum DownloadType {
         CANCEL,
diff --git a/tests/download.vala b/tests/download.vala
index 9bdbc02..c5e0f98 100644
--- a/tests/download.vala
+++ b/tests/download.vala
@@ -9,10 +9,6 @@
  See the file COPYING for the full license text.
 */
 
-namespace Katze {
-    extern static string assert_str_equal (string input, string result, string expected);
-}
-
 struct TestCase {
     public string data;
     public string? expected;
diff --git a/tests/searchaction.vala b/tests/searchaction.vala
new file mode 100644
index 0000000..60dc81a
--- /dev/null
+++ b/tests/searchaction.vala
@@ -0,0 +1,46 @@
+/*
+ Copyright (C) 2012 Christian Dywan <christian at twotoasts.de>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+
+/*
+struct FormSpec {
+    public string html;
+    public Pango.EllipsizeMode ellipsize;
+    public string? uri;
+    public string? title;
+}
+
+const FormSpec[] forms = {
+    { "<form></form>", Pango.EllipsizeMode.END, null, null },
+    { "<form><input></form>", Pango.EllipsizeMode.END, null, null }
+};
+*/
+
+void searchaction_form () {
+    /*
+    foreach (var form in forms) {
+        var view = new Midori.View.with_title ();
+        view.get_web_view ().load_html_string (form.html, "");
+        Katze.Item? result = Midori.SearchAction.get_engine_for_form (
+            view.get_web_view (), form.ellipsize);
+        Katze.assert_str_equal (form.html,
+            result != null ? result.uri : null, form.uri);
+        Katze.assert_str_equal (form.html,
+            result != null ? result.name : null, form.title);
+    }
+    */
+}
+
+void main (string[] args) {
+    Test.init (ref args);
+    Test.add_func ("/searchaction/form", searchaction_form);
+    Test.run ();
+}
+
diff --git a/tests/speeddial.vala b/tests/speeddial.vala
index e696ea4..513f0b5 100644
--- a/tests/speeddial.vala
+++ b/tests/speeddial.vala
@@ -16,10 +16,6 @@ string get_test_file (string contents) {
     return file;
 }
 
-namespace Katze {
-    extern static string assert_str_equal (string input, string result, string expected);
-}
-
 static void speeddial_load () {
     string data = get_test_file ("""
             [Dial 1]


More information about the Xfce4-commits mailing list