From 003b5ade14e8376319a1af52e8cef480d80e8827 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Wed, 30 Apr 2014 15:43:03 -0700 Subject: [PATCH] fix(ngSrc, ngSrcset): only interpolate if all expressions are defined BREAKING CHANGE If `bar` is `undefined`, before `` yields ``. With this change, the binding will not set `src`. If you want the old behavior, you can do this: ``. The same applies for `srcset` as well. Closes #6984 --- src/ng/compile.js | 10 +++++++++- test/ng/directive/booleanAttrsSpec.js | 2 +- test/ng/directive/ngSrcsetSpec.js | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 4256adcaabc7..64f8a076acff 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1854,9 +1854,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { "ng- versions (such as ng-click instead of onclick) instead."); } + var allOrNothingAttrs = [ + 'ngSrc', + 'ngSrcset', + 'src', + 'srcset' + ]; + // we need to interpolate again, in case the attribute value has been updated // (e.g. by another directive's compile function) - interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name)); + interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name), + allOrNothingAttrs.indexOf(name) > -1); // if attribute was updated so that there is no interpolation going on we don't want to // register any observers diff --git a/test/ng/directive/booleanAttrsSpec.js b/test/ng/directive/booleanAttrsSpec.js index 6bcb57eeb2b8..2057cb25937f 100644 --- a/test/ng/directive/booleanAttrsSpec.js +++ b/test/ng/directive/booleanAttrsSpec.js @@ -204,7 +204,7 @@ describe('ngSrcset', function() { var element = $compile('
')($rootScope); $rootScope.$digest(); - expect(element.attr('srcset')).toEqual('some/ 2x'); + expect(element.attr('srcset')).toBeUndefined(); $rootScope.$apply(function() { $rootScope.id = 1; diff --git a/test/ng/directive/ngSrcsetSpec.js b/test/ng/directive/ngSrcsetSpec.js index 8fccb00ae906..a95a108b9ba6 100644 --- a/test/ng/directive/ngSrcsetSpec.js +++ b/test/ng/directive/ngSrcsetSpec.js @@ -11,6 +11,6 @@ describe('ngSrcset', function() { $rootScope.image = {}; element = $compile('')($rootScope); $rootScope.$digest(); - expect(element.attr('srcset')).toEqual(' 2x'); + expect(element.attr('srcset')).toBeUndefined(); })); });