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

Commit 2bce71e

Browse files
committed
feat(ngHref): bind ng-href to xlink:href for SVGAElement
This change makes the ngHref directive useful for SVGAElements by having it bind to the xlink:href attribute rather than the href attribute. Closes #5904
1 parent c8e03e3 commit 2bce71e

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/ng/directive/booleanAttrs.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -369,17 +369,27 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
369369
return {
370370
priority: 99, // it needs to run after the attributes are interpolated
371371
link: function(scope, element, attr) {
372+
var propName = attrName,
373+
name = attrName;
374+
375+
if (attrName === 'href' &&
376+
toString.call(element.prop('href')) === '[object SVGAnimatedString]') {
377+
name = 'xlinkHref';
378+
attr.$attr[name] = 'xlink:href';
379+
propName = null;
380+
}
381+
372382
attr.$observe(normalized, function(value) {
373383
if (!value)
374384
return;
375385

376-
attr.$set(attrName, value);
386+
attr.$set(name, value);
377387

378388
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
379389
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
380390
// to set the property as well to achieve the desired effect.
381391
// we use attr[attrName] value since $set can sanitize the url.
382-
if (msie) element.prop(attrName, attr[attrName]);
392+
if (msie && propName) element.prop(propName, attr[name]);
383393
});
384394
}
385395
};

test/ng/directive/booleanAttrsSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,28 @@ describe('ngHref', function() {
251251
$rootScope.$digest();
252252
expect(element.attr('href')).toEqual('http://server');
253253
}));
254+
255+
if (isDefined(window.SVGElement)) {
256+
describe('SVGAElement', function() {
257+
it('should interpolate the expression and bind to xlink:href', inject(function($compile, $rootScope) {
258+
element = $compile('<svg><a ng-href="some/{{id}}"></a></svg>')($rootScope);
259+
var child = element.children('a');
260+
$rootScope.$digest();
261+
expect(child.attr('xlink:href')).toEqual('some/');
262+
263+
$rootScope.$apply(function() {
264+
$rootScope.id = 1;
265+
});
266+
expect(child.attr('xlink:href')).toEqual('some/1');
267+
}));
268+
269+
270+
it('should bind xlink:href even if no interpolation', inject(function($rootScope, $compile) {
271+
element = $compile('<svg><a ng-href="http://server"></a></svg>')($rootScope);
272+
var child = element.children('a');
273+
$rootScope.$digest();
274+
expect(child.attr('xlink:href')).toEqual('http://server');
275+
}));
276+
});
277+
}
254278
});

0 commit comments

Comments
 (0)