diff options
| author | Vojta Jina | 2011-10-20 08:46:09 -0700 | 
|---|---|---|
| committer | Igor Minar | 2011-10-22 15:35:18 -0700 | 
| commit | 9b85757102fbd44e88d0a3909fdf8b90f191b593 (patch) | |
| tree | 88eb638ba4b91e51ef9e4a1315aa357de22ae02a | |
| parent | c6c3949b14f4003ecab291243edfca61262f2c3d (diff) | |
| download | angular.js-9b85757102fbd44e88d0a3909fdf8b90f191b593.tar.bz2 | |
fix($location): rewrite links with nested elements
For example:
<a href="some/link">inner <span>text</span></a>
If you click on "text", then the span element is event.target, so we need to traverse the DOM.
| -rw-r--r-- | src/service/location.js | 12 | ||||
| -rw-r--r-- | test/service/locationSpec.js | 14 | 
2 files changed, 20 insertions, 6 deletions
| diff --git a/src/service/location.js b/src/service/location.js index 2ffc5587..d1d34e67 100644 --- a/src/service/location.js +++ b/src/service/location.js @@ -442,12 +442,16 @@ angularServiceInject('$location', function($browser, $sniffer, $locationConfig,        // TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)        // currently we open nice url link and redirect then -      if (uppercase(event.target.nodeName) != 'A' || event.ctrlKey || event.metaKey || -          event.which == 2) return; +      if (event.ctrlKey || event.metaKey || event.which == 2) return; -      var elm = jqLite(event.target), -          href = elm.attr('href'); +      var elm = jqLite(event.target); +      // traverse the DOM up to find first A tag +      while (elm.length && lowercase(elm[0].nodeName) !== 'a') { +        elm = elm.parent(); +      } + +      var href = elm.attr('href');        if (!href || isDefined(elm.attr('ng:ext-link')) || elm.attr('target')) return;        // remove same domain from full url links (IE7 always returns full hrefs) diff --git a/test/service/locationSpec.js b/test/service/locationSpec.js index 6cf302fe..9a7aa943 100644 --- a/test/service/locationSpec.js +++ b/test/service/locationSpec.js @@ -556,10 +556,11 @@ describe('$location', function() {      var root, link, extLink, $browser, originalBrowser, lastEventPreventDefault; -    function init(linkHref, html5Mode, supportHist, attrs) { +    function init(linkHref, html5Mode, supportHist, attrs, content) {        var jqRoot = jqLite('<div></div>');        attrs = attrs ? ' ' + attrs + ' ' : ''; -      link = jqLite('<a href="' + linkHref + '"' + attrs + '>link</a>')[0]; +      content = content || 'link'; +      link = jqLite('<a href="' + linkHref + '"' + attrs + '>' + content + '</a>')[0];        root = jqRoot.append(link)[0];        jqLite(document.body).append(jqRoot); @@ -670,6 +671,15 @@ describe('$location', function() {      }); +    it('should rewrite when clicked span inside link', function() { +      init('some/link', true, true, '', '<span>link</span>'); +      var span = jqLite(link).find('span'); + +      browserTrigger(span, 'click'); +      expectRewriteTo('http://host.com/base/some/link'); +    }); + +      // don't run next tests on IE<9, as browserTrigger does not simulate pressed keys      if (!(msie < 9)) { | 
