Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #4455 from jasondscott/4119_BadHash

Fixes #4119 - Bad URLs with hashes are not gracefully handled
  • Loading branch information...
commit c8c4cde9de53ee261e231c8da639ba2a9d517cd7 2 parents eb0b668 + 15f404e
Jason Scott jasondscott authored
6 js/jquery.mobile.init.js
View
@@ -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
4 js/jquery.mobile.navigation.js
View
@@ -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 ) {
10 tests/unit/navigation/navigation_helpers.js
View
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.