Permalink
Browse files

Moved hash validation into $.mobile.path.isHashValid

Added unit tests for isHashValid
  • Loading branch information...
1 parent fb0a766 commit 15f404e638596e7b9cccd6210fb66ffc04899443 Jason Scott committed May 25, 2012
Showing with 14 additions and 2 deletions.
  1. +1 −1 js/jquery.mobile.init.js
  2. +4 −0 js/jquery.mobile.navigation.js
  3. +9 −1 tests/unit/navigation/navigation_helpers.js
@@ -80,7 +80,7 @@ define( [ "jquery", "./jquery.mobile.core", "./jquery.mobile.support", "./jquery
// if hashchange listening is disabled, there's no hash deeplink,
// the hash is not valid (contains more than one # or does not start with #)
// or there is no page with that hash, change to the first page in the DOM
- if ( !$.mobile.hashListeningEnabled || /(^#$)|(#.*#.*)|(^[^#].+)/.test( location.hash ) || !$( location.hash + ':jqmData(role="page")' ).length ) {
+ if ( !$.mobile.hashListeningEnabled || !$.mobile.path.isHashValid( location.hash ) || !$( location.hash + ':jqmData(role="page")' ).length ) {
$.mobile.changePage( $.mobile.firstPage, { transition: "none", reverse: true, changeHash: false, fromHashChange: true } );
}
// otherwise, trigger a hashchange to load a deeplink
@@ -212,6 +212,10 @@ define( [
return path.stripHash( hash.replace( /\?.*$/, "" ).replace( dialogHashKey, "" ) );
},
+ isHashValid: function( hash ) {
+ return !( /(^#$)|(#.*#.*)|(^[^#].+)/ ).test( hash );
+ },
+
//check whether a url is referencing the same domain, or an external domain or different protocol
//could be mailto, etc
isExternal: function( url ) {
@@ -215,4 +215,12 @@
same( $.mobile.path.cleanHash( "#anything/atall?akjfdjjf" ), "anything/atall", "removes query param");
same( $.mobile.path.cleanHash( "#nothing/atall" ), "nothing/atall", "removes query param");
});
-})(jQuery);
+
+ test( "path.isHashValid", function(){
+ same( $.mobile.path.isHashValid( "#id" ), true, "Valid hash");
+ same( $.mobile.path.isHashValid( "#" ), false, "Empty hash");
+ same( $.mobile.path.isHashValid( "#id#" ), false, "Hash with more than one #");
+ same( $.mobile.path.isHashValid( "id" ), false, "Hash without #");
+ same( $.mobile.path.isHashValid( "i#d" ), false, "Hash with # in the wrong spot");
+ });
+})(jQuery);

0 comments on commit 15f404e

Please sign in to comment.