Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for query params to the TrailingHistory location #117

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/locations/trailing-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import HistoryLocation from '@ember/routing/history-location';
export default HistoryLocation.extend({
formatURL() {
let url = this._super(...arguments);
return formatURL(url);
},
});

if (url.includes('#')) {
return url.replace(/([^/])#(.*)/, '$1/#$2');
} else {
return url.replace(/\/?$/, '/');
}
export function formatURL(url) {
let modifiedURL = new URL(url, 'http://example.com');
if (!modifiedURL.pathname.endsWith('/')) {
modifiedURL.pathname += '/';
}
});
return `${modifiedURL.pathname}${modifiedURL.search}${modifiedURL.hash}`;
}
26 changes: 26 additions & 0 deletions tests/unit/locations/trailing-history-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { formatURL } from 'dummy/locations/trailing-history';

module('Unit | Locations | trailing history', function (hooks) {
setupTest(hooks);

test('supports query params', function (assert) {
assert.deepEqual(formatURL('/foo?bar=baz'), '/foo/?bar=baz');
});

test('supports anchors', function (assert) {
assert.deepEqual(formatURL('/foo#placeholder'), '/foo/#placeholder');
});

test('does not add double slashes', function (assert) {
assert.deepEqual(formatURL('/foo/'), '/foo/');
});

test('supports query params and anchors', function (assert) {
assert.deepEqual(
formatURL('/foo?bar=baz#placeholder'),
'/foo/?bar=baz#placeholder',
);
});
});
Loading