Skip to content

Commit

Permalink
[BUGFIX release] Fix Location#getURL
Browse files Browse the repository at this point in the history
Prior fixes to NoneLocation and HistoryLocation's getURL broke the
case where the rootURL or baseURL matched the start of the main URL.
Resolve that case by checking for a subsequent forward slash or end
of string.
  • Loading branch information
wagenet committed Jul 6, 2016
1 parent f54a209 commit fe473a2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/ember-routing/lib/location/history_location.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export default EmberObject.extend({

// remove baseURL and rootURL from start of path
var url = path
.replace(new RegExp('^' + baseURL), '')
.replace(new RegExp('^' + rootURL), '');
.replace(new RegExp('^' + baseURL + '(?=/|$)'), '')
.replace(new RegExp('^' + rootURL + '(?=/|$)'), '');

var search = location.search || '';
url += search;
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-routing/lib/location/none_location.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default EmberObject.extend({
rootURL = rootURL.replace(/\/$/, '');

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

/**
Expand Down
32 changes: 32 additions & 0 deletions packages/ember-routing/tests/location/history_location_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ QUnit.test('HistoryLocation.getURL() returns the current url, does not remove ro
equal(location.getURL(), '/foo/bar/baz');
});

QUnit.test('HistoryLocation.getURL() will not remove the rootURL when only a partial match', function() {
expect(1);

HistoryTestLocation.reopen({
init() {
this._super(...arguments);
set(this, 'location', mockBrowserLocation('/bars/baz'));
set(this, 'rootURL', '/bar/');
}
});

createLocation();

equal(location.getURL(), '/bars/baz');
});

QUnit.test('HistoryLocation.getURL() returns the current url, does not remove baseURL if its not at start of url', function() {
expect(1);

Expand All @@ -206,6 +222,22 @@ QUnit.test('HistoryLocation.getURL() returns the current url, does not remove ba
equal(location.getURL(), '/foo/bar/baz');
});

QUnit.test('HistoryLocation.getURL() will not remove the baseURL when only a partial match', function() {
expect(1);

HistoryTestLocation.reopen({
init() {
this._super(...arguments);
set(this, 'location', mockBrowserLocation('/bars/baz'));
set(this, 'baseURL', '/bar/');
}
});

createLocation();

equal(location.getURL(), '/bars/baz');
});

QUnit.test('HistoryLocation.getURL() includes location.search', function() {
expect(1);

Expand Down
18 changes: 17 additions & 1 deletion packages/ember-routing/tests/location/none_location_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ QUnit.test('NoneLocation.getURL() returns the current path minus rootURL', funct
equal(location.getURL(), '/bar');
});

QUnit.test('NonoLocation.getURL() will remove the rootURL only from the beginning of a url', function() {
QUnit.test('NoneLocation.getURL() will remove the rootURL only from the beginning of a url', function() {
expect(1);

NoneTestLocation.reopen({
Expand All @@ -67,3 +67,19 @@ QUnit.test('NonoLocation.getURL() will remove the rootURL only from the beginnin

equal(location.getURL(), '/foo/bar/baz');
});

QUnit.test('NoneLocation.getURL() will not remove the rootURL when only a partial match', function() {
expect(1);

NoneTestLocation.reopen({
init() {
this._super(...arguments);
set(this, 'rootURL', '/bar/');
set(this, 'path', '/bars/baz');
}
});

createLocation();

equal(location.getURL(), '/bars/baz');
});

0 comments on commit fe473a2

Please sign in to comment.