Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d4d34ab

Browse files
committed
fix($location): don't initialize hash url unnecessarily
After a recent refactoring using $location in the default hashbang mode would result in hash url being initialized unnecessarily in cases when the base url didn't end with a slash. for example http://localhost:8000/temp.html would get rewritten as http://location:8000/temp.html#/temp.html by error.
1 parent 04cebcc commit d4d34ab

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/ng/location.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function serverBase(url) {
8484
* @param {string} basePrefix url path prefix
8585
*/
8686
function LocationHtml5Url(appBase, basePrefix) {
87+
this.$$html5 = true;
8788
basePrefix = basePrefix || '';
8889
var appBaseNoFile = stripFile(appBase);
8990
/**
@@ -140,7 +141,8 @@ function LocationHtml5Url(appBase, basePrefix) {
140141

141142
/**
142143
* LocationHashbangUrl represents url
143-
* This object is exposed as $location service when html5 history api is disabled or not supported
144+
* This object is exposed as $location service when developer doesn't opt into html5 mode.
145+
* It also serves as the base class for html5 mode fallback on legacy browsers.
144146
*
145147
* @constructor
146148
* @param {string} appBase application base URL
@@ -149,18 +151,25 @@ function LocationHtml5Url(appBase, basePrefix) {
149151
function LocationHashbangUrl(appBase, hashPrefix) {
150152
var appBaseNoFile = stripFile(appBase);
151153

154+
matchUrl(appBase, this);
155+
156+
152157
/**
153158
* Parse given hashbang url into properties
154159
* @param {string} url Hashbang url
155160
* @private
156161
*/
157162
this.$$parse = function(url) {
158-
matchUrl(url, this);
159163
var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url);
160164
if (!isString(withoutBaseUrl)) {
161165
throw $locationMinErr('istart', 'Invalid url "{0}", does not start with "{1}".', url, appBase);
162166
}
163-
var withoutHashUrl = withoutBaseUrl.charAt(0) == '#' ? beginsWith(hashPrefix, withoutBaseUrl) : withoutBaseUrl;
167+
var withoutHashUrl = withoutBaseUrl.charAt(0) == '#'
168+
? beginsWith(hashPrefix, withoutBaseUrl)
169+
: (this.$$html5)
170+
? withoutBaseUrl
171+
: '';
172+
164173
if (!isString(withoutHashUrl)) {
165174
throw $locationMinErr('nohash', 'Invalid url "{0}", missing hash prefix "{1}".', url, hashPrefix);
166175
}
@@ -198,6 +207,7 @@ function LocationHashbangUrl(appBase, hashPrefix) {
198207
* @param {string} hashPrefix hashbang prefix
199208
*/
200209
function LocationHashbangInHtml5Url(appBase, hashPrefix) {
210+
this.$$html5 = true;
201211
LocationHashbangUrl.apply(this, arguments);
202212

203213
var appBaseNoFile = stripFile(appBase);
@@ -220,6 +230,12 @@ LocationHashbangInHtml5Url.prototype =
220230
LocationHashbangUrl.prototype =
221231
LocationHtml5Url.prototype = {
222232

233+
/**
234+
* Are we in html5 mode?
235+
* @private
236+
*/
237+
$$html5: false,
238+
223239
/**
224240
* Has any change been replacing ?
225241
* @private

test/ng/locationSpec.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ describe('$location', function() {
627627
);
628628
});
629629

630-
it('should correctly convert html5 url with path matching basepath to hashbang url', function () {
630+
it('should correctly convert html5 url with path matching basepath to hashbang url', function () {
631631
initService(true, '!', false);
632632
inject(
633633
initBrowser('http://domain.com/base/index.html', '/base/index.html'),
@@ -1422,16 +1422,39 @@ describe('$location', function() {
14221422
describe('LocationHashbangUrl', function() {
14231423
var location;
14241424

1425-
beforeEach(function() {
1426-
location = new LocationHashbangUrl('http://server/pre/', 'http://server/pre/#/path');
1427-
});
1428-
14291425
it('should rewrite URL', function() {
1426+
location = new LocationHashbangUrl('http://server/pre/', '#');
1427+
14301428
expect(location.$$rewrite('http://other')).toEqual(undefined);
14311429
expect(location.$$rewrite('http://server/pre/')).toEqual('http://server/pre/');
14321430
expect(location.$$rewrite('http://server/pre/#otherPath')).toEqual('http://server/pre/#otherPath');
14331431
expect(location.$$rewrite('javascript:void(0)')).toEqual(undefined);
14341432
});
1433+
1434+
it("should not set hash if one was not originally specified", function() {
1435+
location = new LocationHashbangUrl('http://server/pre/index.html', '#');
1436+
1437+
location.$$parse('http://server/pre/index.html')
1438+
expect(location.url()).toBe('');
1439+
expect(location.absUrl()).toBe('http://server/pre/index.html');
1440+
});
1441+
1442+
it("should parse hash if one was specified", function() {
1443+
location = new LocationHashbangUrl('http://server/pre/index.html', '#');
1444+
1445+
location.$$parse('http://server/pre/index.html#/foo/bar')
1446+
expect(location.url()).toBe('/foo/bar');
1447+
expect(location.absUrl()).toBe('http://server/pre/index.html#/foo/bar');
1448+
});
1449+
1450+
1451+
it("should prefix hash url with / if one was originally missing", function() {
1452+
location = new LocationHashbangUrl('http://server/pre/index.html', '#');
1453+
1454+
location.$$parse('http://server/pre/index.html#not-starting-with-slash')
1455+
expect(location.url()).toBe('/not-starting-with-slash');
1456+
expect(location.absUrl()).toBe('http://server/pre/index.html#/not-starting-with-slash');
1457+
});
14351458
});
14361459

14371460

0 commit comments

Comments
 (0)