Properly join url and path with / separator in Meteor.absoluteUrl.

This commit is contained in:
Ben Newman
2018-05-12 11:22:20 -04:00
parent 5f131ffe3b
commit a8a97016b7
2 changed files with 10 additions and 4 deletions

View File

@@ -23,11 +23,17 @@ Meteor.absoluteUrl = function (path, options) {
if (!/^http[s]?:\/\//i.test(url)) // url starts with 'http://' or 'https://'
url = 'http://' + url; // we will later fix to https if options.secure is set
if (!/\/$/.test(url)) // url ends with '/'
url += '/';
if (! url.endsWith("/")) {
url += "/";
}
if (path)
if (path) {
// join url and path with a / separator
while (path.startsWith("/")) {
path = path.slice(1);
}
url += path;
}
// turn http to https if secure option is set, and we're not talking
// to localhost.

View File

@@ -11,7 +11,7 @@ Tinytest.add("absolute-url - basics", function(test) {
test.equal(Meteor.absoluteUrl('foo', {rootUrl: prefix + 'asdf.com/'}),
'http://asdf.com/foo');
test.equal(Meteor.absoluteUrl('/foo', {rootUrl: prefix + 'asdf.com'}),
'http://asdf.com//foo');
'http://asdf.com/foo');
test.equal(Meteor.absoluteUrl('#foo', {rootUrl: prefix + 'asdf.com'}),
'http://asdf.com/#foo');