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

fix($location): support alternative means of setting application base path #6129

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/ng/browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var BASE_HREF_REGEXP = /^(https?\:)?\/\/[^\/]*/;

/**
* ! This is a private undocumented service !
Expand Down Expand Up @@ -253,7 +254,7 @@ function Browser(window, document, $log, $sniffer) {
*/
self.baseHref = function() {
var href = baseElement.attr('href');
return href ? href.replace(/^(https?\:)?\/\/[^\/]*/, '') : '';
return href ? href.replace(BASE_HREF_REGEXP, '') : '';
};

//////////////////////////////////////////////////////////////
Expand Down
22 changes: 20 additions & 2 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ function locationGetterSetter(property, preprocess) {
*/
function $LocationProvider(){
var hashPrefix = '',
html5Mode = false;
html5Mode = false,
_baseHref;

/**
* @ngdoc property
Expand Down Expand Up @@ -567,6 +568,22 @@ function $LocationProvider(){
}
};

/**
* @ngdoc property
* @name ng.$locationProvider#baseHref
* @methodOf ng.$locationProvider
* @description
* In situations where it is desireable to specify an external URL within the `<base>` tag, for
* simplifying access to external resources, this configuration method will override the <base>
* tag and enable location rewrites to work as expected;
*
* @param {string} href Base application url
*/
this.baseHref = function(href) {
/* global BASE_HREF_REGEXP */
_baseHref = href && href.replace(BASE_HREF_REGEXP, '');
};

/**
* @ngdoc event
* @name ng.$location#$locationChangeStart
Expand Down Expand Up @@ -600,7 +617,8 @@ function $LocationProvider(){
function( $rootScope, $browser, $sniffer, $rootElement) {
var $location,
LocationMode,
baseHref = $browser.baseHref(), // if base[href] is undefined, it defaults to ''
// if base[href] is undefined, it defaults to ''
baseHref = isString(_baseHref) ? _baseHref : $browser.baseHref(),
initialUrl = $browser.url(),
appBase;

Expand Down
17 changes: 17 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,23 @@ describe('$location', function() {
}).not.toThrow();
});
});


it('should use $locationProvider.baseHref() as base url if available', function() {
module(function($locationProvider) {
$locationProvider.baseHref("http://host.com/ngApp/");
$locationProvider.html5Mode(true);
return function($browser, $rootElement, $document){
$browser.url("http://host.com/ngApp/");
$browser.$$baseHref = "http://server/assets";
};
});

inject(function($rootScope, $location, $browser) {
expect(function() { $location.$$parse($location.$$rewrite("http://host.com/ngApp/routeA")); }).not.toThrow();
expect($location.path()).toBe('/routeA');
});
});
});


Expand Down