Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
/angular.jsPublic archive

Commitbaa3b4a

Browse files
committed
feat(ngHref): bind href attribute to ng-href attribute in case SVG element
1 parent7f1b8bd commitbaa3b4a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

‎src/ng/directive/attrs.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ forEach(ALIASED_ATTR, function(htmlAttr, ngAttr) {
399399
};
400400
});
401401

402+
403+
// Helper
404+
varupdateAttribute=function(attr,name,value){
405+
attr.$set(name,value);
406+
if(name==='xlinkHref'){
407+
attr.$set('href',value);
408+
}
409+
};
402410
// ng-src, ng-srcset, ng-href are interpolated
403411
forEach(['src','srcset','href'],function(attrName){
404412
varnormalized=directiveNormalize('ng-'+attrName);
@@ -419,12 +427,12 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
419427
attr.$observe(normalized,function(value){
420428
if(!value){
421429
if(attrName==='href'){
422-
attr.$set(name,null);
430+
updateAttribute(attr,name,null);
423431
}
424432
return;
425433
}
426434

427-
attr.$set(name,value);
435+
updateAttribute(attr,name,value);
428436

429437
// Support: IE 9-11 only
430438
// On IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist

‎test/ng/directive/booleanAttrsSpec.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,25 +299,44 @@ describe('ngHref', function() {
299299

300300
if(isDefined(window.SVGElement)){
301301
describe('SVGAElement',function(){
302-
it('should interpolate the expression and bind to xlink:href',inject(function($compile,$rootScope){
302+
it('should interpolate the expression and bind to xlink:href and href',inject(function($compile,$rootScope){
303303
element=$compile('<svg><a ng-href="some/{{id}}"></a></svg>')($rootScope);
304304
varchild=element.children('a');
305305
$rootScope.$digest();
306306
expect(child.attr('xlink:href')).toEqual('some/');
307+
expect(child.attr('href')).toEqual('some/');
307308

308309
$rootScope.$apply(function(){
309310
$rootScope.id=1;
310311
});
311312
expect(child.attr('xlink:href')).toEqual('some/1');
313+
expect(child.attr('href')).toEqual('some/1');
312314
}));
313315

314316

315-
it('should bind xlink:href even if no interpolation',inject(function($rootScope,$compile){
317+
it('should bind xlink:hrefand hrefeven if no interpolation',inject(function($rootScope,$compile){
316318
element=$compile('<svg><a ng-href="http://server"></a></svg>')($rootScope);
317319
varchild=element.children('a');
318320
$rootScope.$digest();
319321
expect(child.attr('xlink:href')).toEqual('http://server');
322+
expect(child.attr('href')).toEqual('http://server');
320323
}));
324+
325+
they('should set xlink:href and href to null when ng-href value is $prop',['',0,null,false,undefined],
326+
function(value){
327+
var$compile,$rootScope;
328+
inject(function(_$compile_,_$rootScope_){
329+
$compile=_$compile_;
330+
$rootScope=_$rootScope_;
331+
});
332+
333+
$rootScope.url=value;
334+
element=$compile('<svg><a ng-href="{{url}}"></a></svg>')($rootScope);
335+
varchild=element.children('a');
336+
expect(child.attr('xlink:href')).toBeUndefined();
337+
expect(child.attr('href')).toBeUndefined();
338+
}
339+
);
321340
});
322341
}
323342
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp