Commit 5f514cb7 5f514cb715780be03a24d4cae0ab12d8fc8246d5 by Nicolas Perriault

fixes #414 - better utils.cleanUrl()

1 parent beb33963
...@@ -61,23 +61,19 @@ function betterTypeOf(input) { ...@@ -61,23 +61,19 @@ function betterTypeOf(input) {
61 exports.betterTypeOf = betterTypeOf; 61 exports.betterTypeOf = betterTypeOf;
62 62
63 /** 63 /**
64 * Cleans a passed URL if it lacks a slash at the end when a sole domain is used. 64 * Cleans a passed URL.
65 * 65 *
66 * @param String url An HTTP URL 66 * @param String url An HTTP URL
67 * @return String 67 * @return String
68 */ 68 */
69 function cleanUrl(url) { 69 function cleanUrl(url) {
70 "use strict"; 70 "use strict";
71 var parts = /(https?):\/\/(.*)/i.exec(url); 71 if (url.indexOf('/') === 0) {
72 if (!parts) {
73 return url; 72 return url;
74 } 73 }
75 var protocol = parts[1]; 74 var a = document.createElement('a');
76 var subparts = parts[2].split('/'); 75 a.href = url;
77 if (subparts.length === 1) { 76 return a.href;
78 return format("%s://%s/", protocol, subparts[0]);
79 }
80 return url;
81 } 77 }
82 exports.cleanUrl = cleanUrl; 78 exports.cleanUrl = cleanUrl;
83 79
......
...@@ -24,7 +24,7 @@ casper.test.begin('utils.betterTypeOf() tests', 10, function(test) { ...@@ -24,7 +24,7 @@ casper.test.begin('utils.betterTypeOf() tests', 10, function(test) {
24 test.done(); 24 test.done();
25 }); 25 });
26 26
27 casper.test.begin('utils.cleanUrl() tests', 10, function(test) { 27 casper.test.begin('utils.cleanUrl() tests', 11, function(test) {
28 var testCases = { 28 var testCases = {
29 'http://google.com/': 'http://google.com/', 29 'http://google.com/': 'http://google.com/',
30 'http://google.com': 'http://google.com/', 30 'http://google.com': 'http://google.com/',
...@@ -34,6 +34,7 @@ casper.test.begin('utils.cleanUrl() tests', 10, function(test) { ...@@ -34,6 +34,7 @@ casper.test.begin('utils.cleanUrl() tests', 10, function(test) {
34 'https://google.com': 'https://google.com/', 34 'https://google.com': 'https://google.com/',
35 'https://www.google.com/': 'https://www.google.com/', 35 'https://www.google.com/': 'https://www.google.com/',
36 'https://www.google.com/?plop=2': 'https://www.google.com/?plop=2', 36 'https://www.google.com/?plop=2': 'https://www.google.com/?plop=2',
37 'https://www.google.com?plop=2': 'https://www.google.com/?plop=2',
37 'file:///Users/toto/toto.html': 'file:///Users/toto/toto.html', 38 'file:///Users/toto/toto.html': 'file:///Users/toto/toto.html',
38 '/100': '/100' 39 '/100': '/100'
39 }; 40 };
......