Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed problem with params url and added more unit tests
  • Loading branch information
Oskari Koskimies committed Apr 14, 2011
commit afc5cf0eca1a0d14645e6093aa9bcea265157f1a
23 changes: 16 additions & 7 deletions js/jquery.mobile.navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@

clean: function( url ){
//console.log("clean:", url);

if(path.isQuery(url)){
//console.log("it's a query, returning #" + path.cleanHash(location.hash) + url);
return "#" + path.cleanHash(location.hash) + url;
}

// Replace the protocol, host, and pathname only once at the beginning of the url to avoid
// problems when it's included as a part of a param
var leadingUrlRootRegex, checkForRoot;
var leadingUrlRootRegex;
var hashIndex = url.indexOf("#");
if (hashIndex > 0) { // Hash, but not at beginning
// Add protocol and host to absolute and relative hashed urls so that they will be considered external
Expand All @@ -154,7 +160,7 @@
} else {
leadingUrlRootRegex = new RegExp("^" + location.protocol + "//" + location.host + "(.*)$");
}
checkForRoot = leadingUrlRootRegex.exec(url);
var checkForRoot = leadingUrlRootRegex.exec(url);
if (checkForRoot) {
url = checkForRoot[1];
}
Expand All @@ -166,10 +172,13 @@
// canonize url
//console.log("canonize url:", url);
var urlSegments, locSegments, i, canonPath = [];
var checkForIndex = /^(.*\/)index\.[^/]*$/i.exec(url);
var checkForIndex = /^(.*\/)index\.[^\/?]*(\?.*)?$/i.exec(url);
if (checkForIndex) {
//console.log("Removing index:", checkForIndex);
url = checkForIndex[1];
if (checkForIndex[2]) {
url += checkForIndex[2];
}
}
if (url.charAt(0) === "#") { // Hash-absolute - nothing to do since we already removed extra index file (if present)
if (url.charAt(1) === "/") {
Expand Down Expand Up @@ -212,8 +221,8 @@
if (canonPath[i] === ".." && i > 0 && canonPath[i-1] !== "..") {
i--;
canonPath.splice(i,2);
} else if (canonPath[i] === "." ||
// Cannot remove first/last because they mark beginning/trailing slash
} else if (canonPath[i] === "." || // remove same-dir (dot) segments
// Remove empty segments - cannot remove first/last because they mark beginning/trailing slash
(canonPath[i] === "" && i > 0 && i < canonPath.length-1)) {
canonPath.splice(i,1);
} else {
Expand Down Expand Up @@ -469,7 +478,7 @@

// changepage function
$.mobile.changePage = function( to, transition, reverse, changeHash, fromHashChange ){
//console.log("to:", to);
//console.log("changePage to:", to);

if ($.type(to) === "string") {
to = path.clean(to);
Expand Down Expand Up @@ -927,7 +936,7 @@

//if data-ajax attr is set to false, use the default behavior of a link
hasAjaxDisabled = $this.is( ":jqmData(ajax='false')" );

//console.log("linkHander; url is now: " + url);
//if there's a data-rel=back attr, go back in history
if( $this.is( ":jqmData(rel='back')" ) ){
window.history.back();
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/navigation/domcopies.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

</head>
<body>

<div data-nstest-role="page" id="domcopies">
<p class="domcopy_count">Dom copies test</p>
</div>

</body>
</html>
36 changes: 32 additions & 4 deletions tests/unit/navigation/navigation_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
},

function(){
ok(called == 1, "change page should be called once");
ok(called >= 1, "change page should be called at least once");
ok(called <= 1, "change page should be called at most once");
start();
}], 500);
}], 1000);
});

asyncTest( "forms with data attribute ajax set to false will not call changePage", function(){
Expand Down Expand Up @@ -385,6 +386,34 @@
}], 1000);
});

asyncTest( "navigating to a page via several paths only loads it to DOM once", function(){
$.testHelper.sequence([
// transition to the first page
function(){ $.mobile.changePage("#/"); },

// transition to title2.html directly
function(){ $.mobile.changePage("domcopies.html"); },

// transition to subdirectory
function(){ $.mobile.changePage("#data-url-tests/non-data-url.html"); },

// and then back to title2.html
function(){ $.mobile.changePage("../domcopies.html"); },

// transition to subdirectory again
function(){ $.mobile.changePage("#data-url-tests/non-data-url.html"); },

// and then back to title2.html using absolute url
function(){ $.mobile.changePage(location.pathname + "domcopies.html"); },

// make sure we only have one copy of the dom
function(){
ok($("p.domcopy_count").length <= 1 , "There should be at most one copy of the page in DOM");
ok($("p.domcopy_count").length >= 1 , "There should be at least one copy of the page in DOM");
start();
}], 1000);
});

asyncTest( "Page links to the current active page result in the same active page", function(){
$.testHelper.openPage("#self-link");
$.testHelper.sequence([
Expand Down Expand Up @@ -436,7 +465,6 @@
same(location.hash, "#data-url-tests/non-data-url.html?foo=bar");
start();
}
], 1000);
], 1000);
});

})(jQuery);
14 changes: 11 additions & 3 deletions tests/unit/navigation/navigation_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

test( "path.get method is working properly", function(){
window.location.hash = "foo";
same($.mobile.path.get(), "foo", "get method returns location.hash minus hash character");
same($.mobile.path.get(), "", "get method for location.hash #foo returns empty string");
same($.mobile.path.get( "#foo/bar/baz.html" ), "foo/bar/", "get method with hash arg returns path with no filename or hash prefix");
same($.mobile.path.get( "#foo/bar/baz.html/" ), "foo/bar/baz.html/", "last segment of hash is retained if followed by a trailing slash");
});
Expand Down Expand Up @@ -46,10 +46,10 @@
same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "bar/bing/bang?foo=bar&bak=baz", "appends query string paths to current path");

$.mobile.path.set("");
same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "/tests/unit/navigation/?foo=bar&bak=baz", "uses pathname for empty hash");
same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), location.pathname + "?foo=bar&bak=baz", "uses pathname for empty hash");

$.mobile.path.set("bar");
same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "/tests/unit/navigation/?foo=bar&bak=baz", "uses pathname for embedded pages");
same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), location.pathname + "?foo=bar&bak=baz", "uses pathname for embedded pages");

$.mobile.path.set("bar/bing?foo=bar");
same( $.mobile.path.makeAbsolute("?foo=bar&bak=baz"), "bar/bing?foo=bar&bak=baz", "prevents addition of many sets of query params");
Expand Down Expand Up @@ -80,6 +80,8 @@
hashedrel2 = "../#foo/bar.html",
hashedabs1 = "/foo/#bar.html",
hashedabs2 = location.pathname + "#foo/bar.html",
indexpath = "#foo/index.chm",
indexparampath = "#foo/index.chm?foo=" + localroot,
segments = location.pathname.split("/"),
uppath = "#",
i;
Expand Down Expand Up @@ -142,6 +144,12 @@
"hashed host-absolute, same path: return hash-relative path");
same( $.mobile.path.clean( remotepath ), remotepath,
"Hashed host-absolute, different host: return full url");
// with index.* at the end
same( $.mobile.path.clean( indexpath ), "#foo/",
"Remove index.* from path");
same( $.mobile.path.clean( indexparampath ), "#foo/?foo=" + localroot,
"Remove index.* from path but preserve parameters");

});

test( "path.stripHash is working properly", function(){
Expand Down