@@ -127,21 +127,32 @@ function LocationHtml5Url(appBase, basePrefix) {
127
127
this . $$absUrl = appBaseNoFile + this . $$url . substr ( 1 ) ; // first char is always '/'
128
128
} ;
129
129
130
- this . $$rewrite = function ( url ) {
130
+ this . $$parseLinkUrl = function ( url , relHref ) {
131
+ if ( relHref && relHref [ 0 ] === '#' ) {
132
+ // special case for links to hash fragments:
133
+ // keep the old url and only replace the hash fragment
134
+ this . hash ( relHref . slice ( 1 ) ) ;
135
+ return true ;
136
+ }
131
137
var appUrl , prevAppUrl ;
138
+ var rewrittenUrl ;
132
139
133
140
if ( ( appUrl = beginsWith ( appBase , url ) ) !== undefined ) {
134
141
prevAppUrl = appUrl ;
135
142
if ( ( appUrl = beginsWith ( basePrefix , appUrl ) ) !== undefined ) {
136
- return appBaseNoFile + ( beginsWith ( '/' , appUrl ) || appUrl ) ;
143
+ rewrittenUrl = appBaseNoFile + ( beginsWith ( '/' , appUrl ) || appUrl ) ;
137
144
} else {
138
- return appBase + prevAppUrl ;
145
+ rewrittenUrl = appBase + prevAppUrl ;
139
146
}
140
147
} else if ( ( appUrl = beginsWith ( appBaseNoFile , url ) ) !== undefined ) {
141
- return appBaseNoFile + appUrl ;
148
+ rewrittenUrl = appBaseNoFile + appUrl ;
142
149
} else if ( appBaseNoFile == url + '/' ) {
143
- return appBaseNoFile ;
150
+ rewrittenUrl = appBaseNoFile ;
151
+ }
152
+ if ( rewrittenUrl ) {
153
+ this . $$parse ( rewrittenUrl ) ;
144
154
}
155
+ return ! ! rewrittenUrl ;
145
156
} ;
146
157
}
147
158
@@ -231,10 +242,12 @@ function LocationHashbangUrl(appBase, hashPrefix) {
231
242
this . $$absUrl = appBase + ( this . $$url ? hashPrefix + this . $$url : '' ) ;
232
243
} ;
233
244
234
- this . $$rewrite = function ( url ) {
245
+ this . $$parseLinkUrl = function ( url , relHref ) {
235
246
if ( stripHash ( appBase ) == stripHash ( url ) ) {
236
- return url ;
247
+ this . $$parse ( url ) ;
248
+ return true ;
237
249
}
250
+ return false ;
238
251
} ;
239
252
}
240
253
@@ -254,16 +267,28 @@ function LocationHashbangInHtml5Url(appBase, hashPrefix) {
254
267
255
268
var appBaseNoFile = stripFile ( appBase ) ;
256
269
257
- this . $$rewrite = function ( url ) {
270
+ this . $$parseLinkUrl = function ( url , relHref ) {
271
+ if ( relHref && relHref [ 0 ] === '#' ) {
272
+ // special case for links to hash fragments:
273
+ // keep the old url and only replace the hash fragment
274
+ this . hash ( relHref . slice ( 1 ) ) ;
275
+ return true ;
276
+ }
277
+
278
+ var rewrittenUrl ;
258
279
var appUrl ;
259
280
260
281
if ( appBase == stripHash ( url ) ) {
261
- return url ;
282
+ rewrittenUrl = url ;
262
283
} else if ( ( appUrl = beginsWith ( appBaseNoFile , url ) ) ) {
263
- return appBase + hashPrefix + appUrl ;
284
+ rewrittenUrl = appBase + hashPrefix + appUrl ;
264
285
} else if ( appBaseNoFile === url + '/' ) {
265
- return appBaseNoFile ;
286
+ rewrittenUrl = appBaseNoFile ;
266
287
}
288
+ if ( rewrittenUrl ) {
289
+ this . $$parse ( rewrittenUrl ) ;
290
+ }
291
+ return ! ! rewrittenUrl ;
267
292
} ;
268
293
269
294
this . $$compose = function ( ) {
@@ -626,14 +651,18 @@ function $LocationProvider(){
626
651
appBase ;
627
652
628
653
if ( html5Mode ) {
654
+ if ( ! baseHref ) {
655
+ throw $locationMinErr ( 'nobase' ,
656
+ "$location in HTML5 mode requires a <base> tag to be present!" ) ;
657
+ }
629
658
appBase = serverBase ( initialUrl ) + ( baseHref || '/' ) ;
630
659
LocationMode = $sniffer . history ? LocationHtml5Url : LocationHashbangInHtml5Url ;
631
660
} else {
632
661
appBase = stripHash ( initialUrl ) ;
633
662
LocationMode = LocationHashbangUrl ;
634
663
}
635
664
$location = new LocationMode ( appBase , '#' + hashPrefix ) ;
636
- $location . $$parse ( $location . $$rewrite ( initialUrl ) ) ;
665
+ $location . $$parseLinkUrl ( initialUrl , initialUrl ) ;
637
666
638
667
var IGNORE_URI_REGEXP = / ^ \s * ( j a v a s c r i p t | m a i l t o ) : / i;
639
668
@@ -652,6 +681,9 @@ function $LocationProvider(){
652
681
}
653
682
654
683
var absHref = elm . prop ( 'href' ) ;
684
+ // get the actual href attribute - see
685
+ // http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx
686
+ var relHref = elm . attr ( 'href' ) || elm . attr ( 'xlink:href' ) ;
655
687
656
688
if ( isObject ( absHref ) && absHref . toString ( ) === '[object SVGAnimatedString]' ) {
657
689
// SVGAnimatedString.animVal should be identical to SVGAnimatedString.baseVal, unless during
@@ -662,50 +694,15 @@ function $LocationProvider(){
662
694
// Ignore when url is started with javascript: or mailto:
663
695
if ( IGNORE_URI_REGEXP . test ( absHref ) ) return ;
664
696
665
- // Make relative links work in HTML5 mode for legacy browsers (or at least IE8 & 9)
666
- // The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or
667
- // somewhere#anchor or http://example.com/somewhere
668
- if ( LocationMode === LocationHashbangInHtml5Url ) {
669
- // get the actual href attribute - see
670
- // http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx
671
- var href = elm . attr ( 'href' ) || elm . attr ( 'xlink:href' ) ;
672
-
673
- if ( href && href . indexOf ( '://' ) < 0 ) { // Ignore absolute URLs
674
- var prefix = '#' + hashPrefix ;
675
- if ( href [ 0 ] == '/' ) {
676
- // absolute path - replace old path
677
- absHref = appBase + prefix + href ;
678
- } else if ( href [ 0 ] == '#' ) {
679
- // local anchor
680
- absHref = appBase + prefix + ( $location . path ( ) || '/' ) + href ;
681
- } else {
682
- // relative path - join with current path
683
- var stack = $location . path ( ) . split ( "/" ) ,
684
- parts = href . split ( "/" ) ;
685
- if ( stack . length === 2 && ! stack [ 1 ] ) stack . length = 1 ;
686
- for ( var i = 0 ; i < parts . length ; i ++ ) {
687
- if ( parts [ i ] == "." )
688
- continue ;
689
- else if ( parts [ i ] == ".." )
690
- stack . pop ( ) ;
691
- else if ( parts [ i ] . length )
692
- stack . push ( parts [ i ] ) ;
693
- }
694
- absHref = appBase + prefix + stack . join ( '/' ) ;
695
- }
696
- }
697
- }
698
-
699
- var rewrittenUrl = $location . $$rewrite ( absHref ) ;
700
-
701
- if ( absHref && ! elm . attr ( 'target' ) && rewrittenUrl && ! event . isDefaultPrevented ( ) ) {
702
- event . preventDefault ( ) ;
703
- if ( rewrittenUrl != $browser . url ( ) ) {
697
+ if ( absHref && ! elm . attr ( 'target' ) && ! event . isDefaultPrevented ( ) ) {
698
+ if ( $location . $$parseLinkUrl ( absHref , relHref ) ) {
699
+ event . preventDefault ( ) ;
704
700
// update location manually
705
- $location . $$parse ( rewrittenUrl ) ;
706
- $rootScope . $apply ( ) ;
707
- // hack to work around FF6 bug 684208 when scenario runner clicks on links
708
- window . angular [ 'ff-684208-preventDefault' ] = true ;
701
+ if ( $location . absUrl ( ) != $browser . url ( ) ) {
702
+ $rootScope . $apply ( ) ;
703
+ // hack to work around FF6 bug 684208 when scenario runner clicks on links
704
+ window . angular [ 'ff-684208-preventDefault' ] = true ;
705
+ }
709
706
}
710
707
}
711
708
} ) ;
0 commit comments