@@ -259,6 +259,8 @@ function LocationHashbangInHtml5Url(appBase, hashPrefix) {
259
259
260
260
if ( appBase == stripHash ( url ) ) {
261
261
return url ;
262
+ } else if ( ( appUrl = beginsWith ( appBaseNoFile + hashPrefix + '/' , url ) ) ) {
263
+ return appBase + hashPrefix + appUrl ;
262
264
} else if ( ( appUrl = beginsWith ( appBaseNoFile , url ) ) ) {
263
265
return appBase + hashPrefix + appUrl ;
264
266
} else if ( appBaseNoFile === url + '/' ) {
@@ -675,39 +677,22 @@ function $LocationProvider(){
675
677
676
678
if ( href && href . indexOf ( '://' ) < 0 ) { // Ignore absolute URLs
677
679
var prefix = '#' + hashPrefix ;
678
- var baseHrefNoFile = stripFile ( baseHref ) ;
679
- var appOnRoot = stripFile ( appBase ) == ( serverBase ( appBase ) + '/' ) ;
680
- if ( href [ 0 ] == '/' && ( appOnRoot || ( baseHref && beginsWith ( baseHrefNoFile , href ) ) ) ) {
681
- // absolute path - within base or when the app is on the root
682
- var hrefNoBase = baseHrefNoFile ? href . substr ( baseHrefNoFile . length - 1 ) : href ;
683
- absHref = appBase + prefix + hrefNoBase ;
684
- } else if ( href [ 0 ] != '/' ) { // Ignore absolute path outside of base
685
- var beginsWithPrefix = beginsWith ( prefix , href ) ;
686
- if ( beginsWith ( prefix + '/' , href ) ) {
687
- // local anchor with absolute path
688
- absHref = appBase + href ;
689
- } else if ( ! beginsWithPrefix && href [ 0 ] == '#' ) {
690
- // local anchor
691
- absHref = appBase + prefix + ( $location . path ( ) || '/' ) + href ;
692
- } else if ( ! beginsWithPrefix && baseHref ) {
693
- // local anchor and base[href] exists
694
- absHref = appBase + prefix + '/' + href ;
695
- } else {
696
- href = beginsWithPrefix ? href . substr ( prefix . length ) : href ;
697
- // relative path - join with current path
698
- var stack = $location . path ( ) . split ( "/" ) ,
699
- parts = href . split ( "/" ) ;
700
- if ( stack . length === 2 && ! stack [ 1 ] ) stack . length = 1 ;
701
- for ( var i = 0 ; i < parts . length ; i ++ ) {
702
- if ( parts [ i ] == "." )
703
- continue ;
704
- else if ( parts [ i ] == ".." )
705
- stack . pop ( ) ;
706
- else if ( parts [ i ] . length )
707
- stack . push ( parts [ i ] ) ;
708
- }
709
- absHref = appBase + prefix + stack . join ( '/' ) ;
710
- }
680
+ var appBaseNoFile = stripFile ( appBase ) ;
681
+ var html5Url = appBase + $location . url ( ) . substr ( 1 ) ;
682
+ var baseUri = baseHref ? urlResolveShim ( baseHref , appBase ) : html5Url ;
683
+ var appUrl ;
684
+ if ( appUrl = beginsWith ( prefix + '/' , href ) ) {
685
+ // hashbang beginning with / refers to appbase
686
+ absHref = urlResolveShim ( appBaseNoFile + appUrl ) ;
687
+ } else if ( appUrl = beginsWith ( prefix , href ) ) {
688
+ // hashbang relative path
689
+ absHref = urlResolveShim ( appUrl , baseUri ) ;
690
+ } else if ( href [ 0 ] == '#' ) {
691
+ // local anchor
692
+ absHref = appBase + prefix + ( $location . path ( ) || '/' ) + href ;
693
+ } else {
694
+ // relative path
695
+ absHref = urlResolveShim ( href , baseUri ) ;
711
696
}
712
697
}
713
698
}
@@ -779,5 +764,59 @@ function $LocationProvider(){
779
764
function afterLocationChange ( oldUrl ) {
780
765
$rootScope . $broadcast ( '$locationChangeSuccess' , $location . absUrl ( ) , oldUrl ) ;
781
766
}
767
+
768
+ // Similar to URL(URL, base) of URLUtils
769
+ function urlResolveShim ( url , base ) {
770
+ var absHref ;
771
+ if ( url . indexOf ( '//' ) >= 0 ) {
772
+ absHref = url ;
773
+ } else if ( url . charAt ( 0 ) === '/' ) {
774
+ absHref = serverBase ( base ) + url ;
775
+ } else {
776
+ absHref = stripFile ( base ) + url ;
777
+ }
778
+
779
+ var resolvedUrl = urlResolve ( absHref ) ;
780
+ var hash = resolvedUrl . hash ? '#' + resolvedUrl . hash : '' ;
781
+ var search = resolvedUrl . search ;
782
+ var path = resolvedUrl . pathname ;
783
+ var normalizedPath = normalizePath ( path ) ; // only for IE7 compatibility
784
+ return serverBase ( resolvedUrl . href ) + normalizedPath + ( search ? '?' + search : '' ) + hash ;
785
+ }
786
+
787
+ function normalizePath ( path ) {
788
+ path = path || '' ;
789
+ var inputSegments = path . split ( '/' ) ;
790
+ var outputSegments = [ ] ;
791
+ var inputSegment ;
792
+ for ( var i = 0 ; i < inputSegments . length ; i ++ ) {
793
+ inputSegment = inputSegments [ i ] ;
794
+
795
+ if ( ( inputSegment . length === 0 )
796
+ || ( inputSegment == '.' ) ) {
797
+ // Do nothing
798
+ continue ;
799
+ } else if ( inputSegment == '..' ) {
800
+ if ( outputSegments . length ) {
801
+ outputSegments . pop ( ) ;
802
+ }
803
+ } else {
804
+ outputSegments . push ( inputSegment ) ;
805
+ }
806
+ }
807
+
808
+ var outputSegment , output = '' ;
809
+ for ( i = 0 ; i < outputSegments . length ; i ++ ) {
810
+ outputSegment = outputSegments [ i ] ;
811
+ output += '/' + outputSegment ;
812
+ }
813
+
814
+ if ( path . lastIndexOf ( '/' ) == path . length - 1 ) {
815
+ // path.endsWith("/") || path.equals("")
816
+ output += '/' ;
817
+ }
818
+
819
+ return output ;
820
+ }
782
821
} ] ;
783
822
}
0 commit comments