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

Commit

Permalink
tests for proper squashing in a hand full of scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbender committed Oct 31, 2012
1 parent 4cae082 commit 00f81c9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
24 changes: 19 additions & 5 deletions js/navigation/navigate.js
Expand Up @@ -62,14 +62,25 @@ define([

$.navigate.squash = function( url, data ) {
var state, href,
hash = url,
isPath = path.isPath( hash ),
resolutionUrl = isPath ? path.getLocation() : $.mobile.getDocumentUrl();
isPath = path.isPath( url ),
hash = isPath ? path.stripHash(url) : url,
hashUri = path.parseUrl( hash ),
resolutionUrl = isPath ? path.getLocation() : $.mobile.getDocumentUrl(),
passedSearch, currentSearch, mergedSearch, preservedHash;

// make the hash abolute with the current href
href = path.makeUrlAbsolute( hash, resolutionUrl );

if ( isPath ) {
passedSearch = hashUri.search;
currentSearch = path.parseUrl( resolutionUrl ).search;
mergedSearch = path.mergeSearch( passedSearch, currentSearch );

mergedSearch = mergedSearch ? "?" + mergedSearch : "";
preservedHash = (hashUri.hash || path.parseLocation().hash);

href = path.parseUrl( href );
href = href.protocol + "//" + href.host + href.pathname + mergedSearch + preservedHash;
href = path.resetUIKeys( href );
}

Expand Down Expand Up @@ -134,7 +145,10 @@ define([

// squash a hash with replacestate
if( path.isPath(hash) ) {
console.log( "location before squash" + location.href );
console.log( "squashing hash with:" + hash );
state = $.navigate.squash( hash );
console.log( "location after squash" + location.href );
}

// record the new hash as an additional history entry
Expand Down Expand Up @@ -301,9 +315,9 @@ define([
//
// TODO this is also convoluted and confusing
if ( newActiveIndex < a ) {
( opts.present || opts.back )( this.getActive(), 'back' );
( opts.present || opts.back || $.noop )( this.getActive(), 'back' );
} else if ( newActiveIndex > a ) {
( opts.present || opts.forward )( this.getActive(), 'forward' );
( opts.present || opts.forward || $.noop )( this.getActive(), 'forward' );
} else if ( newActiveIndex === a ) {
if( opts.current ) {
opts.current( this.getActiveIndex );
Expand Down
38 changes: 38 additions & 0 deletions js/navigation/path.js
Expand Up @@ -121,6 +121,7 @@ define([
break;
}
}

return "/" + absStack.join( "/" );
},

Expand Down Expand Up @@ -303,6 +304,43 @@ define([
}

return url;
},

mergeSearch: function( first, second ) {
var firstSearch, secondSearch,
resolvedSearch = {},
searchString = [],
delimiter = "&";

firstSearch = first.replace( /^\?/, "" ).split( /&|;/ );
secondSearch = second.replace( /^\?/, "" ).split( /&|;/ );

if( first.indexOf( ";" ) >= 0 || second.indexOf( ";" ) >=0 ) {
delimiter = ";";
}

this.paramPairsToObject( firstSearch, resolvedSearch );
this.paramPairsToObject( secondSearch, resolvedSearch );

$.each( resolvedSearch, function(key, value) {
searchString.push(key + "=" + value);
});

return searchString.join(delimiter);
},

paramPairsToObject: function( pairs, object ) {
$.each( pairs, function(i, pair) {
var keyValue = pair.split( "=" ),
key = keyValue[0],
value = keyValue[1];

if( !key ){
return;
}

object[key] = value;
});
}
};

Expand Down
33 changes: 32 additions & 1 deletion tests/unit/navigation/navigate_method.js
Expand Up @@ -171,4 +171,35 @@ $.testHelper.setPushState();
}
]);
});
})( jQuery );

if( $.support.pushState ) {
test( "squash is working properly", function() {
var path = $.mobile.path, loc;
$.navigate.squash( url.pathname + url.search + "#test-hash" );

$.navigate.squash("#foo/bar");
loc = path.parseLocation();
equal( loc.pathname, url.directory + "foo/bar", "foo/bar has been squashed onto the url" );
equal( loc.search, url.search, "the search is preserved" );
equal( loc.hash, "#test-hash", "the hash is preserved" );

$.navigate.squash("bar/baz");
loc = path.parseLocation();
equal( loc.pathname, url.directory + "foo/bar/baz", "foo/bar has been squashed onto the url" );
equal( loc.search, url.search, "the search is preserved" );
equal( loc.hash, "#test-hash", "the hash is preserved" );

$.navigate.squash("#foo");
loc = path.parseLocation();
equal( loc.hash, "#foo", "foo is now the hash" );
equal( loc.search, url.search, "the search is preserved" );

// Make sure that the search delimiter is dictated by the squashed value
$.navigate.squash("#foo/bar" + location.search.replace( "&", ";" ));
loc = path.parseLocation();
ok( loc.search.indexOf( "&" ) === -1, "the amp has been replaced" );

$.navigate.squash( url.pathname + url.search );
});
}
})( jQuery );

0 comments on commit 00f81c9

Please sign in to comment.