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

Commit

Permalink
fix($location): prevent navigation when event isDefaultPrevented
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed May 7, 2013
1 parent c575a56 commit 2c69a67
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ function createEventHandler(element, events) {
}

event.isDefaultPrevented = function() {
return event.defaultPrevented;
return event.defaultPrevented || event.returnValue == false;
};

forEach(events[type || event.type], function(fn) {
Expand Down
2 changes: 1 addition & 1 deletion src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ function $LocationProvider(){
var absHref = elm.prop('href');
var rewrittenUrl = $location.$$rewrite(absHref);

if (absHref && !elm.attr('target') && rewrittenUrl) {
if (absHref && !elm.attr('target') && rewrittenUrl && !event.isDefaultPrevented()) {
event.preventDefault();
if (rewrittenUrl != $browser.url()) {
// update location manually
Expand Down
7 changes: 7 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,12 @@ describe('$location', function() {
jqLite(link).attr('href', 'http://host.com/base/');
browserTrigger(link, 'click');
expectRewriteTo($browser, 'http://host.com/base/');

jqLite(link).
attr('href', 'http://host.com/base/foo').
bind('click', function(e) { e.preventDefault(); });
browserTrigger(link, 'click');
expect($browser.url()).toBe('http://host.com/base/');
}
);
});
Expand Down Expand Up @@ -1372,6 +1378,7 @@ describe('$location', function() {
expect(location.$$rewrite('http://other')).toEqual(undefined);
expect(location.$$rewrite('http://server/pre/')).toEqual('http://server/pre/');
expect(location.$$rewrite('http://server/pre/#otherPath')).toEqual('http://server/pre/#otherPath');
expect(location.$$rewrite('javascript:void(0)')).toEqual(undefined);
});
});

Expand Down

0 comments on commit 2c69a67

Please sign in to comment.