Skip to content

Commit

Permalink
Merge pull request #117 from kategengler/add-support-query-params-to-…
Browse files Browse the repository at this point in the history
…trailing-history-location

Add support for query params to the TrailingHistory location
  • Loading branch information
mansona authored Dec 17, 2024
2 parents 39d118d + e7477a7 commit 3066acc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
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',
);
});
});

0 comments on commit 3066acc

Please sign in to comment.