[epiphany] ephy-web-view: do autosearch foo.bar strings where bar is not a TLD



commit 57b873b72f880e85fc0abd7ae26d627e1578a028
Author: Xan Lopez <xan igalia com>
Date:   Tue Aug 7 15:28:21 2012 +0200

    ephy-web-view: do autosearch foo.bar strings where bar is not a TLD
    
    Using the new SoupTLD methods. Had to split the non-search regexp in
    two so that we can reuse its 'is this a domain?' bits. Ugly as hell,
    but not worse than before... At least we have unit tests to catch
    regressions.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=681022

 embed/ephy-embed-private.h |    7 ++++---
 embed/ephy-web-view.c      |   33 +++++++++++++++++++++++++++++++--
 tests/ephy-web-view-test.c |   24 +++++++++++++++++-------
 3 files changed, 52 insertions(+), 12 deletions(-)
---
diff --git a/embed/ephy-embed-private.h b/embed/ephy-embed-private.h
index b23c5a3..0fcc500 100644
--- a/embed/ephy-embed-private.h
+++ b/embed/ephy-embed-private.h
@@ -33,17 +33,18 @@ G_BEGIN_DECLS
 /* EphyWebView */
 
 #define EPHY_WEB_VIEW_NON_SEARCH_REGEX  "(" \
-                                        "^localhost(\\.[^[:space:]]+)?(:\\d+)?(/.*)?$|" \
-                                        "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]$|" \
+                                        "^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9](:[0-9]+)?.*$|" \
                                         "^::[0-9a-f:]*$|" \
                                         "^[0-9a-f:]+:[0-9a-f:]*$|" \
-                                        "^[^\\.[:space:]]+\\.[^\\.[:space:]]+.*$|" \
                                         "^https?://[^/\\.[:space:]]+.*$|" \
                                         "^about:.*$|" \
                                         "^data:.*$|" \
                                         "^file:.*$" \
                                         ")"
 
+#define EPHY_WEB_VIEW_DOMAIN_REGEX "^localhost(\\.[^[:space:]]+)?(:\\d+)?(:[0-9]+)?(/.*)?$|" \
+                                   "^[^\\.[:space:]]+\\.[^\\.[:space:]]+.*$|"
+
 void                       ephy_web_view_set_visit_type           (EphyWebView *view, 
                                                                    EphyHistoryPageVisitType visit_type);
 EphyHistoryPageVisitType   ephy_web_view_get_visit_type           (EphyWebView *view);
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index accde28..3ee698b 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -94,6 +94,7 @@ struct _EphyWebViewPrivate {
 
   /* Regex to figure out if we're dealing with a wanna-be URI */
   GRegex *non_search_regex;
+  GRegex *domain_regex;
 
   GSList *hidden_popups;
   GSList *shown_popups;
@@ -1063,11 +1064,16 @@ ephy_web_view_finalize (GObject *object)
                                         ephy_web_view_history_cleared_cb,
                                         EPHY_WEB_VIEW (object));
 
-  if (priv->non_search_regex != NULL) {
+  if (priv->non_search_regex) {
     g_regex_unref (priv->non_search_regex);
     priv->non_search_regex = NULL;
   }
 
+  if (priv->domain_regex) {
+    g_regex_unref (priv->domain_regex);
+    priv->domain_regex = NULL;
+  }
+
   ephy_web_view_popups_manager_reset (EPHY_WEB_VIEW (object));
 
   g_free (priv->address);
@@ -2694,6 +2700,9 @@ ephy_web_view_init (EphyWebView *web_view)
   priv->non_search_regex = g_regex_new (EPHY_WEB_VIEW_NON_SEARCH_REGEX,
                                         G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL);
 
+  priv->domain_regex = g_regex_new (EPHY_WEB_VIEW_DOMAIN_REGEX,
+                                    G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL);
+
   priv->history_service = EPHY_HISTORY_SERVICE (ephy_embed_shell_get_global_history_service (embed_shell));
   priv->history_service_cancellable = g_cancellable_new ();
 
@@ -2815,6 +2824,25 @@ ephy_web_view_new (void)
   return g_object_new (EPHY_TYPE_WEB_VIEW, NULL);
 }
 
