aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-05-05 06:38:31 +0100
committerStephen Blott2015-05-05 09:19:28 +0100
commit1a1b8ec05aaca867261a3556317697d8cdaf7b6c (patch)
tree866ab2a9a3d5e5c31d696901feca1fffd92d81d1
parentc063619f87d140fe0abf82da73f10d1d019fd24c (diff)
downloadvimium-1a1b8ec05aaca867261a3556317697d8cdaf7b6c.tar.bz2
Fix javascript: URIs.
This is @mrmr9393's suggestion from #1636. It mimic's Chrome's behaviour when a javascript: URI is enetered into the omnibox (or clicked). Fixes #1611.
-rw-r--r--lib/utils.coffee16
-rw-r--r--tests/unit_tests/utils_test.coffee12
2 files changed, 24 insertions, 4 deletions
diff --git a/lib/utils.coffee b/lib/utils.coffee
index fba03b61..db63d53a 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -39,6 +39,17 @@ Utils =
urlPrefix = new RegExp "^[a-z]{3,}://."
(url) -> urlPrefix.test url
+ # Decode valid escape sequences in a Javascript URI. This is intended to mimic the best-effort decoding
+ # Chrome itself seems to apply when a Javascript URI is enetered into the omnibox (or clicked).
+ # See https://code.google.com/p/chromium/issues/detail?id=483000, #1611 and #1636.
+ decodeJavascriptURI: (uri) ->
+ uri.split(/(?=%)/).map((uriComponent) ->
+ try
+ decodeURIComponent uriComponent
+ catch
+ uriComponent
+ ).join ""
+
# Completes a partial URL (without scheme)
createFullUrl: (partialUrl) ->
if @hasFullUrlPrefix(partialUrl) then partialUrl else ("http://" + partialUrl)
@@ -111,10 +122,7 @@ Utils =
if Utils.hasChromePrefix string
string
else if Utils.hasJavascriptPrefix string
- # We blindly URL decode javascript: URLs. That's what Chrome does when they're clicked, or entered into
- # the omnibox. However, Chrome does not URL decode such URLs in chrome.tabs.update.
- # This is arguably a Chrome bug. See https://code.google.com/p/chromium/issues/detail?id=483000.
- decodeURI string
+ Utils.decodeJavascriptURI string
else if Utils.isUrl string
Utils.createFullUrl string
else
diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee
index 88e9a15b..ac3e4d85 100644
--- a/tests/unit_tests/utils_test.coffee
+++ b/tests/unit_tests/utils_test.coffee
@@ -42,6 +42,7 @@ context "convertToUrl",
assert.equal "http://127.0.0.1:8080", Utils.convertToUrl("127.0.0.1:8080")
assert.equal "http://[::]:8080", Utils.convertToUrl("[::]:8080")
assert.equal "view-source: 0.0.0.0", Utils.convertToUrl("view-source: 0.0.0.0")
+ assert.equal "javascript:alert('25 % 20 25 ');", Utils.convertToUrl "javascript:alert('25 % 20 25%20');"
should "convert non-URL terms into search queries", ->
assert.equal "http://www.google.com/search?q=google", Utils.convertToUrl("google")
@@ -62,6 +63,17 @@ context "hasChromePrefix",
assert.isFalse Utils.hasChromePrefix "data"
assert.isFalse Utils.hasChromePrefix "data :foobar"
+context "hasJavascriptPrefix",
+ should "detect javascript: URLs", ->
+ assert.isTrue Utils.hasJavascriptPrefix "javascript:foobar"
+ assert.isFalse Utils.hasJavascriptPrefix "http:foobar"
+
+context "decodeJavascriptURI",
+ should "decode javascript: URLs", ->
+ assert.equal "foobar", Utils.decodeJavascriptURI "foobar"
+ assert.equal " ", Utils.decodeJavascriptURI "%20"
+ assert.equal "25 % 20 25 ", Utils.decodeJavascriptURI "25 % 20 25%20"
+
context "isUrl",
should "identify URLs as URLs", ->
assert.isTrue Utils.isUrl "http://www.example.com/blah"