diff options
| author | Johan Liesén | 2012-08-19 10:18:51 +0200 |
|---|---|---|
| committer | Johan Liesén | 2012-09-04 21:44:47 +0200 |
| commit | 7c7b64fd6d8254ac4d2d815ab1777f3643c65e90 (patch) | |
| tree | 7bbe8501558cbf41d16fe2f3ff6c51507637eb07 | |
| parent | 525276bde9b99d05b133f96f21e1ac187c068bb6 (diff) | |
| download | vimium-7c7b64fd6d8254ac4d2d815ab1777f3643c65e90.tar.bz2 | |
More idiomatic CoffeeScript for Utils.isUrl
| -rw-r--r-- | lib/utils.coffee | 100 |
1 files changed, 54 insertions, 46 deletions
diff --git a/lib/utils.coffee b/lib/utils.coffee index 598b631a..e52c4160 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -43,60 +43,68 @@ Utils = else partialUrl - # Tries to detect, whether :str is a valid URL. + # Tries to detect if :str is a valid URL. isUrl: (str) -> - # more or less RFC compliant URL host part parsing. This should be sufficient - # for our needs + # More or less RFC compliant URL host part parsing. This should be + # sufficient for our needs urlRegex = new RegExp( - '^(?:([^:]+)(?::([^:]+))?@)?' + # user:password (optional) => \1, \2 - '([^:]+|\\[[^\\]]+\\])' + # host name (IPv6 addresses in square brackets allowed) => \3 - '(?::(\\d+))?$' # port number (optional) => \4 + # user:password (optional) => \1, \2 + '^(?:([^:]+)(?::([^:]+))?@)?' + + # host name (IPv6 addresses in square brackets allowed) => \3 + '([^:]+|\\[[^\\]]+\\])' + + # port number (optional) => \4 + '(?::(\\d+))?$' ) - # these are all official ASCII TLDs that are longer than 3 characters - # (including the inofficial .onion TLD used by TOR) - longTlds = [ 'arpa', 'asia', 'coop', 'info', 'jobs', 'local', 'mobi', 'museum', 'name', 'onion' ] + # Official ASCII TLDs that are longer than 3 characters + longTlds = [ + 'arpa' + 'asia' + 'coop' + 'info' + 'jobs' + 'local' + 'mobi' + 'museum' + 'name' + 'onion' # Inofficial .onion TLD used by TOR + ] + + specialHostNames = ['localhost'] + + # Starts with a scheme: URL + return true if /^[a-z]{3,}:\/\//.test str + + # Must not contain spaces + return false if ' ' in str + + # Try to parse the URL into its meaningful parts. If matching fails we're + # pretty sure that we don't have some kind of URL here. + match = urlRegex.exec (str.split '/')[0] + return false unless match + hostName = match[3] - # are there more? - specialHostNames = [ 'localhost' ] + # Allow known special host names + return true if hostName in specialHostNames - # it starts with a scheme, so it's definitely an URL - if (/^[a-z]{3,}:\/\//.test(str)) - return true + # Allow IPv6 addresses (need to be wrapped in brackets as required by + # RFC). It is sufficient to check for a colon, as the regex wouldn't + # match colons in the host name unless it's an v6 address + return true if ':' in hostName - # spaces => definitely not a valid URL - if (str.indexOf(' ') >= 0) - return false + # 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 + dottedParts = hostName.split '.' - # 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. - match = urlRegex.exec(str.split('/')[0]) - if (!match) - return false - hostname = match[3] - - # allow known special host names - if (specialHostNames.indexOf(hostname) >= 0) - 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 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. - dottedParts = hostname.split('.') - lastPart = dottedParts[dottedParts.length-1] - 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 + if dottedParts.length > 1 + lastPart = dottedParts.pop() + return true if 2 <= lastPart.length <= 3 or lastPart in longTlds + + # Allow IPv4 addresses + return true if /^(\d{1,3}\.){3}\d{1,3}$/.test hostName + + # Fallback: no URL return false # Creates a search URL from the given :query. |
