From 021ba14dc33e6fc3ca7d37d71296f3356f2e9325 Mon Sep 17 00:00:00 2001 From: brettp Date: Sat, 19 Feb 2011 18:42:40 +0000 Subject: 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 --- js/lib/elgglib.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'js/lib/elgglib.js') 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('/'); + } }; /** -- cgit v1.2.3