77// exactly the behavior needed here. There is little value is mocking these out for this
88// service.
99var urlParsingNode = document . createElement ( "a" ) ;
10+ /*
11+ Matches paths for file protocol on windows,
12+ such as /C:/foo/bar, and captures only /foo/bar.
13+ */
14+ var windowsFilePathExp = / ^ \/ ? .* ?: ( \/ .* ) / ;
1015var originUrl = urlResolve ( window . location . href , true ) ;
1116
17+
1218/**
1319 *
1420 * Implementation Notes for non-IE browsers
@@ -27,7 +33,7 @@ var originUrl = urlResolve(window.location.href, true);
2733 * browsers. However, the parsed components will not be set if the URL assigned did not specify
2834 * them. (e.g. if you assign a.href = "foo", then a.protocol, a.host, etc. will be empty.) We
2935 * work around that by performing the parsing in a 2nd step by taking a previously normalized
30- * URL (e.g. by assining to a.href) and assigning it a.href again. This correctly populates the
36+ * URL (e.g. by assigning to a.href) and assigning it a.href again. This correctly populates the
3137 * properties such as protocol, hostname, port, etc.
3238 *
3339 * IE7 does not normalize the URL when assigned to an anchor node. (Apparently, it does, if one
@@ -62,7 +68,9 @@ var originUrl = urlResolve(window.location.href, true);
6268 *
6369 */
6470function urlResolve ( url ) {
65- var href = url ;
71+ var href = url ,
72+ pathname ;
73+
6674 if ( msie ) {
6775 // Normalize before parse. Refer Implementation Notes on why this is
6876 // done in two steps on IE.
@@ -72,7 +80,23 @@ function urlResolve(url) {
7280
7381 urlParsingNode . setAttribute ( 'href' , href ) ;
7482
75- // $$urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
83+ /*
84+ * In Windows, on an anchor node on documents loaded from
85+ * the filesystem, the browser will return a pathname
86+ * prefixed with the drive name ('/C:/path') when a
87+ * pathname without a drive is set:
88+ * * a.setAttribute('href', '/foo')
89+ * * a.pathname === '/C:/foo' //true
90+ *
91+ * Inside of Angular, we're always using pathnames that
92+ * do not include drive names for routing.
93+ */
94+
95+ pathname = removeWindowsDriveName ( urlParsingNode . pathname ) ;
96+ pathname = ( pathname . charAt ( 0 ) === '/' ) ? pathname : '/' + pathname ;
97+
98+
99+ // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
76100 return {
77101 href : urlParsingNode . href ,
78102 protocol : urlParsingNode . protocol ? urlParsingNode . protocol . replace ( / : $ / , '' ) : '' ,
@@ -81,9 +105,15 @@ function urlResolve(url) {
81105 hash : urlParsingNode . hash ? urlParsingNode . hash . replace ( / ^ # / , '' ) : '' ,
82106 hostname : urlParsingNode . hostname ,
83107 port : urlParsingNode . port ,
84- pathname : urlParsingNode . pathname && urlParsingNode . pathname . charAt ( 0 ) === '/' ?
85- urlParsingNode . pathname : '/' + urlParsingNode . pathname
108+ pathname : pathname
86109 } ;
110+
111+ function removeWindowsDriveName ( path ) {
112+ var firstPathSegmentMatch ;
113+
114+ firstPathSegmentMatch = windowsFilePathExp . exec ( path ) ;
115+ return firstPathSegmentMatch ? firstPathSegmentMatch [ 1 ] : path ;
116+ }
87117}
88118
89119
0 commit comments