Skip to content
Open
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
6 changes: 3 additions & 3 deletions packages/@ember/routing/history-location.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EmberObject from '@ember/object';
import { assert } from '@ember/debug';
import type { default as EmberLocation, UpdateCallback } from '@ember/routing/location';
import { getHash } from './lib/location-utils';
import { escapeRegExp, getHash } from './lib/location-utils';

/**
@module @ember/routing/history-location
Expand Down Expand Up @@ -134,8 +134,8 @@ export default class HistoryLocation extends EmberObject implements EmberLocatio

// remove baseURL and rootURL from start of path
let url = path
.replace(new RegExp(`^${baseURL}(?=/|$)`), '')
.replace(new RegExp(`^${rootURL}(?=/|$)`), '')
.replace(new RegExp(`^${escapeRegExp(baseURL)}(?=/|$)`), '')
.replace(new RegExp(`^${escapeRegExp(rootURL)}(?=/|$)`), '')
.replace(/\/\//g, '/'); // remove extra slashes

let search = location.search || '';
Expand Down
11 changes: 11 additions & 0 deletions packages/@ember/routing/lib/location-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ export function getFullPath(location: Location): string {
export function replacePath(location: Location, path: string): void {
location.replace(location.origin + path);
}

const REGEXP_SPECIAL_CHARS = /[.*+?^${}()|[\]\\]/g;

/**
@private

Escapes RegExp special characters so a value can be matched literally.
*/
export function escapeRegExp(string: string): string {
return string.replace(REGEXP_SPECIAL_CHARS, '\\$&');
}
3 changes: 2 additions & 1 deletion packages/@ember/routing/none-location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import EmberObject from '@ember/object';
import { assert } from '@ember/debug';
import type { default as EmberLocation, UpdateCallback } from '@ember/routing/location';
import { escapeRegExp } from './lib/location-utils';

/**
@module @ember/routing/none-location
Expand Down Expand Up @@ -63,7 +64,7 @@ export default class NoneLocation extends EmberObject implements EmberLocation {
rootURL = rootURL.replace(/\/$/, '');

// remove rootURL from url
return path.replace(new RegExp(`^${rootURL}(?=/|$)`), '');
return path.replace(new RegExp(`^${escapeRegExp(rootURL)}(?=/|$)`), '');
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/@ember/routing/tests/location/history_location_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ moduleFor(
assert.equal(location.getURL(), '/bars/baz');
}

['@test HistoryLocation.getURL() treats regex special characters in rootURL literally'](
assert
) {
HistoryTestLocation.reopen({
init() {
this._super(...arguments);
set(this, 'location', mockBrowserLocation('/appXv2/posts'));
set(this, 'rootURL', '/app.v2/');
},
});

createLocation();

assert.equal(location.getURL(), '/appXv2/posts');
}

['@test HistoryLocation.getURL() returns the current url, does not remove baseURL if its not at start of url'](
assert
) {
Expand Down