aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.coffee
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.coffee')
-rw-r--r--lib/utils.coffee28
1 files changed, 15 insertions, 13 deletions
diff --git a/lib/utils.coffee b/lib/utils.coffee
index 5d93ae70..57d8a488 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -25,27 +25,29 @@ Utils =
id = 0
-> id += 1
- hasChromePrefix: (url) ->
- chromePrefixes = ["about:", "view-source:", "chrome-extension:", "data:", "javascript:"]
- for prefix in chromePrefixes
- return true if url.startsWith prefix
- false
+ hasChromePrefix: do ->
+ chromePrefixes = [ "about:", "view-source:", "extension:", "chrome-extension:", "data:", "javascript:" ]
+ (url) ->
+ for prefix in chromePrefixes
+ return true if url.startsWith prefix
+ false
+
+ hasFullUrlPrefix: do ->
+ urlPrefix = new RegExp "^[a-z]{3,}://."
+ (url) -> urlPrefix.test url
# Completes a partial URL (without scheme)
createFullUrl: (partialUrl) ->
- unless /^[a-z]{3,}:\/\//.test partialUrl
- "http://" + partialUrl
- else
- partialUrl
+ if @hasFullUrlPrefix(partialUrl) then partialUrl else ("http://" + partialUrl)
# Tries to detect if :str is a valid URL.
isUrl: (str) ->
- # Starts with a scheme: URL
- return true if /^[a-z]{3,}:\/\//.test str
-
# Must not contain spaces
return false if ' ' in str
+ # Starts with a scheme: URL
+ return true if @hasFullUrlPrefix str
+
# More or less RFC compliant URL host part parsing. This should be sufficient for our needs
urlRegex = new RegExp(
'^(?:([^:]+)(?::([^:]+))?@)?' + # user:password (optional) => \1, \2
@@ -96,7 +98,7 @@ Utils =
convertToUrl: (string) ->
string = string.trim()
- # Special-case about:[url] and view-source:[url]
+ # Special-case about:[url], view-source:[url] and the like
if Utils.hasChromePrefix string
string
else if Utils.isUrl string