11import { LocationStrategy } from './location_strategy' ;
2- import { StringWrapper , isPresent , CONST_EXPR } from 'angular2/src/facade/lang' ;
32import { EventEmitter , ObservableWrapper } from 'angular2/src/facade/async' ;
4- import { isBlank } from 'angular2/src/facade/lang' ;
5- import { BaseException , WrappedException } from 'angular2/src/facade/exceptions' ;
6- import { OpaqueToken , Injectable , Optional , Inject } from 'angular2/angular2' ;
7-
8- /**
9- * The `APP_BASE_HREF` token represents the base href to be used with the
10- * {@link PathLocationStrategy}.
11- *
12- * If you're using {@link PathLocationStrategy}, you must provide a provider to a string
13- * representing the URL prefix that should be preserved when generating and recognizing
14- * URLs.
15- *
16- * ### Example
17- *
18- * ```
19- * import {Component} from 'angular2/angular2';
20- * import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from 'angular2/router';
21- *
22- * @Component({directives: [ROUTER_DIRECTIVES]})
23- * @RouteConfig([
24- * {...},
25- * ])
26- * class AppCmp {
27- * // ...
28- * }
29- *
30- * bootstrap(AppCmp, [
31- * ROUTER_PROVIDERS,
32- * PathLocationStrategy,
33- * provide(APP_BASE_HREF, {useValue: '/my/app'})
34- * ]);
35- * ```
36- */
37- export const APP_BASE_HREF : OpaqueToken = CONST_EXPR ( new OpaqueToken ( 'appBaseHref' ) ) ;
3+ import { Injectable , Inject } from 'angular2/angular2' ;
384
395/**
406 * `Location` is a service that applications can use to interact with a browser's URL.
@@ -83,15 +49,8 @@ export class Location {
8349 /** @internal */
8450 _baseHref : string ;
8551
86- constructor ( public platformStrategy : LocationStrategy ,
87- @Optional ( ) @Inject ( APP_BASE_HREF ) href ?: string ) {
88- var browserBaseHref = isPresent ( href ) ? href : this . platformStrategy . getBaseHref ( ) ;
89-
90- if ( isBlank ( browserBaseHref ) ) {
91- throw new BaseException (
92- `No base href set. Either provide a provider for the APP_BASE_HREF token or add a base element to the document.` ) ;
93- }
94-
52+ constructor ( public platformStrategy : LocationStrategy ) {
53+ var browserBaseHref = this . platformStrategy . getBaseHref ( ) ;
9554 this . _baseHref = stripTrailingSlash ( stripIndexHtml ( browserBaseHref ) ) ;
9655 this . platformStrategy . onPopState (
9756 ( _ ) => { ObservableWrapper . callNext ( this . _subject , { 'url' : this . path ( ) , 'pop' : true } ) ; } ) ;
@@ -117,11 +76,10 @@ export class Location {
11776 * used, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use.
11877 */
11978 prepareExternalUrl ( url : string ) : string {
120- if ( ! url . startsWith ( '/' ) ) {
79+ if ( url . length > 0 && ! url . startsWith ( '/' ) ) {
12180 url = '/' + url ;
12281 }
123- return this . platformStrategy . prepareExternalUrl (
124- stripTrailingSlash ( _addBaseHref ( this . _baseHref , url ) ) ) ;
82+ return this . platformStrategy . prepareExternalUrl ( url ) ;
12583 }
12684
12785 /**
@@ -158,13 +116,6 @@ function _stripBaseHref(baseHref: string, url: string): string {
158116 return url ;
159117}
160118
161- function _addBaseHref ( baseHref : string , url : string ) : string {
162- if ( ! url . startsWith ( baseHref ) ) {
163- return baseHref + url ;
164- }
165- return url ;
166- }
167-
168119function stripIndexHtml ( url : string ) : string {
169120 if ( / \/ i n d e x .h t m l $ / g. test ( url ) ) {
170121 // '/index.html'.length == 11
0 commit comments