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

Commit 36e7a6d

Browse files
author
Wei Wang
committed
feat(location): add configurable attr to force html5mode link rewriting
See this discussion: #14959 When using html5mode, sometimes it is necessary to not use the <base> tag in order to support SVG icons. An example of this is in the discussion linked above. When we do this, sometimes we also want to disable "rewriteLinks" so that we can still leave the Angular application via navigation. This feature allows the user to explicitly mark an <a> tag as a link that should not refresh from server, even if rewriteLinks is disabled.
1 parent 1660ddd commit 36e7a6d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/ng/location.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,10 @@ function $LocationProvider() {
748748
* whether or not a <base> tag is required to be present. If `enabled` and `requireBase` are
749749
* true, and a base tag is not present, an error will be thrown when `$location` is injected.
750750
* See the {@link guide/$location $location guide for more information}
751-
* - **rewriteLinks** - `{boolean}` - (default: `true`) When html5Mode is enabled,
752-
* enables/disables url rewriting for relative links.
751+
* - **rewriteLinks** - `{boolean|String}` - (default: `true`) When html5Mode is enabled,
752+
* enables/disables url rewriting for relative links. If set to a string, url rewriting will
753+
* only happen on links with an attribute that matches the given string. For example, if set
754+
* to "internalLink", then the URL will only be rewritten for `<a internal-link>` links.
753755
*
754756
* @returns {Object} html5Mode object if used as getter or itself (chaining) if used as setter
755757
*/
@@ -864,10 +866,11 @@ function $LocationProvider() {
864866
}
865867

866868
$rootElement.on('click', function(event) {
869+
var rewriteLinks = html5Mode.rewriteLinks;
867870
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
868871
// currently we open nice url link and redirect then
869872

870-
if (!html5Mode.rewriteLinks || event.ctrlKey || event.metaKey || event.shiftKey || event.which === 2 || event.button === 2) return;
873+
if (!rewriteLinks || event.ctrlKey || event.metaKey || event.shiftKey || event.which === 2 || event.button === 2) return;
871874

872875
var elm = jqLite(event.target);
873876

@@ -877,6 +880,8 @@ function $LocationProvider() {
877880
if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return;
878881
}
879882

883+
if (isString(rewriteLinks) && isUndefined(elm.attr(rewriteLinks))) return;
884+
880885
var absHref = elm.prop('href');
881886
// get the actual href attribute - see
882887
// http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx

test/ng/locationSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,25 @@ describe('$location', function() {
16011601
});
16021602

16031603

1604+
it('should rewrite links when the specified rewriteLinks attr is detected', function() {
1605+
configureTestLink({
1606+
linkHref: 'link?a#b',
1607+
attrs: 'force-rewrite',
1608+
html5Mode: {enabled: true, rewriteLinks:false},
1609+
supportHist: true
1610+
});
1611+
initService({html5Mode:{enabled: true, rewriteLinks:'forceRewrite'},supportHistory:true});
1612+
inject(
1613+
initBrowser({ url: 'http://host.com/base/index.html', basePath: '/base/index.html' }),
1614+
setupRewriteChecks(),
1615+
function($browser) {
1616+
browserTrigger(link, 'click');
1617+
expectRewriteTo($browser, 'http://host.com/base/link?a#b');
1618+
}
1619+
);
1620+
});
1621+
1622+
16041623
it('should rewrite full url links to same domain and base path', function() {
16051624
configureTestLink({linkHref: 'http://host.com/base/new'});
16061625
initService({html5Mode:true,supportHistory:false,hashPrefix:'!'});

0 commit comments

Comments
 (0)