From a2182dafa96f34104c4bcd85eef6c43e40f270ae Mon Sep 17 00:00:00 2001 From: Kin Blas Date: Wed, 1 Jun 2011 17:15:18 -0700 Subject: [PATCH] - Added a getClosestBaseUrl() utility function for calculating the correct base URL to use for a given element. - We should be using makeUrlAbsolute() instead of makePathAbsolute() when calculating the documentBase. - Removed bogus code in pathname calculatino in makeUrlAbsolute(). - Reworked calculation of search in makeUrlAbsolute() to prevent an undefined in the case where rel and abs urls have no search. --- js/jquery.mobile.navigation.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index c1b1e1b2..b453b881 100644 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -105,17 +105,17 @@ // 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) ) { + if ( !path.isRelativeUrl( relUrl ) ) { return relUrl; } - var relObj = path.parseUrl(relUrl), - absObj = path.parseUrl(absUrl), + var relObj = path.parseUrl( relUrl ), + absObj = path.parseUrl( absUrl ), protocol = relObj.protocol || absObj.protocol, authority = relObj.authority || absObj.authority || "", hasPath = relObj.pathname !== undefined, - pathname = path.isRelativeUrl() ? path.makePathAbsolute( relObj.pathname || absObj.filename, absObj.pathname ) : relObj.pathName, - search = relObj.search || ( hasPath ? "" : absObj.search ), + pathname = path.makePathAbsolute( relObj.pathname || absObj.filename, absObj.pathname ), + search = relObj.search || ( !hasPath && absObj.search ) || "", hash = relObj.hash || ""; return protocol + "//" + authority + pathname + search + hash; @@ -307,7 +307,7 @@ //if the document has an embedded base tag, documentBase is set to its //initial value. If a base tag does not exist, then we default to the documentDomainPath. - documentBase = $base.length ? path.makePathAbsolute( $base.attr( "href" ), documentDomainPath ) : documentDomainPath; + documentBase = $base.length ? path.makeUrlAbsolute( $base.attr( "href" ), documentDomainPath ) : documentDomainPath; //base element management, defined depending on dynamic base tag support var base = $.support.dynamicBaseTag ? { @@ -907,12 +907,26 @@ return ele; } + // The base URL for any given element depends on the page it resides in. + function getClosestBaseUrl( ele ) + { + // Find the closest page and extract out its url. + var url = $( ele ).closest( ".ui-page" ).jqmData( "url" ); + + // If the data-url is an id instead of a path, default to using + // the documentBase. + if ( url && !path.isPath( url ) ) { + url = documentBase; + } + + return path.makeUrlAbsolute( ( url && !path.isPath( url ) ) ? url : documentBase, documentBase); + } + //add active state on vclick $( document ).bind( "vclick", function( event ) { var link = findClosestLink( event.target ); if ( link ) { - var url = path.clean( link.getAttribute( "href" ) || "#" ); - if (url !== "#" && url.replace( path.get(), "") !== "#" ) { + if ( path.parseUrl( link.getAttribute( "href" ) || "#" ).hash !== "#" ) { $( link ).closest( ".ui-btn" ).not( ".ui-disabled" ).addClass( $.mobile.activeBtnClass ); } } @@ -965,6 +979,7 @@ //if data-ajax attr is set to false, use the default behavior of a link hasAjaxDisabled = $link.is( ":jqmData(ajax='false')" ); +alert("a: " + $link.attr("href") + "\np: " + $link.prop("href") + "\nd: " + $link.closest(".ui-page").jqmData("url") + "\nb: " + getClosestBaseUrl($link) + "\nc: " + 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')" ) ) { window.history.back();