diff options
| author | Niklas Baumstark | 2012-01-21 00:23:59 +0100 | 
|---|---|---|
| committer | Niklas Baumstark | 2012-04-10 23:54:35 +0200 | 
| commit | 6f589fedcc826b125884e3a5884c9791802afb7f (patch) | |
| tree | eb117a99558b5456b0367157a09a12f887db8630 /lib/utils.js | |
| parent | 269042a28230bb35406d1447fac8955ca1a5c0b3 (diff) | |
| download | vimium-6f589fedcc826b125884e3a5884c9791802afb7f.tar.bz2 | |
add fuzzy mode
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);    },  }; | 
