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

Commit 2daa179

Browse files
committed
replace location.href references with a centralized method so we can address #4787
1 parent 882c045 commit 2daa179

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

js/jquery.mobile.navigation.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ define( [
4949
//
5050
urlParseRE: /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/,
5151

52+
// Abstraction to address xss (Issue #4787) in browsers that auto decode location.href
53+
// All references to location.href should be replaced with a call to this method so
54+
// that it can be dealt with properly here
55+
getLocation: function() {
56+
return window.location.toString();
57+
},
58+
59+
parseLocation: function() {
60+
return this.parseUrl( this.getLocation() );
61+
},
62+
5263
//Parse a URL into a structure that allows easy access to
5364
//all of the URL components by name.
5465
parseUrl: function( url ) {
@@ -368,7 +379,7 @@ define( [
368379
$base = $head.children( "base" ),
369380

370381
//tuck away the original document URL minus any fragment.
371-
documentUrl = path.parseUrl( location.href ),
382+
documentUrl = path.parseLocation(),
372383

373384
//if the document has an embedded base tag, documentBase is set to its
374385
//initial value. If a base tag does not exist, then we default to the documentUrl.
@@ -1480,7 +1491,7 @@ define( [
14801491
$window.bind( "hashchange", function( e, triggered ) {
14811492
// Firefox auto-escapes the location.hash as for v13 but
14821493
// leaves the href untouched
1483-
$.mobile._handleHashChange( path.parseUrl(location.href).hash );
1494+
$.mobile._handleHashChange( path.parseLocation().hash );
14841495
});
14851496

14861497
//set page min-heights to be device specific

js/jquery.mobile.navigation.pushstate.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ define( [ "jquery", "./jquery.mobile.navigation", "../external/requirejs/depend!
1212
var pushStateHandler = {},
1313
self = pushStateHandler,
1414
$win = $( window ),
15-
url = $.mobile.path.parseUrl( location.href ),
15+
url = $.mobile.path.parseLocation(),
1616
mobileinitDeferred = $.Deferred(),
1717
domreadyDeferred = $.Deferred();
1818

@@ -34,7 +34,8 @@ define( [ "jquery", "./jquery.mobile.navigation", "../external/requirejs/depend!
3434

3535
state: function() {
3636
return {
37-
hash: $.mobile.path.parseUrl( location.href ).hash || "#" + self.initialFilePath,
37+
// firefox auto decodes the url when using location.hash but not href
38+
hash: $.mobile.path.parseLocation().hash || "#" + self.initialFilePath,
3839
title: document.title,
3940

4041
// persist across refresh
@@ -72,9 +73,10 @@ define( [ "jquery", "./jquery.mobile.navigation", "../external/requirejs/depend!
7273
}
7374

7475
var href, state,
75-
hash = $.mobile.path.parseUrl( location.href ).hash,
76+
// firefox auto decodes the url when using location.hash but not href
77+
hash = $.mobile.path.parseLocation().hash,
7678
isPath = $.mobile.path.isPath( hash ),
77-
resolutionUrl = isPath ? location.href : $.mobile.getDocumentUrl();
79+
resolutionUrl = isPath ? $.mobile.path.getLocation() : $.mobile.getDocumentUrl();
7880

7981
hash = isPath ? hash.replace( "#", "" ) : hash;
8082

@@ -139,7 +141,7 @@ define( [ "jquery", "./jquery.mobile.navigation", "../external/requirejs/depend!
139141

140142
// if there's no hash, we need to replacestate for returning to home
141143
if ( location.hash === "" ) {
142-
history.replaceState( self.state(), document.title, location.href );
144+
history.replaceState( self.state(), document.title, $.mobile.path.getLocation() );
143145
}
144146
}
145147
});

0 commit comments

Comments
 (0)