From 3ed9e482cd09b1d75fa6c0ab5e19385bbdf8dbf0 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Tue, 6 Oct 2009 23:43:33 +0200 Subject: [PATCH] Fix display formatting of URIs and unit test it properly --- midori/midori-locationaction.c | 10 +--- midori/sokoke.c | 93 +++++++++++++++++++++++++++------- tests/magic-uri.c | 52 ++++++++++++++++--- 3 files changed, 120 insertions(+), 35 deletions(-) diff --git a/midori/midori-locationaction.c b/midori/midori-locationaction.c index 1c7c9a15..568479fd 100644 --- a/midori/midori-locationaction.c +++ b/midori/midori-locationaction.c @@ -635,7 +635,6 @@ midori_location_entry_render_text_cb (GtkCellLayout* layout, GtkTreeIter* iter, gpointer data) { - gchar* uri_raw; gchar* uri; gchar* title; gchar* desc; @@ -649,14 +648,7 @@ midori_location_entry_render_text_cb (GtkCellLayout* layout, gchar** parts; size_t len; - gtk_tree_model_get (model, iter, URI_COL, &uri_raw, TITLE_COL, &title, -1); - - #if GLIB_CHECK_VERSION (2, 22, 0) - uri = g_hostname_to_unicode (uri_raw); - g_free (uri_raw); - #else - uri = uri_raw; - #endif + gtk_tree_model_get (model, iter, URI_COL, &uri, TITLE_COL, &title, -1); desc = desc_uri = desc_title = key = NULL; if (G_LIKELY (data)) diff --git a/midori/sokoke.c b/midori/sokoke.c index a1fa336f..2a9d966b 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -185,6 +185,59 @@ sokoke_spawn_program (const gchar* command, return TRUE; } +#if defined (HAVE_LIBSOUP_2_27_90) || HAVE_LIBIDN +/** + * sokoke_hostname_from_uri: + * @uri: an URI string + * @path: location of a string pointer + * + * Returns the hostname of the specified URI, + * and stores the path in @path. + * @path is at least set to "" + * + * Return value: a newly allocated hostname + **/ +static gchar* +sokoke_hostname_from_uri (const gchar* uri, + gchar** path) +{ + gchar* hostname; + + *path = ""; + if ((hostname = g_utf8_strchr (uri, -1, '/'))) + { + if (hostname[1] == '/') + hostname += 2; + if ((*path = g_utf8_strchr (hostname, -1, '/'))) + { + gulong offset = g_utf8_pointer_to_offset (hostname, *path); + gchar* buffer = g_malloc0 (offset + 1); + g_utf8_strncpy (buffer, hostname, offset); + hostname = buffer; + } + else + hostname = g_strdup (hostname); + } + else + hostname = g_strdup (uri); + return hostname; +} +#endif + +/** + * sokoke_idn_to_punycode: + * @uri: an URI string + * + * The specified URI is parsed and the hostname + * part of it is encoded if it is not ASCII. + * + * If libIDN is not available at compile time, + * this code will pass the string unaltered. + * + * The called function owns the passed string. + * + * Return value: a newly allocated ASCII URI + **/ gchar* sokoke_idn_to_punycode (gchar* uri) { @@ -212,23 +265,7 @@ sokoke_idn_to_punycode (gchar* uri) proto = buffer; } - path = NULL; - if ((hostname = g_utf8_strchr (uri, -1, '/'))) - { - if (hostname[1] == '/') - hostname += 2; - if ((path = g_utf8_strchr (hostname, -1, '/'))) - { - gulong offset = g_utf8_pointer_to_offset (hostname, path); - gchar* buffer = g_malloc0 (offset + 1); - g_utf8_strncpy (buffer, hostname, offset); - hostname = buffer; - } - else - hostname = g_strdup (hostname); - } - else - hostname = g_strdup (uri); + hostname = sokoke_hostname_from_uri (uri, &path); if (!(q = stringprep_utf8_to_ucs4 (hostname, -1, NULL))) { @@ -294,6 +331,16 @@ gchar* sokoke_search_uri (const gchar* uri, return search; } +/** + * sokoke_magic_uri: + * @uri: a string typed by a user + * @search_engines: search engines + * + * Takes a string that was typed by a user, + * guesses what it is, and returns an URI. + * + * Return value: a newly allocated URI + **/ gchar* sokoke_magic_uri (const gchar* uri, KatzeArray* search_engines) @@ -358,6 +405,7 @@ sokoke_magic_uri (const gchar* uri, return search; } + /** * sokoke_format_uri_for_display: * @uri: an URI string @@ -374,12 +422,19 @@ sokoke_format_uri_for_display (const gchar* uri) { gchar* unescaped = g_uri_unescape_string (uri, NULL); #ifdef HAVE_LIBSOUP_2_27_90 - gchar* decoded = g_hostname_to_unicode (unescaped); + gchar* path; + gchar* hostname = sokoke_hostname_from_uri (unescaped, &path); + gchar* decoded = g_hostname_to_unicode (hostname); + if (decoded) { + gchar* result = g_strconcat ("http://", decoded, path, NULL); g_free (unescaped); - return decoded; + g_free (decoded); + g_free (hostname); + return result; } + g_free (hostname); return unescaped; #elif HAVE_LIBIDN gchar* decoded; diff --git a/tests/magic-uri.c b/tests/magic-uri.c index 38295f8b..bf5bcb7e 100644 --- a/tests/magic-uri.c +++ b/tests/magic-uri.c @@ -18,6 +18,20 @@ #define SM "http://www.searchmash.com/search/" +static void +sokoke_assert_str_equal (const gchar* input, + const gchar* result, + const gchar* expected) +{ + if (g_strcmp0 (result, expected)) + { + g_error ("Input: %s\nExpected: %s\nResult: %s", + input ? input : "NULL", + expected ? expected : "NULL", + result ? result : "NULL"); + } +} + static void test_input (const gchar* input, const gchar* expected) @@ -41,13 +55,7 @@ test_input (const gchar* input, } gchar* uri = sokoke_magic_uri (input, search_engines); - if (g_strcmp0 (uri, expected)) - { - g_error ("Input: %s\nExpected: %s\nResult: %s", - input ? input : "NULL", - expected ? expected : "NULL", - uri ? uri : "NULL"); - } + sokoke_assert_str_equal (input, uri, expected); g_free (uri); } @@ -167,6 +175,35 @@ magic_uri_performance (void) g_print ("\nTime needed for URI tests: %f ", g_test_timer_elapsed ()); } +static void +magic_uri_format (void) +{ + typedef struct + { + const gchar* before; + const gchar* after; + } URIItem; + + static const URIItem items[] = { + { "http://www.csszengarden.com", NULL }, + { "http://live.gnome.org/GTK+/3.0/Tasks", NULL }, + { "http://www.johannkönig.com/index.php?ausw=home", NULL }, + { "http://digilife.bz/wiki/index.php?Python%E3%81%AE%E9%96%8B%E7%99%BA%E6%89%8B%E9%A0%86", + "http://digilife.bz/wiki/index.php?Pythonの開発手順" }, + { "http://die-welt.net/~evgeni/LenovoBatteryLinux/", NULL }, + { "http://wiki.c3sl.ufpr.br/multiseat/index.php/Xephyr_Solution", NULL }, + }; + guint i; + + for (i = 0; i < G_N_ELEMENTS (items); i++) + { + gchar* result = sokoke_format_uri_for_display (items[i].before); + const gchar* after = items[i].after ? items[i].after : items[i].before; + sokoke_assert_str_equal (items[i].before, result, after); + g_free (result); + } +} + int main (int argc, char** argv) @@ -179,6 +216,7 @@ main (int argc, g_test_add_func ("/magic-uri/search", magic_uri_search); g_test_add_func ("/magic-uri/pseudo", magic_uri_pseudo); g_test_add_func ("/magic-uri/performance", magic_uri_performance); + g_test_add_func ("/magic-uri/format", magic_uri_format); return g_test_run (); } -- 2.39.5