Permalink
Browse files

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
  • Loading branch information...
1 parent ad5d2f2 commit 3da4194f98fa0c1ad1e5ab159719e4b25799e6d4 @vojtajina vojtajina committed Jun 16, 2012
Showing with 17 additions and 2 deletions.
  1. +2 −2 src/ng/location.js
  2. +15 −0 test/ng/locationSpec.js
View
@@ -550,8 +550,8 @@ function $LocationProvider(){
// traverse the DOM up to find first A tag
while (lowercase(elm[0].nodeName) !== 'a') {
- if (elm[0] === $rootElement[0]) return;
- elm = elm.parent();
+ // ignore rewriting if no A tag (reached root element, or no parent - removed from document)
+ if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return;
}
var absHref = elm.prop('href'),
@@ -1087,6 +1087,21 @@ describe('$location', function() {
expect(event.preventDefault).not.toHaveBeenCalled();
});
});
+
+
+ // regression https://github.com/angular/angular.js/issues/1058
+ it('should not throw if element was removed', inject(function($document, $rootElement, $location) {
+ // we need to do this otherwise we can't simulate events
+ $document.find('body').append($rootElement);
+
+ $rootElement.html('<button></button>');
+ var button = $rootElement.find('button');
+
+ button.bind('click', function() {
+ button.remove();
+ });
+ browserTrigger(button, 'click');
+ }));
});

0 comments on commit 3da4194

Please sign in to comment.