+static gboolean
+is_public_domain (EphyWebView *view, const char *url)
+{
+  EphyWebViewPrivate *priv = view->priv;
+
+  if (g_regex_match (priv->domain_regex, url, 0, NULL)) {
+    if (g_str_equal (url, "localhost"))
+      return TRUE;
+
+    url = g_strstr_len (url, -1, ".");
+    if (!url || *url == '\0')
+      return FALSE;
+
+    return soup_tld_domain_is_public_suffix (url);
+  }
+
+  return FALSE;
+}
+
 /**
  * ephy_web_view_normalize_or_autosearch_url:
  * @view: an %EphyWebView
@@ -2842,7 +2870,8 @@ ephy_web_view_normalize_or_autosearch_url (EphyWebView *view, const char *url)
       scheme == NULL &&
       !ephy_embed_utils_address_is_existing_absolute_filename (url) &&
       priv->non_search_regex &&
-      !g_regex_match (priv->non_search_regex, url, 0, NULL)) {
+      !g_regex_match (priv->non_search_regex, url, 0, NULL) &&
+      !is_public_domain (view, url)) {
     char *query_param, *url_search;
 
     url_search = g_settings_get_string (EPHY_SETTINGS_MAIN,
diff --git a/tests/ephy-web-view-test.c b/tests/ephy-web-view-test.c
index 19c9fdf..32a86db 100644
--- a/tests/ephy-web-view-test.c
+++ b/tests/ephy-web-view-test.c
@@ -235,19 +235,27 @@ static const RegexTest test_non_search_regex[] = {
 static void
 test_ephy_web_view_non_search_regex ()
 {
-  GRegex *regex;
+  GRegex *regex_non_search, *regex_domain;
   GError *error = NULL;
   int i;
 
-  regex = g_regex_new (EPHY_WEB_VIEW_NON_SEARCH_REGEX,
-                       0, G_REGEX_MATCH_NOTEMPTY, &error);
+  regex_non_search = g_regex_new (EPHY_WEB_VIEW_NON_SEARCH_REGEX,
+                                  0, G_REGEX_MATCH_NOTEMPTY, &error);
 
   if (error) {
     g_test_message ("Regex failed: %s", error->message);
     g_error_free (error);
   }
+  g_assert (regex_non_search);
 
-  g_assert (regex != NULL);
+  regex_domain = g_regex_new (EPHY_WEB_VIEW_DOMAIN_REGEX,
+                              0, G_REGEX_MATCH_NOTEMPTY, &error);
+
+  if (error) {
+    g_test_message ("Regex failed: %s", error->message);
+    g_error_free (error);
+  }
+  g_assert (regex_domain);
 
   for (i = 0; i < G_N_ELEMENTS (test_non_search_regex); i++) {
     RegexTest test;
@@ -258,10 +266,12 @@ test_ephy_web_view_non_search_regex ()
                     test.match ? "NO SEARCH" : "SEARCH",
                     test.url);
 
-    g_assert (g_regex_match (regex, test.url, 0, NULL) == test.match);
+    g_assert (g_regex_match (regex_non_search, test.url, 0, NULL) == test.match ||
+              g_regex_match (regex_domain, test.url, 0, NULL) == test.match);
   }
 
-  g_regex_unref (regex);
+  g_regex_unref (regex_non_search);
+  g_regex_unref (regex_domain);
 }
 
 /* FIXME: we hardcode the google search for now, since it's the
@@ -274,7 +284,7 @@ static struct {
   { "http://google.com";, "http://google.com"; },
   { "search", "http://www.google.com/search?q=search&ie=UTF-8&oe=UTF-8"; },
   { "search.me", "http://search.me"; },
-  { "lala.lala", "http://lala.lala"; }, /* FIXME: should autosearch. */
+  { "lala.lala", "http://www.google.com/search?q=lala%2Elala&ie=UTF-8&oe=UTF-8"; },
   { "127.0.0.1", "http://127.0.0.1"; },
   { "http://127.0.0.1";, "http://127.0.0.1"; },
   { "totalgarbage0xdeadbeef", "http://www.google.com/search?q=totalgarbage0xdeadbeef&ie=UTF-8&oe=UTF-8"; }



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