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

Commit c3fad11

Browse files
winsontamcaitp
authored andcommitted
fix($location) don't rewrite location when clicking on "javascript:" or "mailto:" link
Previously, absent a specified target attribute, when clicking on an anchor tag with an href beginning with either "javascript:" or "mailto:", the framework would rewrite the URL, when it ought not to. With this change, the browser is prevented from rewriting if the URL begins with a case-insensitive match for "javascript:" or "mailto:", optionally preceeded by whitespace. Closes #8407 Closes #8425 Closes #8426
1 parent 9242c58 commit c3fad11

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/ng/location.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ function $LocationProvider(){
635635
$location = new LocationMode(appBase, '#' + hashPrefix);
636636
$location.$$parse($location.$$rewrite(initialUrl));
637637

638+
var IGNORE_URI_REGEXP = /^\s*(javascript|mailto):/i;
639+
638640
$rootElement.on('click', function(event) {
639641
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
640642
// currently we open nice url link and redirect then
@@ -657,6 +659,9 @@ function $LocationProvider(){
657659
absHref = urlResolve(absHref.animVal).href;
658660
}
659661

662+
// Ignore when url is started with javascript: or mailto:
663+
if (IGNORE_URI_REGEXP.test(absHref)) return;
664+
660665
// Make relative links work in HTML5 mode for legacy browsers (or at least IE8 & 9)
661666
// The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or
662667
// somewhere#anchor or http://example.com/somewhere

test/ng/locationSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,32 @@ describe('$location', function() {
986986
});
987987

988988

989+
it('should not rewrite links with `javascript:` URI', function() {
990+
configureService(' jAvAsCrIpT:throw new Error("Boom!")', true, true, true);
991+
inject(
992+
initBrowser(),
993+
initLocation(),
994+
function($browser) {
995+
browserTrigger(link, 'click');
996+
expectNoRewrite($browser);
997+
}
998+
);
999+
});
1000+
1001+
1002+
it('should not rewrite links with `mailto:` URI', function() {
1003+
configureService(' mAiLtO:foo@bar.com', true, true, true);
1004+
inject(
1005+
initBrowser(),
1006+
initLocation(),
1007+
function($browser) {
1008+
browserTrigger(link, 'click');
1009+
expectNoRewrite($browser);
1010+
}
1011+
);
1012+
});
1013+
1014+
9891015
it('should rewrite full url links to same domain and base path', function() {
9901016
configureService('http://host.com/base/new', true);
9911017
inject(

0 commit comments

Comments
 (0)