diff options
Diffstat (limited to 'lib/utils.js')
| -rw-r--r-- | lib/utils.js | 67 |
1 files changed, 49 insertions, 18 deletions
diff --git a/lib/utils.js b/lib/utils.js index 8aada3a1..922e7db3 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -21,17 +21,26 @@ var utils = { }, /** - * Creates a search URL from the given :query. + * Generates a unique ID */ - createSearchUrl: function(query) { - return "http://www.google.com/search?q=" + query; + createUniqueId: (function() { + id = 0; + return function() { return ++id; }; + })(), + + /** + * Completes a partial URL (without scheme) + */ + createFullUrl: function(partialUrl) { + if (!/^[a-z]{3,}:\/\//.test(partialUrl)) + partialUrl = 'http://' + partialUrl; + return partialUrl }, /** - * Tries to convert :str into a valid URL. - * We don't bother with escaping characters, however, as Chrome will do that for us. + * Tries to detect, whether :str is a valid URL. */ - ensureUrl: function(str) { + isUrl: function(str) { // more or less RFC compliant URL host part parsing. This should be sufficient // for our needs var urlRegex = new RegExp( @@ -55,38 +64,60 @@ var utils = { // it starts with a scheme, so it's definitely an URL if (/^[a-z]{3,}:\/\//.test(str)) - return str; - var strWithScheme = 'http://' + str; + return true; - // definitely not a valid URL; treat as search query + // spaces => definitely not a valid URL if (str.indexOf(' ') >= 0) - return utils.createSearchUrl(str); + return false; // assuming that this is an URL, try to parse it into its meaningful parts. If matching fails, we're // pretty sure that we don't have some kind of URL here. var match = urlRegex.exec(str.split('/')[0]); if (!match) - return utils.createSearchUrl(str); + return false; var hostname = match[3]; // allow known special host names if (specialHostNames.indexOf(hostname) >= 0) - return strWithScheme; + return true; // allow IPv6 addresses (need to be wrapped in brackets, as required by RFC). It is sufficient to check // for a colon here, as the regex wouldn't match colons in the host name unless it's an v6 address if (hostname.indexOf(':') >= 0) - return strWithScheme; + return true; // at this point we have to make a decision. As a heuristic, we check if the input has dots in it. If - // yes, and if the last part could be a TLD, treat it as an URL + // yes, and if the last part could be a TLD, treat it as an URL. var dottedParts = hostname.split('.'); var lastPart = dottedParts[dottedParts.length-1]; - if (dottedParts.length > 1 && (lastPart.length <= 3 || longTlds.indexOf(lastPart) >= 0)) - return strWithScheme; + if (dottedParts.length > 1 && ((lastPart.length >= 2 && lastPart.length <= 3) + || longTlds.indexOf(lastPart) >= 0)) + return true; + + // also allow IPv4 addresses + if (/^(\d{1,3}\.){3}\d{1,3}$/.test(hostname)) + return true; + + // fallback: no URL + return false + }, - // fallback: use as search query - return utils.createSearchUrl(str); + /** + * Creates a search URL from the given :query. + */ + createSearchUrl: function(query) { + return "http://www.google.com/search?q=" + query; + }, + + /** + * Tries to convert :str into a valid URL. + * We don't bother with escaping characters, however, as Chrome will do that for us. + */ + ensureUrl: function(str) { + if (utils.isUrl(str)) + return utils.createFullUrl(str); + else + return utils.createSearchUrl(str); }, }; |
