mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
fix: closes #13625, fix utils.params so it works with relative_paths
This commit is contained in:
@@ -56,7 +56,10 @@ define('forum/login', ['hooks', 'translator', 'jquery-form'], function (hooks, t
|
||||
success: function (data) {
|
||||
hooks.fire('action:app.loggedIn', data);
|
||||
const pathname = utils.urlToLocation(data.next).pathname;
|
||||
const params = utils.params({ url: data.next });
|
||||
const params = utils.params({
|
||||
url: data.next,
|
||||
relative_path: config.relative_path,
|
||||
});
|
||||
params.loggedin = true;
|
||||
delete params.register; // clear register message incase it exists
|
||||
const qs = $.param(params);
|
||||
|
||||
@@ -83,7 +83,10 @@ define('forum/register', [
|
||||
if (data.next) {
|
||||
const pathname = utils.urlToLocation(data.next).pathname;
|
||||
|
||||
const params = utils.params({ url: data.next });
|
||||
const params = utils.params({
|
||||
url: data.next,
|
||||
relative_path: config.relative_path,
|
||||
});
|
||||
params.registered = true;
|
||||
const qs = $.param(params);
|
||||
|
||||
|
||||
@@ -571,9 +571,11 @@ const utils = {
|
||||
let url;
|
||||
if (options.url && !options.url.startsWith('http')) {
|
||||
// relative path passed in
|
||||
options.url = options.url.replace(new RegExp(`/?${config.relative_path.slice(1)}/`, 'g'), '');
|
||||
const cleanurl = options.url.replace(new RegExp(`/^${(options.relative_path || '')}/`, 'g'), '');
|
||||
url = new URL(document.location);
|
||||
url.pathname = options.url;
|
||||
const queryIndex = cleanurl.indexOf('?');
|
||||
url.search = queryIndex !== -1 ? cleanurl.slice(queryIndex) : '';
|
||||
url.pathname = cleanurl;
|
||||
} else {
|
||||
url = new URL(options.url || document.location);
|
||||
}
|
||||
|
||||
@@ -320,6 +320,39 @@ describe('Utility Methods', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
it('should get url params for relative url', (done) => {
|
||||
const params = utils.params({
|
||||
url: '/page?foo=1&bar=test&herp=2',
|
||||
relative_path: '',
|
||||
});
|
||||
assert.strictEqual(params.foo, 1);
|
||||
assert.strictEqual(params.bar, 'test');
|
||||
assert.strictEqual(params.herp, 2);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should get url params for relative url', (done) => {
|
||||
const params = utils.params({
|
||||
url: '/page?foo=1&bar=test&herp=2',
|
||||
relative_path: '/forum',
|
||||
});
|
||||
assert.strictEqual(params.foo, 1);
|
||||
assert.strictEqual(params.bar, 'test');
|
||||
assert.strictEqual(params.herp, 2);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should get url params for relative url', (done) => {
|
||||
const params = utils.params({
|
||||
url: '/forum/page?foo=1&bar=test&herp=2',
|
||||
relative_path: '/forum',
|
||||
});
|
||||
assert.strictEqual(params.foo, 1);
|
||||
assert.strictEqual(params.bar, 'test');
|
||||
assert.strictEqual(params.herp, 2);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should get url params as arrays', (done) => {
|
||||
const params = utils.params({ url: 'http://nodebb.org?foo=1&bar=test&herp[]=2&herp[]=3' });
|
||||
assert.strictEqual(params.foo, 1);
|
||||
|
||||
Reference in New Issue
Block a user