Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97f230a

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 angular#8407 Closes angular#8425 Closes angular#8426
1 parent 9a2f8e1 commit 97f230a

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
@@ -1028,6 +1028,32 @@ describe('$location', function() {
10281028
});
10291029

10301030

1031+
it('should not rewrite links with `javascript:` URI', function() {
1032+
configureService(' jAvAsCrIpT:throw new Error("Boom!")', true, true, true);
1033+
inject(
1034+
initBrowser(),
1035+
initLocation(),
1036+
function($browser) {
1037+
browserTrigger(link, 'click');
1038+
expectNoRewrite($browser);
1039+
}
1040+
);
1041+
});
1042+
1043+
1044+
it('should not rewrite links with `mailto:` URI', function() {
1045+
configureService(' mAiLtO:[email protected]', true, true, true);
1046+
inject(
1047+
initBrowser(),
1048+
initLocation(),
1049+
function($browser) {
1050+
browserTrigger(link, 'click');
1051+
expectNoRewrite($browser);
1052+
}
1053+
);
1054+
});
1055+
1056+
10311057
it('should rewrite full url links to same domain and base path', function() {
10321058
configureService('http://host.com/base/new', true);
10331059
inject(

0 commit comments

Comments
 (0)