Fix display formatting of URIs and unit test it properly
This commit is contained in:
parent
ca81aeb034
commit
3ed9e482cd
3 changed files with 120 additions and 35 deletions
|
@ -635,7 +635,6 @@ midori_location_entry_render_text_cb (GtkCellLayout* layout,
|
||||||
GtkTreeIter* iter,
|
GtkTreeIter* iter,
|
||||||
gpointer data)
|
gpointer data)
|
||||||
{
|
{
|
||||||
gchar* uri_raw;
|
|
||||||
gchar* uri;
|
gchar* uri;
|
||||||
gchar* title;
|
gchar* title;
|
||||||
gchar* desc;
|
gchar* desc;
|
||||||
|
@ -649,14 +648,7 @@ midori_location_entry_render_text_cb (GtkCellLayout* layout,
|
||||||
gchar** parts;
|
gchar** parts;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
gtk_tree_model_get (model, iter, URI_COL, &uri_raw, TITLE_COL, &title, -1);
|
gtk_tree_model_get (model, iter, URI_COL, &uri, 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
|
|
||||||
|
|
||||||
desc = desc_uri = desc_title = key = NULL;
|
desc = desc_uri = desc_title = key = NULL;
|
||||||
if (G_LIKELY (data))
|
if (G_LIKELY (data))
|
||||||
|
|
|
@ -185,6 +185,59 @@ sokoke_spawn_program (const gchar* command,
|
||||||
return TRUE;
|
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*
|
gchar*
|
||||||
sokoke_idn_to_punycode (gchar* uri)
|
sokoke_idn_to_punycode (gchar* uri)
|
||||||
{
|
{
|
||||||
|
@ -212,23 +265,7 @@ sokoke_idn_to_punycode (gchar* uri)
|
||||||
proto = buffer;
|
proto = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
path = NULL;
|
hostname = sokoke_hostname_from_uri (uri, &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);
|
|
||||||
|
|
||||||
if (!(q = stringprep_utf8_to_ucs4 (hostname, -1, NULL)))
|
if (!(q = stringprep_utf8_to_ucs4 (hostname, -1, NULL)))
|
||||||
{
|
{
|
||||||
|
@ -294,6 +331,16 @@ gchar* sokoke_search_uri (const gchar* uri,
|
||||||
return search;
|
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*
|
gchar*
|
||||||
sokoke_magic_uri (const gchar* uri,
|
sokoke_magic_uri (const gchar* uri,
|
||||||
KatzeArray* search_engines)
|
KatzeArray* search_engines)
|
||||||
|
@ -358,6 +405,7 @@ sokoke_magic_uri (const gchar* uri,
|
||||||
return search;
|
return search;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sokoke_format_uri_for_display:
|
* sokoke_format_uri_for_display:
|
||||||
* @uri: an URI string
|
* @uri: an URI string
|
||||||
|
@ -374,12 +422,19 @@ sokoke_format_uri_for_display (const gchar* uri)
|
||||||
{
|
{
|
||||||
gchar* unescaped = g_uri_unescape_string (uri, NULL);
|
gchar* unescaped = g_uri_unescape_string (uri, NULL);
|
||||||
#ifdef HAVE_LIBSOUP_2_27_90
|
#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)
|
if (decoded)
|
||||||
{
|
{
|
||||||
|
gchar* result = g_strconcat ("http://", decoded, path, NULL);
|
||||||
g_free (unescaped);
|
g_free (unescaped);
|
||||||
return decoded;
|
g_free (decoded);
|
||||||
|
g_free (hostname);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
g_free (hostname);
|
||||||
return unescaped;
|
return unescaped;
|
||||||
#elif HAVE_LIBIDN
|
#elif HAVE_LIBIDN
|
||||||
gchar* decoded;
|
gchar* decoded;
|
||||||
|
|
|
@ -18,6 +18,20 @@
|
||||||
|
|
||||||
#define SM "http://www.searchmash.com/search/"
|
#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
|
static void
|
||||||
test_input (const gchar* input,
|
test_input (const gchar* input,
|
||||||
const gchar* expected)
|
const gchar* expected)
|
||||||
|
@ -41,13 +55,7 @@ test_input (const gchar* input,
|
||||||
}
|
}
|
||||||
|
|
||||||
gchar* uri = sokoke_magic_uri (input, search_engines);
|
gchar* uri = sokoke_magic_uri (input, search_engines);
|
||||||
if (g_strcmp0 (uri, expected))
|
sokoke_assert_str_equal (input, uri, expected);
|
||||||
{
|
|
||||||
g_error ("Input: %s\nExpected: %s\nResult: %s",
|
|
||||||
input ? input : "NULL",
|
|
||||||
expected ? expected : "NULL",
|
|
||||||
uri ? uri : "NULL");
|
|
||||||
}
|
|
||||||
g_free (uri);
|
g_free (uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,6 +175,35 @@ magic_uri_performance (void)
|
||||||
g_print ("\nTime needed for URI tests: %f ", g_test_timer_elapsed ());
|
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
|
int
|
||||||
main (int argc,
|
main (int argc,
|
||||||
char** argv)
|
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/search", magic_uri_search);
|
||||||
g_test_add_func ("/magic-uri/pseudo", magic_uri_pseudo);
|
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/performance", magic_uri_performance);
|
||||||
|
g_test_add_func ("/magic-uri/format", magic_uri_format);
|
||||||
|
|
||||||
return g_test_run ();
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue