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

Commit 77bf5e0

Browse files
committed
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.
1 parent 7fc18b2 commit 77bf5e0

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/service/location.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,16 @@ angularServiceInject('$location', function($browser, $sniffer, $locationConfig,
442442
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
443443
// currently we open nice url link and redirect then
444444

445-
if (uppercase(event.target.nodeName) != 'A' || event.ctrlKey || event.metaKey ||
446-
event.which == 2) return;
445+
if (event.ctrlKey || event.metaKey || event.which == 2) return;
447446

448-
var elm = jqLite(event.target),
449-
href = elm.attr('href');
447+
var elm = jqLite(event.target);
450448

449+
// traverse the DOM up to find first A tag
450+
while (elm.length && lowercase(elm[0].nodeName) !== 'a') {
451+
elm = elm.parent();
452+
}
453+
454+
var href = elm.attr('href');
451455
if (!href || isDefined(elm.attr('ng:ext-link')) || elm.attr('target')) return;
452456

453457
// remove same domain from full url links (IE7 always returns full hrefs)

test/service/locationSpec.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,11 @@ describe('$location', function() {
556556

557557
var root, link, extLink, $browser, originalBrowser, lastEventPreventDefault;
558558

559-
function init(linkHref, html5Mode, supportHist, attrs) {
559+
function init(linkHref, html5Mode, supportHist, attrs, content) {
560560
var jqRoot = jqLite('<div></div>');
561561
attrs = attrs ? ' ' + attrs + ' ' : '';
562-
link = jqLite('<a href="' + linkHref + '"' + attrs + '>link</a>')[0];
562+
content = content || 'link';
563+
link = jqLite('<a href="' + linkHref + '"' + attrs + '>' + content + '</a>')[0];
563564
root = jqRoot.append(link)[0];
564565

565566
jqLite(document.body).append(jqRoot);
@@ -670,6 +671,15 @@ describe('$location', function() {
670671
});
671672

672673

674+
it('should rewrite when clicked span inside link', function() {
675+
init('some/link', true, true, '', '<span>link</span>');
676+
var span = jqLite(link).find('span');
677+
678+
browserTrigger(span, 'click');
679+
expectRewriteTo('http://host.com/base/some/link');
680+
});
681+
682+
673683
// don't run next tests on IE<9, as browserTrigger does not simulate pressed keys
674684
if (!(msie < 9)) {
675685

0 commit comments

Comments
 (0)