From fe473a2936b0d4514d6bf504a6b98633a65818ba Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Fri, 1 Jul 2016 17:17:56 -0700 Subject: [PATCH] [BUGFIX release] Fix Location#getURL 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. --- .../lib/location/history_location.js | 4 +-- .../lib/location/none_location.js | 2 +- .../tests/location/history_location_test.js | 32 +++++++++++++++++++ .../tests/location/none_location_test.js | 18 ++++++++++- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/packages/ember-routing/lib/location/history_location.js b/packages/ember-routing/lib/location/history_location.js index eb0ed1e8d69..5aaba19c597 100644 --- a/packages/ember-routing/lib/location/history_location.js +++ b/packages/ember-routing/lib/location/history_location.js @@ -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; diff --git a/packages/ember-routing/lib/location/none_location.js b/packages/ember-routing/lib/location/none_location.js index ee31b1a610c..3ccebe41bec 100644 --- a/packages/ember-routing/lib/location/none_location.js +++ b/packages/ember-routing/lib/location/none_location.js @@ -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 + '(?=/|$)'), ''); }, /** diff --git a/packages/ember-routing/tests/location/history_location_test.js b/packages/ember-routing/tests/location/history_location_test.js index ad36620196b..291e775bc45 100644 --- a/packages/ember-routing/tests/location/history_location_test.js +++ b/packages/ember-routing/tests/location/history_location_test.js @@ -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); @@ -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); diff --git a/packages/ember-routing/tests/location/none_location_test.js b/packages/ember-routing/tests/location/none_location_test.js index 9f17a13863b..522414ffac5 100644 --- a/packages/ember-routing/tests/location/none_location_test.js +++ b/packages/ember-routing/tests/location/none_location_test.js @@ -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({ @@ -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'); +});