diff options
| author | Jez Ng | 2012-06-20 20:48:18 -0700 |
|---|---|---|
| committer | Jez Ng | 2012-06-20 20:48:18 -0700 |
| commit | be5ad2ddb59ba24dfce9cab350b3e09de98df9ea (patch) | |
| tree | abfdc72e221ad7977d80a46df7c9a019caa610bb | |
| parent | 9976c25058e5154fc7e275d537a0d05d981aa15f (diff) | |
| download | vimium-be5ad2ddb59ba24dfce9cab350b3e09de98df9ea.tar.bz2 | |
Refactor prefix recognition, add about: prefix as well.
| -rw-r--r-- | lib/utils.coffee | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/lib/utils.coffee b/lib/utils.coffee index 91d79c4c..598b631a 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -30,9 +30,15 @@ Utils = return -> id += 1 )() + hasChromePrefix: (url) -> + chromePrefixes = [ 'about', 'view-source' ] + for prefix in chromePrefixes + return true if url.startsWith prefix + false + # Completes a partial URL (without scheme) createFullUrl: (partialUrl) -> - if (!/^[a-z]{3,}:\/\//.test(partialUrl)) and not /^view-source:/.test partialUrl + if (!/^[a-z]{3,}:\/\//.test(partialUrl)) "http://" + partialUrl else partialUrl @@ -54,10 +60,6 @@ Utils = # are there more? specialHostNames = [ 'localhost' ] - # special-case view-source:[url] - if /^view-source:/.test str - return true - # it starts with a scheme, so it's definitely an URL if (/^[a-z]{3,}:\/\//.test(str)) return true @@ -106,7 +108,10 @@ Utils = # We don't bother with escaping characters as Chrome will do that for us. convertToUrl: (string) -> string = string.trim() - if (Utils.isUrl(string)) then Utils.createFullUrl(string) else Utils.createSearchUrl(string) + # special-case about:[url] and view-source:[url] + if Utils.hasChromePrefix string then string + else + if (Utils.isUrl(string)) then Utils.createFullUrl(string) else Utils.createSearchUrl(string) # This creates a new function out of an existing function, where the new function takes fewer arguments. # This allows us to pass around functions instead of functions + a partial list of arguments. @@ -117,6 +122,8 @@ Function.prototype.curry = -> Array.copy = (array) -> Array.prototype.slice.call(array, 0) +String::startsWith = (str) -> @indexOf(str) == 0 + # A very simple method for defining a new class (constructor and methods) using a single hash. # No support for inheritance is included because we really shouldn't need it. # TODO(philc): remove this. |
