Permalink
Browse files

Navigation: Do not assume "//" is always part of an absolute URL

(cherry picked from commit 9e082b0)

Closes gh-6597
Fixes gh-6574
Fixes gh-6599
  • Loading branch information...
1 parent dca0976 commit ecd5be5ef0631776e4d2e8e1627f602f309a4062 @gabrielschulhof gabrielschulhof committed Mar 20, 2014
Showing with 35 additions and 14 deletions.
  1. +3 −1 js/jquery.mobile.init.js
  2. +3 −2 js/navigation/base.js
  3. +24 −8 js/navigation/path.js
  4. +5 −3 tests/unit/path/path_core.js
@@ -57,6 +57,7 @@ define([
var path = $.mobile.path,
$pages = $( ":jqmData(role='page'), :jqmData(role='dialog')" ),
hash = path.stripHash( path.stripQueryParams(path.parseLocation().hash) ),
+ theLocation = $.mobile.path.parseLocation(),
hashPage = document.getElementById( hash );
// if no pages are found, create one with body's inner html
@@ -70,7 +71,8 @@ define([
// unless the data url is already set set it to the pathname
if ( !$this[ 0 ].getAttribute( "data-" + $.mobile.ns + "url" ) ) {
- $this.attr( "data-" + $.mobile.ns + "url", $this.attr( "id" ) || location.pathname + location.search );
+ $this.attr( "data-" + $.mobile.ns + "url", $this.attr( "id" ) ||
+ theLocation.pathname + theLocation.search );
}
});
@@ -46,13 +46,14 @@ define([
page.find( base.linkSelector ).each(function( i, link ) {
var thisAttr = $( link ).is( "[href]" ) ? "href" :
$( link ).is( "[src]" ) ? "src" : "action",
+ theLocation = $.mobile.path.parseLocation(),
thisUrl = $( link ).attr( thisAttr );
// XXX_jblas: We need to fix this so that it removes the document
// base URL, and then prepends with the new page URL.
// if full path exists and is same, chop it - helps IE out
- thisUrl = thisUrl.replace( location.protocol + "//" +
- location.host + location.pathname, "" );
+ thisUrl = thisUrl.replace( theLocation.protocol + theLocation.doubleSlash +
+ theLocation.host + theLocation.pathname, "" );
if ( !/^(\w+:|#|\/)/.test( thisUrl ) ) {
$( link ).attr( thisAttr, newPath + thisUrl );
View
@@ -41,19 +41,34 @@ define([
urlParseRE: /^\s*(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/,
// Abstraction to address xss (Issue #4787) by removing the authority in
- // browsers that auto decode it. All references to location.href should be
+ // browsers that auto-decode it. All references to location.href should be
// replaced with a call to this method so that it can be dealt with properly here
getLocation: function( url ) {
- var uri = url ? this.parseUrl( url ) : location,
- hash = this.parseUrl( url || location.href ).hash;
+ var parsedUrl = this.parseUrl( url || location.href ),
+ uri = url ? parsedUrl : location,
+
+ // Make sure to parse the url or the location object for the hash because using
+ // location.hash is autodecoded in firefox, the rest of the url should be from
+ // the object (location unless we're testing) to avoid the inclusion of the
+ // authority
+ hash = parsedUrl.hash;
// mimic the browser with an empty string when the hash is empty
hash = hash === "#" ? "" : hash;
- // Make sure to parse the url or the location object for the hash because using location.hash
- // is autodecoded in firefox, the rest of the url should be from the object (location unless
- // we're testing) to avoid the inclusion of the authority
- return uri.protocol + "//" + uri.host + uri.pathname + uri.search + hash;
+ return uri.protocol +
+ parsedUrl.doubleSlash +
+ uri.host +
+
+ // The pathname must start with a slash if there's a protocol, because you
+ // can't have a protocol followed by a relative path. Also, it's impossible to
+ // calculate absolute URLs from relative ones if the absolute one doesn't have
+ // a leading "/".
+ ( ( uri.protocol !== "" && uri.pathname.substring( 0, 1 ) !== "/" ) ?
+ "/" : "" ) +
+ uri.pathname +
+ uri.search +
+ hash;
},
//return the original document url
@@ -323,7 +338,8 @@ define([
// reconstruct each of the pieces with the new search string and hash
href = path.parseUrl( href );
- href = href.protocol + "//" + href.host + href.pathname + search + preservedHash;
+ href = href.protocol + href.doubleSlash + href.host + href.pathname + search +
+ preservedHash;
} else {
href += href.indexOf( "#" ) > -1 ? uiState : "#" + uiState;
}
@@ -239,9 +239,11 @@
test( "path.getLocation works properly", function() {
equal( $.mobile.path.getLocation("http://example.com/"), "http://example.com/" );
- equal( $.mobile.path.getLocation("http://foo@example.com"), "http://example.com" );
- equal( $.mobile.path.getLocation("http://foo:bar@example.com"), "http://example.com" );
- equal( $.mobile.path.getLocation("http://<foo<:bar@example.com"), "http://example.com" );
+ equal( $.mobile.path.getLocation("http://foo@example.com/"), "http://example.com/" );
+ equal( $.mobile.path.getLocation("http://foo:bar@example.com/"), "http://example.com/" );
+ equal( $.mobile.path.getLocation("http://<foo<:bar@example.com/"), "http://example.com/" );
+ equal( $.mobile.path.getLocation("x-wmapp0:www/index.html" ), "x-wmapp0:/www/index.html" );
+ equal( $.mobile.path.getLocation("qrc:/index.html" ), "qrc:/index.html" );
var allUriParts = "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content";

0 comments on commit ecd5be5

Please sign in to comment.