diff options
author | brettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-02-19 18:42:40 +0000 |
---|---|---|
committer | brettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-02-19 18:42:40 +0000 |
commit | 021ba14dc33e6fc3ca7d37d71296f3356f2e9325 (patch) | |
tree | e5093f9caec0fc31cfc226bd8a51ffb790c40391 /js | |
parent | ea74dfc453b04da0c621a95c91ae736815c7815c (diff) | |
download | elgg-021ba14dc33e6fc3ca7d37d71296f3356f2e9325.tar.gz elgg-021ba14dc33e6fc3ca7d37d71296f3356f2e9325.tar.bz2 |
elgg.normalize_url() JS does the same as elgg_normalize_url() PHP.
git-svn-id: http://code.elgg.org/elgg/trunk@8339 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'js')
-rw-r--r-- | js/lib/elgglib.js | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/js/lib/elgglib.js b/js/lib/elgglib.js index f53809a83..60907777b 100644 --- a/js/lib/elgglib.js +++ b/js/lib/elgglib.js @@ -233,9 +233,16 @@ elgg.inherit = function(Child, Parent) { }; /** - * Prepend elgg.config.wwwroot to a url if the url doesn't already have it. + * Converts shorthand urls to absolute urls. * - * @param {String} url The url to extend + * If the url is already absolute or protocol-relative, no change is made. + * + * elgg.normalize_url(''); // 'http://my.site.com/' + * elgg.normalize_url('pg/dashboard'); // 'http://my.site.com/pg/dashboard' + * elgg.normalize_url('http://google.com/'); // no change + * elgg.normalize_url('//google.com/'); // no change + * + * @param {String} url The url to normalize * @return {String} The extended url * @private */ @@ -244,11 +251,33 @@ elgg.normalize_url = function(url) { elgg.assertTypeOf('string', url); // jslint complains if you use /regexp/ shorthand here... ?!?! - if ((new RegExp("^(https?:)?//")).test(url)) { + if ((new RegExp("^(https?:)?//", "i")).test(url)) { + return url; + } + + // 'javascript:' + else if (url.indexOf('javascript:') === 0) { return url; } - return elgg.config.wwwroot + url.ltrim('/'); + // watch those double escapes in JS. + + // 'install.php', 'install.php?step=step' + else if ((new RegExp("^[^\/]*\\.php(\\?.*)?$", "i")).test(url)) { + return elgg.config.wwwroot + url.ltrim('/'); + } + + // 'example.com', 'example.com/subpage' + else if ((new RegExp("^[^/]*\\.", "i")).test(url)) { + return 'http://' + url; + } + + // 'pg/page/handler', 'mod/plugin/file.php' + else { + // trim off any leading / because the site URL is stored + // with a trailing / + return elgg.config.wwwroot + url.ltrim('/'); + } }; /** |