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

Commit

Permalink
Merge pull request #4455 from jasondscott/4119_BadHash
Browse files Browse the repository at this point in the history
Fixes #4119 - Bad URLs with hashes are not gracefully handled
  • Loading branch information
jasondscott committed May 26, 2012
2 parents eb0b668 + 15f404e commit c8c4cde
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions js/jquery.mobile.init.js
Expand Up @@ -77,8 +77,10 @@ define( [ "jquery", "./jquery.mobile.core", "./jquery.mobile.support", "./jquery
//remove initial build class (only present on first pageshow)
hideRenderingClass();

// if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM
if ( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ) {
// 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 || !$.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
Expand Down
4 changes: 4 additions & 0 deletions js/jquery.mobile.navigation.js
Expand Up @@ -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 ) {
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/navigation/navigation_helpers.js
Expand Up @@ -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 c8c4cde

Please sign in to comment.