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

Commit

Permalink
fix($location): rewrite links with nested elements
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vojtajina authored and IgorMinar committed Oct 22, 2011
1 parent c6c3949 commit 9b85757
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/service/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions test/service/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {

Expand Down

0 comments on commit 9b85757

Please sign in to comment.