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

Commit 3da4194

Browse files
committed
fix($location): url rewriting if element was removed
When user clicks a link, $location needs to intercept this event. The <a> doesn't have to be target element of the DOM event, so it needs to traverse the DOM, to find first <a> parent. If the target element was removed from DOM, during the same event, it would throw an exception. This fixes the issue. Closes #1058
1 parent ad5d2f2 commit 3da4194

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/ng/location.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ function $LocationProvider(){
550550

551551
// traverse the DOM up to find first A tag
552552
while (lowercase(elm[0].nodeName) !== 'a') {
553-
if (elm[0] === $rootElement[0]) return;
554-
elm = elm.parent();
553+
// ignore rewriting if no A tag (reached root element, or no parent - removed from document)
554+
if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return;
555555
}
556556

557557
var absHref = elm.prop('href'),

test/ng/locationSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,21 @@ describe('$location', function() {
10871087
expect(event.preventDefault).not.toHaveBeenCalled();
10881088
});
10891089
});
1090+
1091+
1092+
// regression https://github.com/angular/angular.js/issues/1058
1093+
it('should not throw if element was removed', inject(function($document, $rootElement, $location) {
1094+
// we need to do this otherwise we can't simulate events
1095+
$document.find('body').append($rootElement);
1096+
1097+
$rootElement.html('<button></button>');
1098+
var button = $rootElement.find('button');
1099+
1100+
button.bind('click', function() {
1101+
button.remove();
1102+
});
1103+
browserTrigger(button, 'click');
1104+
}));
10901105
});
10911106

10921107

0 commit comments

Comments
 (0)