forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(urlUtils): urlUtils doesn't return right path for file:// on win
Chrome and other browsers on Windows often append the drive name to the pathname, as described in angular#4680. This would cause the location service to browse to odd URLs, such as /C:/myfile.html, when opening apps using file://. Fixes angular#4680
- Loading branch information
1 parent
c5c7538
commit 1ba0144
Showing
2 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,14 @@ | |
// exactly the behavior needed here. There is little value is mocking these out for this | ||
// service. | ||
var urlParsingNode = document.createElement("a"); | ||
/* | ||
Matches paths for file protocol on windows, | ||
such as /C:/foo/bar, and captures only /foo/bar. | ||
*/ | ||
var windowsFilePathExp = /^\/?.*?:(\/.*)/; | ||
var originUrl = urlResolve(window.location.href, true); | ||
|
||
|
||
/** | ||
* | ||
* Implementation Notes for non-IE browsers | ||
|
@@ -27,7 +33,7 @@ var originUrl = urlResolve(window.location.href, true); | |
* browsers. However, the parsed components will not be set if the URL assigned did not specify | ||
* them. (e.g. if you assign a.href = "foo", then a.protocol, a.host, etc. will be empty.) We | ||
* work around that by performing the parsing in a 2nd step by taking a previously normalized | ||
* URL (e.g. by assining to a.href) and assigning it a.href again. This correctly populates the | ||
* URL (e.g. by assigning to a.href) and assigning it a.href again. This correctly populates the | ||
* properties such as protocol, hostname, port, etc. | ||
* | ||
* 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); | |
* | ||
*/ | ||
function urlResolve(url) { | ||
var href = url; | ||
var href = url, | ||
pathname; | ||
|
||
if (msie) { | ||
// Normalize before parse. Refer Implementation Notes on why this is | ||
// done in two steps on IE. | ||
|
@@ -72,7 +80,34 @@ function urlResolve(url) { | |
|
||
urlParsingNode.setAttribute('href', href); | ||
|
||
// $$urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils | ||
// ":"" protocol occurs on IE/Win for file:// | ||
/* jeffbcross and tbosch say: | ||
Per section 3.3 of http://www.ietf.org/rfc/rfc3986.txt, | ||
This comment has been minimized.
Sorry, something went wrong. |
||
the first path segment of a relative path reference | ||
cannot contain a colon, so it is right to assume that | ||
if the first section of the path contains a colon, it | ||
is invalid. | ||
However, to be safe, this check is only obliberating | ||
the first segment if the LAST character of the segment | ||
is a colon. | ||
In Windows, on an anchor node on documents loaded from | ||
the filesystem, the browser will return a pathname | ||
prefixed with the drive name ('/C:/path') when a | ||
pathname without a drive is set: | ||
* a.setAttribute('href', '/foo') | ||
* a.pathname === '/C:/foo' //true | ||
Inside of Angular, we're always using pathnames that | ||
do not include drive names for routing. | ||
*/ | ||
|
||
pathname = removeWindowsDriveName(urlParsingNode.pathname); | ||
pathname = (pathname.charAt(0) === '/') ? pathname : '/' + pathname; | ||
|
||
|
||
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils | ||
return { | ||
href: urlParsingNode.href, | ||
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '', | ||
|
@@ -81,9 +116,15 @@ function urlResolve(url) { | |
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '', | ||
hostname: urlParsingNode.hostname, | ||
port: urlParsingNode.port, | ||
pathname: urlParsingNode.pathname && urlParsingNode.pathname.charAt(0) === '/' ? | ||
urlParsingNode.pathname : '/' + urlParsingNode.pathname | ||
pathname: pathname | ||
}; | ||
|
||
function removeWindowsDriveName (path) { | ||
This comment has been minimized.
Sorry, something went wrong.
tbosch
|
||
var firstPathSegmentMatch; | ||
|
||
firstPathSegmentMatch = windowsFilePathExp.exec(path); | ||
return firstPathSegmentMatch ? firstPathSegmentMatch[1] : path; | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 1ba0144
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, besides the comments above.
I would remove the comment for lines 85-93.