From a99728d1780551c0cd5c5b6368fc1ce56325760d Mon Sep 17 00:00:00 2001 From: Kin Blas Date: Mon, 6 Jun 2011 15:58:21 -0700 Subject: [PATCH] - Added addSearchParams() utility function. - Removed unused path functions: setOrigin(), makeAbsolute(), and isQuery(). - Modified loadPage() so that it uses addSearchParams() instead of naively appending it to the URL. - Switched the global "click" handler back to using the link @href attribute and manually converting it to an absolute URL. This was necessary because it turns out that the href property on a link is dynamically calculated at the time of access based on the *CURRENT* base tag setting. This means the same link can return a different href value if the the base tag is different each time you access it. --- js/jquery.mobile.navigation.js | 75 +++++++++++----------------------- 1 file changed, 24 insertions(+), 51 deletions(-) diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index f35b9d76..01546da9 100644 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -40,8 +40,8 @@ // urlParseRE: /^(((([^:\/#\?]+:)?(?:\/\/((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?]+)(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/, - // Parse a URL into a structure that allows easy access to - // all of the URL components by name. + //Parse a URL into a structure that allows easy access to + //all of the URL components by name. parseUrl: function( url ) { // If we're passed an object, we'll assume that it is // a parsed url object and just return it back to the caller. @@ -75,9 +75,9 @@ return results || {}; }, - // Turn relPath into an asbolute path. absPath is - // an optional absolute path which describes what - // relPath is relative to. + //Turn relPath into an asbolute path. absPath is + //an optional absolute path which describes what + //relPath is relative to. makePathAbsolute: function( relPath, absPath ) { if ( relPath && relPath.charAt( 0 ) === "/" ) { return relPath; @@ -106,24 +106,24 @@ return "/" + absStack.join( "/" ); }, - // Returns true if both urls have the same domain. + //Returns true if both urls have the same domain. isSameDomain: function( absUrl1, absUrl2 ) { return path.parseUrl( absUrl1 ).domain === path.parseUrl( absUrl2 ).domain; }, - // Returns true for any relative variant. + //Returns true for any relative variant. isRelativeUrl: function( url ) { // All relative Url variants have one thing in common, no protocol. return path.parseUrl( url ).protocol === undefined; }, - // Returns true for an absolute url. + //Returns true for an absolute url. isAbsoluteUrl: function( url ) { return path.parseUrl( url ).protocol !== undefined; }, - // Turn the specified realtive URL into an absolute one. This function - // can handle all relative variants (protocol, site, document, query, fragment). + //Turn the specified realtive URL into an absolute one. This function + //can handle all relative variants (protocol, site, document, query, fragment). makeUrlAbsolute: function( relUrl, absUrl ) { if ( !path.isRelativeUrl( relUrl ) ) { return relUrl; @@ -141,6 +141,14 @@ return protocol + "//" + authority + pathname + search + hash; }, + //Add search (aka query) params to the specified url. + addSearchParams: function( url, params ) { + var u = path.parseUrl( url ), + p = ( typeof params === "object" ) ? $.param( params ) : params, + s = u.search || "?"; + return u.hrefNoSearch + s + ( s.charAt( s.length - 1 ) !== "?" ? "&" : "" ) + p + ( u.hash || "" ); + }, + //get path from current hash, or from a file path get: function( newPath ) { if( newPath === undefined ) { @@ -160,40 +168,12 @@ location.hash = path; }, - //location pathname from intial directory request - origin: '', - - setOrigin: function() { - path.origin = path.get( location.protocol + '//' + location.host + location.pathname ); - }, - - // prefix a relative url with the current path - // TODO force old relative deeplinks into new absolute path - makeAbsolute: function( url ) { - var isHashPath = path.isPath( location.hash ); - - if( path.isQuery( url ) ) { - // if the path is a list of query params and the hash is a path - // append the query params to the hash (without params or dialog keys). - // otherwise use the pathname and append the query params - return ( isHashPath ? path.cleanHash( location.hash ) : location.pathname ) + url; - } - - // If the hash is a path, even if its not absolute, use it to prepend to the url - // otherwise use the path with the trailing segement removed - return ( isHashPath ? path.get() : path.get( location.pathname ) ) + url; - }, - - // test if a given url (string) is a path - // NOTE might be exceptionally naive + //test if a given url (string) is a path + //NOTE might be exceptionally naive isPath: function( url ) { return ( /\// ).test( url ); }, - isQuery: function( url ) { - return ( /^\?/ ).test( url ); - }, - //return a url path with the window's location protocol/hostname/pathname removed clean: function( url ) { // Replace the protocol host only once at the beginning of the url to avoid @@ -358,9 +338,6 @@ } : undefined; - //set location pathname from intial directory request - path.setOrigin(); - /* internal utility functions --------------------------------------*/ @@ -540,11 +517,7 @@ // If the caller provided data, and we're using "get" request, // append the data to the URL. if ( settings.data && settings.type === "get" ) { - if ( $.type( settings.data ) === "object" ) { - settings.data = $.param( settings.data ); - } - // XXX_jblas: We should be checking to see if the url already has a query in it. - absUrl += "?" + settings.data; + absUrl = path.addSearchParams( absUrl, settings.data ); settings.data = undefined; fileUrl = path.getFilePath( absUrl ); } @@ -935,11 +908,11 @@ var url = $( ele ).closest( ".ui-page" ).jqmData( "url" ), base = documentBase.hrefNoHash; - if ( url && !path.isPath( url ) ) { + if ( !url || !path.isPath( url ) ) { url = base; } - return path.makeUrlAbsolute( url || base, base); + return path.makeUrlAbsolute( url, base); } //add active state on vclick @@ -963,7 +936,7 @@ var $link = $( link ), //get href, if defined, otherwise default to empty hash - href = $link.prop( "href" ) || $.mobile.documentBase.hrefNoHash + "#"; + href = path.makeUrlAbsolute( $link.attr( "href" ) || "#", getClosestBaseUrl( $link ) ); //if there's a data-rel=back attr, go back in history if( $link.is( ":jqmData(rel='back')" ) ) {