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

Commit 34cf141

Browse files
refactor($location): compute appBaseNoFile only once
1 parent 274e935 commit 34cf141

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

src/ng/location.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ function serverBase(url) {
8989
*
9090
* @constructor
9191
* @param {string} appBase application base URL
92+
* @param {string} appBaseNoFile application base URL stripped of any filename
9293
* @param {string} basePrefix url path prefix
9394
*/
94-
function LocationHtml5Url(appBase, basePrefix) {
95+
function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
9596
this.$$html5 = true;
9697
basePrefix = basePrefix || '';
97-
var appBaseNoFile = stripFile(appBase);
9898
parseAbsoluteUrl(appBase, this);
9999

100100

@@ -168,10 +168,10 @@ function LocationHtml5Url(appBase, basePrefix) {
168168
*
169169
* @constructor
170170
* @param {string} appBase application base URL
171+
* @param {string} appBaseNoFile application base URL stripped of any filename
171172
* @param {string} hashPrefix hashbang prefix
172173
*/
173-
function LocationHashbangUrl(appBase, hashPrefix) {
174-
var appBaseNoFile = stripFile(appBase);
174+
function LocationHashbangUrl(appBase, appBaseNoFile, hashPrefix) {
175175

176176
parseAbsoluteUrl(appBase, this);
177177

@@ -280,14 +280,13 @@ function LocationHashbangUrl(appBase, hashPrefix) {
280280
*
281281
* @constructor
282282
* @param {string} appBase application base URL
283+
* @param {string} appBaseNoFile application base URL stripped of any filename
283284
* @param {string} hashPrefix hashbang prefix
284285
*/
285-
function LocationHashbangInHtml5Url(appBase, hashPrefix) {
286+
function LocationHashbangInHtml5Url(appBase, appBaseNoFile, hashPrefix) {
286287
this.$$html5 = true;
287288
LocationHashbangUrl.apply(this, arguments);
288289

289-
var appBaseNoFile = stripFile(appBase);
290-
291290
this.$$parseLinkUrl = function(url, relHref) {
292291
if (relHref && relHref[0] === '#') {
293292
// special case for links to hash fragments:
@@ -823,7 +822,9 @@ function $LocationProvider() {
823822
appBase = stripHash(initialUrl);
824823
LocationMode = LocationHashbangUrl;
825824
}
826-
$location = new LocationMode(appBase, '#' + hashPrefix);
825+
var appBaseNoFile = stripFile(appBase);
826+
827+
$location = new LocationMode(appBase, appBaseNoFile, '#' + hashPrefix);
827828
$location.$$parseLinkUrl(initialUrl, initialUrl);
828829

829830
$location.$$state = $browser.state();

test/ng/locationSpec.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ describe('$location', function() {
4646

4747
it('should not include the drive name in path() on WIN', function() {
4848
//See issue #4680 for details
49-
var locationUrl = new LocationHashbangUrl('file:///base', '#!');
49+
var locationUrl = new LocationHashbangUrl('file:///base', 'file:///', '#!');
5050
locationUrl.$$parse('file:///base#!/foo?a=b&c#hash');
5151

5252
expect(locationUrl.path()).toBe('/foo');
5353
});
5454

5555

5656
it('should include the drive name if it was provided in the input url', function() {
57-
var locationUrl = new LocationHashbangUrl('file:///base', '#!');
57+
var locationUrl = new LocationHashbangUrl('file:///base', 'file:///', '#!');
5858
locationUrl.$$parse('file:///base#!/C:/foo?a=b&c#hash');
5959

6060
expect(locationUrl.path()).toBe('/C:/foo');
@@ -64,7 +64,7 @@ describe('$location', function() {
6464

6565
describe('NewUrl', function() {
6666
function createLocationHtml5Url() {
67-
var locationUrl = new LocationHtml5Url('http://www.domain.com:9877/');
67+
var locationUrl = new LocationHtml5Url('http://www.domain.com:9877/', 'http://www.domain.com:9877/');
6868
locationUrl.$$parse('http://www.domain.com:9877/path/b?search=a&b=c&d#hash');
6969
return locationUrl;
7070
}
@@ -299,18 +299,18 @@ describe('$location', function() {
299299

300300

301301
it('should parse new url', function() {
302-
var locationUrl = new LocationHtml5Url('http://host.com/');
302+
var locationUrl = new LocationHtml5Url('http://host.com/', 'http://host.com/');
303303
locationUrl.$$parse('http://host.com/base');
304304
expect(locationUrl.path()).toBe('/base');
305305

306-
locationUrl = new LocationHtml5Url('http://host.com/');
306+
locationUrl = new LocationHtml5Url('http://host.com/', 'http://host.com/');
307307
locationUrl.$$parse('http://host.com/base#');
308308
expect(locationUrl.path()).toBe('/base');
309309
});
310310

311311

312312
it('should prefix path with forward-slash', function() {
313-
var locationUrl = new LocationHtml5Url('http://server/');
313+
var locationUrl = new LocationHtml5Url('http://server/', 'http://server/') ;
314314
locationUrl.path('b');
315315

316316
expect(locationUrl.path()).toBe('/b');
@@ -319,7 +319,7 @@ describe('$location', function() {
319319

320320

321321
it('should set path to forward-slash when empty', function() {
322-
var locationUrl = new LocationHtml5Url('http://server/');
322+
var locationUrl = new LocationHtml5Url('http://server/', 'http://server/') ;
323323
locationUrl.$$parse('http://server/');
324324
expect(locationUrl.path()).toBe('/');
325325
expect(locationUrl.absUrl()).toBe('http://server/');
@@ -356,7 +356,7 @@ describe('$location', function() {
356356
});
357357

358358
it('should prepend path with basePath', function() {
359-
var locationUrl = new LocationHtml5Url('http://server/base/');
359+
var locationUrl = new LocationHtml5Url('http://server/base/', 'http://server/base/') ;
360360
locationUrl.$$parse('http://server/base/abc?a');
361361
expect(locationUrl.path()).toBe('/abc');
362362
expect(locationUrl.search()).toEqual({a: true});
@@ -367,7 +367,7 @@ describe('$location', function() {
367367

368368

369369
it('should throw error when invalid server url given', function() {
370-
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', '/base');
370+
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', 'http://server.org/base/', '/base');
371371

372372
expect(function() {
373373
locationUrl.$$parse('http://other.server.org/path#/path');
@@ -376,7 +376,7 @@ describe('$location', function() {
376376

377377

378378
it('should throw error when invalid base url given', function() {
379-
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', '/base');
379+
var locationUrl = new LocationHtml5Url('http://server.org/base/abc', 'http://server.org/base/', '/base');
380380

381381
expect(function() {
382382
locationUrl.$$parse('http://server.org/path#/path');
@@ -444,15 +444,15 @@ describe('$location', function() {
444444

445445

446446
it('should decode special characters', function() {
447-
var locationUrl = new LocationHtml5Url('http://host.com/');
447+
var locationUrl = new LocationHtml5Url('http://host.com/', 'http://host.com/');
448448
locationUrl.$$parse('http://host.com/a%20%3C%3E%23?i%20j=%3C%3E%23#x%20%3C%3E%23');
449449
expect(locationUrl.path()).toBe('/a <>#');
450450
expect(locationUrl.search()).toEqual({'i j': '<>#'});
451451
expect(locationUrl.hash()).toBe('x <>#');
452452
});
453453

454454
it('should decode pluses as spaces in urls', function() {
455-
var locationUrl = new LocationHtml5Url('http://host.com/');
455+
var locationUrl = new LocationHtml5Url('http://host.com/', 'http://host.com/');
456456
locationUrl.$$parse('http://host.com/?a+b=c+d');
457457
expect(locationUrl.search()).toEqual({'a b':'c d'});
458458
});
@@ -470,7 +470,7 @@ describe('$location', function() {
470470
describe('HashbangUrl', function() {
471471

472472
function createHashbangUrl() {
473-
var locationUrl = new LocationHashbangUrl('http://www.server.org:1234/base', '#!');
473+
var locationUrl = new LocationHashbangUrl('http://www.server.org:1234/base', 'http://www.server.org:1234/', '#!');
474474
locationUrl.$$parse('http://www.server.org:1234/base#!/path?a=b&c#hash');
475475
return locationUrl;
476476
}
@@ -499,7 +499,7 @@ describe('$location', function() {
499499

500500

501501
it('should preserve query params in base', function() {
502-
var locationUrl = new LocationHashbangUrl('http://www.server.org:1234/base?base=param', '#');
502+
var locationUrl = new LocationHashbangUrl('http://www.server.org:1234/base?base=param', 'http://www.server.org:1234/', '#');
503503
locationUrl.$$parse('http://www.server.org:1234/base?base=param#/path?a=b&c#hash');
504504
expect(locationUrl.absUrl()).toBe('http://www.server.org:1234/base?base=param#/path?a=b&c#hash');
505505

@@ -511,7 +511,7 @@ describe('$location', function() {
511511

512512

513513
it('should prefix path with forward-slash', function() {
514-
var locationUrl = new LocationHashbangUrl('http://host.com/base', '#');
514+
var locationUrl = new LocationHashbangUrl('http://host.com/base', 'http://host.com/', '#');
515515
locationUrl.$$parse('http://host.com/base#path');
516516
expect(locationUrl.path()).toBe('/path');
517517
expect(locationUrl.absUrl()).toBe('http://host.com/base#/path');
@@ -523,7 +523,7 @@ describe('$location', function() {
523523

524524

525525
it('should set path to forward-slash when empty', function() {
526-
var locationUrl = new LocationHashbangUrl('http://server/base', '#!');
526+
var locationUrl = new LocationHashbangUrl('http://server/base', 'http://server/', '#!');
527527
locationUrl.$$parse('http://server/base');
528528
locationUrl.path('aaa');
529529

@@ -592,7 +592,7 @@ describe('$location', function() {
592592

593593

594594
it('should decode special characters', function() {
595-
var locationUrl = new LocationHashbangUrl('http://host.com/a', '#');
595+
var locationUrl = new LocationHashbangUrl('http://host.com/a', 'http://host.com/', '#');
596596
locationUrl.$$parse('http://host.com/a#/%20%3C%3E%23?i%20j=%3C%3E%23#x%20%3C%3E%23');
597597
expect(locationUrl.path()).toBe('/ <>#');
598598
expect(locationUrl.search()).toEqual({'i j': '<>#'});
@@ -601,35 +601,35 @@ describe('$location', function() {
601601

602602

603603
it('should return decoded characters for search specified in URL', function() {
604-
var locationUrl = new LocationHtml5Url('http://host.com/');
604+
var locationUrl = new LocationHtml5Url('http://host.com/', 'http://host.com/');
605605
locationUrl.$$parse('http://host.com/?q=1%2F2%203');
606606
expect(locationUrl.search()).toEqual({'q': '1/2 3'});
607607
});
608608

609609

610610
it('should return decoded characters for search specified with setter', function() {
611-
var locationUrl = new LocationHtml5Url('http://host.com/');
611+
var locationUrl = new LocationHtml5Url('http://host.com/', 'http://host.com/');
612612
locationUrl.$$parse('http://host.com/');
613613
locationUrl.search('q', '1/2 3');
614614
expect(locationUrl.search()).toEqual({'q': '1/2 3'});
615615
});
616616

617617
it('should return an array for duplicate params', function() {
618-
var locationUrl = new LocationHtml5Url('http://host.com');
618+
var locationUrl = new LocationHtml5Url('http://host.com', 'http://host.com') ;
619619
locationUrl.$$parse('http://host.com');
620620
locationUrl.search('q', ['1/2 3','4/5 6']);
621621
expect(locationUrl.search()).toEqual({'q': ['1/2 3','4/5 6']});
622622
});
623623

624624
it('should encode an array correctly from search and add to url', function() {
625-
var locationUrl = new LocationHtml5Url('http://host.com');
625+
var locationUrl = new LocationHtml5Url('http://host.com', 'http://host.com') ;
626626
locationUrl.$$parse('http://host.com');
627627
locationUrl.search({'q': ['1/2 3','4/5 6']});
628628
expect(locationUrl.absUrl()).toEqual('http://host.com?q=1%2F2%203&q=4%2F5%206');
629629
});
630630

631631
it('should rewrite params when specifing a single param in search', function() {
632-
var locationUrl = new LocationHtml5Url('http://host.com');
632+
var locationUrl = new LocationHtml5Url('http://host.com', 'http://host.com') ;
633633
locationUrl.$$parse('http://host.com');
634634
locationUrl.search({'q': '1/2 3'});
635635
expect(locationUrl.absUrl()).toEqual('http://host.com?q=1%2F2%203');
@@ -2353,8 +2353,8 @@ describe('$location', function() {
23532353
var locationUrl, locationIndexUrl;
23542354

23552355
beforeEach(function() {
2356-
locationUrl = new LocationHtml5Url('http://server/pre/', 'http://server/pre/path');
2357-
locationIndexUrl = new LocationHtml5Url('http://server/pre/index.html', 'http://server/pre/path');
2356+
locationUrl = new LocationHtml5Url('http://server/pre/', 'http://server/pre/', 'http://server/pre/path');
2357+
locationIndexUrl = new LocationHtml5Url('http://server/pre/index.html', 'http://server/pre/', 'http://server/pre/path');
23582358
});
23592359

23602360
it('should rewrite URL', function() {
@@ -2416,7 +2416,7 @@ describe('$location', function() {
24162416

24172417
it('should rewrite URL', function() {
24182418
/* jshint scripturl: true */
2419-
locationUrl = new LocationHashbangUrl('http://server/pre/', '#');
2419+
locationUrl = new LocationHashbangUrl('http://server/pre/', 'http://server/pre/', '#');
24202420

24212421
expect(parseLinkAndReturn(locationUrl, 'http://other')).toEqual(undefined);
24222422
expect(parseLinkAndReturn(locationUrl, 'http://server/pre/')).toEqual('http://server/pre/');
@@ -2425,15 +2425,15 @@ describe('$location', function() {
24252425
});
24262426

24272427
it("should not set hash if one was not originally specified", function() {
2428-
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', '#');
2428+
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', 'http://server/pre/', '#');
24292429

24302430
locationUrl.$$parse('http://server/pre/index.html');
24312431
expect(locationUrl.url()).toBe('');
24322432
expect(locationUrl.absUrl()).toBe('http://server/pre/index.html');
24332433
});
24342434

24352435
it("should parse hash if one was specified", function() {
2436-
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', '#');
2436+
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', 'http://server/pre/', '#');
24372437

24382438
locationUrl.$$parse('http://server/pre/index.html#/foo/bar');
24392439
expect(locationUrl.url()).toBe('/foo/bar');
@@ -2442,7 +2442,7 @@ describe('$location', function() {
24422442

24432443

24442444
it("should prefix hash url with / if one was originally missing", function() {
2445-
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', '#');
2445+
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', 'http://server/pre/', '#');
24462446

24472447
locationUrl.$$parse('http://server/pre/index.html#not-starting-with-slash');
24482448
expect(locationUrl.url()).toBe('/not-starting-with-slash');
@@ -2452,7 +2452,7 @@ describe('$location', function() {
24522452

24532453
it('should not strip stuff from path just because it looks like Windows drive when it\'s not',
24542454
function() {
2455-
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', '#');
2455+
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', 'http://server/pre/', '#');
24562456

24572457
locationUrl.$$parse('http://server/pre/index.html#http%3A%2F%2Fexample.com%2F');
24582458
expect(locationUrl.url()).toBe('/http://example.com/');
@@ -2464,7 +2464,7 @@ describe('$location', function() {
24642464
});
24652465

24662466
it('should allow navigating outside the original base URL', function() {
2467-
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', '#');
2467+
locationUrl = new LocationHashbangUrl('http://server/pre/index.html', 'http://server/pre/', '#');
24682468

24692469
locationUrl.$$parse('http://server/next/index.html');
24702470
expect(locationUrl.url()).toBe('');
@@ -2478,8 +2478,8 @@ describe('$location', function() {
24782478
var locationUrl, locationIndexUrl;
24792479

24802480
beforeEach(function() {
2481-
locationUrl = new LocationHashbangInHtml5Url('http://server/pre/', '#!');
2482-
locationIndexUrl = new LocationHashbangInHtml5Url('http://server/pre/index.html', '#!');
2481+
locationUrl = new LocationHashbangInHtml5Url('http://server/pre/', 'http://server/pre/', '#!');
2482+
locationIndexUrl = new LocationHashbangInHtml5Url('http://server/pre/index.html', 'http://server/pre/', '#!');
24832483
});
24842484

24852485
it('should rewrite URL', function() {

0 commit comments

Comments
 (0)