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

Commit d551d72

Browse files
samaxespetebacondarwin
authored andcommitted
feat(ngSrcset): add new ngSrcset directive
In line with ngSrc and ngHref, this new directive ensures that the `srcset` HTML5 attribute does not include a pre-interpolated string. Without it the browser will fetch from the URL with the literal text `{{hash}}` until AngularJS replaces the expression inside `{{hash}}`. Closes #2601
1 parent 629fb37 commit d551d72

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/ng/directive/booleanAttrs.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@
105105
* @param {template} ngSrc any string which can contain `{{}}` markup.
106106
*/
107107

108+
/**
109+
* @ngdoc directive
110+
* @name ng.directive:ngSrcset
111+
* @restrict A
112+
*
113+
* @description
114+
* Using Angular markup like `{{hash}}` in a `srcset` attribute doesn't
115+
* work right: The browser will fetch from the URL with the literal
116+
* text `{{hash}}` until Angular replaces the expression inside
117+
* `{{hash}}`. The `ngSrcset` directive solves this problem.
118+
*
119+
* The buggy way to write it:
120+
* <pre>
121+
* <img srcset="http://www.gravatar.com/avatar/{{hash}} 2x"/>
122+
* </pre>
123+
*
124+
* The correct way to write it:
125+
* <pre>
126+
* <img ng-srcset="http://www.gravatar.com/avatar/{{hash}} 2x"/>
127+
* </pre>
128+
*
129+
* @element IMG
130+
* @param {template} ngSrcset any string which can contain `{{}}` markup.
131+
*/
132+
108133
/**
109134
* @ngdoc directive
110135
* @name ng.directive:ngDisabled
@@ -325,8 +350,8 @@ forEach(BOOLEAN_ATTR, function(propName, attrName) {
325350
});
326351

327352

328-
// ng-src, ng-href are interpolated
329-
forEach(['src', 'href'], function(attrName) {
353+
// ng-src, ng-srcset, ng-href are interpolated
354+
forEach(['src', 'srcset', 'href'], function(attrName) {
330355
var normalized = directiveNormalize('ng-' + attrName);
331356
ngAttributeAliasDirectives[normalized] = function() {
332357
return {

test/ng/directive/booleanAttrsSpec.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ describe('boolean attr directives', function() {
8888

8989

9090
describe('ngSrc', function() {
91-
9291
it('should interpolate the expression and bind to src', inject(function($compile, $rootScope) {
9392
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope);
9493

@@ -126,6 +125,23 @@ describe('ngSrc', function() {
126125
});
127126

128127

128+
describe('ngSrcset', function() {
129+
it('should interpolate the expression and bind to srcset', inject(function($compile, $rootScope) {
130+
var element = $compile('<div ng-srcset="some/{{id}} 2x"></div>')($rootScope);
131+
132+
$rootScope.$digest();
133+
expect(element.attr('srcset')).toEqual('some/ 2x');
134+
135+
$rootScope.$apply(function() {
136+
$rootScope.id = 1;
137+
});
138+
expect(element.attr('srcset')).toEqual('some/1 2x');
139+
140+
dealoc(element);
141+
}));
142+
});
143+
144+
129145
describe('ngHref', function() {
130146
var element;
131147

test/ng/directive/ngSrcsetSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
describe('ngSrcset', function() {
4+
var element;
5+
6+
afterEach(function() {
7+
dealoc(element);
8+
});
9+
10+
it('should not result empty string in img srcset', inject(function($rootScope, $compile) {
11+
$rootScope.image = {};
12+
element = $compile('<img ng-srcset="{{image.url}} 2x">')($rootScope);
13+
$rootScope.$digest();
14+
expect(element.attr('srcset')).toEqual(' 2x');
15+
}));
16+
});

0 commit comments

Comments
 (0)