Skip to content

Commit

Permalink
fix(urlRouter): add check using $sniffer
Browse files Browse the repository at this point in the history
- Add additional check for situations where `pushState` is not properly supported
  • Loading branch information
wesleycho committed Sep 3, 2015
1 parent ea3355b commit c219e80
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/urlRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
*
*/
this.$get = $get;
$get.$inject = ['$location', '$rootScope', '$injector', '$browser'];
function $get( $location, $rootScope, $injector, $browser) {
$get.$inject = ['$location', '$rootScope', '$injector', '$browser', '$sniffer'];
function $get( $location, $rootScope, $injector, $browser, $sniffer) {

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

Expand Down Expand Up @@ -396,6 +396,8 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
if (angular.isObject(isHtml5)) {
isHtml5 = isHtml5.enabled;
}

isHtml5 = isHtml5 && $sniffer.history;

var url = urlMatcher.format(params);
options = options || {};
Expand Down
12 changes: 10 additions & 2 deletions test/urlRouterSpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
describe("UrlRouter", function () {

var $urp, $lp, $ur, location, match, scope;

var $urp, $lp, $s, $ur, location, match, scope;
describe("provider", function () {

beforeEach(function() {
Expand Down Expand Up @@ -67,6 +66,8 @@ describe("UrlRouter", function () {
scope = $rootScope.$new();
location = $location;
$ur = $injector.invoke($urp.$get);
$s = $injector.get('$sniffer');
$s.history = true;
});
});

Expand Down Expand Up @@ -236,6 +237,13 @@ describe("UrlRouter", function () {
expect($lp.html5Mode()).toBe(true);
expect($urlRouter.href(new UrlMatcher('/hello/:name'), {name: 'world', '#': 'frag'})).toBe('/hello/world#frag');
}));

it('should return URLs with #fragments when html5Mode is true & browser does not support pushState', inject(function($urlRouter) {
$lp.html5Mode(true);
$s.history = false;
expect($lp.html5Mode()).toBe(true);
expect($urlRouter.href(new UrlMatcher('/hello/:name'), {name: 'world', '#': 'frag'})).toBe('#/hello/world#frag');
}));
});
});

Expand Down

0 comments on commit c219e80

Please sign in to comment.