Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 553317c

Browse files
author
Gabriel Schulhof
committed
Navigation: Do not assume "//" is always part of an absolute URL
Note: this does indeed represent a fix for gh-6574, but only once a version of Cordova sporting apache/cordova-wp8#30 is released. Closes gh-6597 Fixes gh-6574 Fixes gh-6599
1 parent 58a03e9 commit 553317c

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

js/init.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ define([
5757
var path = $.mobile.path,
5858
$pages = $( ":jqmData(role='page'), :jqmData(role='dialog')" ),
5959
hash = path.stripHash( path.stripQueryParams(path.parseLocation().hash) ),
60+
theLocation = $.mobile.path.parseLocation(),
6061
hashPage = document.getElementById( hash );
6162

6263
// if no pages are found, create one with body's inner html
@@ -70,7 +71,8 @@ define([
7071

7172
// unless the data url is already set set it to the pathname
7273
if ( !$this[ 0 ].getAttribute( "data-" + $.mobile.ns + "url" ) ) {
73-
$this.attr( "data-" + $.mobile.ns + "url", $this.attr( "id" ) || location.pathname + location.search );
74+
$this.attr( "data-" + $.mobile.ns + "url", $this.attr( "id" ) ||
75+
theLocation.pathname + theLocation.search );
7476
}
7577
});
7678

js/navigation/base.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ define([
4646
page.find( base.linkSelector ).each(function( i, link ) {
4747
var thisAttr = $( link ).is( "[href]" ) ? "href" :
4848
$( link ).is( "[src]" ) ? "src" : "action",
49+
theLocation = $.mobile.path.parseLocation(),
4950
thisUrl = $( link ).attr( thisAttr );
5051

5152
// XXX_jblas: We need to fix this so that it removes the document
5253
// base URL, and then prepends with the new page URL.
5354
// if full path exists and is same, chop it - helps IE out
54-
thisUrl = thisUrl.replace( location.protocol + "//" +
55-
location.host + location.pathname, "" );
55+
thisUrl = thisUrl.replace( theLocation.protocol + theLocation.doubleSlash +
56+
theLocation.host + theLocation.pathname, "" );
5657

5758
if ( !/^(\w+:|#|\/)/.test( thisUrl ) ) {
5859
$( link ).attr( thisAttr, newPath + thisUrl );

js/navigation/path.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,31 @@ define([
4141
urlParseRE: /^\s*(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/,
4242

4343
// Abstraction to address xss (Issue #4787) by removing the authority in
44-
// browsers that auto decode it. All references to location.href should be
44+
// browsers that auto-decode it. All references to location.href should be
4545
// replaced with a call to this method so that it can be dealt with properly here
4646
getLocation: function( url ) {
47-
var uri = url ? this.parseUrl( url ) : location,
48-
hash = this.parseUrl( url || location.href ).hash;
47+
48+
// Always use our own URL parser, even though location potentially provides all the
49+
// fields we may need later on. This way, URL parsing is consistent, and we only
50+
// grab location.href from the browser.
51+
var uri = this.parseUrl( url || location.href ),
52+
hash = uri.hash;
4953

5054
// mimic the browser with an empty string when the hash is empty
5155
hash = hash === "#" ? "" : hash;
5256

57+
// The pathname must start with a slash if there's a protocol, because you can't
58+
// have a protocol followed by a relative path. Also, it's impossible to calculate
59+
// absolute URLs from relative ones if the absolute one doesn't have a leading "/".
60+
if ( uri.protocol !== "" && uri.pathname.substring( 0, 1 ) !== "/" ) {
61+
uri.pathname = "/" + uri.pathname;
62+
uri.directory = "/" + uri.directory;
63+
}
64+
5365
// Make sure to parse the url or the location object for the hash because using location.hash
5466
// is autodecoded in firefox, the rest of the url should be from the object (location unless
5567
// we're testing) to avoid the inclusion of the authority
56-
return uri.protocol + "//" + uri.host + uri.pathname + uri.search + hash;
68+
return uri.protocol + uri.doubleSlash + uri.host + uri.pathname + uri.search + hash;
5769
},
5870

5971
//return the original document url
@@ -323,7 +335,7 @@ define([
323335

324336
// reconstruct each of the pieces with the new search string and hash
325337
href = path.parseUrl( href );
326-
href = href.protocol + "//" + href.host + href.pathname + search + preservedHash;
338+
href = href.protocol + href.doubleSlash + href.host + href.pathname + search + preservedHash;
327339
} else {
328340
href += href.indexOf( "#" ) > -1 ? uiState : "#" + uiState;
329341
}

0 commit comments

Comments
 (0)