Skip to content

Commit c219e80

Browse files
committed
fix(urlRouter): add check using $sniffer
- Add additional check for situations where `pushState` is not properly supported
1 parent ea3355b commit c219e80

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/urlRouter.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
262262
*
263263
*/
264264
this.$get = $get;
265-
$get.$inject = ['$location', '$rootScope', '$injector', '$browser'];
266-
function $get( $location, $rootScope, $injector, $browser) {
265+
$get.$inject = ['$location', '$rootScope', '$injector', '$browser', '$sniffer'];
266+
function $get( $location, $rootScope, $injector, $browser, $sniffer) {
267267

268268
var baseHref = $browser.baseHref(), location = $location.url(), lastPushedUrl;
269269

@@ -396,6 +396,8 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
396396
if (angular.isObject(isHtml5)) {
397397
isHtml5 = isHtml5.enabled;
398398
}
399+
400+
isHtml5 = isHtml5 && $sniffer.history;
399401

400402
var url = urlMatcher.format(params);
401403
options = options || {};

test/urlRouterSpec.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
describe("UrlRouter", function () {
22

3-
var $urp, $lp, $ur, location, match, scope;
4-
3+
var $urp, $lp, $s, $ur, location, match, scope;
54
describe("provider", function () {
65

76
beforeEach(function() {
@@ -67,6 +66,8 @@ describe("UrlRouter", function () {
6766
scope = $rootScope.$new();
6867
location = $location;
6968
$ur = $injector.invoke($urp.$get);
69+
$s = $injector.get('$sniffer');
70+
$s.history = true;
7071
});
7172
});
7273

@@ -236,6 +237,13 @@ describe("UrlRouter", function () {
236237
expect($lp.html5Mode()).toBe(true);
237238
expect($urlRouter.href(new UrlMatcher('/hello/:name'), {name: 'world', '#': 'frag'})).toBe('/hello/world#frag');
238239
}));
240+
241+
it('should return URLs with #fragments when html5Mode is true & browser does not support pushState', inject(function($urlRouter) {
242+
$lp.html5Mode(true);
243+
$s.history = false;
244+
expect($lp.html5Mode()).toBe(true);
245+
expect($urlRouter.href(new UrlMatcher('/hello/:name'), {name: 'world', '#': 'frag'})).toBe('#/hello/world#frag');
246+
}));
239247
});
240248
});
241249

0 commit comments

Comments
 (0)