From b814f74b27a12ede9c063208b20f9f30ddbb9438 Mon Sep 17 00:00:00 2001 From: Manuel QuiƱones Date: Thu, 05 Jan 2012 12:29:21 +0000 Subject: Normalize and autosearch url input. This borrows code from Epiphany. Our normalize_or_autosearch_url is like the method of the same name in ephy-web-view.c . And the function has_web_scheme is like ephy_embed_utils_address_has_web_scheme in ephy-embed-utils.c Acked-by: Simon Schampijer Signed-off-by: Manuel QuiƱones --- diff --git a/browser.py b/browser.py index f6efe26..292dbf9 100644 --- a/browser.py +++ b/browser.py @@ -18,6 +18,7 @@ import os import time +import re from gettext import gettext as _ from gi.repository import GObject @@ -25,6 +26,7 @@ from gi.repository import Gtk from gi.repository import Gdk from gi.repository import Pango from gi.repository import WebKit +from gi.repository import Soup from sugar3 import env from sugar3.activity import activity @@ -40,6 +42,21 @@ from widgets import BrowserNotebook _ZOOM_AMOUNT = 0.1 _LIBRARY_PATH = '/usr/share/library-common/index.html' +_WEB_SCHEMES = ['http', 'https', 'ftp', 'file', 'javascript', 'data', + 'about', 'gopher', 'mailto'] + +_NON_SEARCH_REGEX = re.compile(''' + (^localhost(\\.[^\s]+)?(:\\d+)?(/.*)?$| + ^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]$| + ^::[0-9a-f:]*$| # IPv6 literals + ^[0-9a-f:]+:[0-9a-f:]*$| # IPv6 literals + ^[^\\.\s]+\\.[^\\.\s]+.*$| # foo.bar... + ^https?://[^/\\.\s]+.*$| + ^about:.*$| + ^data:.*$| + ^file:.*$) + ''', re.VERBOSE) + class SaveListener(object): def __init__(self, user_data, callback): @@ -149,6 +166,57 @@ class TabbedView(BrowserNotebook): self._update_closing_buttons() self._update_tab_sizes() + def normalize_or_autosearch_url(self, url): + """Normalize the url input or return a url for search. + + We use SoupURI as an indication of whether the value given in url + is not something we want to search; we only do that, though, if + the address has a web scheme, because SoupURI will consider any + string: as a valid scheme, and we will end up prepending http:// + to it. + + This code is borrowed from Epiphany. + + url -- input string that can be normalized to an url or serve + as search + + Return: a string containing a valid url + + """ + def has_web_scheme(address): + if address == '': + return False + + scheme, sep, after = address.partition(':') + if sep == '': + return False + + return scheme in _WEB_SCHEMES + + soup_uri = None + effective_url = None + + if has_web_scheme(url): + try: + soup_uri = Soup.URI.new(url) + except TypeError: + pass + + if soup_uri is None and not _NON_SEARCH_REGEX.match(url): + # If the string doesn't look like an URI, let's search it: + url_search = \ + _('http://www.google.com/search?q=%s&ie=UTF-8&oe=UTF-8') + query_param = Soup.form_encode_hash({'q': url}) + # [2:] here is getting rid of 'q=': + effective_url = url_search % query_param[2:] + else: + if has_web_scheme(url): + effective_url = url + else: + effective_url = 'http://' + url + + return effective_url + def createChromeWindow(self, parent, flags): if flags & interfaces.nsIWebBrowserChrome.CHROME_OPENAS_CHROME: dialog = PopupDialog() diff --git a/webtoolbar.py b/webtoolbar.py index 47ece72..c0e1396 100644 --- a/webtoolbar.py +++ b/webtoolbar.py @@ -384,7 +384,9 @@ class PrimaryToolbar(ToolbarBase): def _entry_activate_cb(self, entry): browser = self._tabbed_view.props.current_browser - browser.load_uri(entry.props.text) + url = entry.props.text + effective_url = self._tabbed_view.normalize_or_autosearch_url(url) + browser.load_uri(effective_url) browser.grab_focus() def _go_home_cb(self, button): -- cgit v0.9.1