From 469ea3384ad48ca4765af807c0f41201edb527f9 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Fri, 4 Apr 2014 02:26:17 +0300 Subject: [PATCH] fix(ngHref): remove attribute when empty value instead of ignoring Closes #2755 --- src/ng/directive/attrs.js | 8 ++++++-- test/ng/directive/booleanAttrsSpec.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/ng/directive/attrs.js b/src/ng/directive/attrs.js index ae68d711cf3c..6b230a75150c 100644 --- a/src/ng/directive/attrs.js +++ b/src/ng/directive/attrs.js @@ -403,8 +403,12 @@ forEach(['src', 'srcset', 'href'], function(attrName) { } attr.$observe(normalized, function(value) { - if (!value) - return; + if (!value) { + if (attrName === 'href') { + attr.$set(name, null); + } + return; + } attr.$set(name, value); diff --git a/test/ng/directive/booleanAttrsSpec.js b/test/ng/directive/booleanAttrsSpec.js index 2057cb25937f..859566bbbc76 100644 --- a/test/ng/directive/booleanAttrsSpec.js +++ b/test/ng/directive/booleanAttrsSpec.js @@ -252,6 +252,23 @@ describe('ngHref', function() { expect(element.attr('href')).toEqual('http://server'); })); + it('should not set the href if ng-href is empty', inject(function($rootScope, $compile) { + $rootScope.url = null; + element = $compile('')($rootScope); + $rootScope.$digest(); + expect(element.attr('href')).toEqual(undefined); + })); + + it('should remove the href if ng-href changes to empty', inject(function($rootScope, $compile) { + $rootScope.url = 'http://www.google.com/'; + element = $compile('')($rootScope); + $rootScope.$digest(); + + $rootScope.url = null; + $rootScope.$digest(); + expect(element.attr('href')).toEqual(undefined); + })); + if (isDefined(window.SVGElement)) { describe('SVGAElement', function() { it('should interpolate the expression and bind to xlink:href', inject(function($compile, $rootScope) {