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

Commit 2310e10

Browse files
rphvgkalpak
authored andcommitted
fix($compile): properly handle setting srcset to undefined
Previously, calling `Attributes#$set('srcset', value)` on an `<img>` element would throw if `value` were undefined, as it assumed `value` is always a string. This commit fixes the issue, by skipping the unnecessary string manipulation when `value` is not defined. Closes #14470 Closes #14493
1 parent 92752e5 commit 2310e10

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14471447
(nodeName === 'img' && key === 'src')) {
14481448
// sanitize a[href] and img[src] values
14491449
this[key] = value = $$sanitizeUri(value, key === 'src');
1450-
} else if (nodeName === 'img' && key === 'srcset') {
1450+
} else if (nodeName === 'img' && key === 'srcset' && isDefined(value)) {
14511451
// sanitize img[srcset] values
14521452
var result = "";
14531453

test/ng/compileSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -9394,6 +9394,27 @@ describe('$compile', function() {
93949394

93959395
describe('img[srcset] sanitization', function() {
93969396

9397+
it('should not error if undefined', function() {
9398+
var linked = false;
9399+
module(function() {
9400+
directive('setter', valueFn(function(scope, elem, attrs) {
9401+
attrs.$set('srcset', 'http://example.com/');
9402+
expect(attrs.srcset).toBe('http://example.com/');
9403+
9404+
attrs.$set('srcset', undefined);
9405+
expect(attrs.srcset).toBeUndefined();
9406+
9407+
linked = true;
9408+
}));
9409+
});
9410+
inject(function($compile, $rootScope) {
9411+
element = $compile('<img setter></img>')($rootScope);
9412+
9413+
expect(linked).toBe(true);
9414+
expect(element.attr('srcset')).toBeUndefined();
9415+
});
9416+
});
9417+
93979418
it('should NOT require trusted values for img srcset', inject(function($rootScope, $compile, $sce) {
93989419
element = $compile('<img srcset="{{testUrl}}"></img>')($rootScope);
93999420
$rootScope.testUrl = 'http://example.com/image.png';

test/ng/directive/ngSrcsetSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ describe('ngSrcset', function() {
2828
$rootScope.$digest();
2929
expect(element.attr('srcset')).toBe('http://example.com/image1.png 1x,unsafe:javascript:doEvilStuff() 2x');
3030
}));
31+
32+
it('should not throw an error if undefined', inject(function($rootScope, $compile) {
33+
element = $compile('<img ng-attr-srcset="{{undefined}}">')($rootScope);
34+
$rootScope.$digest();
35+
}));
3136
});
3237

0 commit comments

Comments
 (0)