@@ -23,6 +23,10 @@ function encodePath(path) {
23
23
return segments . join ( '/' ) ;
24
24
}
25
25
26
+ function stripHash ( url ) {
27
+ return url . split ( '#' ) [ 0 ] ;
28
+ }
29
+
26
30
27
31
function matchUrl ( url , obj ) {
28
32
var match = URL_MATCH . exec ( url ) ;
@@ -102,19 +106,19 @@ function convertToHashbangUrl(url, basePath, hashPrefix) {
102
106
* @param {string } url HTML5 url
103
107
* @param {string } pathPrefix
104
108
*/
105
- function LocationUrl ( url , pathPrefix ) {
109
+ function LocationUrl ( url , pathPrefix , appBaseUrl ) {
106
110
pathPrefix = pathPrefix || '' ;
107
111
108
112
/**
109
113
* Parse given html5 (regular) url string into properties
110
- * @param {string } url HTML5 url
114
+ * @param {string } newAbsoluteUrl HTML5 url
111
115
* @private
112
116
*/
113
- this . $$parse = function ( url ) {
114
- var match = matchUrl ( url , this ) ;
117
+ this . $$parse = function ( newAbsoluteUrl ) {
118
+ var match = matchUrl ( newAbsoluteUrl , this ) ;
115
119
116
120
if ( match . path . indexOf ( pathPrefix ) !== 0 ) {
117
- throw Error ( 'Invalid url "' + url + '", missing path prefix "' + pathPrefix + '" !' ) ;
121
+ throw Error ( 'Invalid url "' + newAbsoluteUrl + '", missing path prefix "' + pathPrefix + '" !' ) ;
118
122
}
119
123
120
124
this . $$path = decodeURIComponent ( match . path . substr ( pathPrefix . length ) ) ;
@@ -137,6 +141,14 @@ function LocationUrl(url, pathPrefix) {
137
141
pathPrefix + this . $$url ;
138
142
} ;
139
143
144
+
145
+ this . $$rewriteAppUrl = function ( absoluteLinkUrl ) {
146
+ if ( absoluteLinkUrl . indexOf ( appBaseUrl ) == 0 ) {
147
+ return absoluteLinkUrl ;
148
+ }
149
+ }
150
+
151
+
140
152
this . $$parse ( url ) ;
141
153
}
142
154
@@ -149,7 +161,7 @@ function LocationUrl(url, pathPrefix) {
149
161
* @param {string } url Legacy url
150
162
* @param {string } hashPrefix Prefix for hash part (containing path and search)
151
163
*/
152
- function LocationHashbangUrl ( url , hashPrefix ) {
164
+ function LocationHashbangUrl ( url , hashPrefix , appBaseUrl ) {
153
165
var basePath ;
154
166
155
167
/**
@@ -192,6 +204,13 @@ function LocationHashbangUrl(url, hashPrefix) {
192
204
basePath + ( this . $$url ? '#' + hashPrefix + this . $$url : '' ) ;
193
205
} ;
194
206
207
+ this . $$rewriteAppUrl = function ( absoluteLinkUrl ) {
208
+ if ( absoluteLinkUrl . indexOf ( appBaseUrl ) == 0 ) {
209
+ return absoluteLinkUrl ;
210
+ }
211
+ }
212
+
213
+
195
214
this . $$parse ( url ) ;
196
215
}
197
216
@@ -380,6 +399,19 @@ LocationUrl.prototype = {
380
399
381
400
LocationHashbangUrl . prototype = inherit ( LocationUrl . prototype ) ;
382
401
402
+ function LocationHashbangInHtml5Url ( url , hashPrefix , appBaseUrl , baseExtra ) {
403
+ LocationHashbangUrl . apply ( this , arguments ) ;
404
+
405
+
406
+ this . $$rewriteAppUrl = function ( absoluteLinkUrl ) {
407
+ if ( absoluteLinkUrl . indexOf ( appBaseUrl ) == 0 ) {
408
+ return appBaseUrl + baseExtra + '#' + hashPrefix + absoluteLinkUrl . substr ( appBaseUrl . length ) ;
409
+ }
410
+ }
411
+ }
412
+
413
+ LocationHashbangInHtml5Url . prototype = inherit ( LocationHashbangUrl . prototype ) ;
414
+
383
415
function locationGetter ( property ) {
384
416
return function ( ) {
385
417
return this [ property ] ;
@@ -479,26 +511,33 @@ function $LocationProvider(){
479
511
basePath ,
480
512
pathPrefix ,
481
513
initUrl = $browser . url ( ) ,
482
- absUrlPrefix ;
514
+ initUrlParts = matchUrl ( initUrl ) ,
515
+ appBaseUrl ;
483
516
484
517
if ( html5Mode ) {
485
518
basePath = $browser . baseHref ( ) || '/' ;
486
519
pathPrefix = pathPrefixFromBase ( basePath ) ;
520
+ appBaseUrl =
521
+ composeProtocolHostPort ( initUrlParts . protocol , initUrlParts . host , initUrlParts . port ) +
522
+ pathPrefix + '/' ;
523
+
487
524
if ( $sniffer . history ) {
488
525
$location = new LocationUrl (
489
526
convertToHtml5Url ( initUrl , basePath , hashPrefix ) ,
490
- pathPrefix ) ;
527
+ pathPrefix , appBaseUrl ) ;
491
528
} else {
492
- $location = new LocationHashbangUrl (
529
+ $location = new LocationHashbangInHtml5Url (
493
530
convertToHashbangUrl ( initUrl , basePath , hashPrefix ) ,
494
- hashPrefix ) ;
531
+ hashPrefix , appBaseUrl , basePath . substr ( pathPrefix . length + 1 ) ) ;
495
532
}
496
- // link rewriting
497
- absUrlPrefix = composeProtocolHostPort (
498
- $location . protocol ( ) , $location . host ( ) , $location . port ( ) ) + pathPrefix ;
499
533
} else {
500
- $location = new LocationHashbangUrl ( initUrl , hashPrefix ) ;
501
- absUrlPrefix = $location . absUrl ( ) . split ( '#' ) [ 0 ] ;
534
+ appBaseUrl =
535
+ composeProtocolHostPort ( initUrlParts . protocol , initUrlParts . host , initUrlParts . port ) +
536
+ ( initUrlParts . path || '' ) +
537
+ ( initUrlParts . search ? ( '?' + initUrlParts . search ) : '' ) +
538
+ '#' + hashPrefix + '/' ;
539
+
540
+ $location = new LocationHashbangUrl ( initUrl , hashPrefix , appBaseUrl ) ;
502
541
}
503
542
504
543
$rootElement . bind ( 'click' , function ( event ) {
@@ -510,27 +549,22 @@ function $LocationProvider(){
510
549
var elm = jqLite ( event . target ) ;
511
550
512
551
// traverse the DOM up to find first A tag
513
- while ( elm . length && lowercase ( elm [ 0 ] . nodeName ) !== 'a' ) {
552
+ while ( lowercase ( elm [ 0 ] . nodeName ) !== 'a' ) {
553
+ if ( elm [ 0 ] === $rootElement [ 0 ] ) return ;
514
554
elm = elm . parent ( ) ;
515
555
}
516
556
517
557
var absHref = elm . prop ( 'href' ) ,
518
- href ;
519
-
520
- if ( ! absHref ||
521
- elm . attr ( 'target' ) ||
522
- absHref . indexOf ( absUrlPrefix ) !== 0 ) { // link to different domain or base path
523
- return ;
558
+ rewrittenUrl = $location . $$rewriteAppUrl ( absHref ) ;
559
+
560
+ if ( absHref && ! elm . attr ( 'target' ) && rewrittenUrl ) {
561
+ // update location manually
562
+ $location . $$parse ( rewrittenUrl ) ;
563
+ $rootScope . $apply ( ) ;
564
+ event . preventDefault ( ) ;
565
+ // hack to work around FF6 bug 684208 when scenario runner clicks on links
566
+ window . angular [ 'ff-684208-preventDefault' ] = true ;
524
567
}
525
-
526
- // update location with href without the prefix
527
- href = absHref . substr ( absUrlPrefix . length ) ;
528
- if ( href . indexOf ( '#' + hashPrefix ) == 0 ) href = href . substr ( hashPrefix . length + 1 ) ;
529
- $location . url ( href ) ;
530
- $rootScope . $apply ( ) ;
531
- event . preventDefault ( ) ;
532
- // hack to work around FF6 bug 684208 when scenario runner clicks on links
533
- window . angular [ 'ff-684208-preventDefault' ] = true ;
534
568
} ) ;
535
569
536
570
0 commit comments