Skip to content
This repository
Browse code

Fixes #4119 - Bad URLs with hashes are not gracefully handled

Fixes issue #4119
Added regex test to see if hash is valid and if a page with that id
exists, if not load the first page.

Added unit tests for isHashValid
  • Loading branch information...
commit b43ec247fabd16c793a8a1546c78eaf566e5ee34 1 parent 2776e46
Jason Scott authored May 25, 2012
6  js/jquery.mobile.init.js
@@ -142,8 +142,10 @@ define( [ "jquery", "./jquery.mobile.core", "./jquery.mobile.support", "./jquery
142 142
 			//remove initial build class (only present on first pageshow)
143 143
 			hideRenderingClass();
144 144
 
145  
-			// if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM
146  
-			if ( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ) {
  145
+			// if hashchange listening is disabled, there's no hash deeplink,
  146
+			// the hash is not valid (contains more than one # or does not start with #)
  147
+			// or there is no page with that hash, change to the first page in the DOM
  148
+			if ( !$.mobile.hashListeningEnabled || !$.mobile.path.isHashValid( location.hash ) || !$( location.hash + ':jqmData(role="page")' ).length ) {
147 149
 				$.mobile.changePage( $.mobile.firstPage, { transition: "none", reverse: true, changeHash: false, fromHashChange: true } );
148 150
 			}
149 151
 			// otherwise, trigger a hashchange to load a deeplink
4  js/jquery.mobile.navigation.js
@@ -211,6 +211,10 @@ define( [
211 211
 				return path.stripHash( hash.replace( /\?.*$/, "" ).replace( dialogHashKey, "" ) );
212 212
 			},
213 213
 
  214
+			isHashValid: function( hash ) {
  215
+				return !( /(^#$)|(#.*#.*)|(^[^#].+)/ ).test( hash );
  216
+			},
  217
+
214 218
 			//check whether a url is referencing the same domain, or an external domain or different protocol
215 219
 			//could be mailto, etc
216 220
 			isExternal: function( url ) {
10  tests/unit/navigation/navigation_helpers.js
@@ -215,4 +215,12 @@
215 215
 		same( $.mobile.path.cleanHash( "#anything/atall?akjfdjjf" ), "anything/atall", "removes query param");
216 216
 		same( $.mobile.path.cleanHash( "#nothing/atall" ), "nothing/atall", "removes query param");
217 217
 	});
218  
-})(jQuery);
  218
+
  219
+	test( "path.isHashValid", function(){
  220
+        same( $.mobile.path.isHashValid( "#id" ), true, "Valid hash");
  221
+        same( $.mobile.path.isHashValid( "#" ), false, "Empty hash");
  222
+        same( $.mobile.path.isHashValid( "#id#" ), false, "Hash with more than one #");
  223
+        same( $.mobile.path.isHashValid( "id" ), false, "Hash without #");
  224
+        same( $.mobile.path.isHashValid( "i#d" ), false, "Hash with # in the wrong spot");
  225
+	});
  226
+})(jQuery);

0 notes on commit b43ec24

Please sign in to comment.
Something went wrong with that request. Please try again.