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

Commit 8d18038

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 #6984
1 parent c2362e3 commit 8d18038

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

src/ng/compile.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
526526
var hasDirectives = {},
527527
Suffix = 'Directive',
528528
COMMENT_DIRECTIVE_REGEXP = /^\s*directive\:\s*([\d\w\-_]+)\s+(.*)$/,
529-
CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/;
529+
CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/,
530+
ALL_OR_NOTHING_ATTRS = makeMap('ngSrc,ngSrcset,src,srcset');
530531

531532
// Ref: http://developers.whatwg.org/webappapis.html#event-handler-idl-attributes
532533
// The assumption is that future DOM event attribute names will begin with
@@ -1884,7 +1885,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18841885

18851886
// we need to interpolate again, in case the attribute value has been updated
18861887
// (e.g. by another directive's compile function)
1887-
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name));
1888+
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name),
1889+
ALL_OR_NOTHING_ATTRS[name]);
18881890

18891891
// if attribute was updated so that there is no interpolation going on we don't want to
18901892
// 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)