From 25da85957fd94311cfad7e958eec32f8e710c228 Mon Sep 17 00:00:00 2001 From: Nicola Peduzzi Date: Thu, 17 Jul 2014 11:57:17 +0200 Subject: [PATCH 1/5] fix($location): consider baseHref in realtive link for legacy browsers --- src/ng/location.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ng/location.js b/src/ng/location.js index 1d99e9b2ea21..39a5bc4055f6 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -668,6 +668,13 @@ function $LocationProvider(){ if (href.indexOf('://') < 0) { // Ignore absolute URLs var prefix = '#' + hashPrefix; if (href[0] == '/') { + // Account for base href already present in appBase + if ($browser.baseHref() != '' && href.indexOf($browser.baseHref()) === 0) { + href = href.substr($browser.baseHref().length); + if (href == '' || href[0] != '/') { + href = '/' + href; + } + } // absolute path - replace old path absHref = appBase + prefix + href; } else if (href[0] == '#') { From dfd2959c6fbc4692229b41198799308d6a7bfc35 Mon Sep 17 00:00:00 2001 From: Nicola Peduzzi Date: Tue, 29 Jul 2014 12:44:19 +0200 Subject: [PATCH 2/5] Use cached value of baseHref instead of calculating it --- src/ng/location.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index 39a5bc4055f6..57fb91178090 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -669,9 +669,9 @@ function $LocationProvider(){ var prefix = '#' + hashPrefix; if (href[0] == '/') { // Account for base href already present in appBase - if ($browser.baseHref() != '' && href.indexOf($browser.baseHref()) === 0) { - href = href.substr($browser.baseHref().length); - if (href == '' || href[0] != '/') { + if (baseHref && href.indexOf(baseHref) === 0) { + href = href.substr(baseHref.length); + if (!href || href[0] != '/') { href = '/' + href; } } From b6d00f77bc6e83a747b514516ba81f4b7ceaee40 Mon Sep 17 00:00:00 2001 From: Nicola Peduzzi Date: Tue, 29 Jul 2014 12:48:02 +0200 Subject: [PATCH 3/5] Add test for html5mode fallback with base path --- test/ng/locationSpec.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 7e842e16a749..84c38da838ed 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -1330,6 +1330,36 @@ describe('$location', function() { }).not.toThrow(); }); }); + + + it('should transform the url correctly when a base path is present and html5mode is enabled but not supported', function() { + var base; + module(function() { + return function($browser) { + base = 'http://server/foo'; + $browser.url(base); + $browser.$$baseHref = '/foo'; + }; + }); + inject(initService(true, '', false), function($rootScope, $compile, $browser, $rootElement, $document, $location) { + // we need to do this otherwise we can't simulate events + $document.find('body').append($rootElement); + + var element = $compile('v1v2')($rootScope); + $rootElement.append(element); + var av1 = $rootElement.find('a').eq(0); + var av2 = $rootElement.find('a').eq(1); + + + browserTrigger(av1, 'click'); + expect($browser.url()).toEqual(base + '#/view1'); + + browserTrigger(av2, 'click'); + expect($browser.url()).toEqual(base + '#/bar/view2'); + + $rootElement.remove(); + }); + }); }); From 5b99a005ca756e6d500e7f55c4163d878a783953 Mon Sep 17 00:00:00 2001 From: Nicola Peduzzi Date: Wed, 20 Aug 2014 12:48:01 +0200 Subject: [PATCH 4/5] Remove unneccessary setup and cleanup to legacy browser html5mode support spec --- test/ng/locationSpec.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 84c38da838ed..2868776990a2 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -1345,8 +1345,7 @@ describe('$location', function() { // we need to do this otherwise we can't simulate events $document.find('body').append($rootElement); - var element = $compile('v1v2')($rootScope); - $rootElement.append(element); + $rootElement.append('v1v2'); var av1 = $rootElement.find('a').eq(0); var av2 = $rootElement.find('a').eq(1); @@ -1356,8 +1355,6 @@ describe('$location', function() { browserTrigger(av2, 'click'); expect($browser.url()).toEqual(base + '#/bar/view2'); - - $rootElement.remove(); }); }); }); From b65fd3ce2c92062f9a78a39c6de693c9cca7ba08 Mon Sep 17 00:00:00 2001 From: Nicola Peduzzi Date: Wed, 20 Aug 2014 13:05:12 +0200 Subject: [PATCH 5/5] Augment html5mode spec for legacy browser base support to include non-base absolute references --- test/ng/locationSpec.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 2868776990a2..863e7e37a6b8 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -1333,28 +1333,32 @@ describe('$location', function() { it('should transform the url correctly when a base path is present and html5mode is enabled but not supported', function() { - var base; + var serverUrl, base; module(function() { return function($browser) { - base = 'http://server/foo'; - $browser.url(base); - $browser.$$baseHref = '/foo'; + serverUrl = 'http://server'; + base = '/foo/bar' + $browser.url(serverUrl + base); + $browser.$$baseHref = base; }; }); inject(initService(true, '', false), function($rootScope, $compile, $browser, $rootElement, $document, $location) { // we need to do this otherwise we can't simulate events $document.find('body').append($rootElement); - $rootElement.append('v1v2'); + $rootElement.append('v1v2v3'); var av1 = $rootElement.find('a').eq(0); var av2 = $rootElement.find('a').eq(1); - + var av3 = $rootElement.find('a').eq(2); browserTrigger(av1, 'click'); - expect($browser.url()).toEqual(base + '#/view1'); + expect($browser.url()).toEqual(serverUrl + base + '#/view1'); browserTrigger(av2, 'click'); - expect($browser.url()).toEqual(base + '#/bar/view2'); + expect($browser.url()).toEqual(serverUrl + base + '#/baz/view2'); + + browserTrigger(av3, 'click'); + expect($browser.url()).toEqual(serverUrl + base + '#/view3'); }); }); });