Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b3e09be

Browse files
sgrifIgorMinar
authored andcommitted
feat($location): allow automatic rewriting of links to be disabled
Currently, when the location provider is set to html5 mode, all links on the page are hijacked and automatically rewritten. While this may be desirable behavior in some cases (such as using ngRoute), not all cases where html5 mode are enabled imply the desire for this behavior. One example would be an application using the [ui-router](https://github.com/angular-ui/ui-router) library, with some pages that exist outside of angular. Links that are meant to go through the router use the `ui-sref` directive, so the rewrite behavior is unnecessary. Closes #5487
1 parent 0c2378d commit b3e09be

File tree

2 files changed

+54
-37
lines changed

2 files changed

+54
-37
lines changed

src/ng/location.js

+27-17
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,8 @@ function $LocationProvider(){
624624
var hashPrefix = '',
625625
html5Mode = {
626626
enabled: false,
627-
requireBase: true
627+
requireBase: true,
628+
rewriteLinks: true
628629
};
629630

630631
/**
@@ -648,15 +649,17 @@ function $LocationProvider(){
648649
* @name $locationProvider#html5Mode
649650
* @description
650651
* @param {(boolean|Object)=} mode If boolean, sets `html5Mode.enabled` to value.
651-
* If object, sets `enabled` and `requireBase` to respective values.
652-
* - **enabled** – `{boolean}` – Sets `html5Mode.enabled`. If true, will rely on
653-
* `history.pushState` to change urls where supported. Will fall back to hash-prefixed paths
654-
* in browsers that do not support `pushState`.
655-
* - **requireBase** - `{boolean}` - Sets `html5Mode.requireBase` (default: `true`). When
656-
* html5Mode is enabled, specifies whether or not a <base> tag is required to be present. If
657-
* `enabled` and `requireBase` are true, and a base tag is not present, an error will be
658-
* thrown when `$location` is injected. See the
659-
* {@link guide/$location $location guide for more information}
652+
* If object, sets `enabled`, `requireBase` and `rewriteLinks` to respective values. Supported
653+
* properties:
654+
* - **enabled** – `{boolean}` – (default: false) If true, will rely on `history.pushState` to
655+
* change urls where supported. Will fall back to hash-prefixed paths in browsers that do not
656+
* support `pushState`.
657+
* - **requireBase** - `{boolean}` - (default: `true`) When html5Mode is enabled, specifies
658+
* whether or not a <base> tag is required to be present. If `enabled` and `requireBase` are
659+
* true, and a base tag is not present, an error will be thrown when `$location` is injected.
660+
* See the {@link guide/$location $location guide for more information}
661+
* - **rewriteLinks** - `{boolean}` - (default: `false`) When html5Mode is enabled, disables
662+
* url rewriting for relative linksTurns off url rewriting for relative links.
660663
*
661664
* @returns {Object} html5Mode object if used as getter or itself (chaining) if used as setter
662665
*/
@@ -665,12 +668,19 @@ function $LocationProvider(){
665668
html5Mode.enabled = mode;
666669
return this;
667670
} else if (isObject(mode)) {
668-
html5Mode.enabled = isBoolean(mode.enabled) ?
669-
mode.enabled :
670-
html5Mode.enabled;
671-
html5Mode.requireBase = isBoolean(mode.requireBase) ?
672-
mode.requireBase :
673-
html5Mode.requireBase;
671+
672+
if (isBoolean(mode.enabled)) {
673+
html5Mode.enabled = mode.enabled;
674+
}
675+
676+
if (isBoolean(mode.requireBase)) {
677+
html5Mode.requireBase = mode.requireBase;
678+
}
679+
680+
if (isBoolean(mode.rewriteLinks)) {
681+
html5Mode.rewriteLinks = mode.rewriteLinks;
682+
}
683+
674684
return this;
675685
} else {
676686
return html5Mode;
@@ -763,7 +773,7 @@ function $LocationProvider(){
763773
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
764774
// currently we open nice url link and redirect then
765775

766-
if (event.ctrlKey || event.metaKey || event.which == 2) return;
776+
if (!html5Mode.rewriteLinks || event.ctrlKey || event.metaKey || event.which == 2) return;
767777

768778
var elm = jqLite(event.target);
769779

test/ng/locationSpec.js

+27-20
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,19 @@ describe('$location', function() {
12791279
});
12801280

12811281

1282+
it ('should not rewrite links when rewriting links is disabled', function() {
1283+
configureService('/a?b=c', true, true, '', 'some content', false);
1284+
inject(
1285+
initBrowser(),
1286+
initLocation(),
1287+
function($browser) {
1288+
browserTrigger(link, 'click');
1289+
expectNoRewrite($browser);
1290+
}
1291+
);
1292+
});
1293+
1294+
12821295
it('should rewrite full url links to same domain and base path', function() {
12831296
configureService({linkHref: 'http://host.com/base/new', html5Mode: true});
12841297
inject(
@@ -1793,11 +1806,13 @@ describe('$location', function() {
17931806

17941807

17951808
describe('html5Mode', function() {
1796-
it('should set enabled and requireBase when called with object', function() {
1809+
it('should set enabled, requireBase and rewriteLinks when called with object', function() {
17971810
module(function($locationProvider) {
1811+
$locationProvider.html5Mode({enabled: true, requireBase: false, rewriteLinks: false});
17981812
expect($locationProvider.html5Mode()).toEqual({
1799-
enabled: false,
1800-
requireBase: true
1813+
enabled: true,
1814+
requireBase: false,
1815+
rewriteLinks: false
18011816
});
18021817
});
18031818

@@ -1809,12 +1824,14 @@ describe('$location', function() {
18091824
module(function($locationProvider) {
18101825
$locationProvider.html5Mode({
18111826
enabled: 'duh',
1812-
requireBase: 'probably'
1827+
requireBase: 'probably',
1828+
rewriteLinks: 'nope'
18131829
});
18141830

18151831
expect($locationProvider.html5Mode()).toEqual({
18161832
enabled: false,
1817-
requireBase: true
1833+
requireBase: true,
1834+
rewriteLinks: true
18181835
});
18191836
});
18201837

@@ -1830,31 +1847,21 @@ describe('$location', function() {
18301847

18311848
expect($locationProvider.html5Mode()).toEqual({
18321849
enabled: false,
1833-
requireBase: true
1834-
});
1835-
});
1836-
1837-
inject(function(){});
1838-
});
1839-
1840-
1841-
it('should default to enabled:false and requireBase:true', function() {
1842-
module(function($locationProvider) {
1843-
expect($locationProvider.html5Mode()).toEqual({
1844-
enabled: false,
1845-
requireBase: true
1850+
requireBase: true,
1851+
rewriteLinks: true
18461852
});
18471853
});
18481854

18491855
inject(function(){});
18501856
});
18511857

18521858

1853-
it('should return html5Mode object when called without value', function() {
1859+
it('should default to enabled:false, requireBase:true and rewriteLinks:true', function() {
18541860
module(function($locationProvider) {
18551861
expect($locationProvider.html5Mode()).toEqual({
18561862
enabled: false,
1857-
requireBase: true
1863+
requireBase: true,
1864+
rewriteLinks: true
18581865
});
18591866
});
18601867

0 commit comments

Comments
 (0)