Skip to content

Commit 7f4cbd5

Browse files
committed
fix(ngSrc, ngSrcset): only interpolate if all expressions are defined
BREAKING CHANGE If `bar` is `undefined`, before `<img src="foo/{{bar}}.jpg">` yields `<img src="foo/.jpg">`. With this change, the binding will not set `src`. If you want the old behavior, you can do this: `<img src="foo/{{bar || ''}}.jpg">`. The same applies for `srcset` as well. Closes angular#6984
1 parent 1dd67f2 commit 7f4cbd5

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/ng/compile.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18411841
startingTag(node));
18421842
}
18431843

1844+
var allOrNothingAttrs = [
1845+
'ngSrc',
1846+
'ngSrcset',
1847+
'src',
1848+
'srcset'
1849+
];
1850+
18441851
directives.push({
18451852
priority: 100,
18461853
compile: function() {
@@ -1856,7 +1863,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18561863

18571864
// we need to interpolate again, in case the attribute value has been updated
18581865
// (e.g. by another directive's compile function)
1859-
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name));
1866+
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name),
1867+
allOrNothingAttrs.indexOf(name) > -1);
18601868

18611869
// if attribute was updated so that there is no interpolation going on we don't want to
18621870
// register any observers

test/ng/directive/booleanAttrsSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ describe('ngSrcset', function() {
204204
var element = $compile('<div ng-srcset="some/{{id}} 2x"></div>')($rootScope);
205205

206206
$rootScope.$digest();
207-
expect(element.attr('srcset')).toEqual('some/ 2x');
207+
expect(element.attr('srcset')).toBeUndefined();
208208

209209
$rootScope.$apply(function() {
210210
$rootScope.id = 1;

test/ng/directive/ngSrcsetSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ describe('ngSrcset', function() {
1111
$rootScope.image = {};
1212
element = $compile('<img ng-srcset="{{image.url}} 2x">')($rootScope);
1313
$rootScope.$digest();
14-
expect(element.attr('srcset')).toEqual(' 2x');
14+
expect(element.attr('srcset')).toBeUndefined();
1515
}));
1616
});

0 commit comments

Comments
 (0)