diff options
| author | Vojta Jina | 2012-06-16 13:04:40 -0700 | 
|---|---|---|
| committer | Vojta Jina | 2012-06-20 12:18:48 -0700 | 
| commit | 3da4194f98fa0c1ad1e5ab159719e4b25799e6d4 (patch) | |
| tree | 8ee46d67bde82ddcb88c42ab765ae63435278aa2 | |
| parent | ad5d2f2991ecfe1ec6152be71982b8980236c988 (diff) | |
| download | angular.js-3da4194f98fa0c1ad1e5ab159719e4b25799e6d4.tar.bz2 | |
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
| -rw-r--r-- | src/ng/location.js | 4 | ||||
| -rw-r--r-- | test/ng/locationSpec.js | 15 | 
2 files changed, 17 insertions, 2 deletions
| diff --git a/src/ng/location.js b/src/ng/location.js index 727a0c2b..cf50952d 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -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'), diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 88747dff..cdad1f1f 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -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'); +    }));    }); | 
